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
;
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.
1000 mutex_unlock(&chip
->mutex
);
1004 /* Not an idle state */
1005 set_current_state(TASK_UNINTERRUPTIBLE
);
1006 add_wait_queue(&chip
->wq
, &wait
);
1008 mutex_unlock(&chip
->mutex
);
1010 remove_wait_queue(&chip
->wq
, &wait
);
1016 /* Unlock the chips again */
1018 for (i
--; i
>=0; i
--) {
1019 chip
= &cfi
->chips
[i
];
1021 mutex_lock(&chip
->mutex
);
1023 if (chip
->state
== FL_SYNCING
) {
1024 chip
->state
= chip
->oldstate
;
1027 mutex_unlock(&chip
->mutex
);
1031 static inline int do_lock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1033 struct cfi_private
*cfi
= map
->fldrv_priv
;
1034 map_word status
, status_OK
;
1035 unsigned long timeo
= jiffies
+ HZ
;
1036 DECLARE_WAITQUEUE(wait
, current
);
1040 /* Let's determine this according to the interleave only once */
1041 status_OK
= CMD(0x80);
1043 timeo
= jiffies
+ HZ
;
1045 mutex_lock(&chip
->mutex
);
1047 /* Check that the chip's ready to talk to us. */
1048 switch (chip
->state
) {
1050 case FL_JEDEC_QUERY
:
1052 map_write(map
, CMD(0x70), adr
);
1053 chip
->state
= FL_STATUS
;
1056 status
= map_read(map
, adr
);
1057 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1060 /* Urgh. Chip not yet ready to talk to us. */
1061 if (time_after(jiffies
, timeo
)) {
1062 mutex_unlock(&chip
->mutex
);
1063 printk(KERN_ERR
"waiting for chip to be ready timed out in lock\n");
1067 /* Latency issues. Drop the lock, wait a while and retry */
1068 mutex_unlock(&chip
->mutex
);
1073 /* Stick ourselves on a wait queue to be woken when
1074 someone changes the status */
1075 set_current_state(TASK_UNINTERRUPTIBLE
);
1076 add_wait_queue(&chip
->wq
, &wait
);
1077 mutex_unlock(&chip
->mutex
);
1079 remove_wait_queue(&chip
->wq
, &wait
);
1080 timeo
= jiffies
+ HZ
;
1085 map_write(map
, CMD(0x60), adr
);
1086 map_write(map
, CMD(0x01), adr
);
1087 chip
->state
= FL_LOCKING
;
1089 mutex_unlock(&chip
->mutex
);
1091 mutex_lock(&chip
->mutex
);
1093 /* FIXME. Use a timer to check this, and return immediately. */
1094 /* Once the state machine's known to be working I'll do that */
1096 timeo
= jiffies
+ (HZ
*2);
1099 status
= map_read(map
, adr
);
1100 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1103 /* OK Still waiting */
1104 if (time_after(jiffies
, timeo
)) {
1105 map_write(map
, CMD(0x70), adr
);
1106 chip
->state
= FL_STATUS
;
1107 printk(KERN_ERR
"waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1109 mutex_unlock(&chip
->mutex
);
1113 /* Latency issues. Drop the lock, wait a while and retry */
1114 mutex_unlock(&chip
->mutex
);
1116 mutex_lock(&chip
->mutex
);
1119 /* Done and happy. */
1120 chip
->state
= FL_STATUS
;
1123 mutex_unlock(&chip
->mutex
);
1126 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1128 struct map_info
*map
= mtd
->priv
;
1129 struct cfi_private
*cfi
= map
->fldrv_priv
;
1132 #ifdef DEBUG_LOCK_BITS
1133 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1136 if (ofs
& (mtd
->erasesize
- 1))
1139 if (len
& (mtd
->erasesize
-1))
1142 chipnum
= ofs
>> cfi
->chipshift
;
1143 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1147 #ifdef DEBUG_LOCK_BITS
1148 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1149 printk("before lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1150 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1153 ret
= do_lock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1155 #ifdef DEBUG_LOCK_BITS
1156 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1157 printk("after lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1158 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1164 adr
+= mtd
->erasesize
;
1165 len
-= mtd
->erasesize
;
1167 if (adr
>> cfi
->chipshift
) {
1171 if (chipnum
>= cfi
->numchips
)
1177 static inline int do_unlock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1179 struct cfi_private
*cfi
= map
->fldrv_priv
;
1180 map_word status
, status_OK
;
1181 unsigned long timeo
= jiffies
+ HZ
;
1182 DECLARE_WAITQUEUE(wait
, current
);
1186 /* Let's determine this according to the interleave only once */
1187 status_OK
= CMD(0x80);
1189 timeo
= jiffies
+ HZ
;
1191 mutex_lock(&chip
->mutex
);
1193 /* Check that the chip's ready to talk to us. */
1194 switch (chip
->state
) {
1196 case FL_JEDEC_QUERY
:
1198 map_write(map
, CMD(0x70), adr
);
1199 chip
->state
= FL_STATUS
;
1202 status
= map_read(map
, adr
);
1203 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1206 /* Urgh. Chip not yet ready to talk to us. */
1207 if (time_after(jiffies
, timeo
)) {
1208 mutex_unlock(&chip
->mutex
);
1209 printk(KERN_ERR
"waiting for chip to be ready timed out in unlock\n");
1213 /* Latency issues. Drop the lock, wait a while and retry */
1214 mutex_unlock(&chip
->mutex
);
1219 /* Stick ourselves on a wait queue to be woken when
1220 someone changes the status */
1221 set_current_state(TASK_UNINTERRUPTIBLE
);
1222 add_wait_queue(&chip
->wq
, &wait
);
1223 mutex_unlock(&chip
->mutex
);
1225 remove_wait_queue(&chip
->wq
, &wait
);
1226 timeo
= jiffies
+ HZ
;
1231 map_write(map
, CMD(0x60), adr
);
1232 map_write(map
, CMD(0xD0), adr
);
1233 chip
->state
= FL_UNLOCKING
;
1235 mutex_unlock(&chip
->mutex
);
1237 mutex_lock(&chip
->mutex
);
1239 /* FIXME. Use a timer to check this, and return immediately. */
1240 /* Once the state machine's known to be working I'll do that */
1242 timeo
= jiffies
+ (HZ
*2);
1245 status
= map_read(map
, adr
);
1246 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1249 /* OK Still waiting */
1250 if (time_after(jiffies
, timeo
)) {
1251 map_write(map
, CMD(0x70), adr
);
1252 chip
->state
= FL_STATUS
;
1253 printk(KERN_ERR
"waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1255 mutex_unlock(&chip
->mutex
);
1259 /* Latency issues. Drop the unlock, wait a while and retry */
1260 mutex_unlock(&chip
->mutex
);
1262 mutex_lock(&chip
->mutex
);
1265 /* Done and happy. */
1266 chip
->state
= FL_STATUS
;
1269 mutex_unlock(&chip
->mutex
);
1272 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1274 struct map_info
*map
= mtd
->priv
;
1275 struct cfi_private
*cfi
= map
->fldrv_priv
;
1278 #ifdef DEBUG_LOCK_BITS
1279 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1282 chipnum
= ofs
>> cfi
->chipshift
;
1283 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1285 #ifdef DEBUG_LOCK_BITS
1287 unsigned long temp_adr
= adr
;
1288 unsigned long temp_len
= len
;
1290 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1292 printk("before unlock %x: block status register is %x\n",temp_adr
,cfi_read_query(map
, temp_adr
+(2*ofs_factor
)));
1293 temp_adr
+= mtd
->erasesize
;
1294 temp_len
-= mtd
->erasesize
;
1296 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1300 ret
= do_unlock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1302 #ifdef DEBUG_LOCK_BITS
1303 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1304 printk("after unlock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1305 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1311 static int cfi_staa_suspend(struct mtd_info
*mtd
)
1313 struct map_info
*map
= mtd
->priv
;
1314 struct cfi_private
*cfi
= map
->fldrv_priv
;
1316 struct flchip
*chip
;
1319 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1320 chip
= &cfi
->chips
[i
];
1322 mutex_lock(&chip
->mutex
);
1324 switch(chip
->state
) {
1328 case FL_JEDEC_QUERY
:
1329 chip
->oldstate
= chip
->state
;
1330 chip
->state
= FL_PM_SUSPENDED
;
1331 /* No need to wake_up() on this state change -
1332 * as the whole point is that nobody can do anything
1333 * with the chip now anyway.
1335 case FL_PM_SUSPENDED
:
1342 mutex_unlock(&chip
->mutex
);
1345 /* Unlock the chips again */
1348 for (i
--; i
>=0; i
--) {
1349 chip
= &cfi
->chips
[i
];
1351 mutex_lock(&chip
->mutex
);
1353 if (chip
->state
== FL_PM_SUSPENDED
) {
1354 /* No need to force it into a known state here,
1355 because we're returning failure, and it didn't
1357 chip
->state
= chip
->oldstate
;
1360 mutex_unlock(&chip
->mutex
);
1367 static void cfi_staa_resume(struct mtd_info
*mtd
)
1369 struct map_info
*map
= mtd
->priv
;
1370 struct cfi_private
*cfi
= map
->fldrv_priv
;
1372 struct flchip
*chip
;
1374 for (i
=0; i
<cfi
->numchips
; i
++) {
1376 chip
= &cfi
->chips
[i
];
1378 mutex_lock(&chip
->mutex
);
1380 /* Go to known state. Chip may have been power cycled */
1381 if (chip
->state
== FL_PM_SUSPENDED
) {
1382 map_write(map
, CMD(0xFF), 0);
1383 chip
->state
= FL_READY
;
1387 mutex_unlock(&chip
->mutex
);
1391 static void cfi_staa_destroy(struct mtd_info
*mtd
)
1393 struct map_info
*map
= mtd
->priv
;
1394 struct cfi_private
*cfi
= map
->fldrv_priv
;
1395 kfree(cfi
->cmdset_priv
);
1399 MODULE_LICENSE("GPL");