blob: 121be022d1eae7d1c4d07aae5f11fc96bbd53354 [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;
Anatolij Gustschin13ce77f2011-02-10 16:01:46 +0100458 mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100459
460 mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 if (cfi->cfi_mode == CFI_MODE_CFI) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000463 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 * It's a real CFI chip, not one for which the probe
465 * routine faked a CFI structure. So we read the feature
466 * table from it.
467 */
468 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
469 struct cfi_pri_intelext *extp;
470
471 extp = read_pri_intelext(map, adr);
472 if (!extp) {
473 kfree(mtd);
474 return NULL;
475 }
476
477 /* Install our own private info structure */
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000478 cfi->cmdset_priv = extp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
480 cfi_fixup(mtd, cfi_fixup_table);
481
482#ifdef DEBUG_CFI_FEATURES
483 /* Tell the user about it in lots of lovely detail */
484 cfi_tell_features(extp);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000485#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 if(extp->SuspendCmdSupport & 1) {
488 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
489 }
490 }
491 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
492 /* Apply jedec specific fixups */
493 cfi_fixup(mtd, jedec_fixup_table);
494 }
495 /* Apply generic fixups */
496 cfi_fixup(mtd, fixup_table);
497
498 for (i=0; i< cfi->numchips; i++) {
David Woodhouse2a5bd592007-02-09 14:39:10 +0000499 if (cfi->cfiq->WordWriteTimeoutTyp)
500 cfi->chips[i].word_write_time =
501 1<<cfi->cfiq->WordWriteTimeoutTyp;
502 else
503 cfi->chips[i].word_write_time = 50000;
504
505 if (cfi->cfiq->BufWriteTimeoutTyp)
506 cfi->chips[i].buffer_write_time =
507 1<<cfi->cfiq->BufWriteTimeoutTyp;
508 /* No default; if it isn't specified, we won't use it */
509
510 if (cfi->cfiq->BlockEraseTimeoutTyp)
511 cfi->chips[i].erase_time =
512 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
513 else
514 cfi->chips[i].erase_time = 2000000;
515
Anders Grafströme93cafe2008-08-05 18:37:41 +0200516 if (cfi->cfiq->WordWriteTimeoutTyp &&
517 cfi->cfiq->WordWriteTimeoutMax)
518 cfi->chips[i].word_write_time_max =
519 1<<(cfi->cfiq->WordWriteTimeoutTyp +
520 cfi->cfiq->WordWriteTimeoutMax);
521 else
522 cfi->chips[i].word_write_time_max = 50000 * 8;
523
524 if (cfi->cfiq->BufWriteTimeoutTyp &&
525 cfi->cfiq->BufWriteTimeoutMax)
526 cfi->chips[i].buffer_write_time_max =
527 1<<(cfi->cfiq->BufWriteTimeoutTyp +
528 cfi->cfiq->BufWriteTimeoutMax);
529
530 if (cfi->cfiq->BlockEraseTimeoutTyp &&
531 cfi->cfiq->BlockEraseTimeoutMax)
532 cfi->chips[i].erase_time_max =
533 1000<<(cfi->cfiq->BlockEraseTimeoutTyp +
534 cfi->cfiq->BlockEraseTimeoutMax);
535 else
536 cfi->chips[i].erase_time_max = 2000000 * 8;
537
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 cfi->chips[i].ref_point_counter = 0;
Simon Voglc314b6f2006-02-24 13:04:09 -0800539 init_waitqueue_head(&(cfi->chips[i].wq));
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000540 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 map->fldrv = &cfi_intelext_chipdrv;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 return cfi_intelext_setup(mtd);
545}
David Woodhousea15bdee2006-05-08 22:35:05 +0100546struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
547struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
548EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
549EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
550EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
552static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
553{
554 struct map_info *map = mtd->priv;
555 struct cfi_private *cfi = map->fldrv_priv;
556 unsigned long offset = 0;
557 int i,j;
558 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
559
560 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
561
562 mtd->size = devsize * cfi->numchips;
563
564 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000565 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 * mtd->numeraseregions, GFP_KERNEL);
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000567 if (!mtd->eraseregions) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 printk(KERN_ERR "Failed to allocate memory for MTD erase region info\n");
569 goto setup_err;
570 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000571
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
573 unsigned long ernum, ersize;
574 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
575 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
576
577 if (mtd->erasesize < ersize) {
578 mtd->erasesize = ersize;
579 }
580 for (j=0; j<cfi->numchips; j++) {
581 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
582 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
583 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -0800584 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 }
586 offset += (ersize * ernum);
587 }
588
589 if (offset != devsize) {
590 /* Argh */
591 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
592 goto setup_err;
593 }
594
595 for (i=0; i<mtd->numeraseregions;i++){
Adrian Hunter69423d92008-12-10 13:37:21 +0000596 printk(KERN_DEBUG "erase region %d: offset=0x%llx,size=0x%x,blocks=%d\n",
597 i,(unsigned long long)mtd->eraseregions[i].offset,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 mtd->eraseregions[i].erasesize,
599 mtd->eraseregions[i].numblocks);
600 }
601
Nicolas Pitref77814d2005-02-08 17:11:19 +0000602#ifdef CONFIG_MTD_OTP
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 mtd->read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
Nicolas Pitref77814d2005-02-08 17:11:19 +0000604 mtd->read_user_prot_reg = cfi_intelext_read_user_prot_reg;
605 mtd->write_user_prot_reg = cfi_intelext_write_user_prot_reg;
606 mtd->lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
607 mtd->get_fact_prot_info = cfi_intelext_get_fact_prot_info;
608 mtd->get_user_prot_info = cfi_intelext_get_user_prot_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609#endif
610
611 /* This function has the potential to distort the reality
612 a bit and therefore should be called last. */
613 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
614 goto setup_err;
615
616 __module_get(THIS_MODULE);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +0100617 register_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 return mtd;
619
620 setup_err:
Jiri Slaby17fabf12010-01-10 10:01:19 +0100621 kfree(mtd->eraseregions);
622 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 kfree(cfi->cmdset_priv);
624 return NULL;
625}
626
627static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
628 struct cfi_private **pcfi)
629{
630 struct map_info *map = mtd->priv;
631 struct cfi_private *cfi = *pcfi;
632 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
633
634 /*
Jesper Juhl8f1a8662007-07-05 02:18:34 +0200635 * Probing of multi-partition flash chips.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 *
637 * To support multiple partitions when available, we simply arrange
638 * for each of them to have their own flchip structure even if they
639 * are on the same physical chip. This means completely recreating
640 * a new cfi_private structure right here which is a blatent code
641 * layering violation, but this is still the least intrusive
642 * arrangement at this point. This can be rearranged in the future
643 * if someone feels motivated enough. --nico
644 */
Nicolas Pitre638d9832005-08-06 05:40:46 +0100645 if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 && extp->FeatureSupport & (1 << 9)) {
647 struct cfi_private *newcfi;
648 struct flchip *chip;
649 struct flchip_shared *shared;
650 int offs, numregions, numparts, partshift, numvirtchips, i, j;
651
652 /* Protection Register info */
Nicolas Pitre72b56a22005-02-05 02:06:19 +0000653 offs = (extp->NumProtectionFields - 1) *
654 sizeof(struct cfi_intelext_otpinfo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Burst Read info */
Nicolas Pitre6f6ed052005-10-25 21:28:43 +0100657 offs += extp->extra[offs+1]+2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /* Number of partition regions */
660 numregions = extp->extra[offs];
661 offs += 1;
662
Nicolas Pitre638d9832005-08-06 05:40:46 +0100663 /* skip the sizeof(partregion) field in CFI 1.4 */
664 if (extp->MinorVersion >= '4')
665 offs += 2;
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 /* Number of hardware partitions */
668 numparts = 0;
669 for (i = 0; i < numregions; i++) {
670 struct cfi_intelext_regioninfo *rinfo;
671 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
672 numparts += rinfo->NumIdentPartitions;
673 offs += sizeof(*rinfo)
674 + (rinfo->NumBlockTypes - 1) *
675 sizeof(struct cfi_intelext_blockinfo);
676 }
677
Thomas Kunzefe224662008-04-23 01:40:52 +0200678 if (!numparts)
679 numparts = 1;
680
Nicolas Pitre638d9832005-08-06 05:40:46 +0100681 /* Programming Region info */
682 if (extp->MinorVersion >= '4') {
683 struct cfi_intelext_programming_regioninfo *prinfo;
684 prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
Joern Engel28318772006-05-22 23:18:05 +0200685 mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
Joern Engel5fa43392006-05-22 23:18:29 +0200686 mtd->flags &= ~MTD_BIT_WRITEABLE;
Nicolas Pitre638d9832005-08-06 05:40:46 +0100687 printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
Joern Engel28318772006-05-22 23:18:05 +0200688 map->name, mtd->writesize,
Artem Bityutskiyd4160852007-01-30 10:45:55 +0200689 cfi->interleave * prinfo->ControlValid,
690 cfi->interleave * prinfo->ControlInvalid);
Nicolas Pitre638d9832005-08-06 05:40:46 +0100691 }
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 /*
694 * All functions below currently rely on all chips having
695 * the same geometry so we'll just assume that all hardware
696 * partitions are of the same size too.
697 */
698 partshift = cfi->chipshift - __ffs(numparts);
699
700 if ((1 << partshift) < mtd->erasesize) {
701 printk( KERN_ERR
702 "%s: bad number of hw partitions (%d)\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -0700703 __func__, numparts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 return -EINVAL;
705 }
706
707 numvirtchips = cfi->numchips * numparts;
708 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
709 if (!newcfi)
710 return -ENOMEM;
711 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
712 if (!shared) {
713 kfree(newcfi);
714 return -ENOMEM;
715 }
716 memcpy(newcfi, cfi, sizeof(struct cfi_private));
717 newcfi->numchips = numvirtchips;
718 newcfi->chipshift = partshift;
719
720 chip = &newcfi->chips[0];
721 for (i = 0; i < cfi->numchips; i++) {
722 shared[i].writing = shared[i].erasing = NULL;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200723 mutex_init(&shared[i].lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 for (j = 0; j < numparts; j++) {
725 *chip = cfi->chips[i];
726 chip->start += j << partshift;
727 chip->priv = &shared[i];
728 /* those should be reset too since
729 they create memory references. */
730 init_waitqueue_head(&chip->wq);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200731 mutex_init(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 chip++;
733 }
734 }
735
736 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
737 "--> %d partitions of %d KiB\n",
738 map->name, cfi->numchips, cfi->interleave,
739 newcfi->numchips, 1<<(newcfi->chipshift-10));
740
741 map->fldrv_priv = newcfi;
742 *pcfi = newcfi;
743 kfree(cfi);
744 }
745
746 return 0;
747}
748
749/*
750 * *********** CHIP ACCESS FUNCTIONS ***********
751 */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100752static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753{
754 DECLARE_WAITQUEUE(wait, current);
755 struct cfi_private *cfi = map->fldrv_priv;
756 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100758 unsigned long timeo = jiffies + HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400760 /* Prevent setting state FL_SYNCING for chip in suspended state. */
761 if (mode == FL_SYNCING && chip->oldstate != FL_READY)
762 goto sleep;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 switch (chip->state) {
765
766 case FL_STATUS:
767 for (;;) {
768 status = map_read(map, adr);
769 if (map_word_andequal(map, status, status_OK, status_OK))
770 break;
771
772 /* At this point we're fine with write operations
773 in other partitions as they don't conflict. */
774 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
775 break;
776
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200777 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200779 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 /* Someone else might have been playing with it. */
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100781 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 }
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700783 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 case FL_READY:
785 case FL_CFI_QUERY:
786 case FL_JEDEC_QUERY:
787 return 0;
788
789 case FL_ERASING:
790 if (!cfip ||
791 !(cfip->FeatureSupport & 2) ||
792 !(mode == FL_READY || mode == FL_POINT ||
793 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
794 goto sleep;
795
796
797 /* Erase suspend */
798 map_write(map, CMD(0xB0), adr);
799
800 /* If the flash has finished erasing, then 'erase suspend'
801 * appears to make some (28F320) flash devices switch to
802 * 'read' mode. Make sure that we switch to 'read status'
803 * mode so we get the right data. --rmk
804 */
805 map_write(map, CMD(0x70), adr);
806 chip->oldstate = FL_ERASING;
807 chip->state = FL_ERASE_SUSPENDING;
808 chip->erase_suspended = 1;
809 for (;;) {
810 status = map_read(map, adr);
811 if (map_word_andequal(map, status, status_OK, status_OK))
812 break;
813
814 if (time_after(jiffies, timeo)) {
Tadashi Abe100f2342011-05-19 15:58:15 +0900815 /* Urgh. Resume and pretend we weren't here.
816 * Make sure we're in 'read status' mode if it had finished */
817 put_chip(map, chip, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +0100818 printk(KERN_ERR "%s: Chip not ready after erase "
819 "suspended: status = 0x%lx\n", map->name, status.x[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 return -EIO;
821 }
822
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200823 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 cfi_udelay(1);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200825 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
827 So we can just loop here. */
828 }
829 chip->state = FL_STATUS;
830 return 0;
831
832 case FL_XIP_WHILE_ERASING:
833 if (mode != FL_READY && mode != FL_POINT &&
834 (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
835 goto sleep;
836 chip->oldstate = chip->state;
837 chip->state = FL_READY;
838 return 0;
839
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700840 case FL_SHUTDOWN:
841 /* The machine is rebooting now,so no one can get chip anymore */
842 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 case FL_POINT:
844 /* Only if there's no operation suspended... */
845 if (mode == FL_READY && chip->oldstate == FL_READY)
846 return 0;
Alexey Korolevfb6d0802008-04-04 14:30:01 -0700847 /* Fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848 default:
849 sleep:
850 set_current_state(TASK_UNINTERRUPTIBLE);
851 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200852 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 schedule();
854 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200855 mutex_lock(&chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100856 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 }
858}
859
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100860static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
861{
862 int ret;
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300863 DECLARE_WAITQUEUE(wait, current);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100864
865 retry:
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400866 if (chip->priv &&
867 (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE
868 || mode == FL_SHUTDOWN) && chip->state != FL_SYNCING) {
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100869 /*
870 * OK. We have possibility for contention on the write/erase
871 * operations which are global to the real chip and not per
872 * partition. So let's fight it over in the partition which
873 * currently has authority on the operation.
874 *
875 * The rules are as follows:
876 *
877 * - any write operation must own shared->writing.
878 *
879 * - any erase operation must own _both_ shared->writing and
880 * shared->erasing.
881 *
882 * - contention arbitration is handled in the owner's context.
883 *
884 * The 'shared' struct can be read and/or written only when
885 * its lock is taken.
886 */
887 struct flchip_shared *shared = chip->priv;
888 struct flchip *contender;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200889 mutex_lock(&shared->lock);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100890 contender = shared->writing;
891 if (contender && contender != chip) {
892 /*
893 * The engine to perform desired operation on this
894 * partition is already in use by someone else.
895 * Let's fight over it in the context of the chip
896 * currently using it. If it is possible to suspend,
897 * that other partition will do just that, otherwise
898 * it'll happily send us to sleep. In any case, when
899 * get_chip returns success we're clear to go ahead.
900 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200901 ret = mutex_trylock(&contender->mutex);
Stefani Seibold8ae66412010-08-05 09:19:26 +0200902 mutex_unlock(&shared->lock);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100903 if (!ret)
904 goto retry;
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200905 mutex_unlock(&chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100906 ret = chip_ready(map, contender, contender->start, mode);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200907 mutex_lock(&chip->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100908
909 if (ret == -EAGAIN) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200910 mutex_unlock(&contender->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100911 goto retry;
912 }
913 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200914 mutex_unlock(&contender->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100915 return ret;
916 }
Stefani Seibold8ae66412010-08-05 09:19:26 +0200917 mutex_lock(&shared->lock);
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400918
919 /* We should not own chip if it is already
920 * in FL_SYNCING state. Put contender and retry. */
921 if (chip->state == FL_SYNCING) {
922 put_chip(map, contender, contender->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200923 mutex_unlock(&contender->mutex);
Alexander Belyakov3afe7eb2008-09-25 17:53:24 +0400924 goto retry;
925 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200926 mutex_unlock(&contender->mutex);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100927 }
928
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300929 /* Check if we already have suspended erase
930 * on this chip. Sleep. */
931 if (mode == FL_ERASING && shared->erasing
932 && shared->erasing->oldstate == FL_ERASING) {
Stefani Seibold8ae66412010-08-05 09:19:26 +0200933 mutex_unlock(&shared->lock);
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300934 set_current_state(TASK_UNINTERRUPTIBLE);
935 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200936 mutex_unlock(&chip->mutex);
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300937 schedule();
938 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200939 mutex_lock(&chip->mutex);
Alexander Belyakov6c24e412007-11-07 11:58:07 +0300940 goto retry;
941 }
942
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100943 /* We now own it */
944 shared->writing = chip;
945 if (mode == FL_ERASING)
946 shared->erasing = chip;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200947 mutex_unlock(&shared->lock);
Alexey Korolev5a37cf12007-10-22 17:55:20 +0100948 }
949 ret = chip_ready(map, chip, adr, mode);
950 if (ret == -EAGAIN)
951 goto retry;
952
953 return ret;
954}
955
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
957{
958 struct cfi_private *cfi = map->fldrv_priv;
959
960 if (chip->priv) {
961 struct flchip_shared *shared = chip->priv;
Stefani Seibold8ae66412010-08-05 09:19:26 +0200962 mutex_lock(&shared->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 if (shared->writing == chip && chip->oldstate == FL_READY) {
964 /* We own the ability to write, but we're done */
965 shared->writing = shared->erasing;
966 if (shared->writing && shared->writing != chip) {
967 /* give back ownership to who we loaned it from */
968 struct flchip *loaner = shared->writing;
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200969 mutex_lock(&loaner->mutex);
Stefani Seibold8ae66412010-08-05 09:19:26 +0200970 mutex_unlock(&shared->lock);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200971 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 put_chip(map, loaner, loaner->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +0200973 mutex_lock(&chip->mutex);
974 mutex_unlock(&loaner->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 wake_up(&chip->wq);
976 return;
977 }
978 shared->erasing = NULL;
979 shared->writing = NULL;
980 } else if (shared->erasing == chip && shared->writing != chip) {
981 /*
982 * We own the ability to erase without the ability
983 * to write, which means the erase was suspended
984 * and some other partition is currently writing.
985 * Don't let the switch below mess things up since
986 * we don't have ownership to resume anything.
987 */
Stefani Seibold8ae66412010-08-05 09:19:26 +0200988 mutex_unlock(&shared->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 wake_up(&chip->wq);
990 return;
991 }
Stefani Seibold8ae66412010-08-05 09:19:26 +0200992 mutex_unlock(&shared->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
995 switch(chip->oldstate) {
996 case FL_ERASING:
997 chip->state = chip->oldstate;
Thomas Gleixner1f948b42005-11-07 11:15:37 +0000998 /* What if one interleaved chip has finished and the
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 other hasn't? The old code would leave the finished
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001000 one in READY mode. That's bad, and caused -EROFS
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001 errors to be returned from do_erase_oneblock because
1002 that's the only bit it checked for at the time.
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001003 As the state machine appears to explicitly allow
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004 sending the 0x70 (Read Status) command to an erasing
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001005 chip and expecting it to be ignored, that's what we
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 do. */
1007 map_write(map, CMD(0xd0), adr);
1008 map_write(map, CMD(0x70), adr);
1009 chip->oldstate = FL_READY;
1010 chip->state = FL_ERASING;
1011 break;
1012
1013 case FL_XIP_WHILE_ERASING:
1014 chip->state = chip->oldstate;
1015 chip->oldstate = FL_READY;
1016 break;
1017
1018 case FL_READY:
1019 case FL_STATUS:
1020 case FL_JEDEC_QUERY:
1021 /* We should really make set_vpp() count, rather than doing this */
1022 DISABLE_VPP(map);
1023 break;
1024 default:
Nicolas Pitre48436532005-08-06 05:16:52 +01001025 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 }
1027 wake_up(&chip->wq);
1028}
1029
1030#ifdef CONFIG_MTD_XIP
1031
1032/*
1033 * No interrupt what so ever can be serviced while the flash isn't in array
1034 * mode. This is ensured by the xip_disable() and xip_enable() functions
1035 * enclosing any code path where the flash is known not to be in array mode.
1036 * And within a XIP disabled code path, only functions marked with __xipram
1037 * may be called and nothing else (it's a good thing to inspect generated
1038 * assembly to make sure inline functions were actually inlined and that gcc
1039 * didn't emit calls to its own support functions). Also configuring MTD CFI
1040 * support to a single buswidth and a single interleave is also recommended.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 */
1042
1043static void xip_disable(struct map_info *map, struct flchip *chip,
1044 unsigned long adr)
1045{
1046 /* TODO: chips with no XIP use should ignore and return */
1047 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 local_irq_disable();
1049}
1050
1051static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
1052 unsigned long adr)
1053{
1054 struct cfi_private *cfi = map->fldrv_priv;
1055 if (chip->state != FL_POINT && chip->state != FL_READY) {
1056 map_write(map, CMD(0xff), adr);
1057 chip->state = FL_READY;
1058 }
1059 (void) map_read(map, adr);
Thomas Gleixner97f927a2005-07-07 16:50:16 +02001060 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061 local_irq_enable();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062}
1063
1064/*
1065 * When a delay is required for the flash operation to complete, the
Nicolas Pitrec1724712006-03-30 15:52:41 +01001066 * xip_wait_for_operation() function is polling for both the given timeout
1067 * and pending (but still masked) hardware interrupts. Whenever there is an
1068 * interrupt pending then the flash erase or write operation is suspended,
1069 * array mode restored and interrupts unmasked. Task scheduling might also
1070 * happen at that point. The CPU eventually returns from the interrupt or
1071 * the call to schedule() and the suspended flash operation is resumed for
1072 * the remaining of the delay period.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 *
1074 * Warning: this function _will_ fool interrupt latency tracing tools.
1075 */
1076
Nicolas Pitrec1724712006-03-30 15:52:41 +01001077static int __xipram xip_wait_for_operation(
1078 struct map_info *map, struct flchip *chip,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001079 unsigned long adr, unsigned int chip_op_time_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080{
1081 struct cfi_private *cfi = map->fldrv_priv;
1082 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
1083 map_word status, OK = CMD(0x80);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001084 unsigned long usec, suspended, start, done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085 flstate_t oldstate, newstate;
1086
Nicolas Pitrec1724712006-03-30 15:52:41 +01001087 start = xip_currtime();
Anders Grafströme93cafe2008-08-05 18:37:41 +02001088 usec = chip_op_time_max;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001089 if (usec == 0)
1090 usec = 500000;
1091 done = 0;
1092
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 do {
1094 cpu_relax();
1095 if (xip_irqpending() && cfip &&
1096 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
1097 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
1098 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
1099 /*
1100 * Let's suspend the erase or write operation when
1101 * supported. Note that we currently don't try to
1102 * suspend interleaved chips if there is already
1103 * another operation suspended (imagine what happens
1104 * when one chip was already done with the current
1105 * operation while another chip suspended it, then
1106 * we resume the whole thing at once). Yes, it
1107 * can happen!
1108 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001109 usec -= done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110 map_write(map, CMD(0xb0), adr);
1111 map_write(map, CMD(0x70), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 suspended = xip_currtime();
1113 do {
1114 if (xip_elapsed_since(suspended) > 100000) {
1115 /*
1116 * The chip doesn't want to suspend
1117 * after waiting for 100 msecs.
1118 * This is a critical error but there
1119 * is not much we can do here.
1120 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001121 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 }
1123 status = map_read(map, adr);
1124 } while (!map_word_andequal(map, status, OK, OK));
1125
1126 /* Suspend succeeded */
1127 oldstate = chip->state;
1128 if (oldstate == FL_ERASING) {
1129 if (!map_word_bitsset(map, status, CMD(0x40)))
1130 break;
1131 newstate = FL_XIP_WHILE_ERASING;
1132 chip->erase_suspended = 1;
1133 } else {
1134 if (!map_word_bitsset(map, status, CMD(0x04)))
1135 break;
1136 newstate = FL_XIP_WHILE_WRITING;
1137 chip->write_suspended = 1;
1138 }
1139 chip->state = newstate;
1140 map_write(map, CMD(0xff), adr);
1141 (void) map_read(map, adr);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001142 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143 local_irq_enable();
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001144 mutex_unlock(&chip->mutex);
Paulius Zaleckasca5c23c2008-02-27 01:42:39 +02001145 xip_iprefetch();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 cond_resched();
1147
1148 /*
1149 * We're back. However someone else might have
1150 * decided to go write to the chip if we are in
1151 * a suspended erase state. If so let's wait
1152 * until it's done.
1153 */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001154 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 while (chip->state != newstate) {
1156 DECLARE_WAITQUEUE(wait, current);
1157 set_current_state(TASK_UNINTERRUPTIBLE);
1158 add_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001159 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 schedule();
1161 remove_wait_queue(&chip->wq, &wait);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001162 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 }
1164 /* Disallow XIP again */
1165 local_irq_disable();
1166
1167 /* Resume the write or erase operation */
1168 map_write(map, CMD(0xd0), adr);
1169 map_write(map, CMD(0x70), adr);
1170 chip->state = oldstate;
1171 start = xip_currtime();
1172 } else if (usec >= 1000000/HZ) {
1173 /*
1174 * Try to save on CPU power when waiting delay
1175 * is at least a system timer tick period.
1176 * No need to be extremely accurate here.
1177 */
1178 xip_cpu_idle();
1179 }
1180 status = map_read(map, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001181 done = xip_elapsed_since(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 } while (!map_word_andequal(map, status, OK, OK)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001183 && done < usec);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184
Nicolas Pitrec1724712006-03-30 15:52:41 +01001185 return (done >= usec) ? -ETIME : 0;
1186}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188/*
1189 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1190 * the flash is actively programming or erasing since we have to poll for
1191 * the operation to complete anyway. We can't do that in a generic way with
Nicolas Pitre6da70122005-05-19 18:05:47 +01001192 * a XIP setup so do it before the actual flash operation in this case
Nicolas Pitrec1724712006-03-30 15:52:41 +01001193 * and stub it out from INVAL_CACHE_AND_WAIT.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 */
Nicolas Pitre6da70122005-05-19 18:05:47 +01001195#define XIP_INVAL_CACHED_RANGE(map, from, size) \
1196 INVALIDATE_CACHED_RANGE(map, from, size)
1197
Anders Grafströme93cafe2008-08-05 18:37:41 +02001198#define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec, usec_max) \
1199 xip_wait_for_operation(map, chip, cmd_adr, usec_max)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
1201#else
1202
1203#define xip_disable(map, chip, adr)
1204#define xip_enable(map, chip, adr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205#define XIP_INVAL_CACHED_RANGE(x...)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001206#define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Nicolas Pitrec1724712006-03-30 15:52:41 +01001208static int inval_cache_and_wait_for_operation(
1209 struct map_info *map, struct flchip *chip,
1210 unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001211 unsigned int chip_op_time, unsigned int chip_op_time_max)
Nicolas Pitrec1724712006-03-30 15:52:41 +01001212{
1213 struct cfi_private *cfi = map->fldrv_priv;
1214 map_word status, status_OK = CMD(0x80);
Alexey Korolev46a16522006-06-28 19:22:07 +01001215 int chip_state = chip->state;
Alexey Korolev998453f2008-07-16 15:28:56 +01001216 unsigned int timeo, sleep_time, reset_timeo;
Nicolas Pitre6da70122005-05-19 18:05:47 +01001217
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001218 mutex_unlock(&chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001219 if (inval_len)
1220 INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001221 mutex_lock(&chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001222
Anders Grafströme93cafe2008-08-05 18:37:41 +02001223 timeo = chip_op_time_max;
Alexey Korolev46a16522006-06-28 19:22:07 +01001224 if (!timeo)
1225 timeo = 500000;
Alexey Korolev998453f2008-07-16 15:28:56 +01001226 reset_timeo = timeo;
Alexey Korolev46a16522006-06-28 19:22:07 +01001227 sleep_time = chip_op_time / 2;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001228
Nicolas Pitrec1724712006-03-30 15:52:41 +01001229 for (;;) {
Joakim Tjernlundecf3fde2011-02-07 17:07:11 +01001230 if (chip->state != chip_state) {
1231 /* Someone's suspended the operation: sleep */
1232 DECLARE_WAITQUEUE(wait, current);
1233 set_current_state(TASK_UNINTERRUPTIBLE);
1234 add_wait_queue(&chip->wq, &wait);
1235 mutex_unlock(&chip->mutex);
1236 schedule();
1237 remove_wait_queue(&chip->wq, &wait);
1238 mutex_lock(&chip->mutex);
1239 continue;
1240 }
1241
Nicolas Pitrec1724712006-03-30 15:52:41 +01001242 status = map_read(map, cmd_adr);
1243 if (map_word_andequal(map, status, status_OK, status_OK))
1244 break;
1245
Joakim Tjernlundecf3fde2011-02-07 17:07:11 +01001246 if (chip->erase_suspended && chip_state == FL_ERASING) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001247 /* Erase suspend occurred while sleep: reset timeout */
Joakim Tjernlundecf3fde2011-02-07 17:07:11 +01001248 timeo = reset_timeo;
1249 chip->erase_suspended = 0;
1250 }
1251 if (chip->write_suspended && chip_state == FL_WRITING) {
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001252 /* Write suspend occurred while sleep: reset timeout */
Joakim Tjernlundecf3fde2011-02-07 17:07:11 +01001253 timeo = reset_timeo;
1254 chip->write_suspended = 0;
1255 }
Alexey Korolev46a16522006-06-28 19:22:07 +01001256 if (!timeo) {
Nicolas Pitrec1724712006-03-30 15:52:41 +01001257 map_write(map, CMD(0x70), cmd_adr);
1258 chip->state = FL_STATUS;
1259 return -ETIME;
1260 }
1261
Alexey Korolev46a16522006-06-28 19:22:07 +01001262 /* OK Still waiting. Drop the lock, wait a while and retry. */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001263 mutex_unlock(&chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001264 if (sleep_time >= 1000000/HZ) {
1265 /*
1266 * Half of the normal delay still remaining
1267 * can be performed with a sleeping delay instead
1268 * of busy waiting.
1269 */
1270 msleep(sleep_time/1000);
1271 timeo -= sleep_time;
1272 sleep_time = 1000000/HZ;
1273 } else {
1274 udelay(1);
1275 cond_resched();
1276 timeo--;
1277 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001278 mutex_lock(&chip->mutex);
Alexey Korolev46a16522006-06-28 19:22:07 +01001279 }
Nicolas Pitrec1724712006-03-30 15:52:41 +01001280
1281 /* Done and happy. */
1282 chip->state = FL_STATUS;
1283 return 0;
1284}
Nicolas Pitre6da70122005-05-19 18:05:47 +01001285
Linus Torvalds1da177e2005-04-16 15:20:36 -07001286#endif
1287
Anders Grafströme93cafe2008-08-05 18:37:41 +02001288#define WAIT_TIMEOUT(map, chip, adr, udelay, udelay_max) \
1289 INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay, udelay_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001290
1291
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1293{
1294 unsigned long cmd_addr;
1295 struct cfi_private *cfi = map->fldrv_priv;
1296 int ret = 0;
1297
1298 adr += chip->start;
1299
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001300 /* Ensure cmd read/writes are aligned. */
1301 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001303 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
1305 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1306
1307 if (!ret) {
1308 if (chip->state != FL_POINT && chip->state != FL_READY)
1309 map_write(map, CMD(0xff), cmd_addr);
1310
1311 chip->state = FL_POINT;
1312 chip->ref_point_counter++;
1313 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001314 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 return ret;
1317}
1318
Jared Hulberta98889f2008-04-29 23:26:49 -07001319static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
1320 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001321{
1322 struct map_info *map = mtd->priv;
1323 struct cfi_private *cfi = map->fldrv_priv;
Andy Lowe097f2572007-01-12 18:05:10 -05001324 unsigned long ofs, last_end = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325 int chipnum;
1326 int ret = 0;
1327
1328 if (!map->virt || (from + len > mtd->size))
1329 return -EINVAL;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 /* Now lock the chip(s) to POINT state */
1332
1333 /* ofs: offset within the first chip that the first read should start */
1334 chipnum = (from >> cfi->chipshift);
1335 ofs = from - (chipnum << cfi->chipshift);
1336
Jared Hulberta98889f2008-04-29 23:26:49 -07001337 *virt = map->virt + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001338 *retlen = 0;
Jared Hulberta98889f2008-04-29 23:26:49 -07001339 if (phys)
1340 *phys = map->phys + cfi->chips[chipnum].start + ofs;
Andy Lowe097f2572007-01-12 18:05:10 -05001341
Linus Torvalds1da177e2005-04-16 15:20:36 -07001342 while (len) {
1343 unsigned long thislen;
1344
1345 if (chipnum >= cfi->numchips)
1346 break;
1347
Andy Lowe097f2572007-01-12 18:05:10 -05001348 /* We cannot point across chips that are virtually disjoint */
1349 if (!last_end)
1350 last_end = cfi->chips[chipnum].start;
1351 else if (cfi->chips[chipnum].start != last_end)
1352 break;
1353
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 if ((len + ofs -1) >> cfi->chipshift)
1355 thislen = (1<<cfi->chipshift) - ofs;
1356 else
1357 thislen = len;
1358
1359 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1360 if (ret)
1361 break;
1362
1363 *retlen += thislen;
1364 len -= thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001365
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 ofs = 0;
Andy Lowe097f2572007-01-12 18:05:10 -05001367 last_end += 1 << cfi->chipshift;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368 chipnum++;
1369 }
1370 return 0;
1371}
1372
Jared Hulberta98889f2008-04-29 23:26:49 -07001373static void cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374{
1375 struct map_info *map = mtd->priv;
1376 struct cfi_private *cfi = map->fldrv_priv;
1377 unsigned long ofs;
1378 int chipnum;
1379
1380 /* Now unlock the chip(s) POINT state */
1381
1382 /* ofs: offset within the first chip that the first read should start */
1383 chipnum = (from >> cfi->chipshift);
1384 ofs = from - (chipnum << cfi->chipshift);
1385
1386 while (len) {
1387 unsigned long thislen;
1388 struct flchip *chip;
1389
1390 chip = &cfi->chips[chipnum];
1391 if (chipnum >= cfi->numchips)
1392 break;
1393
1394 if ((len + ofs -1) >> cfi->chipshift)
1395 thislen = (1<<cfi->chipshift) - ofs;
1396 else
1397 thislen = len;
1398
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001399 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 if (chip->state == FL_POINT) {
1401 chip->ref_point_counter--;
1402 if(chip->ref_point_counter == 0)
1403 chip->state = FL_READY;
1404 } else
Nicolas Pitre48436532005-08-06 05:16:52 +01001405 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 -07001406
1407 put_chip(map, chip, chip->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001408 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409
1410 len -= thislen;
1411 ofs = 0;
1412 chipnum++;
1413 }
1414}
1415
1416static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1417{
1418 unsigned long cmd_addr;
1419 struct cfi_private *cfi = map->fldrv_priv;
1420 int ret;
1421
1422 adr += chip->start;
1423
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001424 /* Ensure cmd read/writes are aligned. */
1425 cmd_addr = adr & ~(map_bankwidth(map)-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001427 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 ret = get_chip(map, chip, cmd_addr, FL_READY);
1429 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001430 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001431 return ret;
1432 }
1433
1434 if (chip->state != FL_POINT && chip->state != FL_READY) {
1435 map_write(map, CMD(0xff), cmd_addr);
1436
1437 chip->state = FL_READY;
1438 }
1439
1440 map_copy_from(map, buf, adr, len);
1441
1442 put_chip(map, chip, cmd_addr);
1443
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001444 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001445 return 0;
1446}
1447
1448static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1449{
1450 struct map_info *map = mtd->priv;
1451 struct cfi_private *cfi = map->fldrv_priv;
1452 unsigned long ofs;
1453 int chipnum;
1454 int ret = 0;
1455
1456 /* ofs: offset within the first chip that the first read should start */
1457 chipnum = (from >> cfi->chipshift);
1458 ofs = from - (chipnum << cfi->chipshift);
1459
1460 *retlen = 0;
1461
1462 while (len) {
1463 unsigned long thislen;
1464
1465 if (chipnum >= cfi->numchips)
1466 break;
1467
1468 if ((len + ofs -1) >> cfi->chipshift)
1469 thislen = (1<<cfi->chipshift) - ofs;
1470 else
1471 thislen = len;
1472
1473 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1474 if (ret)
1475 break;
1476
1477 *retlen += thislen;
1478 len -= thislen;
1479 buf += thislen;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001480
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481 ofs = 0;
1482 chipnum++;
1483 }
1484 return ret;
1485}
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00001488 unsigned long adr, map_word datum, int mode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001489{
1490 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001491 map_word status, write_cmd;
1492 int ret=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494 adr += chip->start;
1495
Nicolas Pitref77814d2005-02-08 17:11:19 +00001496 switch (mode) {
Nicolas Pitre638d9832005-08-06 05:40:46 +01001497 case FL_WRITING:
Guillaume LECERFb5d194c2010-10-26 10:55:29 +01001498 write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0x40) : CMD(0x41);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001499 break;
1500 case FL_OTP_WRITE:
1501 write_cmd = CMD(0xc0);
1502 break;
1503 default:
1504 return -EINVAL;
Nicolas Pitref77814d2005-02-08 17:11:19 +00001505 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001507 mutex_lock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001508 ret = get_chip(map, chip, adr, mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001509 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001510 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001511 return ret;
1512 }
1513
1514 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1515 ENABLE_VPP(map);
1516 xip_disable(map, chip, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001517 map_write(map, write_cmd, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 map_write(map, datum, adr);
Nicolas Pitref77814d2005-02-08 17:11:19 +00001519 chip->state = mode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
Nicolas Pitrec1724712006-03-30 15:52:41 +01001521 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1522 adr, map_bankwidth(map),
Anders Grafströme93cafe2008-08-05 18:37:41 +02001523 chip->word_write_time,
1524 chip->word_write_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001525 if (ret) {
1526 xip_enable(map, chip, adr);
1527 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
1528 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530
Nicolas Pitre48436532005-08-06 05:16:52 +01001531 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001532 status = map_read(map, adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001533 if (map_word_bitsset(map, status, CMD(0x1a))) {
1534 unsigned long chipstatus = MERGESTATUS(status);
1535
1536 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 map_write(map, CMD(0x50), adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 map_write(map, CMD(0x70), adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001539 xip_enable(map, chip, adr);
1540
1541 if (chipstatus & 0x02) {
1542 ret = -EROFS;
1543 } else if (chipstatus & 0x08) {
1544 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1545 ret = -EIO;
1546 } else {
1547 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1548 ret = -EINVAL;
1549 }
1550
1551 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 }
1553
1554 xip_enable(map, chip, adr);
1555 out: put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001556 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557 return ret;
1558}
1559
1560
1561static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1562{
1563 struct map_info *map = mtd->priv;
1564 struct cfi_private *cfi = map->fldrv_priv;
1565 int ret = 0;
1566 int chipnum;
1567 unsigned long ofs;
1568
1569 *retlen = 0;
1570 if (!len)
1571 return 0;
1572
1573 chipnum = to >> cfi->chipshift;
1574 ofs = to - (chipnum << cfi->chipshift);
1575
1576 /* If it's not bus-aligned, do the first byte write */
1577 if (ofs & (map_bankwidth(map)-1)) {
1578 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1579 int gap = ofs - bus_ofs;
1580 int n;
1581 map_word datum;
1582
1583 n = min_t(int, len, map_bankwidth(map)-gap);
1584 datum = map_word_ff(map);
1585 datum = map_word_load_partial(map, datum, buf, gap, n);
1586
1587 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001588 bus_ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001589 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 return ret;
1591
1592 len -= n;
1593 ofs += n;
1594 buf += n;
1595 (*retlen) += n;
1596
1597 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001598 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599 ofs = 0;
1600 if (chipnum == cfi->numchips)
1601 return 0;
1602 }
1603 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001604
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 while(len >= map_bankwidth(map)) {
1606 map_word datum = map_word_load(map, buf);
1607
1608 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001609 ofs, datum, FL_WRITING);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 if (ret)
1611 return ret;
1612
1613 ofs += map_bankwidth(map);
1614 buf += map_bankwidth(map);
1615 (*retlen) += map_bankwidth(map);
1616 len -= map_bankwidth(map);
1617
1618 if (ofs >> cfi->chipshift) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001619 chipnum ++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 ofs = 0;
1621 if (chipnum == cfi->numchips)
1622 return 0;
1623 }
1624 }
1625
1626 if (len & (map_bankwidth(map)-1)) {
1627 map_word datum;
1628
1629 datum = map_word_ff(map);
1630 datum = map_word_load_partial(map, datum, buf, 0, len);
1631
1632 ret = do_write_oneword(map, &cfi->chips[chipnum],
Nicolas Pitref77814d2005-02-08 17:11:19 +00001633 ofs, datum, FL_WRITING);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001634 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 return ret;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001636
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637 (*retlen) += len;
1638 }
1639
1640 return 0;
1641}
1642
1643
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001644static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
Nicolas Pitree102d542005-08-06 05:46:59 +01001645 unsigned long adr, const struct kvec **pvec,
1646 unsigned long *pvec_seek, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647{
1648 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001649 map_word status, write_cmd, datum;
1650 unsigned long cmd_adr;
1651 int ret, wbufsize, word_gap, words;
Nicolas Pitree102d542005-08-06 05:46:59 +01001652 const struct kvec *vec;
1653 unsigned long vec_seek;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001654 unsigned long initial_adr;
1655 int initial_len = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656
1657 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1658 adr += chip->start;
Massimo Cirillo646fd122008-01-11 10:24:11 +00001659 initial_adr = adr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 cmd_adr = adr & ~(wbufsize-1);
Nicolas Pitre638d9832005-08-06 05:40:46 +01001661
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 /* Let's determine this according to the interleave only once */
Guillaume LECERFb5d194c2010-10-26 10:55:29 +01001663 write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0xe8) : CMD(0xe9);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001665 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1667 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001668 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 return ret;
1670 }
1671
Massimo Cirillo646fd122008-01-11 10:24:11 +00001672 XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 ENABLE_VPP(map);
1674 xip_disable(map, chip, cmd_adr);
1675
David Woodhouse151e7652006-05-14 01:51:54 +01001676 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001677 [...], the device will not accept any more Write to Buffer commands".
Linus Torvalds1da177e2005-04-16 15:20:36 -07001678 So we must check here and reset those bits if they're set. Otherwise
1679 we're just pissing in the wind */
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001680 if (chip->state != FL_STATUS) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 map_write(map, CMD(0x70), cmd_adr);
Nicolas Pitre6e7a6802006-03-29 23:31:42 +01001682 chip->state = FL_STATUS;
1683 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684 status = map_read(map, cmd_adr);
1685 if (map_word_bitsset(map, status, CMD(0x30))) {
1686 xip_enable(map, chip, cmd_adr);
1687 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1688 xip_disable(map, chip, cmd_adr);
1689 map_write(map, CMD(0x50), cmd_adr);
1690 map_write(map, CMD(0x70), cmd_adr);
1691 }
1692
1693 chip->state = FL_WRITING_TO_BUFFER;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001694 map_write(map, write_cmd, cmd_adr);
Anders Grafströme93cafe2008-08-05 18:37:41 +02001695 ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0, 0);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001696 if (ret) {
1697 /* Argh. Not ready for write to buffer */
1698 map_word Xstatus = map_read(map, cmd_adr);
1699 map_write(map, CMD(0x70), cmd_adr);
1700 chip->state = FL_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 status = map_read(map, cmd_adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001702 map_write(map, CMD(0x50), cmd_adr);
1703 map_write(map, CMD(0x70), cmd_adr);
1704 xip_enable(map, chip, cmd_adr);
1705 printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
1706 map->name, Xstatus.x[0], status.x[0]);
1707 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 }
1709
Nicolas Pitree102d542005-08-06 05:46:59 +01001710 /* Figure out the number of words to write */
1711 word_gap = (-adr & (map_bankwidth(map)-1));
Julia Lawallc8872b02008-08-02 17:14:21 +02001712 words = DIV_ROUND_UP(len - word_gap, map_bankwidth(map));
Nicolas Pitree102d542005-08-06 05:46:59 +01001713 if (!word_gap) {
1714 words--;
1715 } else {
1716 word_gap = map_bankwidth(map) - word_gap;
1717 adr -= word_gap;
1718 datum = map_word_ff(map);
1719 }
1720
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 /* Write length of data to come */
Nicolas Pitree102d542005-08-06 05:46:59 +01001722 map_write(map, CMD(words), cmd_adr );
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723
1724 /* Write data */
Nicolas Pitree102d542005-08-06 05:46:59 +01001725 vec = *pvec;
1726 vec_seek = *pvec_seek;
1727 do {
1728 int n = map_bankwidth(map) - word_gap;
1729 if (n > vec->iov_len - vec_seek)
1730 n = vec->iov_len - vec_seek;
1731 if (n > len)
1732 n = len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733
Nicolas Pitree102d542005-08-06 05:46:59 +01001734 if (!word_gap && len < map_bankwidth(map))
1735 datum = map_word_ff(map);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Nicolas Pitree102d542005-08-06 05:46:59 +01001737 datum = map_word_load_partial(map, datum,
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001738 vec->iov_base + vec_seek,
Nicolas Pitree102d542005-08-06 05:46:59 +01001739 word_gap, n);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740
Nicolas Pitree102d542005-08-06 05:46:59 +01001741 len -= n;
1742 word_gap += n;
1743 if (!len || word_gap == map_bankwidth(map)) {
1744 map_write(map, datum, adr);
1745 adr += map_bankwidth(map);
1746 word_gap = 0;
1747 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
Nicolas Pitree102d542005-08-06 05:46:59 +01001749 vec_seek += n;
1750 if (vec_seek == vec->iov_len) {
1751 vec++;
1752 vec_seek = 0;
1753 }
1754 } while (len);
1755 *pvec = vec;
1756 *pvec_seek = vec_seek;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
1758 /* GO GO GO */
1759 map_write(map, CMD(0xd0), cmd_adr);
1760 chip->state = FL_WRITING;
1761
Nicolas Pitrec1724712006-03-30 15:52:41 +01001762 ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
Massimo Cirillo646fd122008-01-11 10:24:11 +00001763 initial_adr, initial_len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001764 chip->buffer_write_time,
1765 chip->buffer_write_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001766 if (ret) {
1767 map_write(map, CMD(0x70), cmd_adr);
1768 chip->state = FL_STATUS;
1769 xip_enable(map, chip, cmd_adr);
1770 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
1771 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
Nicolas Pitre48436532005-08-06 05:16:52 +01001774 /* check for errors */
Nicolas Pitrec1724712006-03-30 15:52:41 +01001775 status = map_read(map, cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001776 if (map_word_bitsset(map, status, CMD(0x1a))) {
1777 unsigned long chipstatus = MERGESTATUS(status);
1778
1779 /* reset status */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780 map_write(map, CMD(0x50), cmd_adr);
Nicolas Pitre48436532005-08-06 05:16:52 +01001781 map_write(map, CMD(0x70), cmd_adr);
1782 xip_enable(map, chip, cmd_adr);
1783
1784 if (chipstatus & 0x02) {
1785 ret = -EROFS;
1786 } else if (chipstatus & 0x08) {
1787 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1788 ret = -EIO;
1789 } else {
1790 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1791 ret = -EINVAL;
1792 }
1793
1794 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
1796
1797 xip_enable(map, chip, cmd_adr);
1798 out: put_chip(map, chip, cmd_adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001799 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800 return ret;
1801}
1802
Nicolas Pitree102d542005-08-06 05:46:59 +01001803static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1804 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805{
1806 struct map_info *map = mtd->priv;
1807 struct cfi_private *cfi = map->fldrv_priv;
1808 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1809 int ret = 0;
1810 int chipnum;
Nicolas Pitree102d542005-08-06 05:46:59 +01001811 unsigned long ofs, vec_seek, i;
1812 size_t len = 0;
1813
1814 for (i = 0; i < count; i++)
1815 len += vecs[i].iov_len;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816
1817 *retlen = 0;
1818 if (!len)
1819 return 0;
1820
1821 chipnum = to >> cfi->chipshift;
Nicolas Pitree102d542005-08-06 05:46:59 +01001822 ofs = to - (chipnum << cfi->chipshift);
1823 vec_seek = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Nicolas Pitree102d542005-08-06 05:46:59 +01001825 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 /* We must not cross write block boundaries */
1827 int size = wbufsize - (ofs & (wbufsize-1));
1828
1829 if (size > len)
1830 size = len;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001831 ret = do_write_buffer(map, &cfi->chips[chipnum],
Nicolas Pitree102d542005-08-06 05:46:59 +01001832 ofs, &vecs, &vec_seek, size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 if (ret)
1834 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836 ofs += size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 (*retlen) += size;
1838 len -= size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839
1840 if (ofs >> cfi->chipshift) {
1841 chipnum ++;
1842 ofs = 0;
1843 if (chipnum == cfi->numchips)
1844 return 0;
1845 }
Josh Boyerdf54b52c2005-12-06 17:28:19 +00001846
1847 /* Be nice and reschedule with the chip in a usable state for other
1848 processes. */
1849 cond_resched();
1850
Nicolas Pitree102d542005-08-06 05:46:59 +01001851 } while (len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 return 0;
1854}
1855
Nicolas Pitree102d542005-08-06 05:46:59 +01001856static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1857 size_t len, size_t *retlen, const u_char *buf)
1858{
1859 struct kvec vec;
1860
1861 vec.iov_base = (void *) buf;
1862 vec.iov_len = len;
1863
1864 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1865}
1866
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1868 unsigned long adr, int len, void *thunk)
1869{
1870 struct cfi_private *cfi = map->fldrv_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001871 map_word status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 int retries = 3;
Nicolas Pitrec1724712006-03-30 15:52:41 +01001873 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874
1875 adr += chip->start;
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 retry:
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001878 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 ret = get_chip(map, chip, adr, FL_ERASING);
1880 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001881 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 return ret;
1883 }
1884
1885 XIP_INVAL_CACHED_RANGE(map, adr, len);
1886 ENABLE_VPP(map);
1887 xip_disable(map, chip, adr);
1888
1889 /* Clear the status register first */
1890 map_write(map, CMD(0x50), adr);
1891
1892 /* Now erase */
1893 map_write(map, CMD(0x20), adr);
1894 map_write(map, CMD(0xD0), adr);
1895 chip->state = FL_ERASING;
1896 chip->erase_suspended = 0;
1897
Nicolas Pitrec1724712006-03-30 15:52:41 +01001898 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1899 adr, len,
Anders Grafströme93cafe2008-08-05 18:37:41 +02001900 chip->erase_time,
1901 chip->erase_time_max);
Nicolas Pitrec1724712006-03-30 15:52:41 +01001902 if (ret) {
1903 map_write(map, CMD(0x70), adr);
1904 chip->state = FL_STATUS;
1905 xip_enable(map, chip, adr);
1906 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
1907 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 }
1909
1910 /* We've broken this before. It doesn't hurt to be safe */
1911 map_write(map, CMD(0x70), adr);
1912 chip->state = FL_STATUS;
1913 status = map_read(map, adr);
1914
Nicolas Pitre48436532005-08-06 05:16:52 +01001915 /* check for errors */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916 if (map_word_bitsset(map, status, CMD(0x3a))) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001917 unsigned long chipstatus = MERGESTATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918
1919 /* Reset the error bits */
1920 map_write(map, CMD(0x50), adr);
1921 map_write(map, CMD(0x70), adr);
1922 xip_enable(map, chip, adr);
1923
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 if ((chipstatus & 0x30) == 0x30) {
Nicolas Pitre48436532005-08-06 05:16:52 +01001925 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1926 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 } else if (chipstatus & 0x02) {
1928 /* Protection bit set */
1929 ret = -EROFS;
1930 } else if (chipstatus & 0x8) {
1931 /* Voltage */
Nicolas Pitre48436532005-08-06 05:16:52 +01001932 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001933 ret = -EIO;
Nicolas Pitre48436532005-08-06 05:16:52 +01001934 } else if (chipstatus & 0x20 && retries--) {
1935 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
Nicolas Pitre48436532005-08-06 05:16:52 +01001936 put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001937 mutex_unlock(&chip->mutex);
Nicolas Pitre48436532005-08-06 05:16:52 +01001938 goto retry;
1939 } else {
1940 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 -07001941 ret = -EIO;
1942 }
Nicolas Pitre48436532005-08-06 05:16:52 +01001943
1944 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945 }
1946
Nicolas Pitre48436532005-08-06 05:16:52 +01001947 xip_enable(map, chip, adr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 out: put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001949 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 return ret;
1951}
1952
Ben Dooks029a9eb2007-05-28 20:11:37 +01001953static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954{
1955 unsigned long ofs, len;
1956 int ret;
1957
1958 ofs = instr->addr;
1959 len = instr->len;
1960
1961 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
1962 if (ret)
1963 return ret;
1964
1965 instr->state = MTD_ERASE_DONE;
1966 mtd_erase_callback(instr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 return 0;
1969}
1970
1971static void cfi_intelext_sync (struct mtd_info *mtd)
1972{
1973 struct map_info *map = mtd->priv;
1974 struct cfi_private *cfi = map->fldrv_priv;
1975 int i;
1976 struct flchip *chip;
1977 int ret = 0;
1978
1979 for (i=0; !ret && i<cfi->numchips; i++) {
1980 chip = &cfi->chips[i];
1981
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001982 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 ret = get_chip(map, chip, chip->start, FL_SYNCING);
1984
1985 if (!ret) {
1986 chip->oldstate = chip->state;
1987 chip->state = FL_SYNCING;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00001988 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989 * as the whole point is that nobody can do anything
1990 * with the chip now anyway.
1991 */
1992 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02001993 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 }
1995
1996 /* Unlock the chips again */
1997
1998 for (i--; i >=0; i--) {
1999 chip = &cfi->chips[i];
2000
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002001 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 if (chip->state == FL_SYNCING) {
2004 chip->state = chip->oldstate;
Nicolas Pitre09c79332005-03-16 22:41:09 +00002005 chip->oldstate = FL_READY;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 wake_up(&chip->wq);
2007 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002008 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 }
2010}
2011
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002012static int __xipram do_getlockstatus_oneblock(struct map_info *map,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 struct flchip *chip,
2014 unsigned long adr,
2015 int len, void *thunk)
2016{
2017 struct cfi_private *cfi = map->fldrv_priv;
2018 int status, ofs_factor = cfi->interleave * cfi->device_type;
2019
Todd Poynorc25bb1f2005-04-27 21:01:52 +01002020 adr += chip->start;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 xip_disable(map, chip, adr+(2*ofs_factor));
Todd Poynorc25bb1f2005-04-27 21:01:52 +01002022 map_write(map, CMD(0x90), adr+(2*ofs_factor));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002023 chip->state = FL_JEDEC_QUERY;
2024 status = cfi_read_query(map, adr+(2*ofs_factor));
2025 xip_enable(map, chip, 0);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002026 return status;
2027}
2028
2029#ifdef DEBUG_LOCK_BITS
2030static int __xipram do_printlockstatus_oneblock(struct map_info *map,
2031 struct flchip *chip,
2032 unsigned long adr,
2033 int len, void *thunk)
2034{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002035 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002036 adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 return 0;
2038}
2039#endif
2040
2041#define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
2042#define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
2043
2044static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
2045 unsigned long adr, int len, void *thunk)
2046{
2047 struct cfi_private *cfi = map->fldrv_priv;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002048 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Nicolas Pitrec1724712006-03-30 15:52:41 +01002049 int udelay;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002050 int ret;
2051
2052 adr += chip->start;
2053
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002054 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002055 ret = get_chip(map, chip, adr, FL_LOCKING);
2056 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002057 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058 return ret;
2059 }
2060
2061 ENABLE_VPP(map);
2062 xip_disable(map, chip, adr);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002063
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 map_write(map, CMD(0x60), adr);
2065 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2066 map_write(map, CMD(0x01), adr);
2067 chip->state = FL_LOCKING;
2068 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2069 map_write(map, CMD(0xD0), adr);
2070 chip->state = FL_UNLOCKING;
2071 } else
2072 BUG();
2073
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002074 /*
2075 * If Instant Individual Block Locking supported then no need
2076 * to delay.
2077 */
Nicolas Pitrec1724712006-03-30 15:52:41 +01002078 udelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1000000/HZ : 0;
Todd Poynor9a6e73e2005-03-29 23:06:40 +01002079
Anders Grafströme93cafe2008-08-05 18:37:41 +02002080 ret = WAIT_TIMEOUT(map, chip, adr, udelay, udelay * 100);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002081 if (ret) {
2082 map_write(map, CMD(0x70), adr);
2083 chip->state = FL_STATUS;
2084 xip_enable(map, chip, adr);
2085 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
2086 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002088
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 xip_enable(map, chip, adr);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002090out: put_chip(map, chip, adr);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002091 mutex_unlock(&chip->mutex);
Nicolas Pitrec1724712006-03-30 15:52:41 +01002092 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093}
2094
Adrian Hunter69423d92008-12-10 13:37:21 +00002095static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
2097 int ret;
2098
2099#ifdef DEBUG_LOCK_BITS
2100 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002101 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002103 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104#endif
2105
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002106 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002108
Linus Torvalds1da177e2005-04-16 15:20:36 -07002109#ifdef DEBUG_LOCK_BITS
2110 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002111 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002113 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114#endif
2115
2116 return ret;
2117}
2118
Adrian Hunter69423d92008-12-10 13:37:21 +00002119static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120{
2121 int ret;
2122
2123#ifdef DEBUG_LOCK_BITS
2124 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002125 __func__, ofs, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002127 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128#endif
2129
2130 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2131 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002132
Linus Torvalds1da177e2005-04-16 15:20:36 -07002133#ifdef DEBUG_LOCK_BITS
2134 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
Harvey Harrisoncb53b3b2008-04-18 13:44:19 -07002135 __func__, ret);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002136 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
Randy Dunlap1da1caf2007-06-28 23:00:09 +01002137 ofs, len, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138#endif
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 return ret;
2141}
2142
Richard Cochran99384242010-06-14 18:10:33 +02002143static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
2144 uint64_t len)
2145{
2146 return cfi_varsize_frob(mtd, do_getlockstatus_oneblock,
2147 ofs, len, NULL) ? 1 : 0;
2148}
2149
Nicolas Pitref77814d2005-02-08 17:11:19 +00002150#ifdef CONFIG_MTD_OTP
2151
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002152typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002153 u_long data_offset, u_char *buf, u_int size,
2154 u_long prot_offset, u_int groupno, u_int groupsize);
2155
2156static int __xipram
2157do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
2158 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2159{
2160 struct cfi_private *cfi = map->fldrv_priv;
2161 int ret;
2162
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002163 mutex_lock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002164 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2165 if (ret) {
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002166 mutex_unlock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002167 return ret;
2168 }
2169
2170 /* let's ensure we're not reading back cached data from array mode */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002171 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002172
2173 xip_disable(map, chip, chip->start);
2174 if (chip->state != FL_JEDEC_QUERY) {
2175 map_write(map, CMD(0x90), chip->start);
2176 chip->state = FL_JEDEC_QUERY;
2177 }
2178 map_copy_from(map, buf, chip->start + offset, size);
2179 xip_enable(map, chip, chip->start);
2180
2181 /* then ensure we don't keep OTP data in the cache */
Nicolas Pitre6da70122005-05-19 18:05:47 +01002182 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002183
2184 put_chip(map, chip, chip->start);
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002185 mutex_unlock(&chip->mutex);
Nicolas Pitref77814d2005-02-08 17:11:19 +00002186 return 0;
2187}
2188
2189static int
2190do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2191 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2192{
2193 int ret;
2194
2195 while (size) {
2196 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2197 int gap = offset - bus_ofs;
2198 int n = min_t(int, size, map_bankwidth(map)-gap);
2199 map_word datum = map_word_ff(map);
2200
2201 datum = map_word_load_partial(map, datum, buf, gap, n);
2202 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002203 if (ret)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002204 return ret;
2205
2206 offset += n;
2207 buf += n;
2208 size -= n;
2209 }
2210
2211 return 0;
2212}
2213
2214static int
2215do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2216 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2217{
2218 struct cfi_private *cfi = map->fldrv_priv;
2219 map_word datum;
2220
2221 /* make sure area matches group boundaries */
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002222 if (size != grpsz)
Nicolas Pitref77814d2005-02-08 17:11:19 +00002223 return -EXDEV;
2224
2225 datum = map_word_ff(map);
2226 datum = map_word_clr(map, datum, CMD(1 << grpno));
2227 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2228}
2229
2230static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2231 size_t *retlen, u_char *buf,
2232 otp_op_t action, int user_regs)
2233{
2234 struct map_info *map = mtd->priv;
2235 struct cfi_private *cfi = map->fldrv_priv;
2236 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2237 struct flchip *chip;
2238 struct cfi_intelext_otpinfo *otp;
2239 u_long devsize, reg_prot_offset, data_offset;
2240 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2241 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2242 int ret;
2243
2244 *retlen = 0;
2245
2246 /* Check that we actually have some OTP registers */
2247 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2248 return -ENODATA;
2249
2250 /* we need real chips here not virtual ones */
2251 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2252 chip_step = devsize >> cfi->chipshift;
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002253 chip_num = 0;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002254
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002255 /* Some chips have OTP located in the _top_ partition only.
2256 For example: Intel 28F256L18T (T means top-parameter device) */
Hans-Christian Egtvedtb2ef1a22009-11-05 15:53:43 +01002257 if (cfi->mfr == CFI_MFR_INTEL) {
Nicolas Pitredce2b4d2005-04-01 17:36:29 +01002258 switch (cfi->id) {
2259 case 0x880b:
2260 case 0x880c:
2261 case 0x880d:
2262 chip_num = chip_step - 1;
2263 }
2264 }
2265
2266 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002267 chip = &cfi->chips[chip_num];
2268 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2269
2270 /* first OTP region */
2271 field = 0;
2272 reg_prot_offset = extp->ProtRegAddr;
2273 reg_fact_groups = 1;
2274 reg_fact_size = 1 << extp->FactProtRegSize;
2275 reg_user_groups = 1;
2276 reg_user_size = 1 << extp->UserProtRegSize;
2277
2278 while (len > 0) {
2279 /* flash geometry fixup */
2280 data_offset = reg_prot_offset + 1;
2281 data_offset *= cfi->interleave * cfi->device_type;
2282 reg_prot_offset *= cfi->interleave * cfi->device_type;
2283 reg_fact_size *= cfi->interleave;
2284 reg_user_size *= cfi->interleave;
2285
2286 if (user_regs) {
2287 groups = reg_user_groups;
2288 groupsize = reg_user_size;
2289 /* skip over factory reg area */
2290 groupno = reg_fact_groups;
2291 data_offset += reg_fact_groups * reg_fact_size;
2292 } else {
2293 groups = reg_fact_groups;
2294 groupsize = reg_fact_size;
2295 groupno = 0;
2296 }
2297
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002298 while (len > 0 && groups > 0) {
Nicolas Pitref77814d2005-02-08 17:11:19 +00002299 if (!action) {
2300 /*
2301 * Special case: if action is NULL
2302 * we fill buf with otp_info records.
2303 */
2304 struct otp_info *otpinfo;
2305 map_word lockword;
2306 len -= sizeof(struct otp_info);
2307 if (len <= 0)
2308 return -ENOSPC;
2309 ret = do_otp_read(map, chip,
2310 reg_prot_offset,
2311 (u_char *)&lockword,
2312 map_bankwidth(map),
2313 0, 0, 0);
2314 if (ret)
2315 return ret;
2316 otpinfo = (struct otp_info *)buf;
2317 otpinfo->start = from;
2318 otpinfo->length = groupsize;
2319 otpinfo->locked =
2320 !map_word_bitsset(map, lockword,
2321 CMD(1 << groupno));
2322 from += groupsize;
2323 buf += sizeof(*otpinfo);
2324 *retlen += sizeof(*otpinfo);
2325 } else if (from >= groupsize) {
2326 from -= groupsize;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002327 data_offset += groupsize;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002328 } else {
2329 int size = groupsize;
2330 data_offset += from;
2331 size -= from;
2332 from = 0;
2333 if (size > len)
2334 size = len;
2335 ret = action(map, chip, data_offset,
2336 buf, size, reg_prot_offset,
2337 groupno, groupsize);
2338 if (ret < 0)
2339 return ret;
2340 buf += size;
2341 len -= size;
2342 *retlen += size;
Nicolas Pitre332d71f2005-02-17 20:35:04 +00002343 data_offset += size;
Nicolas Pitref77814d2005-02-08 17:11:19 +00002344 }
2345 groupno++;
2346 groups--;
2347 }
2348
2349 /* next OTP region */
2350 if (++field == extp->NumProtectionFields)
2351 break;
2352 reg_prot_offset = otp->ProtRegAddr;
2353 reg_fact_groups = otp->FactGroups;
2354 reg_fact_size = 1 << otp->FactProtRegSize;
2355 reg_user_groups = otp->UserGroups;
2356 reg_user_size = 1 << otp->UserProtRegSize;
2357 otp++;
2358 }
2359 }
2360
2361 return 0;
2362}
2363
2364static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2365 size_t len, size_t *retlen,
2366 u_char *buf)
2367{
2368 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2369 buf, do_otp_read, 0);
2370}
2371
2372static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2373 size_t len, size_t *retlen,
2374 u_char *buf)
2375{
2376 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2377 buf, do_otp_read, 1);
2378}
2379
2380static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2381 size_t len, size_t *retlen,
2382 u_char *buf)
2383{
2384 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2385 buf, do_otp_write, 1);
2386}
2387
2388static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2389 loff_t from, size_t len)
2390{
2391 size_t retlen;
2392 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2393 NULL, do_otp_lock, 1);
2394}
2395
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002396static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd,
Nicolas Pitref77814d2005-02-08 17:11:19 +00002397 struct otp_info *buf, size_t len)
2398{
2399 size_t retlen;
2400 int ret;
2401
2402 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 0);
2403 return ret ? : retlen;
2404}
2405
2406static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd,
2407 struct otp_info *buf, size_t len)
2408{
2409 size_t retlen;
2410 int ret;
2411
2412 ret = cfi_intelext_otp_walk(mtd, 0, len, &retlen, (u_char *)buf, NULL, 1);
2413 return ret ? : retlen;
2414}
2415
2416#endif
2417
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002418static void cfi_intelext_save_locks(struct mtd_info *mtd)
2419{
2420 struct mtd_erase_region_info *region;
2421 int block, status, i;
2422 unsigned long adr;
2423 size_t len;
2424
2425 for (i = 0; i < mtd->numeraseregions; i++) {
2426 region = &mtd->eraseregions[i];
2427 if (!region->lockmap)
2428 continue;
2429
2430 for (block = 0; block < region->numblocks; block++){
2431 len = region->erasesize;
2432 adr = region->offset + block * len;
2433
2434 status = cfi_varsize_frob(mtd,
Ben Dooks029a9eb2007-05-28 20:11:37 +01002435 do_getlockstatus_oneblock, adr, len, NULL);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002436 if (status)
2437 set_bit(block, region->lockmap);
2438 else
2439 clear_bit(block, region->lockmap);
2440 }
2441 }
2442}
2443
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444static int cfi_intelext_suspend(struct mtd_info *mtd)
2445{
2446 struct map_info *map = mtd->priv;
2447 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002448 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002449 int i;
2450 struct flchip *chip;
2451 int ret = 0;
2452
Justin Treone619a752008-01-30 10:25:49 -08002453 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002454 && extp && (extp->FeatureSupport & (1 << 5)))
2455 cfi_intelext_save_locks(mtd);
2456
Linus Torvalds1da177e2005-04-16 15:20:36 -07002457 for (i=0; !ret && i<cfi->numchips; i++) {
2458 chip = &cfi->chips[i];
2459
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002460 mutex_lock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002461
2462 switch (chip->state) {
2463 case FL_READY:
2464 case FL_STATUS:
2465 case FL_CFI_QUERY:
2466 case FL_JEDEC_QUERY:
2467 if (chip->oldstate == FL_READY) {
David Andersa86aaa62006-10-19 19:33:19 +03002468 /* place the chip in a known state before suspend */
2469 map_write(map, CMD(0xFF), cfi->chips[i].start);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 chip->oldstate = chip->state;
2471 chip->state = FL_PM_SUSPENDED;
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002472 /* No need to wake_up() on this state change -
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 * as the whole point is that nobody can do anything
2474 * with the chip now anyway.
2475 */
2476 } else {
2477 /* There seems to be an operation pending. We must wait for it. */
2478 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2479 ret = -EAGAIN;
2480 }
2481 break;
2482 default:
2483 /* Should we actually wait? Once upon a time these routines weren't
2484 allowed to. Or should we return -EAGAIN, because the upper layers
2485 ought to have already shut down anything which was using the device
2486 anyway? The latter for now. */
2487 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->oldstate);
2488 ret = -EAGAIN;
2489 case FL_PM_SUSPENDED:
2490 break;
2491 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002492 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493 }
2494
2495 /* Unlock the chips again */
2496
2497 if (ret) {
2498 for (i--; i >=0; i--) {
2499 chip = &cfi->chips[i];
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002500
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002501 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002502
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503 if (chip->state == FL_PM_SUSPENDED) {
2504 /* No need to force it into a known state here,
2505 because we're returning failure, and it didn't
2506 get power cycled */
2507 chip->state = chip->oldstate;
2508 chip->oldstate = FL_READY;
2509 wake_up(&chip->wq);
2510 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002511 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002512 }
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002513 }
2514
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 return ret;
2516}
2517
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002518static void cfi_intelext_restore_locks(struct mtd_info *mtd)
2519{
2520 struct mtd_erase_region_info *region;
2521 int block, i;
2522 unsigned long adr;
2523 size_t len;
2524
2525 for (i = 0; i < mtd->numeraseregions; i++) {
2526 region = &mtd->eraseregions[i];
2527 if (!region->lockmap)
2528 continue;
2529
2530 for (block = 0; block < region->numblocks; block++) {
2531 len = region->erasesize;
2532 adr = region->offset + block * len;
2533
2534 if (!test_bit(block, region->lockmap))
2535 cfi_intelext_unlock(mtd, adr, len);
2536 }
2537 }
2538}
2539
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540static void cfi_intelext_resume(struct mtd_info *mtd)
2541{
2542 struct map_info *map = mtd->priv;
2543 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002544 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002545 int i;
2546 struct flchip *chip;
2547
2548 for (i=0; i<cfi->numchips; i++) {
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002549
Linus Torvalds1da177e2005-04-16 15:20:36 -07002550 chip = &cfi->chips[i];
2551
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002552 mutex_lock(&chip->mutex);
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002553
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 /* Go to known state. Chip may have been power cycled */
2555 if (chip->state == FL_PM_SUSPENDED) {
2556 map_write(map, CMD(0xFF), cfi->chips[i].start);
2557 chip->oldstate = chip->state = FL_READY;
2558 wake_up(&chip->wq);
2559 }
2560
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002561 mutex_unlock(&chip->mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002562 }
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002563
Justin Treone619a752008-01-30 10:25:49 -08002564 if ((mtd->flags & MTD_POWERUP_LOCK)
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002565 && extp && (extp->FeatureSupport & (1 << 5)))
2566 cfi_intelext_restore_locks(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567}
2568
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002569static int cfi_intelext_reset(struct mtd_info *mtd)
2570{
2571 struct map_info *map = mtd->priv;
2572 struct cfi_private *cfi = map->fldrv_priv;
2573 int i, ret;
2574
2575 for (i=0; i < cfi->numchips; i++) {
2576 struct flchip *chip = &cfi->chips[i];
2577
2578 /* force the completion of any ongoing operation
Thomas Gleixner1f948b42005-11-07 11:15:37 +00002579 and switch to array mode so any bootloader in
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002580 flash is accessible for soft reboot. */
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002581 mutex_lock(&chip->mutex);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002582 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002583 if (!ret) {
2584 map_write(map, CMD(0xff), chip->start);
Kevin Haoc4a9f882007-10-02 13:56:04 -07002585 chip->state = FL_SHUTDOWN;
Nicolas Pitrec9f7ec32009-10-23 16:02:42 -04002586 put_chip(map, chip, chip->start);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002587 }
Stefani Seiboldc4e77372010-04-18 22:46:44 +02002588 mutex_unlock(&chip->mutex);
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002589 }
2590
2591 return 0;
2592}
2593
2594static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2595 void *v)
2596{
2597 struct mtd_info *mtd;
2598
2599 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2600 cfi_intelext_reset(mtd);
2601 return NOTIFY_DONE;
2602}
2603
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604static void cfi_intelext_destroy(struct mtd_info *mtd)
2605{
2606 struct map_info *map = mtd->priv;
2607 struct cfi_private *cfi = map->fldrv_priv;
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002608 struct mtd_erase_region_info *region;
2609 int i;
Nicolas Pitre963a6fb2005-04-01 02:59:56 +01002610 cfi_intelext_reset(mtd);
2611 unregister_reboot_notifier(&mtd->reboot_notifier);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 kfree(cfi->cmdset_priv);
2613 kfree(cfi->cfiq);
2614 kfree(cfi->chips[0].priv);
2615 kfree(cfi);
Rodolfo Giometti0ecbc812007-03-26 21:45:43 -08002616 for (i = 0; i < mtd->numeraseregions; i++) {
2617 region = &mtd->eraseregions[i];
2618 if (region->lockmap)
2619 kfree(region->lockmap);
2620 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 kfree(mtd->eraseregions);
2622}
2623
Linus Torvalds1da177e2005-04-16 15:20:36 -07002624MODULE_LICENSE("GPL");
2625MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2626MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
David Woodhousea15bdee2006-05-08 22:35:05 +01002627MODULE_ALIAS("cfi_cmdset_0003");
2628MODULE_ALIAS("cfi_cmdset_0200");