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>
25 #include <linux/init.h>
27 #include <asm/byteorder.h>
29 #include <linux/errno.h>
30 #include <linux/slab.h>
31 #include <linux/delay.h>
32 #include <linux/interrupt.h>
33 #include <linux/mtd/map.h>
34 #include <linux/mtd/cfi.h>
35 #include <linux/mtd/mtd.h>
38 static int cfi_staa_read(struct mtd_info
*, loff_t
, size_t, size_t *, u_char
*);
39 static int cfi_staa_write_buffers(struct mtd_info
*, loff_t
, size_t, size_t *, const u_char
*);
40 static int cfi_staa_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
41 unsigned long count
, loff_t to
, size_t *retlen
);
42 static int cfi_staa_erase_varsize(struct mtd_info
*, struct erase_info
*);
43 static void cfi_staa_sync (struct mtd_info
*);
44 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
);
45 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
);
46 static int cfi_staa_suspend (struct mtd_info
*);
47 static void cfi_staa_resume (struct mtd_info
*);
49 static void cfi_staa_destroy(struct mtd_info
*);
51 struct mtd_info
*cfi_cmdset_0020(struct map_info
*, int);
53 static struct mtd_info
*cfi_staa_setup (struct map_info
*);
55 static struct mtd_chip_driver cfi_staa_chipdrv
= {
56 .probe
= NULL
, /* Not usable directly */
57 .destroy
= cfi_staa_destroy
,
58 .name
= "cfi_cmdset_0020",
62 /* #define DEBUG_LOCK_BITS */
63 //#define DEBUG_CFI_FEATURES
65 #ifdef DEBUG_CFI_FEATURES
66 static void cfi_tell_features(struct cfi_pri_intelext
*extp
)
69 printk(" Feature/Command Support: %4.4X\n", extp
->FeatureSupport
);
70 printk(" - Chip Erase: %s\n", extp
->FeatureSupport
&1?"supported":"unsupported");
71 printk(" - Suspend Erase: %s\n", extp
->FeatureSupport
&2?"supported":"unsupported");
72 printk(" - Suspend Program: %s\n", extp
->FeatureSupport
&4?"supported":"unsupported");
73 printk(" - Legacy Lock/Unlock: %s\n", extp
->FeatureSupport
&8?"supported":"unsupported");
74 printk(" - Queued Erase: %s\n", extp
->FeatureSupport
&16?"supported":"unsupported");
75 printk(" - Instant block lock: %s\n", extp
->FeatureSupport
&32?"supported":"unsupported");
76 printk(" - Protection Bits: %s\n", extp
->FeatureSupport
&64?"supported":"unsupported");
77 printk(" - Page-mode read: %s\n", extp
->FeatureSupport
&128?"supported":"unsupported");
78 printk(" - Synchronous read: %s\n", extp
->FeatureSupport
&256?"supported":"unsupported");
79 for (i
=9; i
<32; i
++) {
80 if (extp
->FeatureSupport
& (1<<i
))
81 printk(" - Unknown Bit %X: supported\n", i
);
84 printk(" Supported functions after Suspend: %2.2X\n", extp
->SuspendCmdSupport
);
85 printk(" - Program after Erase Suspend: %s\n", extp
->SuspendCmdSupport
&1?"supported":"unsupported");
87 if (extp
->SuspendCmdSupport
& (1<<i
))
88 printk(" - Unknown Bit %X: supported\n", i
);
91 printk(" Block Status Register Mask: %4.4X\n", extp
->BlkStatusRegMask
);
92 printk(" - Lock Bit Active: %s\n", extp
->BlkStatusRegMask
&1?"yes":"no");
93 printk(" - Valid Bit Active: %s\n", extp
->BlkStatusRegMask
&2?"yes":"no");
94 for (i
=2; i
<16; i
++) {
95 if (extp
->BlkStatusRegMask
& (1<<i
))
96 printk(" - Unknown Bit %X Active: yes\n",i
);
99 printk(" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
100 extp
->VccOptimal
>> 8, extp
->VccOptimal
& 0xf);
101 if (extp
->VppOptimal
)
102 printk(" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
103 extp
->VppOptimal
>> 8, extp
->VppOptimal
& 0xf);
107 /* This routine is made available to other mtd code via
108 * inter_module_register. It must only be accessed through
109 * inter_module_get which will bump the use count of this module. The
110 * addresses passed back in cfi are valid as long as the use count of
111 * this module is non-zero, i.e. between inter_module_get and
112 * inter_module_put. Keith Owens <kaos@ocs.com.au> 29 Oct 2000.
114 struct mtd_info
*cfi_cmdset_0020(struct map_info
*map
, int primary
)
116 struct cfi_private
*cfi
= map
->fldrv_priv
;
121 * It's a real CFI chip, not one for which the probe
122 * routine faked a CFI structure. So we read the feature
125 __u16 adr
= primary
?cfi
->cfiq
->P_ADR
:cfi
->cfiq
->A_ADR
;
126 struct cfi_pri_intelext
*extp
;
128 extp
= (struct cfi_pri_intelext
*)cfi_read_pri(map
, adr
, sizeof(*extp
), "ST Microelectronics");
132 if (extp
->MajorVersion
!= '1' ||
133 (extp
->MinorVersion
< '0' || extp
->MinorVersion
> '3')) {
134 printk(KERN_ERR
" Unknown ST Microelectronics"
135 " Extended Query version %c.%c.\n",
136 extp
->MajorVersion
, extp
->MinorVersion
);
141 /* Do some byteswapping if necessary */
142 extp
->FeatureSupport
= cfi32_to_cpu(extp
->FeatureSupport
);
143 extp
->BlkStatusRegMask
= cfi32_to_cpu(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 printk(KERN_ERR
"Failed to allocate memory for MTD device\n");
179 kfree(cfi
->cmdset_priv
);
184 mtd
->type
= MTD_NORFLASH
;
185 mtd
->size
= devsize
* cfi
->numchips
;
187 mtd
->numeraseregions
= cfi
->cfiq
->NumEraseRegions
* cfi
->numchips
;
188 mtd
->eraseregions
= kmalloc(sizeof(struct mtd_erase_region_info
)
189 * mtd
->numeraseregions
, GFP_KERNEL
);
190 if (!mtd
->eraseregions
) {
191 printk(KERN_ERR
"Failed to allocate memory for MTD erase region info\n");
192 kfree(cfi
->cmdset_priv
);
197 for (i
=0; i
<cfi
->cfiq
->NumEraseRegions
; i
++) {
198 unsigned long ernum
, ersize
;
199 ersize
= ((cfi
->cfiq
->EraseRegionInfo
[i
] >> 8) & ~0xff) * cfi
->interleave
;
200 ernum
= (cfi
->cfiq
->EraseRegionInfo
[i
] & 0xffff) + 1;
202 if (mtd
->erasesize
< ersize
) {
203 mtd
->erasesize
= ersize
;
205 for (j
=0; j
<cfi
->numchips
; j
++) {
206 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].offset
= (j
*devsize
)+offset
;
207 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].erasesize
= ersize
;
208 mtd
->eraseregions
[(j
*cfi
->cfiq
->NumEraseRegions
)+i
].numblocks
= ernum
;
210 offset
+= (ersize
* ernum
);
213 if (offset
!= devsize
) {
215 printk(KERN_WARNING
"Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset
, devsize
);
216 kfree(mtd
->eraseregions
);
217 kfree(cfi
->cmdset_priv
);
222 for (i
=0; i
<mtd
->numeraseregions
;i
++){
223 printk(KERN_DEBUG
"%d: offset=0x%llx,size=0x%x,blocks=%d\n",
224 i
, (unsigned long long)mtd
->eraseregions
[i
].offset
,
225 mtd
->eraseregions
[i
].erasesize
,
226 mtd
->eraseregions
[i
].numblocks
);
229 /* Also select the correct geometry setup too */
230 mtd
->erase
= cfi_staa_erase_varsize
;
231 mtd
->read
= cfi_staa_read
;
232 mtd
->write
= cfi_staa_write_buffers
;
233 mtd
->writev
= cfi_staa_writev
;
234 mtd
->sync
= cfi_staa_sync
;
235 mtd
->lock
= cfi_staa_lock
;
236 mtd
->unlock
= cfi_staa_unlock
;
237 mtd
->suspend
= cfi_staa_suspend
;
238 mtd
->resume
= cfi_staa_resume
;
239 mtd
->flags
= MTD_CAP_NORFLASH
& ~MTD_BIT_WRITEABLE
;
240 mtd
->writesize
= 8; /* FIXME: Should be 0 for STMicro flashes w/out ECC */
241 mtd
->writebufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
242 map
->fldrv
= &cfi_staa_chipdrv
;
243 __module_get(THIS_MODULE
);
244 mtd
->name
= map
->name
;
249 static inline int do_read_onechip(struct map_info
*map
, struct flchip
*chip
, loff_t adr
, size_t len
, u_char
*buf
)
251 map_word status
, status_OK
;
253 DECLARE_WAITQUEUE(wait
, current
);
255 unsigned long cmd_addr
;
256 struct cfi_private
*cfi
= map
->fldrv_priv
;
260 /* Ensure cmd read/writes are aligned. */
261 cmd_addr
= adr
& ~(map_bankwidth(map
)-1);
263 /* Let's determine this according to the interleave only once */
264 status_OK
= CMD(0x80);
266 timeo
= jiffies
+ HZ
;
268 mutex_lock(&chip
->mutex
);
270 /* Check that the chip's ready to talk to us.
271 * If it's in FL_ERASING state, suspend it and make it talk now.
273 switch (chip
->state
) {
275 if (!(((struct cfi_pri_intelext
*)cfi
->cmdset_priv
)->FeatureSupport
& 2))
276 goto sleep
; /* We don't support erase suspend */
278 map_write (map
, CMD(0xb0), cmd_addr
);
279 /* If the flash has finished erasing, then 'erase suspend'
280 * appears to make some (28F320) flash devices switch to
281 * 'read' mode. Make sure that we switch to 'read status'
282 * mode so we get the right data. --rmk
284 map_write(map
, CMD(0x70), cmd_addr
);
285 chip
->oldstate
= FL_ERASING
;
286 chip
->state
= FL_ERASE_SUSPENDING
;
287 // printk("Erase suspending at 0x%lx\n", cmd_addr);
289 status
= map_read(map
, cmd_addr
);
290 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
293 if (time_after(jiffies
, timeo
)) {
295 map_write(map
, CMD(0xd0), cmd_addr
);
296 /* make sure we're in 'read status' mode */
297 map_write(map
, CMD(0x70), cmd_addr
);
298 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
);
398 unsigned long thislen
;
400 if (chipnum
>= cfi
->numchips
)
403 if ((len
+ ofs
-1) >> cfi
->chipshift
)
404 thislen
= (1<<cfi
->chipshift
) - ofs
;
408 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
422 static inline int do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
423 unsigned long adr
, const u_char
*buf
, int len
)
425 struct cfi_private
*cfi
= map
->fldrv_priv
;
426 map_word status
, status_OK
;
427 unsigned long cmd_adr
, timeo
;
428 DECLARE_WAITQUEUE(wait
, current
);
431 /* M58LW064A requires bus alignment for buffer wriets -- saw */
432 if (adr
& (map_bankwidth(map
)-1))
435 wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
437 cmd_adr
= adr
& ~(wbufsize
-1);
439 /* Let's determine this according to the interleave only once */
440 status_OK
= CMD(0x80);
442 timeo
= jiffies
+ HZ
;
445 #ifdef DEBUG_CFI_FEATURES
446 printk("%s: chip->state[%d]\n", __func__
, chip
->state
);
448 mutex_lock(&chip
->mutex
);
450 /* Check that the chip's ready to talk to us.
451 * Later, we can actually think about interrupting it
452 * if it's in FL_ERASING state.
453 * Not just yet, though.
455 switch (chip
->state
) {
461 map_write(map
, CMD(0x70), cmd_adr
);
462 chip
->state
= FL_STATUS
;
463 #ifdef DEBUG_CFI_FEATURES
464 printk("%s: 1 status[%x]\n", __func__
, map_read(map
, cmd_adr
));
468 status
= map_read(map
, cmd_adr
);
469 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
471 /* Urgh. Chip not yet ready to talk to us. */
472 if (time_after(jiffies
, timeo
)) {
473 mutex_unlock(&chip
->mutex
);
474 printk(KERN_ERR
"waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
475 status
.x
[0], map_read(map
, cmd_adr
).x
[0]);
479 /* Latency issues. Drop the lock, wait a while and retry */
480 mutex_unlock(&chip
->mutex
);
485 /* Stick ourselves on a wait queue to be woken when
486 someone changes the status */
487 set_current_state(TASK_UNINTERRUPTIBLE
);
488 add_wait_queue(&chip
->wq
, &wait
);
489 mutex_unlock(&chip
->mutex
);
491 remove_wait_queue(&chip
->wq
, &wait
);
492 timeo
= jiffies
+ HZ
;
497 map_write(map
, CMD(0xe8), cmd_adr
);
498 chip
->state
= FL_WRITING_TO_BUFFER
;
502 status
= map_read(map
, cmd_adr
);
503 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
506 mutex_unlock(&chip
->mutex
);
508 mutex_lock(&chip
->mutex
);
511 /* Argh. Not ready for write to buffer */
513 map_write(map
, CMD(0x70), cmd_adr
);
514 chip
->state
= FL_STATUS
;
515 mutex_unlock(&chip
->mutex
);
516 printk(KERN_ERR
"Chip not ready for buffer write. Xstatus = %lx\n", status
.x
[0]);
521 /* Write length of data to come */
522 map_write(map
, CMD(len
/map_bankwidth(map
)-1), cmd_adr
);
526 z
+= map_bankwidth(map
), buf
+= map_bankwidth(map
)) {
528 d
= map_word_load(map
, buf
);
529 map_write(map
, d
, adr
+z
);
532 map_write(map
, CMD(0xd0), cmd_adr
);
533 chip
->state
= FL_WRITING
;
535 mutex_unlock(&chip
->mutex
);
536 cfi_udelay(chip
->buffer_write_time
);
537 mutex_lock(&chip
->mutex
);
539 timeo
= jiffies
+ (HZ
/2);
542 if (chip
->state
!= FL_WRITING
) {
543 /* Someone's suspended the write. Sleep */
544 set_current_state(TASK_UNINTERRUPTIBLE
);
545 add_wait_queue(&chip
->wq
, &wait
);
546 mutex_unlock(&chip
->mutex
);
548 remove_wait_queue(&chip
->wq
, &wait
);
549 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
550 mutex_lock(&chip
->mutex
);
554 status
= map_read(map
, cmd_adr
);
555 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
558 /* OK Still waiting */
559 if (time_after(jiffies
, timeo
)) {
561 map_write(map
, CMD(0x50), cmd_adr
);
562 /* put back into read status register mode */
563 map_write(map
, CMD(0x70), adr
);
564 chip
->state
= FL_STATUS
;
566 mutex_unlock(&chip
->mutex
);
567 printk(KERN_ERR
"waiting for chip to be ready timed out in bufwrite\n");
571 /* Latency issues. Drop the lock, wait a while and retry */
572 mutex_unlock(&chip
->mutex
);
575 mutex_lock(&chip
->mutex
);
578 chip
->buffer_write_time
--;
579 if (!chip
->buffer_write_time
)
580 chip
->buffer_write_time
++;
583 chip
->buffer_write_time
++;
585 /* Done and happy. */
587 chip
->state
= FL_STATUS
;
589 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */
590 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
591 #ifdef DEBUG_CFI_FEATURES
592 printk("%s: 2 status[%lx]\n", __func__
, status
.x
[0]);
595 map_write(map
, CMD(0x50), cmd_adr
);
596 /* put back into read status register mode */
597 map_write(map
, CMD(0x70), adr
);
599 mutex_unlock(&chip
->mutex
);
600 return map_word_bitsset(map
, status
, CMD(0x02)) ? -EROFS
: -EIO
;
603 mutex_unlock(&chip
->mutex
);
608 static int cfi_staa_write_buffers (struct mtd_info
*mtd
, loff_t to
,
609 size_t len
, size_t *retlen
, const u_char
*buf
)
611 struct map_info
*map
= mtd
->priv
;
612 struct cfi_private
*cfi
= map
->fldrv_priv
;
613 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
622 chipnum
= to
>> cfi
->chipshift
;
623 ofs
= to
- (chipnum
<< cfi
->chipshift
);
625 #ifdef DEBUG_CFI_FEATURES
626 printk("%s: map_bankwidth(map)[%x]\n", __func__
, map_bankwidth(map
));
627 printk("%s: chipnum[%x] wbufsize[%x]\n", __func__
, chipnum
, wbufsize
);
628 printk("%s: ofs[%x] len[%x]\n", __func__
, ofs
, len
);
631 /* Write buffer is worth it only if more than one word to write... */
633 /* We must not cross write block boundaries */
634 int size
= wbufsize
- (ofs
& (wbufsize
-1));
639 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
649 if (ofs
>> cfi
->chipshift
) {
652 if (chipnum
== cfi
->numchips
)
661 * Writev for ECC-Flashes is a little more complicated. We need to maintain
662 * a small buffer for this.
663 * XXX: If the buffer size is not a multiple of 2, this will break
665 #define ECCBUF_SIZE (mtd->writesize)
666 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
667 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
669 cfi_staa_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
670 unsigned long count
, loff_t to
, size_t *retlen
)
673 size_t totlen
= 0, thislen
;
679 /* We should fall back to a general writev implementation.
680 * Until that is written, just break.
684 buffer
= kmalloc(ECCBUF_SIZE
, GFP_KERNEL
);
688 for (i
=0; i
<count
; i
++) {
689 size_t elem_len
= vecs
[i
].iov_len
;
690 void *elem_base
= vecs
[i
].iov_base
;
691 if (!elem_len
) /* FIXME: Might be unnecessary. Check that */
693 if (buflen
) { /* cut off head */
694 if (buflen
+ elem_len
< ECCBUF_SIZE
) { /* just accumulate */
695 memcpy(buffer
+buflen
, elem_base
, elem_len
);
699 memcpy(buffer
+buflen
, elem_base
, ECCBUF_SIZE
-buflen
);
700 ret
= mtd
->write(mtd
, to
, ECCBUF_SIZE
, &thislen
, buffer
);
702 if (ret
|| thislen
!= ECCBUF_SIZE
)
704 elem_len
-= thislen
-buflen
;
705 elem_base
+= thislen
-buflen
;
708 if (ECCBUF_DIV(elem_len
)) { /* write clean aligned data */
709 ret
= mtd
->write(mtd
, to
, ECCBUF_DIV(elem_len
), &thislen
, elem_base
);
711 if (ret
|| thislen
!= ECCBUF_DIV(elem_len
))
715 buflen
= ECCBUF_MOD(elem_len
); /* cut off tail */
717 memset(buffer
, 0xff, ECCBUF_SIZE
);
718 memcpy(buffer
, elem_base
+ thislen
, buflen
);
721 if (buflen
) { /* flush last page, even if not full */
722 /* This is sometimes intended behaviour, really */
723 ret
= mtd
->write(mtd
, to
, buflen
, &thislen
, buffer
);
725 if (ret
|| thislen
!= ECCBUF_SIZE
)
736 static inline int do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
738 struct cfi_private
*cfi
= map
->fldrv_priv
;
739 map_word status
, status_OK
;
742 DECLARE_WAITQUEUE(wait
, current
);
747 /* Let's determine this according to the interleave only once */
748 status_OK
= CMD(0x80);
750 timeo
= jiffies
+ HZ
;
752 mutex_lock(&chip
->mutex
);
754 /* Check that the chip's ready to talk to us. */
755 switch (chip
->state
) {
759 map_write(map
, CMD(0x70), adr
);
760 chip
->state
= FL_STATUS
;
763 status
= map_read(map
, adr
);
764 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
767 /* Urgh. Chip not yet ready to talk to us. */
768 if (time_after(jiffies
, timeo
)) {
769 mutex_unlock(&chip
->mutex
);
770 printk(KERN_ERR
"waiting for chip to be ready timed out in erase\n");
774 /* Latency issues. Drop the lock, wait a while and retry */
775 mutex_unlock(&chip
->mutex
);
780 /* Stick ourselves on a wait queue to be woken when
781 someone changes the status */
782 set_current_state(TASK_UNINTERRUPTIBLE
);
783 add_wait_queue(&chip
->wq
, &wait
);
784 mutex_unlock(&chip
->mutex
);
786 remove_wait_queue(&chip
->wq
, &wait
);
787 timeo
= jiffies
+ HZ
;
792 /* Clear the status register first */
793 map_write(map
, CMD(0x50), adr
);
796 map_write(map
, CMD(0x20), adr
);
797 map_write(map
, CMD(0xD0), adr
);
798 chip
->state
= FL_ERASING
;
800 mutex_unlock(&chip
->mutex
);
802 mutex_lock(&chip
->mutex
);
804 /* FIXME. Use a timer to check this, and return immediately. */
805 /* Once the state machine's known to be working I'll do that */
807 timeo
= jiffies
+ (HZ
*20);
809 if (chip
->state
!= FL_ERASING
) {
810 /* Someone's suspended the erase. Sleep */
811 set_current_state(TASK_UNINTERRUPTIBLE
);
812 add_wait_queue(&chip
->wq
, &wait
);
813 mutex_unlock(&chip
->mutex
);
815 remove_wait_queue(&chip
->wq
, &wait
);
816 timeo
= jiffies
+ (HZ
*20); /* FIXME */
817 mutex_lock(&chip
->mutex
);
821 status
= map_read(map
, adr
);
822 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
825 /* OK Still waiting */
826 if (time_after(jiffies
, timeo
)) {
827 map_write(map
, CMD(0x70), adr
);
828 chip
->state
= FL_STATUS
;
829 printk(KERN_ERR
"waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
831 mutex_unlock(&chip
->mutex
);
835 /* Latency issues. Drop the lock, wait a while and retry */
836 mutex_unlock(&chip
->mutex
);
838 mutex_lock(&chip
->mutex
);
844 /* We've broken this before. It doesn't hurt to be safe */
845 map_write(map
, CMD(0x70), adr
);
846 chip
->state
= FL_STATUS
;
847 status
= map_read(map
, adr
);
849 /* check for lock bit */
850 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
851 unsigned char chipstatus
= status
.x
[0];
852 if (!map_word_equal(map
, status
, CMD(chipstatus
))) {
854 for (w
=0; w
<map_words(map
); w
++) {
855 for (i
= 0; i
<cfi_interleave(cfi
); i
++) {
856 chipstatus
|= status
.x
[w
] >> (cfi
->device_type
* 8);
859 printk(KERN_WARNING
"Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
860 status
.x
[0], chipstatus
);
862 /* Reset the error bits */
863 map_write(map
, CMD(0x50), adr
);
864 map_write(map
, CMD(0x70), adr
);
866 if ((chipstatus
& 0x30) == 0x30) {
867 printk(KERN_NOTICE
"Chip reports improper command sequence: status 0x%x\n", chipstatus
);
869 } else if (chipstatus
& 0x02) {
870 /* Protection bit set */
872 } else if (chipstatus
& 0x8) {
874 printk(KERN_WARNING
"Chip reports voltage low on erase: status 0x%x\n", chipstatus
);
876 } else if (chipstatus
& 0x20) {
878 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr
, chipstatus
);
879 timeo
= jiffies
+ HZ
;
880 chip
->state
= FL_STATUS
;
881 mutex_unlock(&chip
->mutex
);
884 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x\n", adr
, chipstatus
);
890 mutex_unlock(&chip
->mutex
);
894 static int cfi_staa_erase_varsize(struct mtd_info
*mtd
,
895 struct erase_info
*instr
)
896 { struct map_info
*map
= mtd
->priv
;
897 struct cfi_private
*cfi
= map
->fldrv_priv
;
898 unsigned long adr
, len
;
899 int chipnum
, ret
= 0;
901 struct mtd_erase_region_info
*regions
= mtd
->eraseregions
;
903 if (instr
->addr
> mtd
->size
)
906 if ((instr
->len
+ instr
->addr
) > mtd
->size
)
909 /* Check that both start and end of the requested erase are
910 * aligned with the erasesize at the appropriate addresses.
915 /* Skip all erase regions which are ended before the start of
916 the requested erase. Actually, to save on the calculations,
917 we skip to the first erase region which starts after the
918 start of the requested erase, and then go back one.
921 while (i
< mtd
->numeraseregions
&& instr
->addr
>= regions
[i
].offset
)
925 /* OK, now i is pointing at the erase region in which this
926 erase request starts. Check the start of the requested
927 erase range is aligned with the erase size which is in
931 if (instr
->addr
& (regions
[i
].erasesize
-1))
934 /* Remember the erase region we start on */
937 /* Next, check that the end of the requested erase is aligned
938 * with the erase region at that address.
941 while (i
<mtd
->numeraseregions
&& (instr
->addr
+ instr
->len
) >= regions
[i
].offset
)
944 /* As before, drop back one to point at the region in which
945 the address actually falls
949 if ((instr
->addr
+ instr
->len
) & (regions
[i
].erasesize
-1))
952 chipnum
= instr
->addr
>> cfi
->chipshift
;
953 adr
= instr
->addr
- (chipnum
<< cfi
->chipshift
);
959 ret
= do_erase_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
964 adr
+= regions
[i
].erasesize
;
965 len
-= regions
[i
].erasesize
;
967 if (adr
% (1<< cfi
->chipshift
) == (((unsigned long)regions
[i
].offset
+ (regions
[i
].erasesize
* regions
[i
].numblocks
)) %( 1<< cfi
->chipshift
)))
970 if (adr
>> cfi
->chipshift
) {
974 if (chipnum
>= cfi
->numchips
)
979 instr
->state
= MTD_ERASE_DONE
;
980 mtd_erase_callback(instr
);
985 static void cfi_staa_sync (struct mtd_info
*mtd
)
987 struct map_info
*map
= mtd
->priv
;
988 struct cfi_private
*cfi
= map
->fldrv_priv
;
992 DECLARE_WAITQUEUE(wait
, current
);
994 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
995 chip
= &cfi
->chips
[i
];
998 mutex_lock(&chip
->mutex
);
1000 switch(chip
->state
) {
1004 case FL_JEDEC_QUERY
:
1005 chip
->oldstate
= chip
->state
;
1006 chip
->state
= FL_SYNCING
;
1007 /* No need to wake_up() on this state change -
1008 * as the whole point is that nobody can do anything
1009 * with the chip now anyway.
1012 mutex_unlock(&chip
->mutex
);
1016 /* Not an idle state */
1017 set_current_state(TASK_UNINTERRUPTIBLE
);
1018 add_wait_queue(&chip
->wq
, &wait
);
1020 mutex_unlock(&chip
->mutex
);
1022 remove_wait_queue(&chip
->wq
, &wait
);
1028 /* Unlock the chips again */
1030 for (i
--; i
>=0; i
--) {
1031 chip
= &cfi
->chips
[i
];
1033 mutex_lock(&chip
->mutex
);
1035 if (chip
->state
== FL_SYNCING
) {
1036 chip
->state
= chip
->oldstate
;
1039 mutex_unlock(&chip
->mutex
);
1043 static inline int do_lock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1045 struct cfi_private
*cfi
= map
->fldrv_priv
;
1046 map_word status
, status_OK
;
1047 unsigned long timeo
= jiffies
+ HZ
;
1048 DECLARE_WAITQUEUE(wait
, current
);
1052 /* Let's determine this according to the interleave only once */
1053 status_OK
= CMD(0x80);
1055 timeo
= jiffies
+ HZ
;
1057 mutex_lock(&chip
->mutex
);
1059 /* Check that the chip's ready to talk to us. */
1060 switch (chip
->state
) {
1062 case FL_JEDEC_QUERY
:
1064 map_write(map
, CMD(0x70), adr
);
1065 chip
->state
= FL_STATUS
;
1068 status
= map_read(map
, adr
);
1069 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1072 /* Urgh. Chip not yet ready to talk to us. */
1073 if (time_after(jiffies
, timeo
)) {
1074 mutex_unlock(&chip
->mutex
);
1075 printk(KERN_ERR
"waiting for chip to be ready timed out in lock\n");
1079 /* Latency issues. Drop the lock, wait a while and retry */
1080 mutex_unlock(&chip
->mutex
);
1085 /* Stick ourselves on a wait queue to be woken when
1086 someone changes the status */
1087 set_current_state(TASK_UNINTERRUPTIBLE
);
1088 add_wait_queue(&chip
->wq
, &wait
);
1089 mutex_unlock(&chip
->mutex
);
1091 remove_wait_queue(&chip
->wq
, &wait
);
1092 timeo
= jiffies
+ HZ
;
1097 map_write(map
, CMD(0x60), adr
);
1098 map_write(map
, CMD(0x01), adr
);
1099 chip
->state
= FL_LOCKING
;
1101 mutex_unlock(&chip
->mutex
);
1103 mutex_lock(&chip
->mutex
);
1105 /* FIXME. Use a timer to check this, and return immediately. */
1106 /* Once the state machine's known to be working I'll do that */
1108 timeo
= jiffies
+ (HZ
*2);
1111 status
= map_read(map
, adr
);
1112 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1115 /* OK Still waiting */
1116 if (time_after(jiffies
, timeo
)) {
1117 map_write(map
, CMD(0x70), adr
);
1118 chip
->state
= FL_STATUS
;
1119 printk(KERN_ERR
"waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1121 mutex_unlock(&chip
->mutex
);
1125 /* Latency issues. Drop the lock, wait a while and retry */
1126 mutex_unlock(&chip
->mutex
);
1128 mutex_lock(&chip
->mutex
);
1131 /* Done and happy. */
1132 chip
->state
= FL_STATUS
;
1135 mutex_unlock(&chip
->mutex
);
1138 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1140 struct map_info
*map
= mtd
->priv
;
1141 struct cfi_private
*cfi
= map
->fldrv_priv
;
1143 int chipnum
, ret
= 0;
1144 #ifdef DEBUG_LOCK_BITS
1145 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1148 if (ofs
& (mtd
->erasesize
- 1))
1151 if (len
& (mtd
->erasesize
-1))
1154 if ((len
+ ofs
) > mtd
->size
)
1157 chipnum
= ofs
>> cfi
->chipshift
;
1158 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1162 #ifdef DEBUG_LOCK_BITS
1163 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1164 printk("before lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1165 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1168 ret
= do_lock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1170 #ifdef DEBUG_LOCK_BITS
1171 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1172 printk("after lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1173 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1179 adr
+= mtd
->erasesize
;
1180 len
-= mtd
->erasesize
;
1182 if (adr
>> cfi
->chipshift
) {
1186 if (chipnum
>= cfi
->numchips
)
1192 static inline int do_unlock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1194 struct cfi_private
*cfi
= map
->fldrv_priv
;
1195 map_word status
, status_OK
;
1196 unsigned long timeo
= jiffies
+ HZ
;
1197 DECLARE_WAITQUEUE(wait
, current
);
1201 /* Let's determine this according to the interleave only once */
1202 status_OK
= CMD(0x80);
1204 timeo
= jiffies
+ HZ
;
1206 mutex_lock(&chip
->mutex
);
1208 /* Check that the chip's ready to talk to us. */
1209 switch (chip
->state
) {
1211 case FL_JEDEC_QUERY
:
1213 map_write(map
, CMD(0x70), adr
);
1214 chip
->state
= FL_STATUS
;
1217 status
= map_read(map
, adr
);
1218 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1221 /* Urgh. Chip not yet ready to talk to us. */
1222 if (time_after(jiffies
, timeo
)) {
1223 mutex_unlock(&chip
->mutex
);
1224 printk(KERN_ERR
"waiting for chip to be ready timed out in unlock\n");
1228 /* Latency issues. Drop the lock, wait a while and retry */
1229 mutex_unlock(&chip
->mutex
);
1234 /* Stick ourselves on a wait queue to be woken when
1235 someone changes the status */
1236 set_current_state(TASK_UNINTERRUPTIBLE
);
1237 add_wait_queue(&chip
->wq
, &wait
);
1238 mutex_unlock(&chip
->mutex
);
1240 remove_wait_queue(&chip
->wq
, &wait
);
1241 timeo
= jiffies
+ HZ
;
1246 map_write(map
, CMD(0x60), adr
);
1247 map_write(map
, CMD(0xD0), adr
);
1248 chip
->state
= FL_UNLOCKING
;
1250 mutex_unlock(&chip
->mutex
);
1252 mutex_lock(&chip
->mutex
);
1254 /* FIXME. Use a timer to check this, and return immediately. */
1255 /* Once the state machine's known to be working I'll do that */
1257 timeo
= jiffies
+ (HZ
*2);
1260 status
= map_read(map
, adr
);
1261 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1264 /* OK Still waiting */
1265 if (time_after(jiffies
, timeo
)) {
1266 map_write(map
, CMD(0x70), adr
);
1267 chip
->state
= FL_STATUS
;
1268 printk(KERN_ERR
"waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1270 mutex_unlock(&chip
->mutex
);
1274 /* Latency issues. Drop the unlock, wait a while and retry */
1275 mutex_unlock(&chip
->mutex
);
1277 mutex_lock(&chip
->mutex
);
1280 /* Done and happy. */
1281 chip
->state
= FL_STATUS
;
1284 mutex_unlock(&chip
->mutex
);
1287 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1289 struct map_info
*map
= mtd
->priv
;
1290 struct cfi_private
*cfi
= map
->fldrv_priv
;
1292 int chipnum
, ret
= 0;
1293 #ifdef DEBUG_LOCK_BITS
1294 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1297 chipnum
= ofs
>> cfi
->chipshift
;
1298 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1300 #ifdef DEBUG_LOCK_BITS
1302 unsigned long temp_adr
= adr
;
1303 unsigned long temp_len
= len
;
1305 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1307 printk("before unlock %x: block status register is %x\n",temp_adr
,cfi_read_query(map
, temp_adr
+(2*ofs_factor
)));
1308 temp_adr
+= mtd
->erasesize
;
1309 temp_len
-= mtd
->erasesize
;
1311 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1315 ret
= do_unlock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1317 #ifdef DEBUG_LOCK_BITS
1318 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1319 printk("after unlock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1320 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1326 static int cfi_staa_suspend(struct mtd_info
*mtd
)
1328 struct map_info
*map
= mtd
->priv
;
1329 struct cfi_private
*cfi
= map
->fldrv_priv
;
1331 struct flchip
*chip
;
1334 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1335 chip
= &cfi
->chips
[i
];
1337 mutex_lock(&chip
->mutex
);
1339 switch(chip
->state
) {
1343 case FL_JEDEC_QUERY
:
1344 chip
->oldstate
= chip
->state
;
1345 chip
->state
= FL_PM_SUSPENDED
;
1346 /* No need to wake_up() on this state change -
1347 * as the whole point is that nobody can do anything
1348 * with the chip now anyway.
1350 case FL_PM_SUSPENDED
:
1357 mutex_unlock(&chip
->mutex
);
1360 /* Unlock the chips again */
1363 for (i
--; i
>=0; i
--) {
1364 chip
= &cfi
->chips
[i
];
1366 mutex_lock(&chip
->mutex
);
1368 if (chip
->state
== FL_PM_SUSPENDED
) {
1369 /* No need to force it into a known state here,
1370 because we're returning failure, and it didn't
1372 chip
->state
= chip
->oldstate
;
1375 mutex_unlock(&chip
->mutex
);
1382 static void cfi_staa_resume(struct mtd_info
*mtd
)
1384 struct map_info
*map
= mtd
->priv
;
1385 struct cfi_private
*cfi
= map
->fldrv_priv
;
1387 struct flchip
*chip
;
1389 for (i
=0; i
<cfi
->numchips
; i
++) {
1391 chip
= &cfi
->chips
[i
];
1393 mutex_lock(&chip
->mutex
);
1395 /* Go to known state. Chip may have been power cycled */
1396 if (chip
->state
== FL_PM_SUSPENDED
) {
1397 map_write(map
, CMD(0xFF), 0);
1398 chip
->state
= FL_READY
;
1402 mutex_unlock(&chip
->mutex
);
1406 static void cfi_staa_destroy(struct mtd_info
*mtd
)
1408 struct map_info
*map
= mtd
->priv
;
1409 struct cfi_private
*cfi
= map
->fldrv_priv
;
1410 kfree(cfi
->cmdset_priv
);
1414 MODULE_LICENSE("GPL");