2 * Common Flash Interface support:
3 * ST Advanced Architecture Command Set (ID 0x0020)
5 * (C) 2000 Red Hat. GPL'd
7 * $Id: cfi_cmdset_0020.c,v 1.19 2005/07/13 15:52:45 dwmw2 Exp $
9 * 10/10/2000 Nicolas Pitre <nico@cam.org>
10 * - completely revamped method functions so they are aware and
11 * independent of the flash geometry (buswidth, interleave, etc.)
12 * - scalability vs code size is completely set at compile-time
13 * (see include/linux/mtd/cfi.h for selection)
14 * - optimized write buffer method
15 * 06/21/2002 Joern Engel <joern@wh.fh-wedel.de> and others
16 * - modified Intel Command Set 0x0001 to support ST Advanced Architecture
17 * (command set 0x0020)
18 * - added a writev function
19 * 07/13/2005 Joern Engel <joern@wh.fh-wedel.de>
20 * - Plugged memory leak in cfi_staa_writev().
23 #include <linux/version.h>
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/sched.h>
28 #include <linux/init.h>
30 #include <asm/byteorder.h>
32 #include <linux/errno.h>
33 #include <linux/slab.h>
34 #include <linux/delay.h>
35 #include <linux/interrupt.h>
36 #include <linux/mtd/map.h>
37 #include <linux/mtd/cfi.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/compatmac.h>
42 static int cfi_staa_read(struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
43 static int cfi_staa_write_buffers(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
44 static int cfi_staa_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
45 unsigned long count
, loff_t to
, size_t *retlen
);
46 static int cfi_staa_erase_varsize(struct mtd_info
*, struct erase_info
*);
47 static void cfi_staa_sync (struct mtd_info
*);
48 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
);
49 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
);
50 static int cfi_staa_suspend (struct mtd_info
*);
51 static void cfi_staa_resume (struct mtd_info
*);
53 static void cfi_staa_destroy(struct mtd_info
*);
55 struct mtd_info
*cfi_cmdset_0020(struct map_info
*, int);
57 static struct mtd_info
*cfi_staa_setup (struct map_info
*);
59 static struct mtd_chip_driver cfi_staa_chipdrv
= {
60 .probe
= NULL
, /* Not usable directly */
61 .destroy
= cfi_staa_destroy
,
62 .name
= "cfi_cmdset_0020",
66 /* #define DEBUG_LOCK_BITS */
67 //#define DEBUG_CFI_FEATURES
69 #ifdef DEBUG_CFI_FEATURES
70 static void cfi_tell_features(struct cfi_pri_intelext
*extp
)
73 printk(" Feature/Command Support: %4.4X\n", extp
->FeatureSupport
);
74 printk(" - Chip Erase: %s\n", extp
->FeatureSupport
&1?"supported":"unsupported");
75 printk(" - Suspend Erase: %s\n", extp
->FeatureSupport
&2?"supported":"unsupported");
76 printk(" - Suspend Program: %s\n", extp
->FeatureSupport
&4?"supported":"unsupported");
77 printk(" - Legacy Lock/Unlock: %s\n", extp
->FeatureSupport
&8?"supported":"unsupported");
78 printk(" - Queued Erase: %s\n", extp
->FeatureSupport
&16?"supported":"unsupported");
79 printk(" - Instant block lock: %s\n", extp
->FeatureSupport
&32?"supported":"unsupported");
80 printk(" - Protection Bits: %s\n", extp
->FeatureSupport
&64?"supported":"unsupported");
81 printk(" - Page-mode read: %s\n", extp
->FeatureSupport
&128?"supported":"unsupported");
82 printk(" - Synchronous read: %s\n", extp
->FeatureSupport
&256?"supported":"unsupported");
83 for (i
=9; i
<32; i
++) {
84 if (extp
->FeatureSupport
& (1<<i
))
85 printk(" - Unknown Bit %X: supported\n", i
);
88 printk(" Supported functions after Suspend: %2.2X\n", extp
->SuspendCmdSupport
);
89 printk(" - Program after Erase Suspend: %s\n", extp
->SuspendCmdSupport
&1?"supported":"unsupported");
91 if (extp
->SuspendCmdSupport
& (1<<i
))
92 printk(" - Unknown Bit %X: supported\n", i
);
95 printk(" Block Status Register Mask: %4.4X\n", extp
->BlkStatusRegMask
);
96 printk(" - Lock Bit Active: %s\n", extp
->BlkStatusRegMask
&1?"yes":"no");
97 printk(" - Valid Bit Active: %s\n", extp
->BlkStatusRegMask
&2?"yes":"no");
98 for (i
=2; i
<16; i
++) {
99 if (extp
->BlkStatusRegMask
& (1<<i
))
100 printk(" - Unknown Bit %X Active: yes\n",i
);
103 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
104 extp
->VccOptimal
>> 8, extp
->VccOptimal
& 0xf);
105 if (extp
->VppOptimal
)
106 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
107 extp
->VppOptimal
>> 8, extp
->VppOptimal
& 0xf);
111 /* This routine is made available to other mtd code via
112 * inter_module_register. It must only be accessed through
113 * inter_module_get which will bump the use count of this module. The
114 * addresses passed back in cfi are valid as long as the use count of
115 * this module is non-zero, i.e. between inter_module_get and
116 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
118 struct mtd_info
*cfi_cmdset_0020(struct map_info
*map
, int primary
)
120 struct cfi_private
*cfi
= map
->fldrv_priv
;
125 * It's a real CFI chip, not one for which the probe
126 * routine faked a CFI structure. So we read the feature
129 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
130 struct cfi_pri_intelext
*extp
;
132 extp
= (struct cfi_pri_intelext
*)cfi_read_pri(map
, adr
, sizeof(*extp
), "ST Microelectronics");
136 /* Do some byteswapping if necessary */
137 extp
->FeatureSupport
= cfi32_to_cpu(extp
->FeatureSupport
);
138 extp
->BlkStatusRegMask
= cfi32_to_cpu(extp
->BlkStatusRegMask
);
140 #ifdef DEBUG_CFI_FEATURES
141 /* Tell the user about it in lots of lovely detail */
142 cfi_tell_features(extp
);
145 /* Install our own private info structure */
146 cfi
->cmdset_priv
= extp
;
149 for (i
=0; i
< cfi
->numchips
; i
++) {
150 cfi
->chips
[i
].word_write_time
= 128;
151 cfi
->chips
[i
].buffer_write_time
= 128;
152 cfi
->chips
[i
].erase_time
= 1024;
155 return cfi_staa_setup(map
);
158 static struct mtd_info
*cfi_staa_setup(struct map_info
*map
)
160 struct cfi_private
*cfi
= map
->fldrv_priv
;
161 struct mtd_info
*mtd
;
162 unsigned long offset
= 0;
164 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
166 mtd
= kmalloc(sizeof(*mtd
), GFP_KERNEL
);
167 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
170 printk(KERN_ERR
"Failed to allocate memory for MTD device\n");
171 kfree(cfi
->cmdset_priv
);
175 memset(mtd
, 0, sizeof(*mtd
));
177 mtd
->type
= MTD_NORFLASH
;
178 mtd
->size
= devsize
* cfi
->numchips
;
180 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
181 mtd
->eraseregions
= kmalloc(sizeof(struct mtd_erase_region_info
)
182 * mtd
->numeraseregions
, GFP_KERNEL
);
183 if (!mtd
->eraseregions
) {
184 printk(KERN_ERR
"Failed to allocate memory for MTD erase region info\n");
185 kfree(cfi
->cmdset_priv
);
190 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
191 unsigned long ernum
, ersize
;
192 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
193 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
195 if (mtd
->erasesize
< ersize
) {
196 mtd
->erasesize
= ersize
;
198 for (j
=0; j
<cfi
->numchips
; j
++) {
199 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
200 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
201 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
203 offset
+= (ersize
* ernum
);
206 if (offset
!= devsize
) {
208 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
209 kfree(mtd
->eraseregions
);
210 kfree(cfi
->cmdset_priv
);
215 for (i
=0; i
<mtd
->numeraseregions
;i
++){
216 printk(KERN_DEBUG
"%d: offset=0x%x,size=0x%x,blocks=%d\n",
217 i
,mtd
->eraseregions
[i
].offset
,
218 mtd
->eraseregions
[i
].erasesize
,
219 mtd
->eraseregions
[i
].numblocks
);
222 /* Also select the correct geometry setup too */
223 mtd
->erase
= cfi_staa_erase_varsize
;
224 mtd
->read
= cfi_staa_read
;
225 mtd
->write
= cfi_staa_write_buffers
;
226 mtd
->writev
= cfi_staa_writev
;
227 mtd
->sync
= cfi_staa_sync
;
228 mtd
->lock
= cfi_staa_lock
;
229 mtd
->unlock
= cfi_staa_unlock
;
230 mtd
->suspend
= cfi_staa_suspend
;
231 mtd
->resume
= cfi_staa_resume
;
232 mtd
->flags
= MTD_CAP_NORFLASH
;
233 mtd
->flags
|= MTD_ECC
; /* FIXME: Not all STMicro flashes have this */
234 mtd
->eccsize
= 8; /* FIXME: Should be 0 for STMicro flashes w/out ECC */
235 map
->fldrv
= &cfi_staa_chipdrv
;
236 __module_get(THIS_MODULE
);
237 mtd
->name
= map
->name
;
242 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
244 map_word status
, status_OK
;
246 DECLARE_WAITQUEUE(wait
, current
);
248 unsigned long cmd_addr
;
249 struct cfi_private
*cfi
= map
->fldrv_priv
;
253 /* Ensure cmd read/writes are aligned. */
254 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
256 /* Let's determine this according to the interleave only once */
257 status_OK
= CMD(0x80);
259 timeo
= jiffies
+ HZ
;
261 spin_lock_bh(chip
->mutex
);
263 /* Check that the chip's ready to talk to us.
264 * If it's in FL_ERASING state, suspend it and make it talk now.
266 switch (chip
->state
) {
268 if (!(((struct cfi_pri_intelext
*)cfi
->cmdset_priv
)->FeatureSupport
& 2))
269 goto sleep
; /* We don't support erase suspend */
271 map_write (map
, CMD(0xb0), cmd_addr
);
272 /* If the flash has finished erasing, then 'erase suspend'
273 * appears to make some (28F320) flash devices switch to
274 * 'read' mode. Make sure that we switch to 'read status'
275 * mode so we get the right data. --rmk
277 map_write(map
, CMD(0x70), cmd_addr
);
278 chip
->oldstate
= FL_ERASING
;
279 chip
->state
= FL_ERASE_SUSPENDING
;
280 // printk("Erase suspending at 0x%lx\n", cmd_addr);
282 status
= map_read(map
, cmd_addr
);
283 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
286 if (time_after(jiffies
, timeo
)) {
288 map_write(map
, CMD(0xd0), cmd_addr
);
289 /* make sure we're in 'read status' mode */
290 map_write(map
, CMD(0x70), cmd_addr
);
291 chip
->state
= FL_ERASING
;
292 spin_unlock_bh(chip
->mutex
);
293 printk(KERN_ERR
"Chip not ready after erase "
294 "suspended: status = 0x%lx\n", status
.x
[0]);
298 spin_unlock_bh(chip
->mutex
);
300 spin_lock_bh(chip
->mutex
);
304 map_write(map
, CMD(0xff), cmd_addr
);
305 chip
->state
= FL_READY
;
318 map_write(map
, CMD(0x70), cmd_addr
);
319 chip
->state
= FL_STATUS
;
322 status
= map_read(map
, cmd_addr
);
323 if (map_word_andequal(map
, status
, status_OK
, status_OK
)) {
324 map_write(map
, CMD(0xff), cmd_addr
);
325 chip
->state
= FL_READY
;
329 /* Urgh. Chip not yet ready to talk to us. */
330 if (time_after(jiffies
, timeo
)) {
331 spin_unlock_bh(chip
->mutex
);
332 printk(KERN_ERR
"waiting for chip to be ready timed out in read. WSM status = %lx\n", status
.x
[0]);
336 /* Latency issues. Drop the lock, wait a while and retry */
337 spin_unlock_bh(chip
->mutex
);
343 /* Stick ourselves on a wait queue to be woken when
344 someone changes the status */
345 set_current_state(TASK_UNINTERRUPTIBLE
);
346 add_wait_queue(&chip
->wq
, &wait
);
347 spin_unlock_bh(chip
->mutex
);
349 remove_wait_queue(&chip
->wq
, &wait
);
350 timeo
= jiffies
+ HZ
;
354 map_copy_from(map
, buf
, adr
, len
);
357 chip
->state
= chip
->oldstate
;
358 /* What if one interleaved chip has finished and the
359 other hasn't? The old code would leave the finished
360 one in READY mode. That's bad, and caused -EROFS
361 errors to be returned from do_erase_oneblock because
362 that's the only bit it checked for at the time.
363 As the state machine appears to explicitly allow
364 sending the 0x70 (Read Status) command to an erasing
365 chip and expecting it to be ignored, that's what we
367 map_write(map
, CMD(0xd0), cmd_addr
);
368 map_write(map
, CMD(0x70), cmd_addr
);
372 spin_unlock_bh(chip
->mutex
);
376 static int cfi_staa_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
378 struct map_info
*map
= mtd
->priv
;
379 struct cfi_private
*cfi
= map
->fldrv_priv
;
384 /* ofs: offset within the first chip that the first read should start */
385 chipnum
= (from
>> cfi
->chipshift
);
386 ofs
= from
- (chipnum
<< cfi
->chipshift
);
391 unsigned long thislen
;
393 if (chipnum
>= cfi
->numchips
)
396 if ((len
+ ofs
-1) >> cfi
->chipshift
)
397 thislen
= (1<<cfi
->chipshift
) - ofs
;
401 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
415 static inline int do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
416 unsigned long adr
, const u_char
*buf
, int len
)
418 struct cfi_private
*cfi
= map
->fldrv_priv
;
419 map_word status
, status_OK
;
420 unsigned long cmd_adr
, timeo
;
421 DECLARE_WAITQUEUE(wait
, current
);
424 /* M58LW064A requires bus alignment for buffer wriets -- saw */
425 if (adr
& (map_bankwidth(map
)-1))
428 wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
430 cmd_adr
= adr
& ~(wbufsize
-1);
432 /* Let's determine this according to the interleave only once */
433 status_OK
= CMD(0x80);
435 timeo
= jiffies
+ HZ
;
438 #ifdef DEBUG_CFI_FEATURES
439 printk("%s: chip->state[%d]\n", __FUNCTION__
, chip
->state
);
441 spin_lock_bh(chip
->mutex
);
443 /* Check that the chip's ready to talk to us.
444 * Later, we can actually think about interrupting it
445 * if it's in FL_ERASING state.
446 * Not just yet, though.
448 switch (chip
->state
) {
454 map_write(map
, CMD(0x70), cmd_adr
);
455 chip
->state
= FL_STATUS
;
456 #ifdef DEBUG_CFI_FEATURES
457 printk("%s: 1 status[%x]\n", __FUNCTION__
, map_read(map
, cmd_adr
));
461 status
= map_read(map
, cmd_adr
);
462 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
464 /* Urgh. Chip not yet ready to talk to us. */
465 if (time_after(jiffies
, timeo
)) {
466 spin_unlock_bh(chip
->mutex
);
467 printk(KERN_ERR
"waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
468 status
.x
[0], map_read(map
, cmd_adr
).x
[0]);
472 /* Latency issues. Drop the lock, wait a while and retry */
473 spin_unlock_bh(chip
->mutex
);
478 /* Stick ourselves on a wait queue to be woken when
479 someone changes the status */
480 set_current_state(TASK_UNINTERRUPTIBLE
);
481 add_wait_queue(&chip
->wq
, &wait
);
482 spin_unlock_bh(chip
->mutex
);
484 remove_wait_queue(&chip
->wq
, &wait
);
485 timeo
= jiffies
+ HZ
;
490 map_write(map
, CMD(0xe8), cmd_adr
);
491 chip
->state
= FL_WRITING_TO_BUFFER
;
495 status
= map_read(map
, cmd_adr
);
496 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
499 spin_unlock_bh(chip
->mutex
);
501 spin_lock_bh(chip
->mutex
);
504 /* Argh. Not ready for write to buffer */
506 map_write(map
, CMD(0x70), cmd_adr
);
507 chip
->state
= FL_STATUS
;
508 spin_unlock_bh(chip
->mutex
);
509 printk(KERN_ERR
"Chip not ready for buffer write. Xstatus = %lx\n", status
.x
[0]);
514 /* Write length of data to come */
515 map_write(map
, CMD(len
/map_bankwidth(map
)-1), cmd_adr
);
519 z
+= map_bankwidth(map
), buf
+= map_bankwidth(map
)) {
521 d
= map_word_load(map
, buf
);
522 map_write(map
, d
, adr
+z
);
525 map_write(map
, CMD(0xd0), cmd_adr
);
526 chip
->state
= FL_WRITING
;
528 spin_unlock_bh(chip
->mutex
);
529 cfi_udelay(chip
->buffer_write_time
);
530 spin_lock_bh(chip
->mutex
);
532 timeo
= jiffies
+ (HZ
/2);
535 if (chip
->state
!= FL_WRITING
) {
536 /* Someone's suspended the write. Sleep */
537 set_current_state(TASK_UNINTERRUPTIBLE
);
538 add_wait_queue(&chip
->wq
, &wait
);
539 spin_unlock_bh(chip
->mutex
);
541 remove_wait_queue(&chip
->wq
, &wait
);
542 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
543 spin_lock_bh(chip
->mutex
);
547 status
= map_read(map
, cmd_adr
);
548 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
551 /* OK Still waiting */
552 if (time_after(jiffies
, timeo
)) {
554 map_write(map
, CMD(0x50), cmd_adr
);
555 /* put back into read status register mode */
556 map_write(map
, CMD(0x70), adr
);
557 chip
->state
= FL_STATUS
;
559 spin_unlock_bh(chip
->mutex
);
560 printk(KERN_ERR
"waiting for chip to be ready timed out in bufwrite\n");
564 /* Latency issues. Drop the lock, wait a while and retry */
565 spin_unlock_bh(chip
->mutex
);
568 spin_lock_bh(chip
->mutex
);
571 chip
->buffer_write_time
--;
572 if (!chip
->buffer_write_time
)
573 chip
->buffer_write_time
++;
576 chip
->buffer_write_time
++;
578 /* Done and happy. */
580 chip
->state
= FL_STATUS
;
582 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */
583 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
584 #ifdef DEBUG_CFI_FEATURES
585 printk("%s: 2 status[%lx]\n", __FUNCTION__
, status
.x
[0]);
588 map_write(map
, CMD(0x50), cmd_adr
);
589 /* put back into read status register mode */
590 map_write(map
, CMD(0x70), adr
);
592 spin_unlock_bh(chip
->mutex
);
593 return map_word_bitsset(map
, status
, CMD(0x02)) ? -EROFS
: -EIO
;
596 spin_unlock_bh(chip
->mutex
);
601 static int cfi_staa_write_buffers (struct mtd_info
*mtd
, loff_t to
,
602 size_t len
, size_t *retlen
, const u_char
*buf
)
604 struct map_info
*map
= mtd
->priv
;
605 struct cfi_private
*cfi
= map
->fldrv_priv
;
606 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
615 chipnum
= to
>> cfi
->chipshift
;
616 ofs
= to
- (chipnum
<< cfi
->chipshift
);
618 #ifdef DEBUG_CFI_FEATURES
619 printk("%s: map_bankwidth(map)[%x]\n", __FUNCTION__
, map_bankwidth(map
));
620 printk("%s: chipnum[%x] wbufsize[%x]\n", __FUNCTION__
, chipnum
, wbufsize
);
621 printk("%s: ofs[%x] len[%x]\n", __FUNCTION__
, ofs
, len
);
624 /* Write buffer is worth it only if more than one word to write... */
626 /* We must not cross write block boundaries */
627 int size
= wbufsize
- (ofs
& (wbufsize
-1));
632 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
642 if (ofs
>> cfi
->chipshift
) {
645 if (chipnum
== cfi
->numchips
)
654 * Writev for ECC-Flashes is a little more complicated. We need to maintain
655 * a small buffer for this.
656 * XXX: If the buffer size is not a multiple of 2, this will break
658 #define ECCBUF_SIZE (mtd->eccsize)
659 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
660 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
662 cfi_staa_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
663 unsigned long count
, loff_t to
, size_t *retlen
)
666 size_t totlen
= 0, thislen
;
672 /* We should fall back to a general writev implementation.
673 * Until that is written, just break.
677 buffer
= kmalloc(ECCBUF_SIZE
, GFP_KERNEL
);
681 for (i
=0; i
<count
; i
++) {
682 size_t elem_len
= vecs
[i
].iov_len
;
683 void *elem_base
= vecs
[i
].iov_base
;
684 if (!elem_len
) /* FIXME: Might be unnecessary. Check that */
686 if (buflen
) { /* cut off head */
687 if (buflen
+ elem_len
< ECCBUF_SIZE
) { /* just accumulate */
688 memcpy(buffer
+buflen
, elem_base
, elem_len
);
692 memcpy(buffer
+buflen
, elem_base
, ECCBUF_SIZE
-buflen
);
693 ret
= mtd
->write(mtd
, to
, ECCBUF_SIZE
, &thislen
, buffer
);
695 if (ret
|| thislen
!= ECCBUF_SIZE
)
697 elem_len
-= thislen
-buflen
;
698 elem_base
+= thislen
-buflen
;
701 if (ECCBUF_DIV(elem_len
)) { /* write clean aligned data */
702 ret
= mtd
->write(mtd
, to
, ECCBUF_DIV(elem_len
), &thislen
, elem_base
);
704 if (ret
|| thislen
!= ECCBUF_DIV(elem_len
))
708 buflen
= ECCBUF_MOD(elem_len
); /* cut off tail */
710 memset(buffer
, 0xff, ECCBUF_SIZE
);
711 memcpy(buffer
, elem_base
+ thislen
, buflen
);
714 if (buflen
) { /* flush last page, even if not full */
715 /* This is sometimes intended behaviour, really */
716 ret
= mtd
->write(mtd
, to
, buflen
, &thislen
, buffer
);
718 if (ret
|| thislen
!= ECCBUF_SIZE
)
729 static inline int do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
731 struct cfi_private
*cfi
= map
->fldrv_priv
;
732 map_word status
, status_OK
;
735 DECLARE_WAITQUEUE(wait
, current
);
740 /* Let's determine this according to the interleave only once */
741 status_OK
= CMD(0x80);
743 timeo
= jiffies
+ HZ
;
745 spin_lock_bh(chip
->mutex
);
747 /* Check that the chip's ready to talk to us. */
748 switch (chip
->state
) {
752 map_write(map
, CMD(0x70), adr
);
753 chip
->state
= FL_STATUS
;
756 status
= map_read(map
, adr
);
757 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
760 /* Urgh. Chip not yet ready to talk to us. */
761 if (time_after(jiffies
, timeo
)) {
762 spin_unlock_bh(chip
->mutex
);
763 printk(KERN_ERR
"waiting for chip to be ready timed out in erase\n");
767 /* Latency issues. Drop the lock, wait a while and retry */
768 spin_unlock_bh(chip
->mutex
);
773 /* Stick ourselves on a wait queue to be woken when
774 someone changes the status */
775 set_current_state(TASK_UNINTERRUPTIBLE
);
776 add_wait_queue(&chip
->wq
, &wait
);
777 spin_unlock_bh(chip
->mutex
);
779 remove_wait_queue(&chip
->wq
, &wait
);
780 timeo
= jiffies
+ HZ
;
785 /* Clear the status register first */
786 map_write(map
, CMD(0x50), adr
);
789 map_write(map
, CMD(0x20), adr
);
790 map_write(map
, CMD(0xD0), adr
);
791 chip
->state
= FL_ERASING
;
793 spin_unlock_bh(chip
->mutex
);
795 spin_lock_bh(chip
->mutex
);
797 /* FIXME. Use a timer to check this, and return immediately. */
798 /* Once the state machine's known to be working I'll do that */
800 timeo
= jiffies
+ (HZ
*20);
802 if (chip
->state
!= FL_ERASING
) {
803 /* Someone's suspended the erase. Sleep */
804 set_current_state(TASK_UNINTERRUPTIBLE
);
805 add_wait_queue(&chip
->wq
, &wait
);
806 spin_unlock_bh(chip
->mutex
);
808 remove_wait_queue(&chip
->wq
, &wait
);
809 timeo
= jiffies
+ (HZ
*20); /* FIXME */
810 spin_lock_bh(chip
->mutex
);
814 status
= map_read(map
, adr
);
815 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
818 /* OK Still waiting */
819 if (time_after(jiffies
, timeo
)) {
820 map_write(map
, CMD(0x70), adr
);
821 chip
->state
= FL_STATUS
;
822 printk(KERN_ERR
"waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
824 spin_unlock_bh(chip
->mutex
);
828 /* Latency issues. Drop the lock, wait a while and retry */
829 spin_unlock_bh(chip
->mutex
);
831 spin_lock_bh(chip
->mutex
);
837 /* We've broken this before. It doesn't hurt to be safe */
838 map_write(map
, CMD(0x70), adr
);
839 chip
->state
= FL_STATUS
;
840 status
= map_read(map
, adr
);
842 /* check for lock bit */
843 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
844 unsigned char chipstatus
= status
.x
[0];
845 if (!map_word_equal(map
, status
, CMD(chipstatus
))) {
847 for (w
=0; w
<map_words(map
); w
++) {
848 for (i
= 0; i
<cfi_interleave(cfi
); i
++) {
849 chipstatus
|= status
.x
[w
] >> (cfi
->device_type
* 8);
852 printk(KERN_WARNING
"Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
853 status
.x
[0], chipstatus
);
855 /* Reset the error bits */
856 map_write(map
, CMD(0x50), adr
);
857 map_write(map
, CMD(0x70), adr
);
859 if ((chipstatus
& 0x30) == 0x30) {
860 printk(KERN_NOTICE
"Chip reports improper command sequence: status 0x%x\n", chipstatus
);
862 } else if (chipstatus
& 0x02) {
863 /* Protection bit set */
865 } else if (chipstatus
& 0x8) {
867 printk(KERN_WARNING
"Chip reports voltage low on erase: status 0x%x\n", chipstatus
);
869 } else if (chipstatus
& 0x20) {
871 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr
, chipstatus
);
872 timeo
= jiffies
+ HZ
;
873 chip
->state
= FL_STATUS
;
874 spin_unlock_bh(chip
->mutex
);
877 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x\n", adr
, chipstatus
);
883 spin_unlock_bh(chip
->mutex
);
887 int cfi_staa_erase_varsize(struct mtd_info
*mtd
, struct erase_info
*instr
)
888 { struct map_info
*map
= mtd
->priv
;
889 struct cfi_private
*cfi
= map
->fldrv_priv
;
890 unsigned long adr
, len
;
891 int chipnum
, ret
= 0;
893 struct mtd_erase_region_info
*regions
= mtd
->eraseregions
;
895 if (instr
->addr
> mtd
->size
)
898 if ((instr
->len
+ instr
->addr
) > mtd
->size
)
901 /* Check that both start and end of the requested erase are
902 * aligned with the erasesize at the appropriate addresses.
907 /* Skip all erase regions which are ended before the start of
908 the requested erase. Actually, to save on the calculations,
909 we skip to the first erase region which starts after the
910 start of the requested erase, and then go back one.
913 while (i
< mtd
->numeraseregions
&& instr
->addr
>= regions
[i
].offset
)
917 /* OK, now i is pointing at the erase region in which this
918 erase request starts. Check the start of the requested
919 erase range is aligned with the erase size which is in
923 if (instr
->addr
& (regions
[i
].erasesize
-1))
926 /* Remember the erase region we start on */
929 /* Next, check that the end of the requested erase is aligned
930 * with the erase region at that address.
933 while (i
<mtd
->numeraseregions
&& (instr
->addr
+ instr
->len
) >= regions
[i
].offset
)
936 /* As before, drop back one to point at the region in which
937 the address actually falls
941 if ((instr
->addr
+ instr
->len
) & (regions
[i
].erasesize
-1))
944 chipnum
= instr
->addr
>> cfi
->chipshift
;
945 adr
= instr
->addr
- (chipnum
<< cfi
->chipshift
);
951 ret
= do_erase_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
956 adr
+= regions
[i
].erasesize
;
957 len
-= regions
[i
].erasesize
;
959 if (adr
% (1<< cfi
->chipshift
) == ((regions
[i
].offset
+ (regions
[i
].erasesize
* regions
[i
].numblocks
)) %( 1<< cfi
->chipshift
)))
962 if (adr
>> cfi
->chipshift
) {
966 if (chipnum
>= cfi
->numchips
)
971 instr
->state
= MTD_ERASE_DONE
;
972 mtd_erase_callback(instr
);
977 static void cfi_staa_sync (struct mtd_info
*mtd
)
979 struct map_info
*map
= mtd
->priv
;
980 struct cfi_private
*cfi
= map
->fldrv_priv
;
984 DECLARE_WAITQUEUE(wait
, current
);
986 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
987 chip
= &cfi
->chips
[i
];
990 spin_lock_bh(chip
->mutex
);
992 switch(chip
->state
) {
997 chip
->oldstate
= chip
->state
;
998 chip
->state
= FL_SYNCING
;
999 /* No need to wake_up() on this state change -
1000 * as the whole point is that nobody can do anything
1001 * with the chip now anyway.
1004 spin_unlock_bh(chip
->mutex
);
1008 /* Not an idle state */
1009 add_wait_queue(&chip
->wq
, &wait
);
1011 spin_unlock_bh(chip
->mutex
);
1013 remove_wait_queue(&chip
->wq
, &wait
);
1019 /* Unlock the chips again */
1021 for (i
--; i
>=0; i
--) {
1022 chip
= &cfi
->chips
[i
];
1024 spin_lock_bh(chip
->mutex
);
1026 if (chip
->state
== FL_SYNCING
) {
1027 chip
->state
= chip
->oldstate
;
1030 spin_unlock_bh(chip
->mutex
);
1034 static inline int do_lock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1036 struct cfi_private
*cfi
= map
->fldrv_priv
;
1037 map_word status
, status_OK
;
1038 unsigned long timeo
= jiffies
+ HZ
;
1039 DECLARE_WAITQUEUE(wait
, current
);
1043 /* Let's determine this according to the interleave only once */
1044 status_OK
= CMD(0x80);
1046 timeo
= jiffies
+ HZ
;
1048 spin_lock_bh(chip
->mutex
);
1050 /* Check that the chip's ready to talk to us. */
1051 switch (chip
->state
) {
1053 case FL_JEDEC_QUERY
:
1055 map_write(map
, CMD(0x70), adr
);
1056 chip
->state
= FL_STATUS
;
1059 status
= map_read(map
, adr
);
1060 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1063 /* Urgh. Chip not yet ready to talk to us. */
1064 if (time_after(jiffies
, timeo
)) {
1065 spin_unlock_bh(chip
->mutex
);
1066 printk(KERN_ERR
"waiting for chip to be ready timed out in lock\n");
1070 /* Latency issues. Drop the lock, wait a while and retry */
1071 spin_unlock_bh(chip
->mutex
);
1076 /* Stick ourselves on a wait queue to be woken when
1077 someone changes the status */
1078 set_current_state(TASK_UNINTERRUPTIBLE
);
1079 add_wait_queue(&chip
->wq
, &wait
);
1080 spin_unlock_bh(chip
->mutex
);
1082 remove_wait_queue(&chip
->wq
, &wait
);
1083 timeo
= jiffies
+ HZ
;
1088 map_write(map
, CMD(0x60), adr
);
1089 map_write(map
, CMD(0x01), adr
);
1090 chip
->state
= FL_LOCKING
;
1092 spin_unlock_bh(chip
->mutex
);
1094 spin_lock_bh(chip
->mutex
);
1096 /* FIXME. Use a timer to check this, and return immediately. */
1097 /* Once the state machine's known to be working I'll do that */
1099 timeo
= jiffies
+ (HZ
*2);
1102 status
= map_read(map
, adr
);
1103 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1106 /* OK Still waiting */
1107 if (time_after(jiffies
, timeo
)) {
1108 map_write(map
, CMD(0x70), adr
);
1109 chip
->state
= FL_STATUS
;
1110 printk(KERN_ERR
"waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1112 spin_unlock_bh(chip
->mutex
);
1116 /* Latency issues. Drop the lock, wait a while and retry */
1117 spin_unlock_bh(chip
->mutex
);
1119 spin_lock_bh(chip
->mutex
);
1122 /* Done and happy. */
1123 chip
->state
= FL_STATUS
;
1126 spin_unlock_bh(chip
->mutex
);
1129 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1131 struct map_info
*map
= mtd
->priv
;
1132 struct cfi_private
*cfi
= map
->fldrv_priv
;
1134 int chipnum
, ret
= 0;
1135 #ifdef DEBUG_LOCK_BITS
1136 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1139 if (ofs
& (mtd
->erasesize
- 1))
1142 if (len
& (mtd
->erasesize
-1))
1145 if ((len
+ ofs
) > mtd
->size
)
1148 chipnum
= ofs
>> cfi
->chipshift
;
1149 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1153 #ifdef DEBUG_LOCK_BITS
1154 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1155 printk("before lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1156 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1159 ret
= do_lock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1161 #ifdef DEBUG_LOCK_BITS
1162 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1163 printk("after lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1164 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1170 adr
+= mtd
->erasesize
;
1171 len
-= mtd
->erasesize
;
1173 if (adr
>> cfi
->chipshift
) {
1177 if (chipnum
>= cfi
->numchips
)
1183 static inline int do_unlock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1185 struct cfi_private
*cfi
= map
->fldrv_priv
;
1186 map_word status
, status_OK
;
1187 unsigned long timeo
= jiffies
+ HZ
;
1188 DECLARE_WAITQUEUE(wait
, current
);
1192 /* Let's determine this according to the interleave only once */
1193 status_OK
= CMD(0x80);
1195 timeo
= jiffies
+ HZ
;
1197 spin_lock_bh(chip
->mutex
);
1199 /* Check that the chip's ready to talk to us. */
1200 switch (chip
->state
) {
1202 case FL_JEDEC_QUERY
:
1204 map_write(map
, CMD(0x70), adr
);
1205 chip
->state
= FL_STATUS
;
1208 status
= map_read(map
, adr
);
1209 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1212 /* Urgh. Chip not yet ready to talk to us. */
1213 if (time_after(jiffies
, timeo
)) {
1214 spin_unlock_bh(chip
->mutex
);
1215 printk(KERN_ERR
"waiting for chip to be ready timed out in unlock\n");
1219 /* Latency issues. Drop the lock, wait a while and retry */
1220 spin_unlock_bh(chip
->mutex
);
1225 /* Stick ourselves on a wait queue to be woken when
1226 someone changes the status */
1227 set_current_state(TASK_UNINTERRUPTIBLE
);
1228 add_wait_queue(&chip
->wq
, &wait
);
1229 spin_unlock_bh(chip
->mutex
);
1231 remove_wait_queue(&chip
->wq
, &wait
);
1232 timeo
= jiffies
+ HZ
;
1237 map_write(map
, CMD(0x60), adr
);
1238 map_write(map
, CMD(0xD0), adr
);
1239 chip
->state
= FL_UNLOCKING
;
1241 spin_unlock_bh(chip
->mutex
);
1243 spin_lock_bh(chip
->mutex
);
1245 /* FIXME. Use a timer to check this, and return immediately. */
1246 /* Once the state machine's known to be working I'll do that */
1248 timeo
= jiffies
+ (HZ
*2);
1251 status
= map_read(map
, adr
);
1252 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1255 /* OK Still waiting */
1256 if (time_after(jiffies
, timeo
)) {
1257 map_write(map
, CMD(0x70), adr
);
1258 chip
->state
= FL_STATUS
;
1259 printk(KERN_ERR
"waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1261 spin_unlock_bh(chip
->mutex
);
1265 /* Latency issues. Drop the unlock, wait a while and retry */
1266 spin_unlock_bh(chip
->mutex
);
1268 spin_lock_bh(chip
->mutex
);
1271 /* Done and happy. */
1272 chip
->state
= FL_STATUS
;
1275 spin_unlock_bh(chip
->mutex
);
1278 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, size_t len
)
1280 struct map_info
*map
= mtd
->priv
;
1281 struct cfi_private
*cfi
= map
->fldrv_priv
;
1283 int chipnum
, ret
= 0;
1284 #ifdef DEBUG_LOCK_BITS
1285 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1288 chipnum
= ofs
>> cfi
->chipshift
;
1289 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1291 #ifdef DEBUG_LOCK_BITS
1293 unsigned long temp_adr
= adr
;
1294 unsigned long temp_len
= len
;
1296 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1298 printk("before unlock %x: block status register is %x\n",temp_adr
,cfi_read_query(map
, temp_adr
+(2*ofs_factor
)));
1299 temp_adr
+= mtd
->erasesize
;
1300 temp_len
-= mtd
->erasesize
;
1302 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1306 ret
= do_unlock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1308 #ifdef DEBUG_LOCK_BITS
1309 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1310 printk("after unlock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1311 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1317 static int cfi_staa_suspend(struct mtd_info
*mtd
)
1319 struct map_info
*map
= mtd
->priv
;
1320 struct cfi_private
*cfi
= map
->fldrv_priv
;
1322 struct flchip
*chip
;
1325 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1326 chip
= &cfi
->chips
[i
];
1328 spin_lock_bh(chip
->mutex
);
1330 switch(chip
->state
) {
1334 case FL_JEDEC_QUERY
:
1335 chip
->oldstate
= chip
->state
;
1336 chip
->state
= FL_PM_SUSPENDED
;
1337 /* No need to wake_up() on this state change -
1338 * as the whole point is that nobody can do anything
1339 * with the chip now anyway.
1341 case FL_PM_SUSPENDED
:
1348 spin_unlock_bh(chip
->mutex
);
1351 /* Unlock the chips again */
1354 for (i
--; i
>=0; i
--) {
1355 chip
= &cfi
->chips
[i
];
1357 spin_lock_bh(chip
->mutex
);
1359 if (chip
->state
== FL_PM_SUSPENDED
) {
1360 /* No need to force it into a known state here,
1361 because we're returning failure, and it didn't
1363 chip
->state
= chip
->oldstate
;
1366 spin_unlock_bh(chip
->mutex
);
1373 static void cfi_staa_resume(struct mtd_info
*mtd
)
1375 struct map_info
*map
= mtd
->priv
;
1376 struct cfi_private
*cfi
= map
->fldrv_priv
;
1378 struct flchip
*chip
;
1380 for (i
=0; i
<cfi
->numchips
; i
++) {
1382 chip
= &cfi
->chips
[i
];
1384 spin_lock_bh(chip
->mutex
);
1386 /* Go to known state. Chip may have been power cycled */
1387 if (chip
->state
== FL_PM_SUSPENDED
) {
1388 map_write(map
, CMD(0xFF), 0);
1389 chip
->state
= FL_READY
;
1393 spin_unlock_bh(chip
->mutex
);
1397 static void cfi_staa_destroy(struct mtd_info
*mtd
)
1399 struct map_info
*map
= mtd
->priv
;
1400 struct cfi_private
*cfi
= map
->fldrv_priv
;
1401 kfree(cfi
->cmdset_priv
);
1405 static char im_name
[]="cfi_cmdset_0020";
1407 static int __init
cfi_staa_init(void)
1409 inter_module_register(im_name
, THIS_MODULE
, &cfi_cmdset_0020
);
1413 static void __exit
cfi_staa_exit(void)
1415 inter_module_unregister(im_name
);
1418 module_init(cfi_staa_init
);
1419 module_exit(cfi_staa_exit
);
1421 MODULE_LICENSE("GPL");