21 #include <linux/module.h>
22 #include <linux/types.h>
23 #include <linux/kernel.h>
24 #include <linux/sched.h>
27 #include <asm/byteorder.h>
29 #include <linux/errno.h>
30 #include <linux/slab.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);
43 static void cfi_staa_sync (
struct mtd_info *);
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 *);
57 .destroy = cfi_staa_destroy,
58 .name =
"cfi_cmdset_0020",
65 #ifdef DEBUG_CFI_FEATURES
79 for (i=9; i<32; i++) {
81 printk(
" - Unknown Bit %X: supported\n", i);
88 printk(
" - Unknown Bit %X: supported\n", i);
94 for (i=2; i<16; i++) {
96 printk(
" - Unknown Bit %X Active: yes\n",i);
99 printk(
" Vcc Logic Supply Optimum Program/Erase Voltage: %d.%d V\n",
102 printk(
" Vpp Programming Supply Optimum Program/Erase Voltage: %d.%d V\n",
135 " Extended Query version %c.%c.\n",
146 #ifdef DEBUG_CFI_FEATURES
148 cfi_tell_features(extp);
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;
163 return cfi_staa_setup(map);
173 unsigned long devsize = (1<<cfi->
cfiq->DevSize) * cfi->
interleave;
192 printk(
KERN_ERR "Failed to allocate memory for MTD erase region info\n");
198 for (i=0; i<cfi->
cfiq->NumEraseRegions; i++) {
199 unsigned long ernum, ersize;
200 ersize = ((cfi->
cfiq->EraseRegionInfo[
i] >> 8) & ~0xff) * cfi->
interleave;
201 ernum = (cfi->
cfiq->EraseRegionInfo[
i] & 0xffff) + 1;
211 offset += (ersize * ernum);
214 if (offset != devsize) {
216 printk(
KERN_WARNING "Sum of regions (%lx) != total size of set of interleaved chips (%lx)\n", offset, devsize);
231 mtd->
_erase = cfi_staa_erase_varsize;
232 mtd->
_read = cfi_staa_read;
233 mtd->
_write = cfi_staa_write_buffers;
234 mtd->
_writev = cfi_staa_writev;
235 mtd->
_sync = cfi_staa_sync;
236 mtd->
_lock = cfi_staa_lock;
237 mtd->
_unlock = cfi_staa_unlock;
239 mtd->
_resume = cfi_staa_resume;
243 map->
fldrv = &cfi_staa_chipdrv;
256 unsigned long cmd_addr;
262 cmd_addr = adr & ~(map_bankwidth(map)-1);
265 status_OK =
CMD(0x80);
274 switch (chip->
state) {
303 "suspended: status = 0x%lx\n", status.
x[0]);
341 printk(
KERN_ERR "waiting for chip to be ready timed out in read. WSM status = %lx\n", status.
x[0]);
385 static int cfi_staa_read (
struct mtd_info *mtd, loff_t
from,
size_t len,
size_t *retlen,
u_char *buf)
395 ofs = from - (chipnum << cfi->
chipshift);
398 unsigned long thislen;
408 ret = do_read_onechip(map, &cfi->
chips[chipnum], ofs, thislen, buf);
423 unsigned long adr,
const u_char *buf,
int len)
427 unsigned long cmd_adr, timeo;
432 if (adr & (map_bankwidth(map)-1))
435 wbufsize = cfi_interleave(cfi) << cfi->
cfiq->MaxBufWriteSize;
437 cmd_adr = adr & ~(wbufsize-1);
440 status_OK =
CMD(0x80);
445 #ifdef DEBUG_CFI_FEATURES
446 printk(
"%s: chip->state[%d]\n", __func__, chip->
state);
455 switch (chip->
state) {
463 #ifdef DEBUG_CFI_FEATURES
474 printk(
KERN_ERR "waiting for chip to be ready timed out in buffer write Xstatus = %lx, status = %lx\n",
516 printk(
KERN_ERR "Chip not ready for buffer write. Xstatus = %lx\n", status.
x[0]);
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);
567 printk(
KERN_ERR "waiting for chip to be ready timed out in bufwrite\n");
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]);
600 return map_word_bitsset(map, status,
CMD(0x02)) ? -
EROFS : -
EIO;
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)
613 int wbufsize = cfi_interleave(cfi) << cfi->
cfiq->MaxBufWriteSize;
621 #ifdef DEBUG_CFI_FEATURES
622 printk(
"%s: map_bankwidth(map)[%x]\n", __func__, map_bankwidth(map));
623 printk(
"%s: chipnum[%x] wbufsize[%x]\n", __func__, chipnum, wbufsize);
624 printk(
"%s: ofs[%x] len[%x]\n", __func__, ofs, len);
630 int size = wbufsize - (ofs & (wbufsize-1));
661 #define ECCBUF_SIZE (mtd->writesize)
662 #define ECCBUF_DIV(x) ((x) & ~(ECCBUF_SIZE - 1))
663 #define ECCBUF_MOD(x) ((x) & (ECCBUF_SIZE - 1))
665 cfi_staa_writev(
struct mtd_info *mtd,
const struct kvec *vecs,
666 unsigned long count, loff_t to,
size_t *retlen)
669 size_t totlen = 0, thislen;
684 for (i=0; i<
count; i++) {
691 memcpy(buffer+buflen, elem_base, elem_len);
701 elem_len -= thislen-
buflen;
702 elem_base += thislen-
buflen;
707 &thislen, elem_base);
716 memcpy(buffer, elem_base + thislen, buflen);
721 ret =
mtd_write(mtd, to, buflen, &thislen, buffer);
746 status_OK =
CMD(0x80);
753 switch (chip->
state) {
768 printk(
KERN_ERR "waiting for chip to be ready timed out in erase\n");
827 printk(
KERN_ERR "waiting for erase to complete timed out. Xstatus = %lx, status = %lx.\n", status.
x[0],
map_read(map, adr).
x[0]);
848 if (map_word_bitsset(map, status,
CMD(0x3a))) {
849 unsigned char chipstatus = status.
x[0];
850 if (!map_word_equal(map, status,
CMD(chipstatus))) {
853 for (i = 0; i<cfi_interleave(cfi); i++) {
857 printk(
KERN_WARNING "Status is not identical for all chips: 0x%lx. Merging to give 0x%02x\n",
858 status.
x[0], chipstatus);
864 if ((chipstatus & 0x30) == 0x30) {
865 printk(
KERN_NOTICE "Chip reports improper command sequence: status 0x%x\n", chipstatus);
867 }
else if (chipstatus & 0x02) {
870 }
else if (chipstatus & 0x8) {
874 }
else if (chipstatus & 0x20) {
876 printk(
KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x. Retrying...\n", adr, chipstatus);
882 printk(
KERN_DEBUG "Chip erase failed at 0x%08lx: status 0x%x\n", adr, chipstatus);
892 static int cfi_staa_erase_varsize(
struct mtd_info *mtd,
896 unsigned long adr, len;
897 int chipnum, ret = 0;
913 while (i < mtd->numeraseregions && instr->
addr >= regions[i].
offset)
933 while (i<mtd->numeraseregions && (instr->
addr + instr->
len) >= regions[i].
offset)
977 static void cfi_staa_sync (
struct mtd_info *mtd)
986 for (i=0; !ret && i<cfi->
numchips; i++) {
992 switch(chip->
state) {
1022 for (i--; i >=0; i--) {
1035 static inline int do_lock_oneblock(
struct map_info *map,
struct flchip *chip,
unsigned long adr)
1045 status_OK =
CMD(0x80);
1052 switch (chip->
state) {
1067 printk(
KERN_ERR "waiting for chip to be ready timed out in lock\n");
1111 printk(
KERN_ERR "waiting for lock to complete timed out. Xstatus = %lx, status = %lx.\n", status.
x[0],
map_read(map, adr).
x[0]);
1135 int chipnum, ret = 0;
1136 #ifdef DEBUG_LOCK_BITS
1147 adr = ofs - (chipnum << cfi->
chipshift);
1151 #ifdef DEBUG_LOCK_BITS
1153 printk(
"before lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1157 ret = do_lock_oneblock(map, &cfi->
chips[chipnum], adr);
1159 #ifdef DEBUG_LOCK_BITS
1161 printk(
"after lock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1181 static inline int do_unlock_oneblock(
struct map_info *map,
struct flchip *chip,
unsigned long adr)
1191 status_OK =
CMD(0x80);
1198 switch (chip->
state) {
1213 printk(
KERN_ERR "waiting for chip to be ready timed out in unlock\n");
1257 printk(
KERN_ERR "waiting for unlock to complete timed out. Xstatus = %lx, status = %lx.\n", status.
x[0],
map_read(map, adr).
x[0]);
1281 int chipnum, ret = 0;
1282 #ifdef DEBUG_LOCK_BITS
1287 adr = ofs - (chipnum << cfi->
chipshift);
1289 #ifdef DEBUG_LOCK_BITS
1291 unsigned long temp_adr = adr;
1292 unsigned long temp_len = len;
1296 printk(
"before unlock %x: block status register is %x\n",temp_adr,cfi_read_query(map, temp_adr+(2*ofs_factor)));
1304 ret = do_unlock_oneblock(map, &cfi->
chips[chipnum], adr);
1306 #ifdef DEBUG_LOCK_BITS
1308 printk(
"after unlock: block status register is %x\n",cfi_read_query(map, adr+(2*ofs_factor)));
1315 static int cfi_staa_suspend(
struct mtd_info *mtd)
1323 for (i=0; !ret && i<cfi->
numchips; i++) {
1328 switch(chip->
state) {
1352 for (i--; i >=0; i--) {
1371 static void cfi_staa_resume(
struct mtd_info *mtd)
1395 static void cfi_staa_destroy(
struct mtd_info *mtd)