1 // SPDX-License-Identifier: GPL-2.0
3 * Common Flash Interface support:
4 * ST Advanced Architecture Command Set (ID 0x0020)
8 * 10/10/2000 Nicolas Pitre <nico@fluxnic.net>
9 * - completely revamped method functions so they are aware and
10 * independent of the flash geometry (buswidth, interleave, etc.)
11 * - scalability vs code size is completely set at compile-time
12 * (see include/linux/mtd/cfi.h for selection)
13 * - optimized write buffer method
14 * 06/21/2002 Joern Engel <joern@wh.fh-wedel.de> and others
15 * - modified Intel Command Set 0x0001 to support ST Advanced Architecture
16 * (command set 0x0020)
17 * - added a writev function
18 * 07/13/2005 Joern Engel <joern@wh.fh-wedel.de>
19 * - Plugged memory leak in cfi_staa_writev().
22 #include <linux/module.h>
23 #include <linux/types.h>
24 #include <linux/kernel.h>
25 #include <linux/sched.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(map
, extp
->FeatureSupport
);
143 extp
->BlkStatusRegMask
= cfi32_to_cpu(map
,
144 extp
->BlkStatusRegMask
);
146 #ifdef DEBUG_CFI_FEATURES
147 /* Tell the user about it in lots of lovely detail */
148 cfi_tell_features(extp
);
151 /* Install our own private info structure */
152 cfi
->cmdset_priv
= extp
;
155 for (i
=0; i
< cfi
->numchips
; i
++) {
156 cfi
->chips
[i
].word_write_time
= 128;
157 cfi
->chips
[i
].buffer_write_time
= 128;
158 cfi
->chips
[i
].erase_time
= 1024;
159 cfi
->chips
[i
].ref_point_counter
= 0;
160 init_waitqueue_head(&(cfi
->chips
[i
].wq
));
163 return cfi_staa_setup(map
);
165 EXPORT_SYMBOL_GPL(cfi_cmdset_0020
);
167 static struct mtd_info
*cfi_staa_setup(struct map_info
*map
)
169 struct cfi_private
*cfi
= map
->fldrv_priv
;
170 struct mtd_info
*mtd
;
171 unsigned long offset
= 0;
173 unsigned long devsize
= (1<<cfi
->cfiq
->DevSize
) * cfi
->interleave
;
175 mtd
= kzalloc(sizeof(*mtd
), GFP_KERNEL
);
176 //printk(KERN_DEBUG "number of CFI chips: %d\n", cfi->numchips);
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_array(mtd
->numeraseregions
,
189 sizeof(struct mtd_erase_region_info
),
191 if (!mtd
->eraseregions
) {
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
;
300 mutex_unlock(&chip
->mutex
);
301 printk(KERN_ERR
"Chip not ready after erase "
302 "suspended: status = 0x%lx\n", status
.x
[0]);
306 mutex_unlock(&chip
->mutex
);
308 mutex_lock(&chip
->mutex
);
312 map_write(map
, CMD(0xff), cmd_addr
);
313 chip
->state
= FL_READY
;
326 map_write(map
, CMD(0x70), cmd_addr
);
327 chip
->state
= FL_STATUS
;
330 status
= map_read(map
, cmd_addr
);
331 if (map_word_andequal(map
, status
, status_OK
, status_OK
)) {
332 map_write(map
, CMD(0xff), cmd_addr
);
333 chip
->state
= FL_READY
;
337 /* Urgh. Chip not yet ready to talk to us. */
338 if (time_after(jiffies
, timeo
)) {
339 mutex_unlock(&chip
->mutex
);
340 printk(KERN_ERR
"waiting for chip to be ready timed out in read. WSM status = %lx\n", status
.x
[0]);
344 /* Latency issues. Drop the lock, wait a while and retry */
345 mutex_unlock(&chip
->mutex
);
351 /* Stick ourselves on a wait queue to be woken when
352 someone changes the status */
353 set_current_state(TASK_UNINTERRUPTIBLE
);
354 add_wait_queue(&chip
->wq
, &wait
);
355 mutex_unlock(&chip
->mutex
);
357 remove_wait_queue(&chip
->wq
, &wait
);
358 timeo
= jiffies
+ HZ
;
362 map_copy_from(map
, buf
, adr
, len
);
365 chip
->state
= chip
->oldstate
;
366 /* What if one interleaved chip has finished and the
367 other hasn't? The old code would leave the finished
368 one in READY mode. That's bad, and caused -EROFS
369 errors to be returned from do_erase_oneblock because
370 that's the only bit it checked for at the time.
371 As the state machine appears to explicitly allow
372 sending the 0x70 (Read Status) command to an erasing
373 chip and expecting it to be ignored, that's what we
375 map_write(map
, CMD(0xd0), cmd_addr
);
376 map_write(map
, CMD(0x70), cmd_addr
);
380 mutex_unlock(&chip
->mutex
);
384 static int cfi_staa_read (struct mtd_info
*mtd
, loff_t from
, size_t len
, size_t *retlen
, u_char
*buf
)
386 struct map_info
*map
= mtd
->priv
;
387 struct cfi_private
*cfi
= map
->fldrv_priv
;
392 /* ofs: offset within the first chip that the first read should start */
393 chipnum
= (from
>> cfi
->chipshift
);
394 ofs
= from
- (chipnum
<< cfi
->chipshift
);
397 unsigned long thislen
;
399 if (chipnum
>= cfi
->numchips
)
402 if ((len
+ ofs
-1) >> cfi
->chipshift
)
403 thislen
= (1<<cfi
->chipshift
) - ofs
;
407 ret
= do_read_onechip(map
, &cfi
->chips
[chipnum
], ofs
, thislen
, buf
);
421 static int do_write_buffer(struct map_info
*map
, struct flchip
*chip
,
422 unsigned long adr
, const u_char
*buf
, int len
)
424 struct cfi_private
*cfi
= map
->fldrv_priv
;
425 map_word status
, status_OK
;
426 unsigned long cmd_adr
, timeo
;
427 DECLARE_WAITQUEUE(wait
, current
);
430 /* M58LW064A requires bus alignment for buffer wriets -- saw */
431 if (adr
& (map_bankwidth(map
)-1))
434 wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
436 cmd_adr
= adr
& ~(wbufsize
-1);
438 /* Let's determine this according to the interleave only once */
439 status_OK
= CMD(0x80);
441 timeo
= jiffies
+ HZ
;
444 #ifdef DEBUG_CFI_FEATURES
445 printk("%s: chip->state[%d]\n", __func__
, chip
->state
);
447 mutex_lock(&chip
->mutex
);
449 /* Check that the chip's ready to talk to us.
450 * Later, we can actually think about interrupting it
451 * if it's in FL_ERASING state.
452 * Not just yet, though.
454 switch (chip
->state
) {
460 map_write(map
, CMD(0x70), cmd_adr
);
461 chip
->state
= FL_STATUS
;
462 #ifdef DEBUG_CFI_FEATURES
463 printk("%s: 1 status[%x]\n", __func__
, map_read(map
, cmd_adr
));
467 status
= map_read(map
, cmd_adr
);
468 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
470 /* Urgh. Chip not yet ready to talk to us. */
471 if (time_after(jiffies
, timeo
)) {
472 mutex_unlock(&chip
->mutex
);
473 printk(KERN_ERR
"waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
474 status
.x
[0], map_read(map
, cmd_adr
).x
[0]);
478 /* Latency issues. Drop the lock, wait a while and retry */
479 mutex_unlock(&chip
->mutex
);
484 /* Stick ourselves on a wait queue to be woken when
485 someone changes the status */
486 set_current_state(TASK_UNINTERRUPTIBLE
);
487 add_wait_queue(&chip
->wq
, &wait
);
488 mutex_unlock(&chip
->mutex
);
490 remove_wait_queue(&chip
->wq
, &wait
);
491 timeo
= jiffies
+ HZ
;
496 map_write(map
, CMD(0xe8), cmd_adr
);
497 chip
->state
= FL_WRITING_TO_BUFFER
;
501 status
= map_read(map
, cmd_adr
);
502 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
505 mutex_unlock(&chip
->mutex
);
507 mutex_lock(&chip
->mutex
);
510 /* Argh. Not ready for write to buffer */
512 map_write(map
, CMD(0x70), cmd_adr
);
513 chip
->state
= FL_STATUS
;
514 mutex_unlock(&chip
->mutex
);
515 printk(KERN_ERR
"Chip not ready for buffer write. Xstatus = %lx\n", status
.x
[0]);
520 /* Write length of data to come */
521 map_write(map
, CMD(len
/map_bankwidth(map
)-1), cmd_adr
);
525 z
+= map_bankwidth(map
), buf
+= map_bankwidth(map
)) {
527 d
= map_word_load(map
, buf
);
528 map_write(map
, d
, adr
+z
);
531 map_write(map
, CMD(0xd0), cmd_adr
);
532 chip
->state
= FL_WRITING
;
534 mutex_unlock(&chip
->mutex
);
535 cfi_udelay(chip
->buffer_write_time
);
536 mutex_lock(&chip
->mutex
);
538 timeo
= jiffies
+ (HZ
/2);
541 if (chip
->state
!= FL_WRITING
) {
542 /* Someone's suspended the write. Sleep */
543 set_current_state(TASK_UNINTERRUPTIBLE
);
544 add_wait_queue(&chip
->wq
, &wait
);
545 mutex_unlock(&chip
->mutex
);
547 remove_wait_queue(&chip
->wq
, &wait
);
548 timeo
= jiffies
+ (HZ
/ 2); /* FIXME */
549 mutex_lock(&chip
->mutex
);
553 status
= map_read(map
, cmd_adr
);
554 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
557 /* OK Still waiting */
558 if (time_after(jiffies
, timeo
)) {
560 map_write(map
, CMD(0x50), cmd_adr
);
561 /* put back into read status register mode */
562 map_write(map
, CMD(0x70), adr
);
563 chip
->state
= FL_STATUS
;
565 mutex_unlock(&chip
->mutex
);
566 printk(KERN_ERR
"waiting for chip to be ready timed out in bufwrite\n");
570 /* Latency issues. Drop the lock, wait a while and retry */
571 mutex_unlock(&chip
->mutex
);
574 mutex_lock(&chip
->mutex
);
577 chip
->buffer_write_time
--;
578 if (!chip
->buffer_write_time
)
579 chip
->buffer_write_time
++;
582 chip
->buffer_write_time
++;
584 /* Done and happy. */
586 chip
->state
= FL_STATUS
;
588 /* check for errors: 'lock bit', 'VPP', 'dead cell'/'unerased cell' or 'incorrect cmd' -- saw */
589 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
590 #ifdef DEBUG_CFI_FEATURES
591 printk("%s: 2 status[%lx]\n", __func__
, status
.x
[0]);
594 map_write(map
, CMD(0x50), cmd_adr
);
595 /* put back into read status register mode */
596 map_write(map
, CMD(0x70), adr
);
598 mutex_unlock(&chip
->mutex
);
599 return map_word_bitsset(map
, status
, CMD(0x02)) ? -EROFS
: -EIO
;
602 mutex_unlock(&chip
->mutex
);
607 static int cfi_staa_write_buffers (struct mtd_info
*mtd
, loff_t to
,
608 size_t len
, size_t *retlen
, const u_char
*buf
)
610 struct map_info
*map
= mtd
->priv
;
611 struct cfi_private
*cfi
= map
->fldrv_priv
;
612 int wbufsize
= cfi_interleave(cfi
) << cfi
->cfiq
->MaxBufWriteSize
;
617 chipnum
= to
>> cfi
->chipshift
;
618 ofs
= to
- (chipnum
<< cfi
->chipshift
);
620 #ifdef DEBUG_CFI_FEATURES
621 printk("%s: map_bankwidth(map)[%x]\n", __func__
, map_bankwidth(map
));
622 printk("%s: chipnum[%x] wbufsize[%x]\n", __func__
, chipnum
, wbufsize
);
623 printk("%s: ofs[%x] len[%x]\n", __func__
, ofs
, len
);
626 /* Write buffer is worth it only if more than one word to write... */
628 /* We must not cross write block boundaries */
629 int size
= wbufsize
- (ofs
& (wbufsize
-1));
634 ret
= do_write_buffer(map
, &cfi
->chips
[chipnum
],
644 if (ofs
>> cfi
->chipshift
) {
647 if (chipnum
== cfi
->numchips
)
656 * Writev for ECC-Flashes is a little more complicated. We need to maintain
657 * a small buffer for this.
658 * XXX: If the buffer size is not a multiple of 2, this will break
660 #define ECCBUF_SIZE (mtd->writesize)
661 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
662 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
664 cfi_staa_writev(struct mtd_info
*mtd
, const struct kvec
*vecs
,
665 unsigned long count
, loff_t to
, size_t *retlen
)
668 size_t totlen
= 0, thislen
;
674 /* We should fall back to a general writev implementation.
675 * Until that is written, just break.
679 buffer
= kmalloc(ECCBUF_SIZE
, GFP_KERNEL
);
683 for (i
=0; i
<count
; i
++) {
684 size_t elem_len
= vecs
[i
].iov_len
;
685 void *elem_base
= vecs
[i
].iov_base
;
686 if (!elem_len
) /* FIXME: Might be unnecessary. Check that */
688 if (buflen
) { /* cut off head */
689 if (buflen
+ elem_len
< ECCBUF_SIZE
) { /* just accumulate */
690 memcpy(buffer
+buflen
, elem_base
, elem_len
);
694 memcpy(buffer
+buflen
, elem_base
, ECCBUF_SIZE
-buflen
);
695 ret
= mtd_write(mtd
, to
, ECCBUF_SIZE
, &thislen
,
698 if (ret
|| thislen
!= ECCBUF_SIZE
)
700 elem_len
-= thislen
-buflen
;
701 elem_base
+= thislen
-buflen
;
704 if (ECCBUF_DIV(elem_len
)) { /* write clean aligned data */
705 ret
= mtd_write(mtd
, to
, ECCBUF_DIV(elem_len
),
706 &thislen
, elem_base
);
708 if (ret
|| thislen
!= ECCBUF_DIV(elem_len
))
712 buflen
= ECCBUF_MOD(elem_len
); /* cut off tail */
714 memset(buffer
, 0xff, ECCBUF_SIZE
);
715 memcpy(buffer
, elem_base
+ thislen
, buflen
);
718 if (buflen
) { /* flush last page, even if not full */
719 /* This is sometimes intended behaviour, really */
720 ret
= mtd_write(mtd
, to
, buflen
, &thislen
, buffer
);
722 if (ret
|| thislen
!= ECCBUF_SIZE
)
733 static inline int do_erase_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
735 struct cfi_private
*cfi
= map
->fldrv_priv
;
736 map_word status
, status_OK
;
739 DECLARE_WAITQUEUE(wait
, current
);
744 /* Let's determine this according to the interleave only once */
745 status_OK
= CMD(0x80);
747 timeo
= jiffies
+ HZ
;
749 mutex_lock(&chip
->mutex
);
751 /* Check that the chip's ready to talk to us. */
752 switch (chip
->state
) {
756 map_write(map
, CMD(0x70), adr
);
757 chip
->state
= FL_STATUS
;
760 status
= map_read(map
, adr
);
761 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
764 /* Urgh. Chip not yet ready to talk to us. */
765 if (time_after(jiffies
, timeo
)) {
766 mutex_unlock(&chip
->mutex
);
767 printk(KERN_ERR
"waiting for chip to be ready timed out in erase\n");
771 /* Latency issues. Drop the lock, wait a while and retry */
772 mutex_unlock(&chip
->mutex
);
777 /* Stick ourselves on a wait queue to be woken when
778 someone changes the status */
779 set_current_state(TASK_UNINTERRUPTIBLE
);
780 add_wait_queue(&chip
->wq
, &wait
);
781 mutex_unlock(&chip
->mutex
);
783 remove_wait_queue(&chip
->wq
, &wait
);
784 timeo
= jiffies
+ HZ
;
789 /* Clear the status register first */
790 map_write(map
, CMD(0x50), adr
);
793 map_write(map
, CMD(0x20), adr
);
794 map_write(map
, CMD(0xD0), adr
);
795 chip
->state
= FL_ERASING
;
797 mutex_unlock(&chip
->mutex
);
799 mutex_lock(&chip
->mutex
);
801 /* FIXME. Use a timer to check this, and return immediately. */
802 /* Once the state machine's known to be working I'll do that */
804 timeo
= jiffies
+ (HZ
*20);
806 if (chip
->state
!= FL_ERASING
) {
807 /* Someone's suspended the erase. Sleep */
808 set_current_state(TASK_UNINTERRUPTIBLE
);
809 add_wait_queue(&chip
->wq
, &wait
);
810 mutex_unlock(&chip
->mutex
);
812 remove_wait_queue(&chip
->wq
, &wait
);
813 timeo
= jiffies
+ (HZ
*20); /* FIXME */
814 mutex_lock(&chip
->mutex
);
818 status
= map_read(map
, adr
);
819 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
822 /* OK Still waiting */
823 if (time_after(jiffies
, timeo
)) {
824 map_write(map
, CMD(0x70), adr
);
825 chip
->state
= FL_STATUS
;
826 printk(KERN_ERR
"waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
828 mutex_unlock(&chip
->mutex
);
832 /* Latency issues. Drop the lock, wait a while and retry */
833 mutex_unlock(&chip
->mutex
);
835 mutex_lock(&chip
->mutex
);
841 /* We've broken this before. It doesn't hurt to be safe */
842 map_write(map
, CMD(0x70), adr
);
843 chip
->state
= FL_STATUS
;
844 status
= map_read(map
, adr
);
846 /* check for lock bit */
847 if (map_word_bitsset(map
, status
, CMD(0x3a))) {
848 unsigned char chipstatus
= status
.x
[0];
849 if (!map_word_equal(map
, status
, CMD(chipstatus
))) {
851 for (w
=0; w
<map_words(map
); w
++) {
852 for (i
= 0; i
<cfi_interleave(cfi
); i
++) {
853 chipstatus
|= status
.x
[w
] >> (cfi
->device_type
* 8);
856 printk(KERN_WARNING
"Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
857 status
.x
[0], chipstatus
);
859 /* Reset the error bits */
860 map_write(map
, CMD(0x50), adr
);
861 map_write(map
, CMD(0x70), adr
);
863 if ((chipstatus
& 0x30) == 0x30) {
864 printk(KERN_NOTICE
"Chip reports improper command sequence: status 0x%x\n", chipstatus
);
866 } else if (chipstatus
& 0x02) {
867 /* Protection bit set */
869 } else if (chipstatus
& 0x8) {
871 printk(KERN_WARNING
"Chip reports voltage low on erase: status 0x%x\n", chipstatus
);
873 } else if (chipstatus
& 0x20) {
875 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr
, chipstatus
);
876 timeo
= jiffies
+ HZ
;
877 chip
->state
= FL_STATUS
;
878 mutex_unlock(&chip
->mutex
);
881 printk(KERN_DEBUG
"Chip erase failed at 0x%08lx: status 0x%x\n", adr
, chipstatus
);
887 mutex_unlock(&chip
->mutex
);
891 static int cfi_staa_erase_varsize(struct mtd_info
*mtd
,
892 struct erase_info
*instr
)
893 { struct map_info
*map
= mtd
->priv
;
894 struct cfi_private
*cfi
= map
->fldrv_priv
;
895 unsigned long adr
, len
;
898 struct mtd_erase_region_info
*regions
= mtd
->eraseregions
;
900 /* Check that both start and end of the requested erase are
901 * aligned with the erasesize at the appropriate addresses.
906 /* Skip all erase regions which are ended before the start of
907 the requested erase. Actually, to save on the calculations,
908 we skip to the first erase region which starts after the
909 start of the requested erase, and then go back one.
912 while (i
< mtd
->numeraseregions
&& instr
->addr
>= regions
[i
].offset
)
916 /* OK, now i is pointing at the erase region in which this
917 erase request starts. Check the start of the requested
918 erase range is aligned with the erase size which is in
922 if (instr
->addr
& (regions
[i
].erasesize
-1))
925 /* Remember the erase region we start on */
928 /* Next, check that the end of the requested erase is aligned
929 * with the erase region at that address.
932 while (i
<mtd
->numeraseregions
&& (instr
->addr
+ instr
->len
) >= regions
[i
].offset
)
935 /* As before, drop back one to point at the region in which
936 the address actually falls
940 if ((instr
->addr
+ instr
->len
) & (regions
[i
].erasesize
-1))
943 chipnum
= instr
->addr
>> cfi
->chipshift
;
944 adr
= instr
->addr
- (chipnum
<< cfi
->chipshift
);
950 ret
= do_erase_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
955 adr
+= regions
[i
].erasesize
;
956 len
-= regions
[i
].erasesize
;
958 if (adr
% (1<< cfi
->chipshift
) == (((unsigned long)regions
[i
].offset
+ (regions
[i
].erasesize
* regions
[i
].numblocks
)) %( 1<< cfi
->chipshift
)))
961 if (adr
>> cfi
->chipshift
) {
965 if (chipnum
>= cfi
->numchips
)
973 static void cfi_staa_sync (struct mtd_info
*mtd
)
975 struct map_info
*map
= mtd
->priv
;
976 struct cfi_private
*cfi
= map
->fldrv_priv
;
980 DECLARE_WAITQUEUE(wait
, current
);
982 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
983 chip
= &cfi
->chips
[i
];
986 mutex_lock(&chip
->mutex
);
988 switch(chip
->state
) {
993 chip
->oldstate
= chip
->state
;
994 chip
->state
= FL_SYNCING
;
995 /* No need to wake_up() on this state change -
996 * as the whole point is that nobody can do anything
997 * with the chip now anyway.
1001 mutex_unlock(&chip
->mutex
);
1005 /* Not an idle state */
1006 set_current_state(TASK_UNINTERRUPTIBLE
);
1007 add_wait_queue(&chip
->wq
, &wait
);
1009 mutex_unlock(&chip
->mutex
);
1011 remove_wait_queue(&chip
->wq
, &wait
);
1017 /* Unlock the chips again */
1019 for (i
--; i
>=0; i
--) {
1020 chip
= &cfi
->chips
[i
];
1022 mutex_lock(&chip
->mutex
);
1024 if (chip
->state
== FL_SYNCING
) {
1025 chip
->state
= chip
->oldstate
;
1028 mutex_unlock(&chip
->mutex
);
1032 static inline int do_lock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1034 struct cfi_private
*cfi
= map
->fldrv_priv
;
1035 map_word status
, status_OK
;
1036 unsigned long timeo
= jiffies
+ HZ
;
1037 DECLARE_WAITQUEUE(wait
, current
);
1041 /* Let's determine this according to the interleave only once */
1042 status_OK
= CMD(0x80);
1044 timeo
= jiffies
+ HZ
;
1046 mutex_lock(&chip
->mutex
);
1048 /* Check that the chip's ready to talk to us. */
1049 switch (chip
->state
) {
1051 case FL_JEDEC_QUERY
:
1053 map_write(map
, CMD(0x70), adr
);
1054 chip
->state
= FL_STATUS
;
1057 status
= map_read(map
, adr
);
1058 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1061 /* Urgh. Chip not yet ready to talk to us. */
1062 if (time_after(jiffies
, timeo
)) {
1063 mutex_unlock(&chip
->mutex
);
1064 printk(KERN_ERR
"waiting for chip to be ready timed out in lock\n");
1068 /* Latency issues. Drop the lock, wait a while and retry */
1069 mutex_unlock(&chip
->mutex
);
1074 /* Stick ourselves on a wait queue to be woken when
1075 someone changes the status */
1076 set_current_state(TASK_UNINTERRUPTIBLE
);
1077 add_wait_queue(&chip
->wq
, &wait
);
1078 mutex_unlock(&chip
->mutex
);
1080 remove_wait_queue(&chip
->wq
, &wait
);
1081 timeo
= jiffies
+ HZ
;
1086 map_write(map
, CMD(0x60), adr
);
1087 map_write(map
, CMD(0x01), adr
);
1088 chip
->state
= FL_LOCKING
;
1090 mutex_unlock(&chip
->mutex
);
1092 mutex_lock(&chip
->mutex
);
1094 /* FIXME. Use a timer to check this, and return immediately. */
1095 /* Once the state machine's known to be working I'll do that */
1097 timeo
= jiffies
+ (HZ
*2);
1100 status
= map_read(map
, adr
);
1101 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1104 /* OK Still waiting */
1105 if (time_after(jiffies
, timeo
)) {
1106 map_write(map
, CMD(0x70), adr
);
1107 chip
->state
= FL_STATUS
;
1108 printk(KERN_ERR
"waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1110 mutex_unlock(&chip
->mutex
);
1114 /* Latency issues. Drop the lock, wait a while and retry */
1115 mutex_unlock(&chip
->mutex
);
1117 mutex_lock(&chip
->mutex
);
1120 /* Done and happy. */
1121 chip
->state
= FL_STATUS
;
1124 mutex_unlock(&chip
->mutex
);
1127 static int cfi_staa_lock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1129 struct map_info
*map
= mtd
->priv
;
1130 struct cfi_private
*cfi
= map
->fldrv_priv
;
1133 #ifdef DEBUG_LOCK_BITS
1134 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1137 if (ofs
& (mtd
->erasesize
- 1))
1140 if (len
& (mtd
->erasesize
-1))
1143 chipnum
= ofs
>> cfi
->chipshift
;
1144 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1148 #ifdef DEBUG_LOCK_BITS
1149 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1150 printk("before lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1151 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1154 ret
= do_lock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1156 #ifdef DEBUG_LOCK_BITS
1157 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1158 printk("after lock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1159 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1165 adr
+= mtd
->erasesize
;
1166 len
-= mtd
->erasesize
;
1168 if (adr
>> cfi
->chipshift
) {
1172 if (chipnum
>= cfi
->numchips
)
1178 static inline int do_unlock_oneblock(struct map_info
*map
, struct flchip
*chip
, unsigned long adr
)
1180 struct cfi_private
*cfi
= map
->fldrv_priv
;
1181 map_word status
, status_OK
;
1182 unsigned long timeo
= jiffies
+ HZ
;
1183 DECLARE_WAITQUEUE(wait
, current
);
1187 /* Let's determine this according to the interleave only once */
1188 status_OK
= CMD(0x80);
1190 timeo
= jiffies
+ HZ
;
1192 mutex_lock(&chip
->mutex
);
1194 /* Check that the chip's ready to talk to us. */
1195 switch (chip
->state
) {
1197 case FL_JEDEC_QUERY
:
1199 map_write(map
, CMD(0x70), adr
);
1200 chip
->state
= FL_STATUS
;
1203 status
= map_read(map
, adr
);
1204 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1207 /* Urgh. Chip not yet ready to talk to us. */
1208 if (time_after(jiffies
, timeo
)) {
1209 mutex_unlock(&chip
->mutex
);
1210 printk(KERN_ERR
"waiting for chip to be ready timed out in unlock\n");
1214 /* Latency issues. Drop the lock, wait a while and retry */
1215 mutex_unlock(&chip
->mutex
);
1220 /* Stick ourselves on a wait queue to be woken when
1221 someone changes the status */
1222 set_current_state(TASK_UNINTERRUPTIBLE
);
1223 add_wait_queue(&chip
->wq
, &wait
);
1224 mutex_unlock(&chip
->mutex
);
1226 remove_wait_queue(&chip
->wq
, &wait
);
1227 timeo
= jiffies
+ HZ
;
1232 map_write(map
, CMD(0x60), adr
);
1233 map_write(map
, CMD(0xD0), adr
);
1234 chip
->state
= FL_UNLOCKING
;
1236 mutex_unlock(&chip
->mutex
);
1238 mutex_lock(&chip
->mutex
);
1240 /* FIXME. Use a timer to check this, and return immediately. */
1241 /* Once the state machine's known to be working I'll do that */
1243 timeo
= jiffies
+ (HZ
*2);
1246 status
= map_read(map
, adr
);
1247 if (map_word_andequal(map
, status
, status_OK
, status_OK
))
1250 /* OK Still waiting */
1251 if (time_after(jiffies
, timeo
)) {
1252 map_write(map
, CMD(0x70), adr
);
1253 chip
->state
= FL_STATUS
;
1254 printk(KERN_ERR
"waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status
.x
[0], map_read(map
, adr
).x
[0]);
1256 mutex_unlock(&chip
->mutex
);
1260 /* Latency issues. Drop the unlock, wait a while and retry */
1261 mutex_unlock(&chip
->mutex
);
1263 mutex_lock(&chip
->mutex
);
1266 /* Done and happy. */
1267 chip
->state
= FL_STATUS
;
1270 mutex_unlock(&chip
->mutex
);
1273 static int cfi_staa_unlock(struct mtd_info
*mtd
, loff_t ofs
, uint64_t len
)
1275 struct map_info
*map
= mtd
->priv
;
1276 struct cfi_private
*cfi
= map
->fldrv_priv
;
1279 #ifdef DEBUG_LOCK_BITS
1280 int ofs_factor
= cfi
->interleave
* cfi
->device_type
;
1283 chipnum
= ofs
>> cfi
->chipshift
;
1284 adr
= ofs
- (chipnum
<< cfi
->chipshift
);
1286 #ifdef DEBUG_LOCK_BITS
1288 unsigned long temp_adr
= adr
;
1289 unsigned long temp_len
= len
;
1291 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1293 printk("before unlock %x: block status register is %x\n",temp_adr
,cfi_read_query(map
, temp_adr
+(2*ofs_factor
)));
1294 temp_adr
+= mtd
->erasesize
;
1295 temp_len
-= mtd
->erasesize
;
1297 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1301 ret
= do_unlock_oneblock(map
, &cfi
->chips
[chipnum
], adr
);
1303 #ifdef DEBUG_LOCK_BITS
1304 cfi_send_gen_cmd(0x90, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1305 printk("after unlock: block status register is %x\n",cfi_read_query(map
, adr
+(2*ofs_factor
)));
1306 cfi_send_gen_cmd(0xff, 0x55, 0, map
, cfi
, cfi
->device_type
, NULL
);
1312 static int cfi_staa_suspend(struct mtd_info
*mtd
)
1314 struct map_info
*map
= mtd
->priv
;
1315 struct cfi_private
*cfi
= map
->fldrv_priv
;
1317 struct flchip
*chip
;
1320 for (i
=0; !ret
&& i
<cfi
->numchips
; i
++) {
1321 chip
= &cfi
->chips
[i
];
1323 mutex_lock(&chip
->mutex
);
1325 switch(chip
->state
) {
1329 case FL_JEDEC_QUERY
:
1330 chip
->oldstate
= chip
->state
;
1331 chip
->state
= FL_PM_SUSPENDED
;
1332 /* No need to wake_up() on this state change -
1333 * as the whole point is that nobody can do anything
1334 * with the chip now anyway.
1338 case FL_PM_SUSPENDED
:
1345 mutex_unlock(&chip
->mutex
);
1348 /* Unlock the chips again */
1351 for (i
--; i
>=0; i
--) {
1352 chip
= &cfi
->chips
[i
];
1354 mutex_lock(&chip
->mutex
);
1356 if (chip
->state
== FL_PM_SUSPENDED
) {
1357 /* No need to force it into a known state here,
1358 because we're returning failure, and it didn't
1360 chip
->state
= chip
->oldstate
;
1363 mutex_unlock(&chip
->mutex
);
1370 static void cfi_staa_resume(struct mtd_info
*mtd
)
1372 struct map_info
*map
= mtd
->priv
;
1373 struct cfi_private
*cfi
= map
->fldrv_priv
;
1375 struct flchip
*chip
;
1377 for (i
=0; i
<cfi
->numchips
; i
++) {
1379 chip
= &cfi
->chips
[i
];
1381 mutex_lock(&chip
->mutex
);
1383 /* Go to known state. Chip may have been power cycled */
1384 if (chip
->state
== FL_PM_SUSPENDED
) {
1385 map_write(map
, CMD(0xFF), 0);
1386 chip
->state
= FL_READY
;
1390 mutex_unlock(&chip
->mutex
);
1394 static void cfi_staa_destroy(struct mtd_info
*mtd
)
1396 struct map_info
*map
= mtd
->priv
;
1397 struct cfi_private
*cfi
= map
->fldrv_priv
;
1398 kfree(cfi
->cmdset_priv
);
1402 MODULE_DESCRIPTION("MTD chip driver for ST Advanced Architecture Command Set (ID 0x0020)");
1403 MODULE_LICENSE("GPL");