2 * Common Flash Interface support:
3 * Intel Extended Vendor Command Set (ID 0x0001)
5 * (C) 2000 Red Hat. GPL'd
8 * 10/10/2000 Nicolas Pitre <nico@fluxnic.net>
9 * - completely revamped method functions so they are aware and
10 * independent of the flash geometry (buswidth, interleave, etc.)
11 * - scalability vs code size is completely set at compile-time
12 * (see include/linux/mtd/cfi.h for selection)
13 * - optimized write buffer method
14 * 02/05/2002 Christopher Hoover <ch@hpl.hp.com>/<ch@murgatroid.com>
15 * - reworked lock/unlock/erase support for var size flash
16 * 21/03/2007 Rodolfo Giometti <giometti@linux.it>
17 * - auto unlock sectors on resume for auto locking flash on power up
20 #include <linux/module.h>
21 #include <linux/types.h>
22 #include <linux/kernel.h>
23 #include <linux/sched.h>
25 #include <asm/byteorder.h>
27 #include <linux/errno.h>
28 #include <linux/slab.h>
29 #include <linux/delay.h>
30 #include <linux/interrupt.h>
31 #include <linux/reboot.h>
32 #include <linux/bitmap.h>
33 #include <linux/mtd/xip.h>
34 #include <linux/mtd/map.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/cfi.h>
38 /* #define CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE */
39 /* #define CMDSET0001_DISABLE_WRITE_SUSPEND */
41 // debugging, turns off buffer write mode if set to 1
42 #define FORCE_WORD_WRITE 0
45 #define I82802AB 0x00ad
46 #define I82802AC 0x00ac
47 #define PF38F4476 0x881c
48 #define M28F00AP30 0x8963
49 /* STMicroelectronics chips */
50 #define M50LPW080 0x002F
51 #define M50FLW080A 0x0080
52 #define M50FLW080B 0x0081
54 #define AT49BV640D 0x02de
55 #define AT49BV640DT 0x02db
57 #define LH28F640BFHE_PTTL90 0x00b0
58 #define LH28F640BFHE_PBTL90 0x00b1
59 #define LH28F640BFHE_PTTL70A 0x00b2
60 #define LH28F640BFHE_PBTL70A 0x00b3
62 static int cfi_intelext_read (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
63 static int cfi_intelext_write_words(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
64 static int cfi_intelext_write_buffers(struct mtd_info *, loff_t, size_t, size_t *, const u_char *);
65 static int cfi_intelext_writev(struct mtd_info *, const struct kvec *, unsigned long, loff_t, size_t *);
66 static int cfi_intelext_erase_varsize(struct mtd_info *, struct erase_info *);
67 static void cfi_intelext_sync (struct mtd_info *);
68 static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
69 static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len);
70 static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
73 static int cfi_intelext_read_fact_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
74 static int cfi_intelext_read_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
75 static int cfi_intelext_write_user_prot_reg (struct mtd_info *, loff_t, size_t, size_t *, u_char *);
76 static int cfi_intelext_lock_user_prot_reg (struct mtd_info *, loff_t, size_t);
77 static int cfi_intelext_get_fact_prot_info(struct mtd_info *, size_t,
78 size_t *, struct otp_info *);
79 static int cfi_intelext_get_user_prot_info(struct mtd_info *, size_t,
80 size_t *, struct otp_info *);
82 static int cfi_intelext_suspend (struct mtd_info *);
83 static void cfi_intelext_resume (struct mtd_info *);
84 static int cfi_intelext_reboot (struct notifier_block *, unsigned long, void *);
86 static void cfi_intelext_destroy(struct mtd_info *);
88 struct mtd_info *cfi_cmdset_0001(struct map_info *, int);
90 static struct mtd_info *cfi_intelext_setup (struct mtd_info *);
91 static int cfi_intelext_partition_fixup(struct mtd_info *, struct cfi_private **);
93 static int cfi_intelext_point (struct mtd_info *mtd, loff_t from, size_t len,
94 size_t *retlen, void **virt, resource_size_t *phys);
95 static int cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len);
97 static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
98 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode);
99 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr);
100 #include "fwh_lock.h"
105 * *********** SETUP AND PROBE BITS ***********
108 static struct mtd_chip_driver cfi_intelext_chipdrv = {
109 .probe = NULL, /* Not usable directly */
110 .destroy = cfi_intelext_destroy,
111 .name = "cfi_cmdset_0001",
112 .module = THIS_MODULE
115 /* #define DEBUG_LOCK_BITS */
116 /* #define DEBUG_CFI_FEATURES */
118 #ifdef DEBUG_CFI_FEATURES
119 static void cfi_tell_features(struct cfi_pri_intelext *extp)
122 printk(" Extended Query version %c.%c\n", extp->MajorVersion, extp->MinorVersion);
123 printk(" Feature/Command Support: %4.4X\n", extp->FeatureSupport);
124 printk(" - Chip Erase: %s\n", extp->FeatureSupport&1?"supported":"unsupported");
125 printk(" - Suspend Erase: %s\n", extp->FeatureSupport&2?"supported":"unsupported");
126 printk(" - Suspend Program: %s\n", extp->FeatureSupport&4?"supported":"unsupported");
127 printk(" - Legacy Lock/Unlock: %s\n", extp->FeatureSupport&8?"supported":"unsupported");
128 printk(" - Queued Erase: %s\n", extp->FeatureSupport&16?"supported":"unsupported");
129 printk(" - Instant block lock: %s\n", extp->FeatureSupport&32?"supported":"unsupported");
130 printk(" - Protection Bits: %s\n", extp->FeatureSupport&64?"supported":"unsupported");
131 printk(" - Page-mode read: %s\n", extp->FeatureSupport&128?"supported":"unsupported");
132 printk(" - Synchronous read: %s\n", extp->FeatureSupport&256?"supported":"unsupported");
133 printk(" - Simultaneous operations: %s\n", extp->FeatureSupport&512?"supported":"unsupported");
134 printk(" - Extended Flash Array: %s\n", extp->FeatureSupport&1024?"supported":"unsupported");
135 for (i=11; i<32; i++) {
136 if (extp->FeatureSupport & (1<<i))
137 printk(" - Unknown Bit %X: supported\n", i);
140 printk(" Supported functions after Suspend: %2.2X\n", extp->SuspendCmdSupport);
141 printk(" - Program after Erase Suspend: %s\n", extp->SuspendCmdSupport&1?"supported":"unsupported");
142 for (i=1; i<8; i++) {
143 if (extp->SuspendCmdSupport & (1<<i))
144 printk(" - Unknown Bit %X: supported\n", i);
147 printk(" Block Status Register Mask: %4.4X\n", extp->BlkStatusRegMask);
148 printk(" - Lock Bit Active: %s\n", extp->BlkStatusRegMask&1?"yes":"no");
149 printk(" - Lock-Down Bit Active: %s\n", extp->BlkStatusRegMask&2?"yes":"no");
150 for (i=2; i<3; i++) {
151 if (extp->BlkStatusRegMask & (1<<i))
152 printk(" - Unknown Bit %X Active: yes\n",i);
154 printk(" - EFA Lock Bit: %s\n", extp->BlkStatusRegMask&16?"yes":"no");
155 printk(" - EFA Lock-Down Bit: %s\n", extp->BlkStatusRegMask&32?"yes":"no");
156 for (i=6; i<16; i++) {
157 if (extp->BlkStatusRegMask & (1<<i))
158 printk(" - Unknown Bit %X Active: yes\n",i);
161 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
162 extp->VccOptimal >> 4, extp->VccOptimal & 0xf);
163 if (extp->VppOptimal)
164 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
165 extp->VppOptimal >> 4, extp->VppOptimal & 0xf);
169 /* Atmel chips don't use the same PRI format as Intel chips */
170 static void fixup_convert_atmel_pri(struct mtd_info *mtd)
172 struct map_info *map = mtd->priv;
173 struct cfi_private *cfi = map->fldrv_priv;
174 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
175 struct cfi_pri_atmel atmel_pri;
176 uint32_t features = 0;
178 /* Reverse byteswapping */
179 extp->FeatureSupport = cpu_to_le32(extp->FeatureSupport);
180 extp->BlkStatusRegMask = cpu_to_le16(extp->BlkStatusRegMask);
181 extp->ProtRegAddr = cpu_to_le16(extp->ProtRegAddr);
183 memcpy(&atmel_pri, extp, sizeof(atmel_pri));
184 memset((char *)extp + 5, 0, sizeof(*extp) - 5);
186 printk(KERN_ERR "atmel Features: %02x\n", atmel_pri.Features);
188 if (atmel_pri.Features & 0x01) /* chip erase supported */
190 if (atmel_pri.Features & 0x02) /* erase suspend supported */
192 if (atmel_pri.Features & 0x04) /* program suspend supported */
194 if (atmel_pri.Features & 0x08) /* simultaneous operations supported */
196 if (atmel_pri.Features & 0x20) /* page mode read supported */
198 if (atmel_pri.Features & 0x40) /* queued erase supported */
200 if (atmel_pri.Features & 0x80) /* Protection bits supported */
203 extp->FeatureSupport = features;
205 /* burst write mode not supported */
206 cfi->cfiq->BufWriteTimeoutTyp = 0;
207 cfi->cfiq->BufWriteTimeoutMax = 0;
210 static void fixup_at49bv640dx_lock(struct mtd_info *mtd)
212 struct map_info *map = mtd->priv;
213 struct cfi_private *cfi = map->fldrv_priv;
214 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
216 cfip->FeatureSupport |= (1 << 5);
217 mtd->flags |= MTD_POWERUP_LOCK;
220 #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
221 /* Some Intel Strata Flash prior to FPO revision C has bugs in this area */
222 static void fixup_intel_strataflash(struct mtd_info *mtd)
224 struct map_info *map = mtd->priv;
225 struct cfi_private *cfi = map->fldrv_priv;
226 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
228 printk(KERN_WARNING "cfi_cmdset_0001: Suspend "
229 "erase on write disabled.\n");
230 extp->SuspendCmdSupport &= ~1;
234 #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
235 static void fixup_no_write_suspend(struct mtd_info *mtd)
237 struct map_info *map = mtd->priv;
238 struct cfi_private *cfi = map->fldrv_priv;
239 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
241 if (cfip && (cfip->FeatureSupport&4)) {
242 cfip->FeatureSupport &= ~4;
243 printk(KERN_WARNING "cfi_cmdset_0001: write suspend disabled\n");
248 static void fixup_st_m28w320ct(struct mtd_info *mtd)
250 struct map_info *map = mtd->priv;
251 struct cfi_private *cfi = map->fldrv_priv;
253 cfi->cfiq->BufWriteTimeoutTyp = 0; /* Not supported */
254 cfi->cfiq->BufWriteTimeoutMax = 0; /* Not supported */
257 static void fixup_st_m28w320cb(struct mtd_info *mtd)
259 struct map_info *map = mtd->priv;
260 struct cfi_private *cfi = map->fldrv_priv;
262 /* Note this is done after the region info is endian swapped */
263 cfi->cfiq->EraseRegionInfo[1] =
264 (cfi->cfiq->EraseRegionInfo[1] & 0xffff0000) | 0x3e;
267 static int is_LH28F640BF(struct cfi_private *cfi)
269 /* Sharp LH28F640BF Family */
270 if (cfi->mfr == CFI_MFR_SHARP && (
271 cfi->id == LH28F640BFHE_PTTL90 || cfi->id == LH28F640BFHE_PBTL90 ||
272 cfi->id == LH28F640BFHE_PTTL70A || cfi->id == LH28F640BFHE_PBTL70A))
277 static void fixup_LH28F640BF(struct mtd_info *mtd)
279 struct map_info *map = mtd->priv;
280 struct cfi_private *cfi = map->fldrv_priv;
281 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
283 /* Reset the Partition Configuration Register on LH28F640BF
284 * to a single partition (PCR = 0x000): PCR is embedded into A0-A15. */
285 if (is_LH28F640BF(cfi)) {
286 printk(KERN_INFO "Reset Partition Config. Register: 1 Partition of 4 planes\n");
287 map_write(map, CMD(0x60), 0);
288 map_write(map, CMD(0x04), 0);
290 /* We have set one single partition thus
291 * Simultaneous Operations are not allowed */
292 printk(KERN_INFO "cfi_cmdset_0001: Simultaneous Operations disabled\n");
293 extp->FeatureSupport &= ~512;
297 static void fixup_use_point(struct mtd_info *mtd)
299 struct map_info *map = mtd->priv;
300 if (!mtd->_point && map_is_linear(map)) {
301 mtd->_point = cfi_intelext_point;
302 mtd->_unpoint = cfi_intelext_unpoint;
306 static void fixup_use_write_buffers(struct mtd_info *mtd)
308 struct map_info *map = mtd->priv;
309 struct cfi_private *cfi = map->fldrv_priv;
310 if (cfi->cfiq->BufWriteTimeoutTyp) {
311 printk(KERN_INFO "Using buffer write method\n" );
312 mtd->_write = cfi_intelext_write_buffers;
313 mtd->_writev = cfi_intelext_writev;
318 * Some chips power-up with all sectors locked by default.
320 static void fixup_unlock_powerup_lock(struct mtd_info *mtd)
322 struct map_info *map = mtd->priv;
323 struct cfi_private *cfi = map->fldrv_priv;
324 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
326 if (cfip->FeatureSupport&32) {
327 printk(KERN_INFO "Using auto-unlock on power-up/resume\n" );
328 mtd->flags |= MTD_POWERUP_LOCK;
332 static struct cfi_fixup cfi_fixup_table[] = {
333 { CFI_MFR_ATMEL, CFI_ID_ANY, fixup_convert_atmel_pri },
334 { CFI_MFR_ATMEL, AT49BV640D, fixup_at49bv640dx_lock },
335 { CFI_MFR_ATMEL, AT49BV640DT, fixup_at49bv640dx_lock },
336 #ifdef CMDSET0001_DISABLE_ERASE_SUSPEND_ON_WRITE
337 { CFI_MFR_ANY, CFI_ID_ANY, fixup_intel_strataflash },
339 #ifdef CMDSET0001_DISABLE_WRITE_SUSPEND
340 { CFI_MFR_ANY, CFI_ID_ANY, fixup_no_write_suspend },
342 #if !FORCE_WORD_WRITE
343 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_write_buffers },
345 { CFI_MFR_ST, 0x00ba, /* M28W320CT */ fixup_st_m28w320ct },
346 { CFI_MFR_ST, 0x00bb, /* M28W320CB */ fixup_st_m28w320cb },
347 { CFI_MFR_INTEL, CFI_ID_ANY, fixup_unlock_powerup_lock },
348 { CFI_MFR_SHARP, CFI_ID_ANY, fixup_unlock_powerup_lock },
349 { CFI_MFR_SHARP, CFI_ID_ANY, fixup_LH28F640BF },
353 static struct cfi_fixup jedec_fixup_table[] = {
354 { CFI_MFR_INTEL, I82802AB, fixup_use_fwh_lock },
355 { CFI_MFR_INTEL, I82802AC, fixup_use_fwh_lock },
356 { CFI_MFR_ST, M50LPW080, fixup_use_fwh_lock },
357 { CFI_MFR_ST, M50FLW080A, fixup_use_fwh_lock },
358 { CFI_MFR_ST, M50FLW080B, fixup_use_fwh_lock },
361 static struct cfi_fixup fixup_table[] = {
362 /* The CFI vendor ids and the JEDEC vendor IDs appear
363 * to be common. It is like the devices id's are as
364 * well. This table is to pick all cases where
365 * we know that is the case.
367 { CFI_MFR_ANY, CFI_ID_ANY, fixup_use_point },
371 static void cfi_fixup_major_minor(struct cfi_private *cfi,
372 struct cfi_pri_intelext *extp)
374 if (cfi->mfr == CFI_MFR_INTEL &&
375 cfi->id == PF38F4476 && extp->MinorVersion == '3')
376 extp->MinorVersion = '1';
379 static int cfi_is_micron_28F00AP30(struct cfi_private *cfi, struct flchip *chip)
382 * Micron(was Numonyx) 1Gbit bottom boot are buggy w.r.t
383 * Erase Supend for their small Erase Blocks(0x8000)
385 if (cfi->mfr == CFI_MFR_INTEL && cfi->id == M28F00AP30)
390 static inline struct cfi_pri_intelext *
391 read_pri_intelext(struct map_info *map, __u16 adr)
393 struct cfi_private *cfi = map->fldrv_priv;
394 struct cfi_pri_intelext *extp;
395 unsigned int extra_size = 0;
396 unsigned int extp_size = sizeof(*extp);
399 extp = (struct cfi_pri_intelext *)cfi_read_pri(map, adr, extp_size, "Intel/Sharp");
403 cfi_fixup_major_minor(cfi, extp);
405 if (extp->MajorVersion != '1' ||
406 (extp->MinorVersion < '0' || extp->MinorVersion > '5')) {
407 printk(KERN_ERR " Unknown Intel/Sharp Extended Query "
408 "version %c.%c.\n", extp->MajorVersion,
414 /* Do some byteswapping if necessary */
415 extp->FeatureSupport = le32_to_cpu(extp->FeatureSupport);
416 extp->BlkStatusRegMask = le16_to_cpu(extp->BlkStatusRegMask);
417 extp->ProtRegAddr = le16_to_cpu(extp->ProtRegAddr);
419 if (extp->MinorVersion >= '0') {
422 /* Protection Register info */
423 extra_size += (extp->NumProtectionFields - 1) *
424 sizeof(struct cfi_intelext_otpinfo);
427 if (extp->MinorVersion >= '1') {
428 /* Burst Read info */
430 if (extp_size < sizeof(*extp) + extra_size)
432 extra_size += extp->extra[extra_size - 1];
435 if (extp->MinorVersion >= '3') {
438 /* Number of hardware-partitions */
440 if (extp_size < sizeof(*extp) + extra_size)
442 nb_parts = extp->extra[extra_size - 1];
444 /* skip the sizeof(partregion) field in CFI 1.4 */
445 if (extp->MinorVersion >= '4')
448 for (i = 0; i < nb_parts; i++) {
449 struct cfi_intelext_regioninfo *rinfo;
450 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[extra_size];
451 extra_size += sizeof(*rinfo);
452 if (extp_size < sizeof(*extp) + extra_size)
454 rinfo->NumIdentPartitions=le16_to_cpu(rinfo->NumIdentPartitions);
455 extra_size += (rinfo->NumBlockTypes - 1)
456 * sizeof(struct cfi_intelext_blockinfo);
459 if (extp->MinorVersion >= '4')
460 extra_size += sizeof(struct cfi_intelext_programming_regioninfo);
462 if (extp_size < sizeof(*extp) + extra_size) {
464 extp_size = sizeof(*extp) + extra_size;
466 if (extp_size > 4096) {
468 "%s: cfi_pri_intelext is too fat\n",
479 struct mtd_info *cfi_cmdset_0001(struct map_info *map, int primary)
481 struct cfi_private *cfi = map->fldrv_priv;
482 struct mtd_info *mtd;
485 mtd = kzalloc(sizeof(*mtd), GFP_KERNEL);
489 mtd->type = MTD_NORFLASH;
491 /* Fill in the default mtd operations */
492 mtd->_erase = cfi_intelext_erase_varsize;
493 mtd->_read = cfi_intelext_read;
494 mtd->_write = cfi_intelext_write_words;
495 mtd->_sync = cfi_intelext_sync;
496 mtd->_lock = cfi_intelext_lock;
497 mtd->_unlock = cfi_intelext_unlock;
498 mtd->_is_locked = cfi_intelext_is_locked;
499 mtd->_suspend = cfi_intelext_suspend;
500 mtd->_resume = cfi_intelext_resume;
501 mtd->flags = MTD_CAP_NORFLASH;
502 mtd->name = map->name;
504 mtd->writebufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
506 mtd->reboot_notifier.notifier_call = cfi_intelext_reboot;
508 if (cfi->cfi_mode == CFI_MODE_CFI) {
510 * It's a real CFI chip, not one for which the probe
511 * routine faked a CFI structure. So we read the feature
514 __u16 adr = primary?cfi->cfiq->P_ADR:cfi->cfiq->A_ADR;
515 struct cfi_pri_intelext *extp;
517 extp = read_pri_intelext(map, adr);
523 /* Install our own private info structure */
524 cfi->cmdset_priv = extp;
526 cfi_fixup(mtd, cfi_fixup_table);
528 #ifdef DEBUG_CFI_FEATURES
529 /* Tell the user about it in lots of lovely detail */
530 cfi_tell_features(extp);
533 if(extp->SuspendCmdSupport & 1) {
534 printk(KERN_NOTICE "cfi_cmdset_0001: Erase suspend on write enabled\n");
537 else if (cfi->cfi_mode == CFI_MODE_JEDEC) {
538 /* Apply jedec specific fixups */
539 cfi_fixup(mtd, jedec_fixup_table);
541 /* Apply generic fixups */
542 cfi_fixup(mtd, fixup_table);
544 for (i=0; i< cfi->numchips; i++) {
545 if (cfi->cfiq->WordWriteTimeoutTyp)
546 cfi->chips[i].word_write_time =
547 1<<cfi->cfiq->WordWriteTimeoutTyp;
549 cfi->chips[i].word_write_time = 50000;
551 if (cfi->cfiq->BufWriteTimeoutTyp)
552 cfi->chips[i].buffer_write_time =
553 1<<cfi->cfiq->BufWriteTimeoutTyp;
554 /* No default; if it isn't specified, we won't use it */
556 if (cfi->cfiq->BlockEraseTimeoutTyp)
557 cfi->chips[i].erase_time =
558 1000<<cfi->cfiq->BlockEraseTimeoutTyp;
560 cfi->chips[i].erase_time = 2000000;
562 if (cfi->cfiq->WordWriteTimeoutTyp &&
563 cfi->cfiq->WordWriteTimeoutMax)
564 cfi->chips[i].word_write_time_max =
565 1<<(cfi->cfiq->WordWriteTimeoutTyp +
566 cfi->cfiq->WordWriteTimeoutMax);
568 cfi->chips[i].word_write_time_max = 50000 * 8;
570 if (cfi->cfiq->BufWriteTimeoutTyp &&
571 cfi->cfiq->BufWriteTimeoutMax)
572 cfi->chips[i].buffer_write_time_max =
573 1<<(cfi->cfiq->BufWriteTimeoutTyp +
574 cfi->cfiq->BufWriteTimeoutMax);
576 if (cfi->cfiq->BlockEraseTimeoutTyp &&
577 cfi->cfiq->BlockEraseTimeoutMax)
578 cfi->chips[i].erase_time_max =
579 1000<<(cfi->cfiq->BlockEraseTimeoutTyp +
580 cfi->cfiq->BlockEraseTimeoutMax);
582 cfi->chips[i].erase_time_max = 2000000 * 8;
584 cfi->chips[i].ref_point_counter = 0;
585 init_waitqueue_head(&(cfi->chips[i].wq));
588 map->fldrv = &cfi_intelext_chipdrv;
590 return cfi_intelext_setup(mtd);
592 struct mtd_info *cfi_cmdset_0003(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
593 struct mtd_info *cfi_cmdset_0200(struct map_info *map, int primary) __attribute__((alias("cfi_cmdset_0001")));
594 EXPORT_SYMBOL_GPL(cfi_cmdset_0001);
595 EXPORT_SYMBOL_GPL(cfi_cmdset_0003);
596 EXPORT_SYMBOL_GPL(cfi_cmdset_0200);
598 static struct mtd_info *cfi_intelext_setup(struct mtd_info *mtd)
600 struct map_info *map = mtd->priv;
601 struct cfi_private *cfi = map->fldrv_priv;
602 unsigned long offset = 0;
604 unsigned long devsize = (1<<cfi->cfiq->DevSize) * cfi->interleave;
606 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
608 mtd->size = devsize * cfi->numchips;
610 mtd->numeraseregions = cfi->cfiq->NumEraseRegions * cfi->numchips;
611 mtd->eraseregions = kmalloc(sizeof(struct mtd_erase_region_info)
612 * mtd->numeraseregions, GFP_KERNEL);
613 if (!mtd->eraseregions)
616 for (i=0; i<cfi->cfiq->NumEraseRegions; i++) {
617 unsigned long ernum, ersize;
618 ersize = ((cfi->cfiq->EraseRegionInfo[i] >> 8) & ~0xff) * cfi->interleave;
619 ernum = (cfi->cfiq->EraseRegionInfo[i] & 0xffff) + 1;
621 if (mtd->erasesize < ersize) {
622 mtd->erasesize = ersize;
624 for (j=0; j<cfi->numchips; j++) {
625 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].offset = (j*devsize)+offset;
626 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].erasesize = ersize;
627 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].numblocks = ernum;
628 mtd->eraseregions[(j*cfi->cfiq->NumEraseRegions)+i].lockmap = kmalloc(ernum / 8 + 1, GFP_KERNEL);
630 offset += (ersize * ernum);
633 if (offset != devsize) {
635 printk(KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
639 for (i=0; i<mtd->numeraseregions;i++){
640 printk(KERN_DEBUG "erase region %d: offset=0x%llx,size=0x%x,blocks=%d\n",
641 i,(unsigned long long)mtd->eraseregions[i].offset,
642 mtd->eraseregions[i].erasesize,
643 mtd->eraseregions[i].numblocks);
646 #ifdef CONFIG_MTD_OTP
647 mtd->_read_fact_prot_reg = cfi_intelext_read_fact_prot_reg;
648 mtd->_read_user_prot_reg = cfi_intelext_read_user_prot_reg;
649 mtd->_write_user_prot_reg = cfi_intelext_write_user_prot_reg;
650 mtd->_lock_user_prot_reg = cfi_intelext_lock_user_prot_reg;
651 mtd->_get_fact_prot_info = cfi_intelext_get_fact_prot_info;
652 mtd->_get_user_prot_info = cfi_intelext_get_user_prot_info;
655 /* This function has the potential to distort the reality
656 a bit and therefore should be called last. */
657 if (cfi_intelext_partition_fixup(mtd, &cfi) != 0)
660 __module_get(THIS_MODULE);
661 register_reboot_notifier(&mtd->reboot_notifier);
665 kfree(mtd->eraseregions);
667 kfree(cfi->cmdset_priv);
671 static int cfi_intelext_partition_fixup(struct mtd_info *mtd,
672 struct cfi_private **pcfi)
674 struct map_info *map = mtd->priv;
675 struct cfi_private *cfi = *pcfi;
676 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
679 * Probing of multi-partition flash chips.
681 * To support multiple partitions when available, we simply arrange
682 * for each of them to have their own flchip structure even if they
683 * are on the same physical chip. This means completely recreating
684 * a new cfi_private structure right here which is a blatent code
685 * layering violation, but this is still the least intrusive
686 * arrangement at this point. This can be rearranged in the future
687 * if someone feels motivated enough. --nico
689 if (extp && extp->MajorVersion == '1' && extp->MinorVersion >= '3'
690 && extp->FeatureSupport & (1 << 9)) {
691 struct cfi_private *newcfi;
693 struct flchip_shared *shared;
694 int offs, numregions, numparts, partshift, numvirtchips, i, j;
696 /* Protection Register info */
697 offs = (extp->NumProtectionFields - 1) *
698 sizeof(struct cfi_intelext_otpinfo);
700 /* Burst Read info */
701 offs += extp->extra[offs+1]+2;
703 /* Number of partition regions */
704 numregions = extp->extra[offs];
707 /* skip the sizeof(partregion) field in CFI 1.4 */
708 if (extp->MinorVersion >= '4')
711 /* Number of hardware partitions */
713 for (i = 0; i < numregions; i++) {
714 struct cfi_intelext_regioninfo *rinfo;
715 rinfo = (struct cfi_intelext_regioninfo *)&extp->extra[offs];
716 numparts += rinfo->NumIdentPartitions;
717 offs += sizeof(*rinfo)
718 + (rinfo->NumBlockTypes - 1) *
719 sizeof(struct cfi_intelext_blockinfo);
725 /* Programming Region info */
726 if (extp->MinorVersion >= '4') {
727 struct cfi_intelext_programming_regioninfo *prinfo;
728 prinfo = (struct cfi_intelext_programming_regioninfo *)&extp->extra[offs];
729 mtd->writesize = cfi->interleave << prinfo->ProgRegShift;
730 mtd->flags &= ~MTD_BIT_WRITEABLE;
731 printk(KERN_DEBUG "%s: program region size/ctrl_valid/ctrl_inval = %d/%d/%d\n",
732 map->name, mtd->writesize,
733 cfi->interleave * prinfo->ControlValid,
734 cfi->interleave * prinfo->ControlInvalid);
738 * All functions below currently rely on all chips having
739 * the same geometry so we'll just assume that all hardware
740 * partitions are of the same size too.
742 partshift = cfi->chipshift - __ffs(numparts);
744 if ((1 << partshift) < mtd->erasesize) {
746 "%s: bad number of hw partitions (%d)\n",
751 numvirtchips = cfi->numchips * numparts;
752 newcfi = kmalloc(sizeof(struct cfi_private) + numvirtchips * sizeof(struct flchip), GFP_KERNEL);
755 shared = kmalloc(sizeof(struct flchip_shared) * cfi->numchips, GFP_KERNEL);
760 memcpy(newcfi, cfi, sizeof(struct cfi_private));
761 newcfi->numchips = numvirtchips;
762 newcfi->chipshift = partshift;
764 chip = &newcfi->chips[0];
765 for (i = 0; i < cfi->numchips; i++) {
766 shared[i].writing = shared[i].erasing = NULL;
767 mutex_init(&shared[i].lock);
768 for (j = 0; j < numparts; j++) {
769 *chip = cfi->chips[i];
770 chip->start += j << partshift;
771 chip->priv = &shared[i];
772 /* those should be reset too since
773 they create memory references. */
774 init_waitqueue_head(&chip->wq);
775 mutex_init(&chip->mutex);
780 printk(KERN_DEBUG "%s: %d set(s) of %d interleaved chips "
781 "--> %d partitions of %d KiB\n",
782 map->name, cfi->numchips, cfi->interleave,
783 newcfi->numchips, 1<<(newcfi->chipshift-10));
785 map->fldrv_priv = newcfi;
794 * *********** CHIP ACCESS FUNCTIONS ***********
796 static int chip_ready (struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
798 DECLARE_WAITQUEUE(wait, current);
799 struct cfi_private *cfi = map->fldrv_priv;
800 map_word status, status_OK = CMD(0x80), status_PWS = CMD(0x01);
801 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
802 unsigned long timeo = jiffies + HZ;
804 /* Prevent setting state FL_SYNCING for chip in suspended state. */
805 if (mode == FL_SYNCING && chip->oldstate != FL_READY)
808 switch (chip->state) {
812 status = map_read(map, adr);
813 if (map_word_andequal(map, status, status_OK, status_OK))
816 /* At this point we're fine with write operations
817 in other partitions as they don't conflict. */
818 if (chip->priv && map_word_andequal(map, status, status_PWS, status_PWS))
821 mutex_unlock(&chip->mutex);
823 mutex_lock(&chip->mutex);
824 /* Someone else might have been playing with it. */
835 !(cfip->FeatureSupport & 2) ||
836 !(mode == FL_READY || mode == FL_POINT ||
837 (mode == FL_WRITING && (cfip->SuspendCmdSupport & 1))))
840 /* Do not allow suspend iff read/write to EB address */
841 if ((adr & chip->in_progress_block_mask) ==
842 chip->in_progress_block_addr)
845 /* do not suspend small EBs, buggy Micron Chips */
846 if (cfi_is_micron_28F00AP30(cfi, chip) &&
847 (chip->in_progress_block_mask == ~(0x8000-1)))
851 map_write(map, CMD(0xB0), chip->in_progress_block_addr);
853 /* If the flash has finished erasing, then 'erase suspend'
854 * appears to make some (28F320) flash devices switch to
855 * 'read' mode. Make sure that we switch to 'read status'
856 * mode so we get the right data. --rmk
858 map_write(map, CMD(0x70), chip->in_progress_block_addr);
859 chip->oldstate = FL_ERASING;
860 chip->state = FL_ERASE_SUSPENDING;
861 chip->erase_suspended = 1;
863 status = map_read(map, chip->in_progress_block_addr);
864 if (map_word_andequal(map, status, status_OK, status_OK))
867 if (time_after(jiffies, timeo)) {
868 /* Urgh. Resume and pretend we weren't here.
869 * Make sure we're in 'read status' mode if it had finished */
870 put_chip(map, chip, adr);
871 printk(KERN_ERR "%s: Chip not ready after erase "
872 "suspended: status = 0x%lx\n", map->name, status.x[0]);
876 mutex_unlock(&chip->mutex);
878 mutex_lock(&chip->mutex);
879 /* Nobody will touch it while it's in state FL_ERASE_SUSPENDING.
880 So we can just loop here. */
882 chip->state = FL_STATUS;
885 case FL_XIP_WHILE_ERASING:
886 if (mode != FL_READY && mode != FL_POINT &&
887 (mode != FL_WRITING || !cfip || !(cfip->SuspendCmdSupport&1)))
889 chip->oldstate = chip->state;
890 chip->state = FL_READY;
894 /* The machine is rebooting now,so no one can get chip anymore */
897 /* Only if there's no operation suspended... */
898 if (mode == FL_READY && chip->oldstate == FL_READY)
903 set_current_state(TASK_UNINTERRUPTIBLE);
904 add_wait_queue(&chip->wq, &wait);
905 mutex_unlock(&chip->mutex);
907 remove_wait_queue(&chip->wq, &wait);
908 mutex_lock(&chip->mutex);
913 static int get_chip(struct map_info *map, struct flchip *chip, unsigned long adr, int mode)
916 DECLARE_WAITQUEUE(wait, current);
920 (mode == FL_WRITING || mode == FL_ERASING || mode == FL_OTP_WRITE
921 || mode == FL_SHUTDOWN) && chip->state != FL_SYNCING) {
923 * OK. We have possibility for contention on the write/erase
924 * operations which are global to the real chip and not per
925 * partition. So let's fight it over in the partition which
926 * currently has authority on the operation.
928 * The rules are as follows:
930 * - any write operation must own shared->writing.
932 * - any erase operation must own _both_ shared->writing and
935 * - contention arbitration is handled in the owner's context.
937 * The 'shared' struct can be read and/or written only when
940 struct flchip_shared *shared = chip->priv;
941 struct flchip *contender;
942 mutex_lock(&shared->lock);
943 contender = shared->writing;
944 if (contender && contender != chip) {
946 * The engine to perform desired operation on this
947 * partition is already in use by someone else.
948 * Let's fight over it in the context of the chip
949 * currently using it. If it is possible to suspend,
950 * that other partition will do just that, otherwise
951 * it'll happily send us to sleep. In any case, when
952 * get_chip returns success we're clear to go ahead.
954 ret = mutex_trylock(&contender->mutex);
955 mutex_unlock(&shared->lock);
958 mutex_unlock(&chip->mutex);
959 ret = chip_ready(map, contender, contender->start, mode);
960 mutex_lock(&chip->mutex);
962 if (ret == -EAGAIN) {
963 mutex_unlock(&contender->mutex);
967 mutex_unlock(&contender->mutex);
970 mutex_lock(&shared->lock);
972 /* We should not own chip if it is already
973 * in FL_SYNCING state. Put contender and retry. */
974 if (chip->state == FL_SYNCING) {
975 put_chip(map, contender, contender->start);
976 mutex_unlock(&contender->mutex);
979 mutex_unlock(&contender->mutex);
982 /* Check if we already have suspended erase
983 * on this chip. Sleep. */
984 if (mode == FL_ERASING && shared->erasing
985 && shared->erasing->oldstate == FL_ERASING) {
986 mutex_unlock(&shared->lock);
987 set_current_state(TASK_UNINTERRUPTIBLE);
988 add_wait_queue(&chip->wq, &wait);
989 mutex_unlock(&chip->mutex);
991 remove_wait_queue(&chip->wq, &wait);
992 mutex_lock(&chip->mutex);
997 shared->writing = chip;
998 if (mode == FL_ERASING)
999 shared->erasing = chip;
1000 mutex_unlock(&shared->lock);
1002 ret = chip_ready(map, chip, adr, mode);
1009 static void put_chip(struct map_info *map, struct flchip *chip, unsigned long adr)
1011 struct cfi_private *cfi = map->fldrv_priv;
1014 struct flchip_shared *shared = chip->priv;
1015 mutex_lock(&shared->lock);
1016 if (shared->writing == chip && chip->oldstate == FL_READY) {
1017 /* We own the ability to write, but we're done */
1018 shared->writing = shared->erasing;
1019 if (shared->writing && shared->writing != chip) {
1020 /* give back ownership to who we loaned it from */
1021 struct flchip *loaner = shared->writing;
1022 mutex_lock(&loaner->mutex);
1023 mutex_unlock(&shared->lock);
1024 mutex_unlock(&chip->mutex);
1025 put_chip(map, loaner, loaner->start);
1026 mutex_lock(&chip->mutex);
1027 mutex_unlock(&loaner->mutex);
1031 shared->erasing = NULL;
1032 shared->writing = NULL;
1033 } else if (shared->erasing == chip && shared->writing != chip) {
1035 * We own the ability to erase without the ability
1036 * to write, which means the erase was suspended
1037 * and some other partition is currently writing.
1038 * Don't let the switch below mess things up since
1039 * we don't have ownership to resume anything.
1041 mutex_unlock(&shared->lock);
1045 mutex_unlock(&shared->lock);
1048 switch(chip->oldstate) {
1050 /* What if one interleaved chip has finished and the
1051 other hasn't? The old code would leave the finished
1052 one in READY mode. That's bad, and caused -EROFS
1053 errors to be returned from do_erase_oneblock because
1054 that's the only bit it checked for at the time.
1055 As the state machine appears to explicitly allow
1056 sending the 0x70 (Read Status) command to an erasing
1057 chip and expecting it to be ignored, that's what we
1059 map_write(map, CMD(0xd0), chip->in_progress_block_addr);
1060 map_write(map, CMD(0x70), chip->in_progress_block_addr);
1061 chip->oldstate = FL_READY;
1062 chip->state = FL_ERASING;
1065 case FL_XIP_WHILE_ERASING:
1066 chip->state = chip->oldstate;
1067 chip->oldstate = FL_READY;
1072 case FL_JEDEC_QUERY:
1075 printk(KERN_ERR "%s: put_chip() called with oldstate %d!!\n", map->name, chip->oldstate);
1080 #ifdef CONFIG_MTD_XIP
1083 * No interrupt what so ever can be serviced while the flash isn't in array
1084 * mode. This is ensured by the xip_disable() and xip_enable() functions
1085 * enclosing any code path where the flash is known not to be in array mode.
1086 * And within a XIP disabled code path, only functions marked with __xipram
1087 * may be called and nothing else (it's a good thing to inspect generated
1088 * assembly to make sure inline functions were actually inlined and that gcc
1089 * didn't emit calls to its own support functions). Also configuring MTD CFI
1090 * support to a single buswidth and a single interleave is also recommended.
1093 static void xip_disable(struct map_info *map, struct flchip *chip,
1096 /* TODO: chips with no XIP use should ignore and return */
1097 (void) map_read(map, adr); /* ensure mmu mapping is up to date */
1098 local_irq_disable();
1101 static void __xipram xip_enable(struct map_info *map, struct flchip *chip,
1104 struct cfi_private *cfi = map->fldrv_priv;
1105 if (chip->state != FL_POINT && chip->state != FL_READY) {
1106 map_write(map, CMD(0xff), adr);
1107 chip->state = FL_READY;
1109 (void) map_read(map, adr);
1115 * When a delay is required for the flash operation to complete, the
1116 * xip_wait_for_operation() function is polling for both the given timeout
1117 * and pending (but still masked) hardware interrupts. Whenever there is an
1118 * interrupt pending then the flash erase or write operation is suspended,
1119 * array mode restored and interrupts unmasked. Task scheduling might also
1120 * happen at that point. The CPU eventually returns from the interrupt or
1121 * the call to schedule() and the suspended flash operation is resumed for
1122 * the remaining of the delay period.
1124 * Warning: this function _will_ fool interrupt latency tracing tools.
1127 static int __xipram xip_wait_for_operation(
1128 struct map_info *map, struct flchip *chip,
1129 unsigned long adr, unsigned int chip_op_time_max)
1131 struct cfi_private *cfi = map->fldrv_priv;
1132 struct cfi_pri_intelext *cfip = cfi->cmdset_priv;
1133 map_word status, OK = CMD(0x80);
1134 unsigned long usec, suspended, start, done;
1135 flstate_t oldstate, newstate;
1137 start = xip_currtime();
1138 usec = chip_op_time_max;
1145 if (xip_irqpending() && cfip &&
1146 ((chip->state == FL_ERASING && (cfip->FeatureSupport&2)) ||
1147 (chip->state == FL_WRITING && (cfip->FeatureSupport&4))) &&
1148 (cfi_interleave_is_1(cfi) || chip->oldstate == FL_READY)) {
1150 * Let's suspend the erase or write operation when
1151 * supported. Note that we currently don't try to
1152 * suspend interleaved chips if there is already
1153 * another operation suspended (imagine what happens
1154 * when one chip was already done with the current
1155 * operation while another chip suspended it, then
1156 * we resume the whole thing at once). Yes, it
1160 map_write(map, CMD(0xb0), adr);
1161 map_write(map, CMD(0x70), adr);
1162 suspended = xip_currtime();
1164 if (xip_elapsed_since(suspended) > 100000) {
1166 * The chip doesn't want to suspend
1167 * after waiting for 100 msecs.
1168 * This is a critical error but there
1169 * is not much we can do here.
1173 status = map_read(map, adr);
1174 } while (!map_word_andequal(map, status, OK, OK));
1176 /* Suspend succeeded */
1177 oldstate = chip->state;
1178 if (oldstate == FL_ERASING) {
1179 if (!map_word_bitsset(map, status, CMD(0x40)))
1181 newstate = FL_XIP_WHILE_ERASING;
1182 chip->erase_suspended = 1;
1184 if (!map_word_bitsset(map, status, CMD(0x04)))
1186 newstate = FL_XIP_WHILE_WRITING;
1187 chip->write_suspended = 1;
1189 chip->state = newstate;
1190 map_write(map, CMD(0xff), adr);
1191 (void) map_read(map, adr);
1194 mutex_unlock(&chip->mutex);
1199 * We're back. However someone else might have
1200 * decided to go write to the chip if we are in
1201 * a suspended erase state. If so let's wait
1204 mutex_lock(&chip->mutex);
1205 while (chip->state != newstate) {
1206 DECLARE_WAITQUEUE(wait, current);
1207 set_current_state(TASK_UNINTERRUPTIBLE);
1208 add_wait_queue(&chip->wq, &wait);
1209 mutex_unlock(&chip->mutex);
1211 remove_wait_queue(&chip->wq, &wait);
1212 mutex_lock(&chip->mutex);
1214 /* Disallow XIP again */
1215 local_irq_disable();
1217 /* Resume the write or erase operation */
1218 map_write(map, CMD(0xd0), adr);
1219 map_write(map, CMD(0x70), adr);
1220 chip->state = oldstate;
1221 start = xip_currtime();
1222 } else if (usec >= 1000000/HZ) {
1224 * Try to save on CPU power when waiting delay
1225 * is at least a system timer tick period.
1226 * No need to be extremely accurate here.
1230 status = map_read(map, adr);
1231 done = xip_elapsed_since(start);
1232 } while (!map_word_andequal(map, status, OK, OK)
1235 return (done >= usec) ? -ETIME : 0;
1239 * The INVALIDATE_CACHED_RANGE() macro is normally used in parallel while
1240 * the flash is actively programming or erasing since we have to poll for
1241 * the operation to complete anyway. We can't do that in a generic way with
1242 * a XIP setup so do it before the actual flash operation in this case
1243 * and stub it out from INVAL_CACHE_AND_WAIT.
1245 #define XIP_INVAL_CACHED_RANGE(map, from, size) \
1246 INVALIDATE_CACHED_RANGE(map, from, size)
1248 #define INVAL_CACHE_AND_WAIT(map, chip, cmd_adr, inval_adr, inval_len, usec, usec_max) \
1249 xip_wait_for_operation(map, chip, cmd_adr, usec_max)
1253 #define xip_disable(map, chip, adr)
1254 #define xip_enable(map, chip, adr)
1255 #define XIP_INVAL_CACHED_RANGE(x...)
1256 #define INVAL_CACHE_AND_WAIT inval_cache_and_wait_for_operation
1258 static int inval_cache_and_wait_for_operation(
1259 struct map_info *map, struct flchip *chip,
1260 unsigned long cmd_adr, unsigned long inval_adr, int inval_len,
1261 unsigned int chip_op_time, unsigned int chip_op_time_max)
1263 struct cfi_private *cfi = map->fldrv_priv;
1264 map_word status, status_OK = CMD(0x80);
1265 int chip_state = chip->state;
1266 unsigned int timeo, sleep_time, reset_timeo;
1268 mutex_unlock(&chip->mutex);
1270 INVALIDATE_CACHED_RANGE(map, inval_adr, inval_len);
1271 mutex_lock(&chip->mutex);
1273 timeo = chip_op_time_max;
1276 reset_timeo = timeo;
1277 sleep_time = chip_op_time / 2;
1280 if (chip->state != chip_state) {
1281 /* Someone's suspended the operation: sleep */
1282 DECLARE_WAITQUEUE(wait, current);
1283 set_current_state(TASK_UNINTERRUPTIBLE);
1284 add_wait_queue(&chip->wq, &wait);
1285 mutex_unlock(&chip->mutex);
1287 remove_wait_queue(&chip->wq, &wait);
1288 mutex_lock(&chip->mutex);
1292 status = map_read(map, cmd_adr);
1293 if (map_word_andequal(map, status, status_OK, status_OK))
1296 if (chip->erase_suspended && chip_state == FL_ERASING) {
1297 /* Erase suspend occurred while sleep: reset timeout */
1298 timeo = reset_timeo;
1299 chip->erase_suspended = 0;
1301 if (chip->write_suspended && chip_state == FL_WRITING) {
1302 /* Write suspend occurred while sleep: reset timeout */
1303 timeo = reset_timeo;
1304 chip->write_suspended = 0;
1307 map_write(map, CMD(0x70), cmd_adr);
1308 chip->state = FL_STATUS;
1312 /* OK Still waiting. Drop the lock, wait a while and retry. */
1313 mutex_unlock(&chip->mutex);
1314 if (sleep_time >= 1000000/HZ) {
1316 * Half of the normal delay still remaining
1317 * can be performed with a sleeping delay instead
1320 msleep(sleep_time/1000);
1321 timeo -= sleep_time;
1322 sleep_time = 1000000/HZ;
1328 mutex_lock(&chip->mutex);
1331 /* Done and happy. */
1332 chip->state = FL_STATUS;
1338 #define WAIT_TIMEOUT(map, chip, adr, udelay, udelay_max) \
1339 INVAL_CACHE_AND_WAIT(map, chip, adr, 0, 0, udelay, udelay_max);
1342 static int do_point_onechip (struct map_info *map, struct flchip *chip, loff_t adr, size_t len)
1344 unsigned long cmd_addr;
1345 struct cfi_private *cfi = map->fldrv_priv;
1350 /* Ensure cmd read/writes are aligned. */
1351 cmd_addr = adr & ~(map_bankwidth(map)-1);
1353 mutex_lock(&chip->mutex);
1355 ret = get_chip(map, chip, cmd_addr, FL_POINT);
1358 if (chip->state != FL_POINT && chip->state != FL_READY)
1359 map_write(map, CMD(0xff), cmd_addr);
1361 chip->state = FL_POINT;
1362 chip->ref_point_counter++;
1364 mutex_unlock(&chip->mutex);
1369 static int cfi_intelext_point(struct mtd_info *mtd, loff_t from, size_t len,
1370 size_t *retlen, void **virt, resource_size_t *phys)
1372 struct map_info *map = mtd->priv;
1373 struct cfi_private *cfi = map->fldrv_priv;
1374 unsigned long ofs, last_end = 0;
1381 /* Now lock the chip(s) to POINT state */
1383 /* ofs: offset within the first chip that the first read should start */
1384 chipnum = (from >> cfi->chipshift);
1385 ofs = from - (chipnum << cfi->chipshift);
1387 *virt = map->virt + cfi->chips[chipnum].start + ofs;
1389 *phys = map->phys + cfi->chips[chipnum].start + ofs;
1392 unsigned long thislen;
1394 if (chipnum >= cfi->numchips)
1397 /* We cannot point across chips that are virtually disjoint */
1399 last_end = cfi->chips[chipnum].start;
1400 else if (cfi->chips[chipnum].start != last_end)
1403 if ((len + ofs -1) >> cfi->chipshift)
1404 thislen = (1<<cfi->chipshift) - ofs;
1408 ret = do_point_onechip(map, &cfi->chips[chipnum], ofs, thislen);
1416 last_end += 1 << cfi->chipshift;
1422 static int cfi_intelext_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
1424 struct map_info *map = mtd->priv;
1425 struct cfi_private *cfi = map->fldrv_priv;
1427 int chipnum, err = 0;
1429 /* Now unlock the chip(s) POINT state */
1431 /* ofs: offset within the first chip that the first read should start */
1432 chipnum = (from >> cfi->chipshift);
1433 ofs = from - (chipnum << cfi->chipshift);
1435 while (len && !err) {
1436 unsigned long thislen;
1437 struct flchip *chip;
1439 chip = &cfi->chips[chipnum];
1440 if (chipnum >= cfi->numchips)
1443 if ((len + ofs -1) >> cfi->chipshift)
1444 thislen = (1<<cfi->chipshift) - ofs;
1448 mutex_lock(&chip->mutex);
1449 if (chip->state == FL_POINT) {
1450 chip->ref_point_counter--;
1451 if(chip->ref_point_counter == 0)
1452 chip->state = FL_READY;
1454 printk(KERN_ERR "%s: Error: unpoint called on non pointed region\n", map->name);
1458 put_chip(map, chip, chip->start);
1459 mutex_unlock(&chip->mutex);
1469 static inline int do_read_onechip(struct map_info *map, struct flchip *chip, loff_t adr, size_t len, u_char *buf)
1471 unsigned long cmd_addr;
1472 struct cfi_private *cfi = map->fldrv_priv;
1477 /* Ensure cmd read/writes are aligned. */
1478 cmd_addr = adr & ~(map_bankwidth(map)-1);
1480 mutex_lock(&chip->mutex);
1481 ret = get_chip(map, chip, cmd_addr, FL_READY);
1483 mutex_unlock(&chip->mutex);
1487 if (chip->state != FL_POINT && chip->state != FL_READY) {
1488 map_write(map, CMD(0xff), cmd_addr);
1490 chip->state = FL_READY;
1493 map_copy_from(map, buf, adr, len);
1495 put_chip(map, chip, cmd_addr);
1497 mutex_unlock(&chip->mutex);
1501 static int cfi_intelext_read (struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen, u_char *buf)
1503 struct map_info *map = mtd->priv;
1504 struct cfi_private *cfi = map->fldrv_priv;
1509 /* ofs: offset within the first chip that the first read should start */
1510 chipnum = (from >> cfi->chipshift);
1511 ofs = from - (chipnum << cfi->chipshift);
1514 unsigned long thislen;
1516 if (chipnum >= cfi->numchips)
1519 if ((len + ofs -1) >> cfi->chipshift)
1520 thislen = (1<<cfi->chipshift) - ofs;
1524 ret = do_read_onechip(map, &cfi->chips[chipnum], ofs, thislen, buf);
1538 static int __xipram do_write_oneword(struct map_info *map, struct flchip *chip,
1539 unsigned long adr, map_word datum, int mode)
1541 struct cfi_private *cfi = map->fldrv_priv;
1542 map_word status, write_cmd;
1549 write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0x40) : CMD(0x41);
1552 write_cmd = CMD(0xc0);
1558 mutex_lock(&chip->mutex);
1559 ret = get_chip(map, chip, adr, mode);
1561 mutex_unlock(&chip->mutex);
1565 XIP_INVAL_CACHED_RANGE(map, adr, map_bankwidth(map));
1567 xip_disable(map, chip, adr);
1568 map_write(map, write_cmd, adr);
1569 map_write(map, datum, adr);
1572 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1573 adr, map_bankwidth(map),
1574 chip->word_write_time,
1575 chip->word_write_time_max);
1577 xip_enable(map, chip, adr);
1578 printk(KERN_ERR "%s: word write error (status timeout)\n", map->name);
1582 /* check for errors */
1583 status = map_read(map, adr);
1584 if (map_word_bitsset(map, status, CMD(0x1a))) {
1585 unsigned long chipstatus = MERGESTATUS(status);
1588 map_write(map, CMD(0x50), adr);
1589 map_write(map, CMD(0x70), adr);
1590 xip_enable(map, chip, adr);
1592 if (chipstatus & 0x02) {
1594 } else if (chipstatus & 0x08) {
1595 printk(KERN_ERR "%s: word write error (bad VPP)\n", map->name);
1598 printk(KERN_ERR "%s: word write error (status 0x%lx)\n", map->name, chipstatus);
1605 xip_enable(map, chip, adr);
1606 out: DISABLE_VPP(map);
1607 put_chip(map, chip, adr);
1608 mutex_unlock(&chip->mutex);
1613 static int cfi_intelext_write_words (struct mtd_info *mtd, loff_t to , size_t len, size_t *retlen, const u_char *buf)
1615 struct map_info *map = mtd->priv;
1616 struct cfi_private *cfi = map->fldrv_priv;
1621 chipnum = to >> cfi->chipshift;
1622 ofs = to - (chipnum << cfi->chipshift);
1624 /* If it's not bus-aligned, do the first byte write */
1625 if (ofs & (map_bankwidth(map)-1)) {
1626 unsigned long bus_ofs = ofs & ~(map_bankwidth(map)-1);
1627 int gap = ofs - bus_ofs;
1631 n = min_t(int, len, map_bankwidth(map)-gap);
1632 datum = map_word_ff(map);
1633 datum = map_word_load_partial(map, datum, buf, gap, n);
1635 ret = do_write_oneword(map, &cfi->chips[chipnum],
1636 bus_ofs, datum, FL_WRITING);
1645 if (ofs >> cfi->chipshift) {
1648 if (chipnum == cfi->numchips)
1653 while(len >= map_bankwidth(map)) {
1654 map_word datum = map_word_load(map, buf);
1656 ret = do_write_oneword(map, &cfi->chips[chipnum],
1657 ofs, datum, FL_WRITING);
1661 ofs += map_bankwidth(map);
1662 buf += map_bankwidth(map);
1663 (*retlen) += map_bankwidth(map);
1664 len -= map_bankwidth(map);
1666 if (ofs >> cfi->chipshift) {
1669 if (chipnum == cfi->numchips)
1674 if (len & (map_bankwidth(map)-1)) {
1677 datum = map_word_ff(map);
1678 datum = map_word_load_partial(map, datum, buf, 0, len);
1680 ret = do_write_oneword(map, &cfi->chips[chipnum],
1681 ofs, datum, FL_WRITING);
1692 static int __xipram do_write_buffer(struct map_info *map, struct flchip *chip,
1693 unsigned long adr, const struct kvec **pvec,
1694 unsigned long *pvec_seek, int len)
1696 struct cfi_private *cfi = map->fldrv_priv;
1697 map_word status, write_cmd, datum;
1698 unsigned long cmd_adr;
1699 int ret, wbufsize, word_gap, words;
1700 const struct kvec *vec;
1701 unsigned long vec_seek;
1702 unsigned long initial_adr;
1703 int initial_len = len;
1705 wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1708 cmd_adr = adr & ~(wbufsize-1);
1710 /* Sharp LH28F640BF chips need the first address for the
1711 * Page Buffer Program command. See Table 5 of
1712 * LH28F320BF, LH28F640BF, LH28F128BF Series (Appendix FUM00701) */
1713 if (is_LH28F640BF(cfi))
1716 /* Let's determine this according to the interleave only once */
1717 write_cmd = (cfi->cfiq->P_ID != P_ID_INTEL_PERFORMANCE) ? CMD(0xe8) : CMD(0xe9);
1719 mutex_lock(&chip->mutex);
1720 ret = get_chip(map, chip, cmd_adr, FL_WRITING);
1722 mutex_unlock(&chip->mutex);
1726 XIP_INVAL_CACHED_RANGE(map, initial_adr, initial_len);
1728 xip_disable(map, chip, cmd_adr);
1730 /* §4.8 of the 28FxxxJ3A datasheet says "Any time SR.4 and/or SR.5 is set
1731 [...], the device will not accept any more Write to Buffer commands".
1732 So we must check here and reset those bits if they're set. Otherwise
1733 we're just pissing in the wind */
1734 if (chip->state != FL_STATUS) {
1735 map_write(map, CMD(0x70), cmd_adr);
1736 chip->state = FL_STATUS;
1738 status = map_read(map, cmd_adr);
1739 if (map_word_bitsset(map, status, CMD(0x30))) {
1740 xip_enable(map, chip, cmd_adr);
1741 printk(KERN_WARNING "SR.4 or SR.5 bits set in buffer write (status %lx). Clearing.\n", status.x[0]);
1742 xip_disable(map, chip, cmd_adr);
1743 map_write(map, CMD(0x50), cmd_adr);
1744 map_write(map, CMD(0x70), cmd_adr);
1747 chip->state = FL_WRITING_TO_BUFFER;
1748 map_write(map, write_cmd, cmd_adr);
1749 ret = WAIT_TIMEOUT(map, chip, cmd_adr, 0, 0);
1751 /* Argh. Not ready for write to buffer */
1752 map_word Xstatus = map_read(map, cmd_adr);
1753 map_write(map, CMD(0x70), cmd_adr);
1754 chip->state = FL_STATUS;
1755 status = map_read(map, cmd_adr);
1756 map_write(map, CMD(0x50), cmd_adr);
1757 map_write(map, CMD(0x70), cmd_adr);
1758 xip_enable(map, chip, cmd_adr);
1759 printk(KERN_ERR "%s: Chip not ready for buffer write. Xstatus = %lx, status = %lx\n",
1760 map->name, Xstatus.x[0], status.x[0]);
1764 /* Figure out the number of words to write */
1765 word_gap = (-adr & (map_bankwidth(map)-1));
1766 words = DIV_ROUND_UP(len - word_gap, map_bankwidth(map));
1770 word_gap = map_bankwidth(map) - word_gap;
1772 datum = map_word_ff(map);
1775 /* Write length of data to come */
1776 map_write(map, CMD(words), cmd_adr );
1780 vec_seek = *pvec_seek;
1782 int n = map_bankwidth(map) - word_gap;
1783 if (n > vec->iov_len - vec_seek)
1784 n = vec->iov_len - vec_seek;
1788 if (!word_gap && len < map_bankwidth(map))
1789 datum = map_word_ff(map);
1791 datum = map_word_load_partial(map, datum,
1792 vec->iov_base + vec_seek,
1797 if (!len || word_gap == map_bankwidth(map)) {
1798 map_write(map, datum, adr);
1799 adr += map_bankwidth(map);
1804 if (vec_seek == vec->iov_len) {
1810 *pvec_seek = vec_seek;
1813 map_write(map, CMD(0xd0), cmd_adr);
1814 chip->state = FL_WRITING;
1816 ret = INVAL_CACHE_AND_WAIT(map, chip, cmd_adr,
1817 initial_adr, initial_len,
1818 chip->buffer_write_time,
1819 chip->buffer_write_time_max);
1821 map_write(map, CMD(0x70), cmd_adr);
1822 chip->state = FL_STATUS;
1823 xip_enable(map, chip, cmd_adr);
1824 printk(KERN_ERR "%s: buffer write error (status timeout)\n", map->name);
1828 /* check for errors */
1829 status = map_read(map, cmd_adr);
1830 if (map_word_bitsset(map, status, CMD(0x1a))) {
1831 unsigned long chipstatus = MERGESTATUS(status);
1834 map_write(map, CMD(0x50), cmd_adr);
1835 map_write(map, CMD(0x70), cmd_adr);
1836 xip_enable(map, chip, cmd_adr);
1838 if (chipstatus & 0x02) {
1840 } else if (chipstatus & 0x08) {
1841 printk(KERN_ERR "%s: buffer write error (bad VPP)\n", map->name);
1844 printk(KERN_ERR "%s: buffer write error (status 0x%lx)\n", map->name, chipstatus);
1851 xip_enable(map, chip, cmd_adr);
1852 out: DISABLE_VPP(map);
1853 put_chip(map, chip, cmd_adr);
1854 mutex_unlock(&chip->mutex);
1858 static int cfi_intelext_writev (struct mtd_info *mtd, const struct kvec *vecs,
1859 unsigned long count, loff_t to, size_t *retlen)
1861 struct map_info *map = mtd->priv;
1862 struct cfi_private *cfi = map->fldrv_priv;
1863 int wbufsize = cfi_interleave(cfi) << cfi->cfiq->MaxBufWriteSize;
1866 unsigned long ofs, vec_seek, i;
1869 for (i = 0; i < count; i++)
1870 len += vecs[i].iov_len;
1875 chipnum = to >> cfi->chipshift;
1876 ofs = to - (chipnum << cfi->chipshift);
1880 /* We must not cross write block boundaries */
1881 int size = wbufsize - (ofs & (wbufsize-1));
1885 ret = do_write_buffer(map, &cfi->chips[chipnum],
1886 ofs, &vecs, &vec_seek, size);
1894 if (ofs >> cfi->chipshift) {
1897 if (chipnum == cfi->numchips)
1901 /* Be nice and reschedule with the chip in a usable state for other
1910 static int cfi_intelext_write_buffers (struct mtd_info *mtd, loff_t to,
1911 size_t len, size_t *retlen, const u_char *buf)
1915 vec.iov_base = (void *) buf;
1918 return cfi_intelext_writev(mtd, &vec, 1, to, retlen);
1921 static int __xipram do_erase_oneblock(struct map_info *map, struct flchip *chip,
1922 unsigned long adr, int len, void *thunk)
1924 struct cfi_private *cfi = map->fldrv_priv;
1932 mutex_lock(&chip->mutex);
1933 ret = get_chip(map, chip, adr, FL_ERASING);
1935 mutex_unlock(&chip->mutex);
1939 XIP_INVAL_CACHED_RANGE(map, adr, len);
1941 xip_disable(map, chip, adr);
1943 /* Clear the status register first */
1944 map_write(map, CMD(0x50), adr);
1947 map_write(map, CMD(0x20), adr);
1948 map_write(map, CMD(0xD0), adr);
1949 chip->state = FL_ERASING;
1950 chip->erase_suspended = 0;
1951 chip->in_progress_block_addr = adr;
1952 chip->in_progress_block_mask = ~(len - 1);
1954 ret = INVAL_CACHE_AND_WAIT(map, chip, adr,
1957 chip->erase_time_max);
1959 map_write(map, CMD(0x70), adr);
1960 chip->state = FL_STATUS;
1961 xip_enable(map, chip, adr);
1962 printk(KERN_ERR "%s: block erase error: (status timeout)\n", map->name);
1966 /* We've broken this before. It doesn't hurt to be safe */
1967 map_write(map, CMD(0x70), adr);
1968 chip->state = FL_STATUS;
1969 status = map_read(map, adr);
1971 /* check for errors */
1972 if (map_word_bitsset(map, status, CMD(0x3a))) {
1973 unsigned long chipstatus = MERGESTATUS(status);
1975 /* Reset the error bits */
1976 map_write(map, CMD(0x50), adr);
1977 map_write(map, CMD(0x70), adr);
1978 xip_enable(map, chip, adr);
1980 if ((chipstatus & 0x30) == 0x30) {
1981 printk(KERN_ERR "%s: block erase error: (bad command sequence, status 0x%lx)\n", map->name, chipstatus);
1983 } else if (chipstatus & 0x02) {
1984 /* Protection bit set */
1986 } else if (chipstatus & 0x8) {
1988 printk(KERN_ERR "%s: block erase error: (bad VPP)\n", map->name);
1990 } else if (chipstatus & 0x20 && retries--) {
1991 printk(KERN_DEBUG "block erase failed at 0x%08lx: status 0x%lx. Retrying...\n", adr, chipstatus);
1993 put_chip(map, chip, adr);
1994 mutex_unlock(&chip->mutex);
1997 printk(KERN_ERR "%s: block erase failed at 0x%08lx (status 0x%lx)\n", map->name, adr, chipstatus);
2004 xip_enable(map, chip, adr);
2005 out: DISABLE_VPP(map);
2006 put_chip(map, chip, adr);
2007 mutex_unlock(&chip->mutex);
2011 static int cfi_intelext_erase_varsize(struct mtd_info *mtd, struct erase_info *instr)
2013 unsigned long ofs, len;
2019 ret = cfi_varsize_frob(mtd, do_erase_oneblock, ofs, len, NULL);
2023 instr->state = MTD_ERASE_DONE;
2024 mtd_erase_callback(instr);
2029 static void cfi_intelext_sync (struct mtd_info *mtd)
2031 struct map_info *map = mtd->priv;
2032 struct cfi_private *cfi = map->fldrv_priv;
2034 struct flchip *chip;
2037 for (i=0; !ret && i<cfi->numchips; i++) {
2038 chip = &cfi->chips[i];
2040 mutex_lock(&chip->mutex);
2041 ret = get_chip(map, chip, chip->start, FL_SYNCING);
2044 chip->oldstate = chip->state;
2045 chip->state = FL_SYNCING;
2046 /* No need to wake_up() on this state change -
2047 * as the whole point is that nobody can do anything
2048 * with the chip now anyway.
2051 mutex_unlock(&chip->mutex);
2054 /* Unlock the chips again */
2056 for (i--; i >=0; i--) {
2057 chip = &cfi->chips[i];
2059 mutex_lock(&chip->mutex);
2061 if (chip->state == FL_SYNCING) {
2062 chip->state = chip->oldstate;
2063 chip->oldstate = FL_READY;
2066 mutex_unlock(&chip->mutex);
2070 static int __xipram do_getlockstatus_oneblock(struct map_info *map,
2071 struct flchip *chip,
2073 int len, void *thunk)
2075 struct cfi_private *cfi = map->fldrv_priv;
2076 int status, ofs_factor = cfi->interleave * cfi->device_type;
2079 xip_disable(map, chip, adr+(2*ofs_factor));
2080 map_write(map, CMD(0x90), adr+(2*ofs_factor));
2081 chip->state = FL_JEDEC_QUERY;
2082 status = cfi_read_query(map, adr+(2*ofs_factor));
2083 xip_enable(map, chip, 0);
2087 #ifdef DEBUG_LOCK_BITS
2088 static int __xipram do_printlockstatus_oneblock(struct map_info *map,
2089 struct flchip *chip,
2091 int len, void *thunk)
2093 printk(KERN_DEBUG "block status register for 0x%08lx is %x\n",
2094 adr, do_getlockstatus_oneblock(map, chip, adr, len, thunk));
2099 #define DO_XXLOCK_ONEBLOCK_LOCK ((void *) 1)
2100 #define DO_XXLOCK_ONEBLOCK_UNLOCK ((void *) 2)
2102 static int __xipram do_xxlock_oneblock(struct map_info *map, struct flchip *chip,
2103 unsigned long adr, int len, void *thunk)
2105 struct cfi_private *cfi = map->fldrv_priv;
2106 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2112 mutex_lock(&chip->mutex);
2113 ret = get_chip(map, chip, adr, FL_LOCKING);
2115 mutex_unlock(&chip->mutex);
2120 xip_disable(map, chip, adr);
2122 map_write(map, CMD(0x60), adr);
2123 if (thunk == DO_XXLOCK_ONEBLOCK_LOCK) {
2124 map_write(map, CMD(0x01), adr);
2125 chip->state = FL_LOCKING;
2126 } else if (thunk == DO_XXLOCK_ONEBLOCK_UNLOCK) {
2127 map_write(map, CMD(0xD0), adr);
2128 chip->state = FL_UNLOCKING;
2133 * If Instant Individual Block Locking supported then no need
2137 * Unlocking may take up to 1.4 seconds on some Intel flashes. So
2138 * lets use a max of 1.5 seconds (1500ms) as timeout.
2140 * See "Clear Block Lock-Bits Time" on page 40 in
2141 * "3 Volt Intel StrataFlash Memory" 28F128J3,28F640J3,28F320J3 manual
2142 * from February 2003
2144 mdelay = (!extp || !(extp->FeatureSupport & (1 << 5))) ? 1500 : 0;
2146 ret = WAIT_TIMEOUT(map, chip, adr, mdelay, mdelay * 1000);
2148 map_write(map, CMD(0x70), adr);
2149 chip->state = FL_STATUS;
2150 xip_enable(map, chip, adr);
2151 printk(KERN_ERR "%s: block unlock error: (status timeout)\n", map->name);
2155 xip_enable(map, chip, adr);
2156 out: DISABLE_VPP(map);
2157 put_chip(map, chip, adr);
2158 mutex_unlock(&chip->mutex);
2162 static int cfi_intelext_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2166 #ifdef DEBUG_LOCK_BITS
2167 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2168 __func__, ofs, len);
2169 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
2173 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2174 ofs, len, DO_XXLOCK_ONEBLOCK_LOCK);
2176 #ifdef DEBUG_LOCK_BITS
2177 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2179 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
2186 static int cfi_intelext_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
2190 #ifdef DEBUG_LOCK_BITS
2191 printk(KERN_DEBUG "%s: lock status before, ofs=0x%08llx, len=0x%08X\n",
2192 __func__, ofs, len);
2193 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
2197 ret = cfi_varsize_frob(mtd, do_xxlock_oneblock,
2198 ofs, len, DO_XXLOCK_ONEBLOCK_UNLOCK);
2200 #ifdef DEBUG_LOCK_BITS
2201 printk(KERN_DEBUG "%s: lock status after, ret=%d\n",
2203 cfi_varsize_frob(mtd, do_printlockstatus_oneblock,
2210 static int cfi_intelext_is_locked(struct mtd_info *mtd, loff_t ofs,
2213 return cfi_varsize_frob(mtd, do_getlockstatus_oneblock,
2214 ofs, len, NULL) ? 1 : 0;
2217 #ifdef CONFIG_MTD_OTP
2219 typedef int (*otp_op_t)(struct map_info *map, struct flchip *chip,
2220 u_long data_offset, u_char *buf, u_int size,
2221 u_long prot_offset, u_int groupno, u_int groupsize);
2224 do_otp_read(struct map_info *map, struct flchip *chip, u_long offset,
2225 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2227 struct cfi_private *cfi = map->fldrv_priv;
2230 mutex_lock(&chip->mutex);
2231 ret = get_chip(map, chip, chip->start, FL_JEDEC_QUERY);
2233 mutex_unlock(&chip->mutex);
2237 /* let's ensure we're not reading back cached data from array mode */
2238 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
2240 xip_disable(map, chip, chip->start);
2241 if (chip->state != FL_JEDEC_QUERY) {
2242 map_write(map, CMD(0x90), chip->start);
2243 chip->state = FL_JEDEC_QUERY;
2245 map_copy_from(map, buf, chip->start + offset, size);
2246 xip_enable(map, chip, chip->start);
2248 /* then ensure we don't keep OTP data in the cache */
2249 INVALIDATE_CACHED_RANGE(map, chip->start + offset, size);
2251 put_chip(map, chip, chip->start);
2252 mutex_unlock(&chip->mutex);
2257 do_otp_write(struct map_info *map, struct flchip *chip, u_long offset,
2258 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2263 unsigned long bus_ofs = offset & ~(map_bankwidth(map)-1);
2264 int gap = offset - bus_ofs;
2265 int n = min_t(int, size, map_bankwidth(map)-gap);
2266 map_word datum = map_word_ff(map);
2268 datum = map_word_load_partial(map, datum, buf, gap, n);
2269 ret = do_write_oneword(map, chip, bus_ofs, datum, FL_OTP_WRITE);
2282 do_otp_lock(struct map_info *map, struct flchip *chip, u_long offset,
2283 u_char *buf, u_int size, u_long prot, u_int grpno, u_int grpsz)
2285 struct cfi_private *cfi = map->fldrv_priv;
2288 /* make sure area matches group boundaries */
2292 datum = map_word_ff(map);
2293 datum = map_word_clr(map, datum, CMD(1 << grpno));
2294 return do_write_oneword(map, chip, prot, datum, FL_OTP_WRITE);
2297 static int cfi_intelext_otp_walk(struct mtd_info *mtd, loff_t from, size_t len,
2298 size_t *retlen, u_char *buf,
2299 otp_op_t action, int user_regs)
2301 struct map_info *map = mtd->priv;
2302 struct cfi_private *cfi = map->fldrv_priv;
2303 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2304 struct flchip *chip;
2305 struct cfi_intelext_otpinfo *otp;
2306 u_long devsize, reg_prot_offset, data_offset;
2307 u_int chip_num, chip_step, field, reg_fact_size, reg_user_size;
2308 u_int groups, groupno, groupsize, reg_fact_groups, reg_user_groups;
2313 /* Check that we actually have some OTP registers */
2314 if (!extp || !(extp->FeatureSupport & 64) || !extp->NumProtectionFields)
2317 /* we need real chips here not virtual ones */
2318 devsize = (1 << cfi->cfiq->DevSize) * cfi->interleave;
2319 chip_step = devsize >> cfi->chipshift;
2322 /* Some chips have OTP located in the _top_ partition only.
2323 For example: Intel 28F256L18T (T means top-parameter device) */
2324 if (cfi->mfr == CFI_MFR_INTEL) {
2329 chip_num = chip_step - 1;
2333 for ( ; chip_num < cfi->numchips; chip_num += chip_step) {
2334 chip = &cfi->chips[chip_num];
2335 otp = (struct cfi_intelext_otpinfo *)&extp->extra[0];
2337 /* first OTP region */
2339 reg_prot_offset = extp->ProtRegAddr;
2340 reg_fact_groups = 1;
2341 reg_fact_size = 1 << extp->FactProtRegSize;
2342 reg_user_groups = 1;
2343 reg_user_size = 1 << extp->UserProtRegSize;
2346 /* flash geometry fixup */
2347 data_offset = reg_prot_offset + 1;
2348 data_offset *= cfi->interleave * cfi->device_type;
2349 reg_prot_offset *= cfi->interleave * cfi->device_type;
2350 reg_fact_size *= cfi->interleave;
2351 reg_user_size *= cfi->interleave;
2354 groups = reg_user_groups;
2355 groupsize = reg_user_size;
2356 /* skip over factory reg area */
2357 groupno = reg_fact_groups;
2358 data_offset += reg_fact_groups * reg_fact_size;
2360 groups = reg_fact_groups;
2361 groupsize = reg_fact_size;
2365 while (len > 0 && groups > 0) {
2368 * Special case: if action is NULL
2369 * we fill buf with otp_info records.
2371 struct otp_info *otpinfo;
2373 len -= sizeof(struct otp_info);
2376 ret = do_otp_read(map, chip,
2378 (u_char *)&lockword,
2383 otpinfo = (struct otp_info *)buf;
2384 otpinfo->start = from;
2385 otpinfo->length = groupsize;
2387 !map_word_bitsset(map, lockword,
2390 buf += sizeof(*otpinfo);
2391 *retlen += sizeof(*otpinfo);
2392 } else if (from >= groupsize) {
2394 data_offset += groupsize;
2396 int size = groupsize;
2397 data_offset += from;
2402 ret = action(map, chip, data_offset,
2403 buf, size, reg_prot_offset,
2404 groupno, groupsize);
2410 data_offset += size;
2416 /* next OTP region */
2417 if (++field == extp->NumProtectionFields)
2419 reg_prot_offset = otp->ProtRegAddr;
2420 reg_fact_groups = otp->FactGroups;
2421 reg_fact_size = 1 << otp->FactProtRegSize;
2422 reg_user_groups = otp->UserGroups;
2423 reg_user_size = 1 << otp->UserProtRegSize;
2431 static int cfi_intelext_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
2432 size_t len, size_t *retlen,
2435 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2436 buf, do_otp_read, 0);
2439 static int cfi_intelext_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
2440 size_t len, size_t *retlen,
2443 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2444 buf, do_otp_read, 1);
2447 static int cfi_intelext_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
2448 size_t len, size_t *retlen,
2451 return cfi_intelext_otp_walk(mtd, from, len, retlen,
2452 buf, do_otp_write, 1);
2455 static int cfi_intelext_lock_user_prot_reg(struct mtd_info *mtd,
2456 loff_t from, size_t len)
2459 return cfi_intelext_otp_walk(mtd, from, len, &retlen,
2460 NULL, do_otp_lock, 1);
2463 static int cfi_intelext_get_fact_prot_info(struct mtd_info *mtd, size_t len,
2464 size_t *retlen, struct otp_info *buf)
2467 return cfi_intelext_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
2471 static int cfi_intelext_get_user_prot_info(struct mtd_info *mtd, size_t len,
2472 size_t *retlen, struct otp_info *buf)
2474 return cfi_intelext_otp_walk(mtd, 0, len, retlen, (u_char *)buf,
2480 static void cfi_intelext_save_locks(struct mtd_info *mtd)
2482 struct mtd_erase_region_info *region;
2483 int block, status, i;
2487 for (i = 0; i < mtd->numeraseregions; i++) {
2488 region = &mtd->eraseregions[i];
2489 if (!region->lockmap)
2492 for (block = 0; block < region->numblocks; block++){
2493 len = region->erasesize;
2494 adr = region->offset + block * len;
2496 status = cfi_varsize_frob(mtd,
2497 do_getlockstatus_oneblock, adr, len, NULL);
2499 set_bit(block, region->lockmap);
2501 clear_bit(block, region->lockmap);
2506 static int cfi_intelext_suspend(struct mtd_info *mtd)
2508 struct map_info *map = mtd->priv;
2509 struct cfi_private *cfi = map->fldrv_priv;
2510 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2512 struct flchip *chip;
2515 if ((mtd->flags & MTD_POWERUP_LOCK)
2516 && extp && (extp->FeatureSupport & (1 << 5)))
2517 cfi_intelext_save_locks(mtd);
2519 for (i=0; !ret && i<cfi->numchips; i++) {
2520 chip = &cfi->chips[i];
2522 mutex_lock(&chip->mutex);
2524 switch (chip->state) {
2528 case FL_JEDEC_QUERY:
2529 if (chip->oldstate == FL_READY) {
2530 /* place the chip in a known state before suspend */
2531 map_write(map, CMD(0xFF), cfi->chips[i].start);
2532 chip->oldstate = chip->state;
2533 chip->state = FL_PM_SUSPENDED;
2534 /* No need to wake_up() on this state change -
2535 * as the whole point is that nobody can do anything
2536 * with the chip now anyway.
2539 /* There seems to be an operation pending. We must wait for it. */
2540 printk(KERN_NOTICE "Flash device refused suspend due to pending operation (oldstate %d)\n", chip->oldstate);
2545 /* Should we actually wait? Once upon a time these routines weren't
2546 allowed to. Or should we return -EAGAIN, because the upper layers
2547 ought to have already shut down anything which was using the device
2548 anyway? The latter for now. */
2549 printk(KERN_NOTICE "Flash device refused suspend due to active operation (state %d)\n", chip->state);
2551 case FL_PM_SUSPENDED:
2554 mutex_unlock(&chip->mutex);
2557 /* Unlock the chips again */
2560 for (i--; i >=0; i--) {
2561 chip = &cfi->chips[i];
2563 mutex_lock(&chip->mutex);
2565 if (chip->state == FL_PM_SUSPENDED) {
2566 /* No need to force it into a known state here,
2567 because we're returning failure, and it didn't
2569 chip->state = chip->oldstate;
2570 chip->oldstate = FL_READY;
2573 mutex_unlock(&chip->mutex);
2580 static void cfi_intelext_restore_locks(struct mtd_info *mtd)
2582 struct mtd_erase_region_info *region;
2587 for (i = 0; i < mtd->numeraseregions; i++) {
2588 region = &mtd->eraseregions[i];
2589 if (!region->lockmap)
2592 for_each_clear_bit(block, region->lockmap, region->numblocks) {
2593 len = region->erasesize;
2594 adr = region->offset + block * len;
2595 cfi_intelext_unlock(mtd, adr, len);
2600 static void cfi_intelext_resume(struct mtd_info *mtd)
2602 struct map_info *map = mtd->priv;
2603 struct cfi_private *cfi = map->fldrv_priv;
2604 struct cfi_pri_intelext *extp = cfi->cmdset_priv;
2606 struct flchip *chip;
2608 for (i=0; i<cfi->numchips; i++) {
2610 chip = &cfi->chips[i];
2612 mutex_lock(&chip->mutex);
2614 /* Go to known state. Chip may have been power cycled */
2615 if (chip->state == FL_PM_SUSPENDED) {
2616 /* Refresh LH28F640BF Partition Config. Register */
2617 fixup_LH28F640BF(mtd);
2618 map_write(map, CMD(0xFF), cfi->chips[i].start);
2619 chip->oldstate = chip->state = FL_READY;
2623 mutex_unlock(&chip->mutex);
2626 if ((mtd->flags & MTD_POWERUP_LOCK)
2627 && extp && (extp->FeatureSupport & (1 << 5)))
2628 cfi_intelext_restore_locks(mtd);
2631 static int cfi_intelext_reset(struct mtd_info *mtd)
2633 struct map_info *map = mtd->priv;
2634 struct cfi_private *cfi = map->fldrv_priv;
2637 for (i=0; i < cfi->numchips; i++) {
2638 struct flchip *chip = &cfi->chips[i];
2640 /* force the completion of any ongoing operation
2641 and switch to array mode so any bootloader in
2642 flash is accessible for soft reboot. */
2643 mutex_lock(&chip->mutex);
2644 ret = get_chip(map, chip, chip->start, FL_SHUTDOWN);
2646 map_write(map, CMD(0xff), chip->start);
2647 chip->state = FL_SHUTDOWN;
2648 put_chip(map, chip, chip->start);
2650 mutex_unlock(&chip->mutex);
2656 static int cfi_intelext_reboot(struct notifier_block *nb, unsigned long val,
2659 struct mtd_info *mtd;
2661 mtd = container_of(nb, struct mtd_info, reboot_notifier);
2662 cfi_intelext_reset(mtd);
2666 static void cfi_intelext_destroy(struct mtd_info *mtd)
2668 struct map_info *map = mtd->priv;
2669 struct cfi_private *cfi = map->fldrv_priv;
2670 struct mtd_erase_region_info *region;
2672 cfi_intelext_reset(mtd);
2673 unregister_reboot_notifier(&mtd->reboot_notifier);
2674 kfree(cfi->cmdset_priv);
2676 kfree(cfi->chips[0].priv);
2678 for (i = 0; i < mtd->numeraseregions; i++) {
2679 region = &mtd->eraseregions[i];
2680 kfree(region->lockmap);
2682 kfree(mtd->eraseregions);
2685 MODULE_LICENSE("GPL");
2686 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org> et al.");
2687 MODULE_DESCRIPTION("MTD chip driver for Intel/Sharp flash chips");
2688 MODULE_ALIAS("cfi_cmdset_0003");
2689 MODULE_ALIAS("cfi_cmdset_0200");