2 * Common Flash Interface support:
3 * ST Advanced Architecture Command Set (ID 0x0020)
5 * (C) 2000 Red Hat. GPL'd
7 * 10/10/2000 Nicolas Pitre <nico@fluxnic.net>
8 * - completely revamped method functions so they are aware and
9 * independent of the flash geometry (buswidth, interleave, etc.)
10 * - scalability vs code size is completely set at compile-time
11 * (see include/linux/mtd/cfi.h for selection)
12 * - optimized write buffer method
13 * 06/21/2002 Joern Engel <joern@wh.fh-wedel.de> and others
14 * - modified Intel Command Set 0x0001 to support ST Advanced Architecture
15 * (command set 0x0020)
16 * - added a writev function
17 * 07/13/2005 Joern Engel <joern@wh.fh-wedel.de>
18 * - Plugged memory leak in cfi_staa_writev().
21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
26 #include <asm/byteorder.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
30 #include <linux/delay.h>
31 #include <linux/interrupt.h>
32 #include <linux/mtd/map.h>
33 #include <linux/mtd/cfi.h>
34 #include <linux/mtd/mtd.h>
37 static int cfi_staa_read(struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
38 static int cfi_staa_write_buffers(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
39 static int cfi_staa_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
40 unsigned long count
, loff_t to
, size_t *retlen
);
41 static int cfi_staa_erase_varsize(struct mtd_info
*, struct erase_info
*);
42 static void cfi_staa_sync (struct mtd_info
*);
43 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
);
44 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
);
45 static int cfi_staa_suspend (struct mtd_info
*);
46 static void cfi_staa_resume (struct mtd_info
*);
48 static void cfi_staa_destroy(struct mtd_info
*);
50 struct mtd_info
*cfi_cmdset_0020(struct map_info
*, int);
52 static struct mtd_info
*cfi_staa_setup (struct map_info
*);
54 static struct mtd_chip_driver cfi_staa_chipdrv
= {
55 .probe
= NULL
, /* Not usable directly */
56 .destroy
= cfi_staa_destroy
,
57 .name
= "cfi_cmdset_0020",
61 /* #define DEBUG_LOCK_BITS */
62 //#define DEBUG_CFI_FEATURES
64 #ifdef DEBUG_CFI_FEATURES
65 static void cfi_tell_features(struct cfi_pri_intelext
*extp
)
68 printk(" Feature/Command Support: %4.4X\n", extp
->FeatureSupport
);
69 printk(" - Chip Erase: %s\n", extp
->FeatureSupport
&1?"supported":"unsupported");
70 printk(" - Suspend Erase: %s\n", extp
->FeatureSupport
&2?"supported":"unsupported");
71 printk(" - Suspend Program: %s\n", extp
->FeatureSupport
&4?"supported":"unsupported");
72 printk(" - Legacy Lock/Unlock: %s\n", extp
->FeatureSupport
&8?"supported":"unsupported");
73 printk(" - Queued Erase: %s\n", extp
->FeatureSupport
&16?"supported":"unsupported");
74 printk(" - Instant block lock: %s\n", extp
->FeatureSupport
&32?"supported":"unsupported");
75 printk(" - Protection Bits: %s\n", extp
->FeatureSupport
&64?"supported":"unsupported");
76 printk(" - Page-mode read: %s\n", extp
->FeatureSupport
&128?"supported":"unsupported");
77 printk(" - Synchronous read: %s\n", extp
->FeatureSupport
&256?"supported":"unsupported");
78 for (i
=9; i
<32; i
++) {
79 if (extp
->FeatureSupport
& (1<<i
))
80 printk(" - Unknown Bit %X: supported\n", i
);
83 printk(" Supported functions after Suspend: %2.2X\n", extp
->SuspendCmdSupport
);
84 printk(" - Program after Erase Suspend: %s\n", extp
->SuspendCmdSupport
&1?"supported":"unsupported");
86 if (extp
->SuspendCmdSupport
& (1<<i
))
87 printk(" - Unknown Bit %X: supported\n", i
);
90 printk(" Block Status Register Mask: %4.4X\n", extp
->BlkStatusRegMask
);
91 printk(" - Lock Bit Active: %s\n", extp
->BlkStatusRegMask
&1?"yes":"no");
92 printk(" - Valid Bit Active: %s\n", extp
->BlkStatusRegMask
&2?"yes":"no");
93 for (i
=2; i
<16; i
++) {
94 if (extp
->BlkStatusRegMask
& (1<<i
))
95 printk(" - Unknown Bit %X Active: yes\n",i
);
98 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
99 extp
->VccOptimal
>> 8, extp
->VccOptimal
& 0xf);
100 if (extp
->VppOptimal
)
101 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
102 extp
->VppOptimal
>> 8, extp
->VppOptimal
& 0xf);
106 /* This routine is made available to other mtd code via
107 * inter_module_register. It must only be accessed through
108 * inter_module_get which will bump the use count of this module. The
109 * addresses passed back in cfi are valid as long as the use count of
110 * this module is non-zero, i.e. between inter_module_get and
111 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
113 struct mtd_info
*cfi_cmdset_0020(struct map_info
*map
, int primary
)
115 struct cfi_private
*cfi
= map
->fldrv_priv
;
120 * It's a real CFI chip, not one for which the probe
121 * routine faked a CFI structure. So we read the feature
124 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
125 struct cfi_pri_intelext
*extp
;
127 extp
= (struct cfi_pri_intelext
*)cfi_read_pri(map
, adr
, sizeof(*extp
), "ST Microelectronics");
131 if (extp
->MajorVersion
!= '1' ||
132 (extp
->MinorVersion
< '0' || extp
->MinorVersion
> '3')) {
133 printk(KERN_ERR
" Unknown ST Microelectronics"
134 " Extended Query version %c.%c.\n",
135 extp
->MajorVersion
, extp
->MinorVersion
);
140 /* Do some byteswapping if necessary */
141 extp
->FeatureSupport
= cfi32_to_cpu(map
, extp
->FeatureSupport
);
142 extp
->BlkStatusRegMask
= cfi32_to_cpu(map
,
143 extp
->BlkStatusRegMask
);
145 #ifdef DEBUG_CFI_FEATURES
146 /* Tell the user about it in lots of lovely detail */
147 cfi_tell_features(extp
);
150 /* Install our own private info structure */
151 cfi
->cmdset_priv
= extp
;
154 for (i
=0; i
< cfi
->numchips
; i
++) {
155 cfi
->chips
[i
].word_write_time
= 128;
156 cfi
->chips
[i
].buffer_write_time
= 128;
157 cfi
->chips
[i
].erase_time
= 1024;
158 cfi
->chips
[i
].ref_point_counter
= 0;
159 init_waitqueue_head(&(cfi
->chips
[i
].wq
));
162 return cfi_staa_setup(map
);
164 EXPORT_SYMBOL_GPL(cfi_cmdset_0020
);
166 static struct mtd_info
*cfi_staa_setup(struct map_info
*map
)
168 struct cfi_private
*cfi
= map
->fldrv_priv
;
169 struct mtd_info
*mtd
;
170 unsigned long offset
= 0;
172 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
174 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
175 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
178 kfree(cfi
->cmdset_priv
);
183 mtd
->type
= MTD_NORFLASH
;
184 mtd
->size
= devsize
* cfi
->numchips
;
186 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
187 mtd
->eraseregions
= kmalloc_array(mtd
->numeraseregions
,
188 sizeof(struct mtd_erase_region_info
),
190 if (!mtd
->eraseregions
) {
191 kfree(cfi
->cmdset_priv
);
196 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
197 unsigned long ernum
, ersize
;
198 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
199 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
201 if (mtd
->erasesize
< ersize
) {
202 mtd
->erasesize
= ersize
;
204 for (j
=0; j
<cfi
->numchips
; j
++) {
205 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
206 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
207 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
209 offset
+= (ersize
* ernum
);
212 if (offset
!= devsize
) {
214 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
215 kfree(mtd
->eraseregions
);
216 kfree(cfi
->cmdset_priv
);
221 for (i
=0; i
<mtd
->numeraseregions
;i
++){
222 printk(KERN_DEBUG
"%d: offset=0x%llx,size=0x%x,blocks=%d\n",
223 i
, (unsigned long long)mtd
->eraseregions
[i
].offset
,
224 mtd
->eraseregions
[i
].erasesize
,
225 mtd
->eraseregions
[i
].numblocks
);
228 /* Also select the correct geometry setup too */
229 mtd
->_erase
= cfi_staa_erase_varsize
;
230 mtd
->_read
= cfi_staa_read
;
231 mtd
->_write
= cfi_staa_write_buffers
;
232 mtd
->_writev
= cfi_staa_writev
;
233 mtd
->_sync
= cfi_staa_sync
;
234 mtd
->_lock
= cfi_staa_lock
;
235 mtd
->_unlock
= cfi_staa_unlock
;
236 mtd
->_suspend
= cfi_staa_suspend
;
237 mtd
->_resume
= cfi_staa_resume
;
238 mtd
->flags
= MTD_CAP_NORFLASH
& ~MTD_BIT_WRITEABLE
;
239 mtd
->writesize
= 8; /* FIXME: Should be 0 for STMicro flashes w/out ECC */
240 mtd
->writebufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
241 map
->fldrv
= &cfi_staa_chipdrv
;
242 __module_get(THIS_MODULE
);
243 mtd
->name
= map
->name
;
248 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
250 map_word status
, status_OK
;
252 DECLARE_WAITQUEUE(wait
, current
);
254 unsigned long cmd_addr
;
255 struct cfi_private
*cfi
= map
->fldrv_priv
;
259 /* Ensure cmd read/writes are aligned. */
260 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
262 /* Let's determine this according to the interleave only once */
263 status_OK
= CMD(0x80);
265 timeo
= jiffies
+ HZ
;
267 mutex_lock(&chip
->mutex
);
269 /* Check that the chip's ready to talk to us.
270 * If it's in FL_ERASING state, suspend it and make it talk now.
272 switch (chip
->state
) {
274 if (!(((struct cfi_pri_intelext
*)cfi
->cmdset_priv
)->FeatureSupport
& 2))
275 goto sleep
; /* We don't support erase suspend */
277 map_write (map
, CMD(0xb0), cmd_addr
);
278 /* If the flash has finished erasing, then 'erase suspend'
279 * appears to make some (28F320) flash devices switch to
280 * 'read' mode. Make sure that we switch to 'read status'
281 * mode so we get the right data. --rmk
283 map_write(map
, CMD(0x70), cmd_addr
);
284 chip
->oldstate
= FL_ERASING
;
285 chip
->state
= FL_ERASE_SUSPENDING
;
286 // printk("Erase suspending at 0x%lx\n", cmd_addr);
288 status
= map_read(map
, cmd_addr
);
289 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
292 if (time_after(jiffies
, timeo
)) {
294 map_write(map
, CMD(0xd0), cmd_addr
);
295 /* make sure we're in 'read status' mode */
296 map_write(map
, CMD(0x70), cmd_addr
);
297 chip
->state
= FL_ERASING
;
299 mutex_unlock(&chip
->mutex
);
300 printk(KERN_ERR
"Chip not ready after erase "
301 "suspended: status = 0x%lx\n", status
.x
[0]);
305 mutex_unlock(&chip
->mutex
);
307 mutex_lock(&chip
->mutex
);
311 map_write(map
, CMD(0xff), cmd_addr
);
312 chip
->state
= FL_READY
;
325 map_write(map
, CMD(0x70), cmd_addr
);
326 chip
->state
= FL_STATUS
;
329 status
= map_read(map
, cmd_addr
);
330 if (map_word_andequal(map
, status
, status_OK
, status_OK
)) {
331 map_write(map
, CMD(0xff), cmd_addr
);
332 chip
->state
= FL_READY
;
336 /* Urgh. Chip not yet ready to talk to us. */
337 if (time_after(jiffies
, timeo
)) {
338 mutex_unlock(&chip
->mutex
);
339 printk(KERN_ERR
"waiting for chip to be ready timed out in read. WSM status = %lx\n", status
.x
[0]);
343 /* Latency issues. Drop the lock, wait a while and retry */
344 mutex_unlock(&chip
->mutex
);
350 /* Stick ourselves on a wait queue to be woken when
351 someone changes the status */
352 set_current_state(TASK_UNINTERRUPTIBLE
);
353 add_wait_queue(&chip
->wq
, &wait
);
354 mutex_unlock(&chip
->mutex
);
356 remove_wait_queue(&chip
->wq
, &wait
);
357 timeo
= jiffies
+ HZ
;
361 map_copy_from(map
, buf
, adr
, len
);
364 chip
->state
= chip
->oldstate
;
365 /* What if one interleaved chip has finished and the
366 other hasn't? The old code would leave the finished
367 one in READY mode. That's bad, and caused -EROFS
368 errors to be returned from do_erase_oneblock because
369 that's the only bit it checked for at the time.
370 As the state machine appears to explicitly allow
371 sending the 0x70 (Read Status) command to an erasing
372 chip and expecting it to be ignored, that's what we
374 map_write(map
, CMD(0xd0), cmd_addr
);
375 map_write(map
, CMD(0x70), cmd_addr
);
379 mutex_unlock(&chip
->mutex
);
383 static int cfi_staa_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
385 struct map_info
*map
= mtd
->priv
;
386 struct cfi_private
*cfi
= map
->fldrv_priv
;
391 /* ofs: offset within the first chip that the first read should start */
392 chipnum
= (from
>> cfi
->chipshift
);
393 ofs
= from
- (chipnum
<< cfi
->chipshift
);
396 unsigned long thislen
;
398 if (chipnum
>= cfi
->numchips
)
401 if ((len
+ ofs
-1) >> cfi
->chipshift
)
402 thislen
= (1<<cfi
->chipshift
) - ofs
;
406 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
420 static int do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
421 unsigned long adr
, const u_char
*buf
, int len
)
423 struct cfi_private
*cfi
= map
->fldrv_priv
;
424 map_word status
, status_OK
;
425 unsigned long cmd_adr
, timeo
;
426 DECLARE_WAITQUEUE(wait
, current
);
429 /* M58LW064A requires bus alignment for buffer wriets -- saw */
430 if (adr
& (map_bankwidth(map
)-1))
433 wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
435 cmd_adr
= adr
& ~(wbufsize
-1);
437 /* Let's determine this according to the interleave only once */
438 status_OK
= CMD(0x80);
440 timeo
= jiffies
+ HZ
;
443 #ifdef DEBUG_CFI_FEATURES
444 printk("%s: chip->state[%d]\n", __func__
, chip
->state
);
446 mutex_lock(&chip
->mutex
);
448 /* Check that the chip's ready to talk to us.
449 * Later, we can actually think about interrupting it
450 * if it's in FL_ERASING state.
451 * Not just yet, though.
453 switch (chip
->state
) {
459 map_write(map
, CMD(0x70), cmd_adr
);
460 chip
->state
= FL_STATUS
;
461 #ifdef DEBUG_CFI_FEATURES
462 printk("%s: 1 status[%x]\n", __func__
, map_read(map
, cmd_adr
));
466 status
= map_read(map
, cmd_adr
);
467 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
469 /* Urgh. Chip not yet ready to talk to us. */
470 if (time_after(jiffies
, timeo
)) {
471 mutex_unlock(&chip
->mutex
);
472 printk(KERN_ERR
"waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
473 status
.x
[0], map_read(map
, cmd_adr
).x
[0]);
477 /* Latency issues. Drop the lock, wait a while and retry */
478 mutex_unlock(&chip
->mutex
);
483 /* Stick ourselves on a wait queue to be woken when
484 someone changes the status */
485 set_current_state(TASK_UNINTERRUPTIBLE
);
486 add_wait_queue(&chip
->wq
, &wait
);
487 mutex_unlock(&chip
->mutex
);
489 remove_wait_queue(&chip
->wq
, &wait
);
490 timeo
= jiffies
+ HZ
;
495 map_write(map
, CMD(0xe8), cmd_adr
);
496 chip
->state
= FL_WRITING_TO_BUFFER
;
500 status
= map_read(map
, cmd_adr
);
501 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
504 mutex_unlock(&chip
->mutex
);
506 mutex_lock(&chip
->mutex
);
509 /* Argh. Not ready for write to buffer */
511 map_write(map
, CMD(0x70), cmd_adr
);
512 chip
->state
= FL_STATUS
;
513 mutex_unlock(&chip
->mutex
);
514 printk(KERN_ERR
"Chip not ready for buffer write. Xstatus = %lx\n", status
.x
[0]);
519 /* Write length of data to come */
520 map_write(map
, CMD(len
/map_bankwidth(map
)-1), cmd_adr
);
524 z
+= map_bankwidth(map
), buf
+= map_bankwidth(map
)) {
526 d
= map_word_load(map
, buf
);
527 map_write(map
, d
, adr
+z
);
530 map_write(map
, CMD(0xd0), cmd_adr
);
531 chip
->state
= FL_WRITING
;
533 mutex_unlock(&chip
->mutex
);
534 cfi_udelay(chip
->buffer_write_time
);
535 mutex_lock(&chip
->mutex
);
537 timeo
= jiffies
+ (HZ
/2);
540 if (chip
->state
!= FL_WRITING
) {
541 /* Someone's suspended the write. Sleep */
542 set_current_state(TASK_UNINTERRUPTIBLE
);
543 add_wait_queue(&chip
->wq
, &wait
);
544 mutex_unlock(&chip
->mutex
);
546 remove_wait_queue(&chip
->wq
, &wait
);
547 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
548 mutex_lock(&chip
->mutex
);
552 status
= map_read(map
, cmd_adr
);
553 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
556 /* OK Still waiting */
557 if (time_after(jiffies
, timeo
)) {
559 map_write(map
, CMD(0x50), cmd_adr
);
560 /* put back into read status register mode */
561 map_write(map
, CMD(0x70), adr
);
562 chip
->state
= FL_STATUS
;
564 mutex_unlock(&chip
->mutex
);
565 printk(KERN_ERR
"waiting for chip to be ready timed out in bufwrite\n");
569 /* Latency issues. Drop the lock, wait a while and retry */
570 mutex_unlock(&chip
->mutex
);
573 mutex_lock(&chip
->mutex
);
576 chip
->buffer_write_time
--;
577 if (!chip
->buffer_write_time
)
578 chip
->buffer_write_time
++;
581 chip
->buffer_write_time
++;
583 /* Done and happy. */
585 chip
->state
= FL_STATUS
;
587 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */
588 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
589 #ifdef DEBUG_CFI_FEATURES
590 printk("%s: 2 status[%lx]\n", __func__
, status
.x
[0]);
593 map_write(map
, CMD(0x50), cmd_adr
);
594 /* put back into read status register mode */
595 map_write(map
, CMD(0x70), adr
);
597 mutex_unlock(&chip
->mutex
);
598 return map_word_bitsset(map
, status
, CMD(0x02)) ? -EROFS
: -EIO
;
601 mutex_unlock(&chip
->mutex
);
606 static int cfi_staa_write_buffers (struct mtd_info
*mtd
, loff_t to
,
607 size_t len
, size_t *retlen
, const u_char
*buf
)
609 struct map_info
*map
= mtd
->priv
;
610 struct cfi_private
*cfi
= map
->fldrv_priv
;
611 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
616 chipnum
= to
>> cfi
->chipshift
;
617 ofs
= to
- (chipnum
<< cfi
->chipshift
);
619 #ifdef DEBUG_CFI_FEATURES
620 printk("%s: map_bankwidth(map)[%x]\n", __func__
, map_bankwidth(map
));
621 printk("%s: chipnum[%x] wbufsize[%x]\n", __func__
, chipnum
, wbufsize
);
622 printk("%s: ofs[%x] len[%x]\n", __func__
, ofs
, len
);
625 /* Write buffer is worth it only if more than one word to write... */
627 /* We must not cross write block boundaries */
628 int size
= wbufsize
- (ofs
& (wbufsize
-1));
633 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
643 if (ofs
>> cfi
->chipshift
) {
646 if (chipnum
== cfi
->numchips
)
655 * Writev for ECC-Flashes is a little more complicated. We need to maintain
656 * a small buffer for this.
657 * XXX: If the buffer size is not a multiple of 2, this will break
659 #define ECCBUF_SIZE (mtd->writesize)
660 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
661 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
663 cfi_staa_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
664 unsigned long count
, loff_t to
, size_t *retlen
)
667 size_t totlen
= 0, thislen
;
673 /* We should fall back to a general writev implementation.
674 * Until that is written, just break.
678 buffer
= kmalloc(ECCBUF_SIZE
, GFP_KERNEL
);
682 for (i
=0; i
<count
; i
++) {
683 size_t elem_len
= vecs
[i
].iov_len
;
684 void *elem_base
= vecs
[i
].iov_base
;
685 if (!elem_len
) /* FIXME: Might be unnecessary. Check that */
687 if (buflen
) { /* cut off head */
688 if (buflen
+ elem_len
< ECCBUF_SIZE
) { /* just accumulate */
689 memcpy(buffer
+buflen
, elem_base
, elem_len
);
693 memcpy(buffer
+buflen
, elem_base
, ECCBUF_SIZE
-buflen
);
694 ret
= mtd_write(mtd
, to
, ECCBUF_SIZE
, &thislen
,
697 if (ret
|| thislen
!= ECCBUF_SIZE
)
699 elem_len
-= thislen
-buflen
;
700 elem_base
+= thislen
-buflen
;
703 if (ECCBUF_DIV(elem_len
)) { /* write clean aligned data */
704 ret
= mtd_write(mtd
, to
, ECCBUF_DIV(elem_len
),
705 &thislen
, elem_base
);
707 if (ret
|| thislen
!= ECCBUF_DIV(elem_len
))
711 buflen
= ECCBUF_MOD(elem_len
); /* cut off tail */
713 memset(buffer
, 0xff, ECCBUF_SIZE
);
714 memcpy(buffer
, elem_base
+ thislen
, buflen
);
717 if (buflen
) { /* flush last page, even if not full */
718 /* This is sometimes intended behaviour, really */
719 ret
= mtd_write(mtd
, to
, buflen
, &thislen
, buffer
);
721 if (ret
|| thislen
!= ECCBUF_SIZE
)
732 static inline int do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
734 struct cfi_private
*cfi
= map
->fldrv_priv
;
735 map_word status
, status_OK
;
738 DECLARE_WAITQUEUE(wait
, current
);
743 /* Let's determine this according to the interleave only once */
744 status_OK
= CMD(0x80);
746 timeo
= jiffies
+ HZ
;
748 mutex_lock(&chip
->mutex
);
750 /* Check that the chip's ready to talk to us. */
751 switch (chip
->state
) {
755 map_write(map
, CMD(0x70), adr
);
756 chip
->state
= FL_STATUS
;
759 status
= map_read(map
, adr
);
760 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
763 /* Urgh. Chip not yet ready to talk to us. */
764 if (time_after(jiffies
, timeo
)) {
765 mutex_unlock(&chip
->mutex
);
766 printk(KERN_ERR
"waiting for chip to be ready timed out in erase\n");
770 /* Latency issues. Drop the lock, wait a while and retry */
771 mutex_unlock(&chip
->mutex
);
776 /* Stick ourselves on a wait queue to be woken when
777 someone changes the status */
778 set_current_state(TASK_UNINTERRUPTIBLE
);
779 add_wait_queue(&chip
->wq
, &wait
);
780 mutex_unlock(&chip
->mutex
);
782 remove_wait_queue(&chip
->wq
, &wait
);
783 timeo
= jiffies
+ HZ
;
788 /* Clear the status register first */
789 map_write(map
, CMD(0x50), adr
);
792 map_write(map
, CMD(0x20), adr
);
793 map_write(map
, CMD(0xD0), adr
);
794 chip
->state
= FL_ERASING
;
796 mutex_unlock(&chip
->mutex
);
798 mutex_lock(&chip
->mutex
);
800 /* FIXME. Use a timer to check this, and return immediately. */
801 /* Once the state machine's known to be working I'll do that */
803 timeo
= jiffies
+ (HZ
*20);
805 if (chip
->state
!= FL_ERASING
) {
806 /* Someone's suspended the erase. Sleep */
807 set_current_state(TASK_UNINTERRUPTIBLE
);
808 add_wait_queue(&chip
->wq
, &wait
);
809 mutex_unlock(&chip
->mutex
);
811 remove_wait_queue(&chip
->wq
, &wait
);
812 timeo
= jiffies
+ (HZ
*20); /* FIXME */
813 mutex_lock(&chip
->mutex
);
817 status
= map_read(map
, adr
);
818 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
821 /* OK Still waiting */
822 if (time_after(jiffies
, timeo
)) {
823 map_write(map
, CMD(0x70), adr
);
824 chip
->state
= FL_STATUS
;
825 printk(KERN_ERR
"waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
827 mutex_unlock(&chip
->mutex
);
831 /* Latency issues. Drop the lock, wait a while and retry */
832 mutex_unlock(&chip
->mutex
);
834 mutex_lock(&chip
->mutex
);
840 /* We've broken this before. It doesn't hurt to be safe */
841 map_write(map
, CMD(0x70), adr
);
842 chip
->state
= FL_STATUS
;
843 status
= map_read(map
, adr
);
845 /* check for lock bit */
846 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
847 unsigned char chipstatus
= status
.x
[0];
848 if (!map_word_equal(map
, status
, CMD(chipstatus
))) {
850 for (w
=0; w
<map_words(map
); w
++) {
851 for (i
= 0; i
<cfi_interleave(cfi
); i
++) {
852 chipstatus
|= status
.x
[w
] >> (cfi
->device_type
* 8);
855 printk(KERN_WARNING
"Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
856 status
.x
[0], chipstatus
);
858 /* Reset the error bits */
859 map_write(map
, CMD(0x50), adr
);
860 map_write(map
, CMD(0x70), adr
);
862 if ((chipstatus
& 0x30) == 0x30) {
863 printk(KERN_NOTICE
"Chip reports improper command sequence: status 0x%x\n", chipstatus
);
865 } else if (chipstatus
& 0x02) {
866 /* Protection bit set */
868 } else if (chipstatus
& 0x8) {
870 printk(KERN_WARNING
"Chip reports voltage low on erase: status 0x%x\n", chipstatus
);
872 } else if (chipstatus
& 0x20) {
874 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr
, chipstatus
);
875 timeo
= jiffies
+ HZ
;
876 chip
->state
= FL_STATUS
;
877 mutex_unlock(&chip
->mutex
);
880 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x\n", adr
, chipstatus
);
886 mutex_unlock(&chip
->mutex
);
890 static int cfi_staa_erase_varsize(struct mtd_info
*mtd
,
891 struct erase_info
*instr
)
892 { struct map_info
*map
= mtd
->priv
;
893 struct cfi_private
*cfi
= map
->fldrv_priv
;
894 unsigned long adr
, len
;
895 int chipnum
, ret
= 0;
897 struct mtd_erase_region_info
*regions
= mtd
->eraseregions
;
899 /* Check that both start and end of the requested erase are
900 * aligned with the erasesize at the appropriate addresses.
905 /* Skip all erase regions which are ended before the start of
906 the requested erase. Actually, to save on the calculations,
907 we skip to the first erase region which starts after the
908 start of the requested erase, and then go back one.
911 while (i
< mtd
->numeraseregions
&& instr
->addr
>= regions
[i
].offset
)
915 /* OK, now i is pointing at the erase region in which this
916 erase request starts. Check the start of the requested
917 erase range is aligned with the erase size which is in
921 if (instr
->addr
& (regions
[i
].erasesize
-1))
924 /* Remember the erase region we start on */
927 /* Next, check that the end of the requested erase is aligned
928 * with the erase region at that address.
931 while (i
<mtd
->numeraseregions
&& (instr
->addr
+ instr
->len
) >= regions
[i
].offset
)
934 /* As before, drop back one to point at the region in which
935 the address actually falls
939 if ((instr
->addr
+ instr
->len
) & (regions
[i
].erasesize
-1))
942 chipnum
= instr
->addr
>> cfi
->chipshift
;
943 adr
= instr
->addr
- (chipnum
<< cfi
->chipshift
);
949 ret
= do_erase_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
954 adr
+= regions
[i
].erasesize
;
955 len
-= regions
[i
].erasesize
;
957 if (adr
% (1<< cfi
->chipshift
) == (((unsigned long)regions
[i
].offset
+ (regions
[i
].erasesize
* regions
[i
].numblocks
)) %( 1<< cfi
->chipshift
)))
960 if (adr
>> cfi
->chipshift
) {
964 if (chipnum
>= cfi
->numchips
)
972 static void cfi_staa_sync (struct mtd_info
*mtd
)
974 struct map_info
*map
= mtd
->priv
;
975 struct cfi_private
*cfi
= map
->fldrv_priv
;
979 DECLARE_WAITQUEUE(wait
, current
);
981 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
982 chip
= &cfi
->chips
[i
];
985 mutex_lock(&chip
->mutex
);
987 switch(chip
->state
) {
992 chip
->oldstate
= chip
->state
;
993 chip
->state
= FL_SYNCING
;
994 /* No need to wake_up() on this state change -
995 * as the whole point is that nobody can do anything
996 * with the chip now anyway.
999 mutex_unlock(&chip
->mutex
);
1003 /* Not an idle state */
1004 set_current_state(TASK_UNINTERRUPTIBLE
);
1005 add_wait_queue(&chip
->wq
, &wait
);
1007 mutex_unlock(&chip
->mutex
);
1009 remove_wait_queue(&chip
->wq
, &wait
);
1015 /* Unlock the chips again */
1017 for (i
--; i
>=0; i
--) {
1018 chip
= &cfi
->chips
[i
];
1020 mutex_lock(&chip
->mutex
);
1022 if (chip
->state
== FL_SYNCING
) {
1023 chip
->state
= chip
->oldstate
;
1026 mutex_unlock(&chip
->mutex
);
1030 static inline int do_lock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1032 struct cfi_private
*cfi
= map
->fldrv_priv
;
1033 map_word status
, status_OK
;
1034 unsigned long timeo
= jiffies
+ HZ
;
1035 DECLARE_WAITQUEUE(wait
, current
);
1039 /* Let's determine this according to the interleave only once */
1040 status_OK
= CMD(0x80);
1042 timeo
= jiffies
+ HZ
;
1044 mutex_lock(&chip
->mutex
);
1046 /* Check that the chip's ready to talk to us. */
1047 switch (chip
->state
) {
1049 case FL_JEDEC_QUERY
:
1051 map_write(map
, CMD(0x70), adr
);
1052 chip
->state
= FL_STATUS
;
1055 status
= map_read(map
, adr
);
1056 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1059 /* Urgh. Chip not yet ready to talk to us. */
1060 if (time_after(jiffies
, timeo
)) {
1061 mutex_unlock(&chip
->mutex
);
1062 printk(KERN_ERR
"waiting for chip to be ready timed out in lock\n");
1066 /* Latency issues. Drop the lock, wait a while and retry */
1067 mutex_unlock(&chip
->mutex
);
1072 /* Stick ourselves on a wait queue to be woken when
1073 someone changes the status */
1074 set_current_state(TASK_UNINTERRUPTIBLE
);
1075 add_wait_queue(&chip
->wq
, &wait
);
1076 mutex_unlock(&chip
->mutex
);
1078 remove_wait_queue(&chip
->wq
, &wait
);
1079 timeo
= jiffies
+ HZ
;
1084 map_write(map
, CMD(0x60), adr
);
1085 map_write(map
, CMD(0x01), adr
);
1086 chip
->state
= FL_LOCKING
;
1088 mutex_unlock(&chip
->mutex
);
1090 mutex_lock(&chip
->mutex
);
1092 /* FIXME. Use a timer to check this, and return immediately. */
1093 /* Once the state machine's known to be working I'll do that */
1095 timeo
= jiffies
+ (HZ
*2);
1098 status
= map_read(map
, adr
);
1099 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1102 /* OK Still waiting */
1103 if (time_after(jiffies
, timeo
)) {
1104 map_write(map
, CMD(0x70), adr
);
1105 chip
->state
= FL_STATUS
;
1106 printk(KERN_ERR
"waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1108 mutex_unlock(&chip
->mutex
);
1112 /* Latency issues. Drop the lock, wait a while and retry */
1113 mutex_unlock(&chip
->mutex
);
1115 mutex_lock(&chip
->mutex
);
1118 /* Done and happy. */
1119 chip
->state
= FL_STATUS
;
1122 mutex_unlock(&chip
->mutex
);
1125 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1127 struct map_info
*map
= mtd
->priv
;
1128 struct cfi_private
*cfi
= map
->fldrv_priv
;
1130 int chipnum
, ret
= 0;
1131 #ifdef DEBUG_LOCK_BITS
1132 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1135 if (ofs
& (mtd
->erasesize
- 1))
1138 if (len
& (mtd
->erasesize
-1))
1141 chipnum
= ofs
>> cfi
->chipshift
;
1142 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1146 #ifdef DEBUG_LOCK_BITS
1147 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1148 printk("before lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1149 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1152 ret
= do_lock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1154 #ifdef DEBUG_LOCK_BITS
1155 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1156 printk("after lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1157 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1163 adr
+= mtd
->erasesize
;
1164 len
-= mtd
->erasesize
;
1166 if (adr
>> cfi
->chipshift
) {
1170 if (chipnum
>= cfi
->numchips
)
1176 static inline int do_unlock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1178 struct cfi_private
*cfi
= map
->fldrv_priv
;
1179 map_word status
, status_OK
;
1180 unsigned long timeo
= jiffies
+ HZ
;
1181 DECLARE_WAITQUEUE(wait
, current
);
1185 /* Let's determine this according to the interleave only once */
1186 status_OK
= CMD(0x80);
1188 timeo
= jiffies
+ HZ
;
1190 mutex_lock(&chip
->mutex
);
1192 /* Check that the chip's ready to talk to us. */
1193 switch (chip
->state
) {
1195 case FL_JEDEC_QUERY
:
1197 map_write(map
, CMD(0x70), adr
);
1198 chip
->state
= FL_STATUS
;
1201 status
= map_read(map
, adr
);
1202 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1205 /* Urgh. Chip not yet ready to talk to us. */
1206 if (time_after(jiffies
, timeo
)) {
1207 mutex_unlock(&chip
->mutex
);
1208 printk(KERN_ERR
"waiting for chip to be ready timed out in unlock\n");
1212 /* Latency issues. Drop the lock, wait a while and retry */
1213 mutex_unlock(&chip
->mutex
);
1218 /* Stick ourselves on a wait queue to be woken when
1219 someone changes the status */
1220 set_current_state(TASK_UNINTERRUPTIBLE
);
1221 add_wait_queue(&chip
->wq
, &wait
);
1222 mutex_unlock(&chip
->mutex
);
1224 remove_wait_queue(&chip
->wq
, &wait
);
1225 timeo
= jiffies
+ HZ
;
1230 map_write(map
, CMD(0x60), adr
);
1231 map_write(map
, CMD(0xD0), adr
);
1232 chip
->state
= FL_UNLOCKING
;
1234 mutex_unlock(&chip
->mutex
);
1236 mutex_lock(&chip
->mutex
);
1238 /* FIXME. Use a timer to check this, and return immediately. */
1239 /* Once the state machine's known to be working I'll do that */
1241 timeo
= jiffies
+ (HZ
*2);
1244 status
= map_read(map
, adr
);
1245 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1248 /* OK Still waiting */
1249 if (time_after(jiffies
, timeo
)) {
1250 map_write(map
, CMD(0x70), adr
);
1251 chip
->state
= FL_STATUS
;
1252 printk(KERN_ERR
"waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1254 mutex_unlock(&chip
->mutex
);
1258 /* Latency issues. Drop the unlock, wait a while and retry */
1259 mutex_unlock(&chip
->mutex
);
1261 mutex_lock(&chip
->mutex
);
1264 /* Done and happy. */
1265 chip
->state
= FL_STATUS
;
1268 mutex_unlock(&chip
->mutex
);
1271 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1273 struct map_info
*map
= mtd
->priv
;
1274 struct cfi_private
*cfi
= map
->fldrv_priv
;
1276 int chipnum
, ret
= 0;
1277 #ifdef DEBUG_LOCK_BITS
1278 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1281 chipnum
= ofs
>> cfi
->chipshift
;
1282 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1284 #ifdef DEBUG_LOCK_BITS
1286 unsigned long temp_adr
= adr
;
1287 unsigned long temp_len
= len
;
1289 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1291 printk("before unlock %x: block status register is %x\n",temp_adr
,cfi_read_query(map
, temp_adr
+(2*ofs_factor
)));
1292 temp_adr
+= mtd
->erasesize
;
1293 temp_len
-= mtd
->erasesize
;
1295 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1299 ret
= do_unlock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1301 #ifdef DEBUG_LOCK_BITS
1302 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1303 printk("after unlock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1304 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1310 static int cfi_staa_suspend(struct mtd_info
*mtd
)
1312 struct map_info
*map
= mtd
->priv
;
1313 struct cfi_private
*cfi
= map
->fldrv_priv
;
1315 struct flchip
*chip
;
1318 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1319 chip
= &cfi
->chips
[i
];
1321 mutex_lock(&chip
->mutex
);
1323 switch(chip
->state
) {
1327 case FL_JEDEC_QUERY
:
1328 chip
->oldstate
= chip
->state
;
1329 chip
->state
= FL_PM_SUSPENDED
;
1330 /* No need to wake_up() on this state change -
1331 * as the whole point is that nobody can do anything
1332 * with the chip now anyway.
1334 case FL_PM_SUSPENDED
:
1341 mutex_unlock(&chip
->mutex
);
1344 /* Unlock the chips again */
1347 for (i
--; i
>=0; i
--) {
1348 chip
= &cfi
->chips
[i
];
1350 mutex_lock(&chip
->mutex
);
1352 if (chip
->state
== FL_PM_SUSPENDED
) {
1353 /* No need to force it into a known state here,
1354 because we're returning failure, and it didn't
1356 chip
->state
= chip
->oldstate
;
1359 mutex_unlock(&chip
->mutex
);
1366 static void cfi_staa_resume(struct mtd_info
*mtd
)
1368 struct map_info
*map
= mtd
->priv
;
1369 struct cfi_private
*cfi
= map
->fldrv_priv
;
1371 struct flchip
*chip
;
1373 for (i
=0; i
<cfi
->numchips
; i
++) {
1375 chip
= &cfi
->chips
[i
];
1377 mutex_lock(&chip
->mutex
);
1379 /* Go to known state. Chip may have been power cycled */
1380 if (chip
->state
== FL_PM_SUSPENDED
) {
1381 map_write(map
, CMD(0xFF), 0);
1382 chip
->state
= FL_READY
;
1386 mutex_unlock(&chip
->mutex
);
1390 static void cfi_staa_destroy(struct mtd_info
*mtd
)
1392 struct map_info
*map
= mtd
->priv
;
1393 struct cfi_private
*cfi
= map
->fldrv_priv
;
1394 kfree(cfi
->cmdset_priv
);
1398 MODULE_LICENSE("GPL");