27 #include <linux/types.h>
28 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/string.h>
40 #include <linux/list.h>
41 #include <linux/random.h>
42 #include <linux/sched.h>
47 #if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
48 !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
49 !defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
50 !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
51 #define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
52 #define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
53 #define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF
54 #define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF
57 #ifndef CONFIG_NANDSIM_ACCESS_DELAY
58 #define CONFIG_NANDSIM_ACCESS_DELAY 25
60 #ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
61 #define CONFIG_NANDSIM_PROGRAMM_DELAY 200
63 #ifndef CONFIG_NANDSIM_ERASE_DELAY
64 #define CONFIG_NANDSIM_ERASE_DELAY 2
66 #ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
67 #define CONFIG_NANDSIM_OUTPUT_CYCLE 40
69 #ifndef CONFIG_NANDSIM_INPUT_CYCLE
70 #define CONFIG_NANDSIM_INPUT_CYCLE 50
72 #ifndef CONFIG_NANDSIM_BUS_WIDTH
73 #define CONFIG_NANDSIM_BUS_WIDTH 8
75 #ifndef CONFIG_NANDSIM_DO_DELAYS
76 #define CONFIG_NANDSIM_DO_DELAYS 0
78 #ifndef CONFIG_NANDSIM_LOG
79 #define CONFIG_NANDSIM_LOG 0
81 #ifndef CONFIG_NANDSIM_DBG
82 #define CONFIG_NANDSIM_DBG 0
84 #ifndef CONFIG_NANDSIM_MAX_PARTS
85 #define CONFIG_NANDSIM_MAX_PARTS 32
102 static unsigned int parts_num;
103 static char *badblocks =
NULL;
104 static char *weakblocks =
NULL;
105 static char *weakpages =
NULL;
106 static unsigned int bitflips = 0;
107 static char *gravepages =
NULL;
108 static unsigned int rptwear = 0;
109 static unsigned int overridesize = 0;
110 static char *cache_file =
NULL;
111 static unsigned int bbt;
112 static unsigned int bch;
139 MODULE_PARM_DESC(first_id_byte,
"The first byte returned by NAND Flash 'read ID' command (manufacturer ID)");
140 MODULE_PARM_DESC(second_id_byte,
"The second byte returned by NAND Flash 'read ID' command (chip ID)");
141 MODULE_PARM_DESC(third_id_byte,
"The third byte returned by NAND Flash 'read ID' command");
142 MODULE_PARM_DESC(fourth_id_byte,
"The fourth byte returned by NAND Flash 'read ID' command");
146 MODULE_PARM_DESC(output_cycle,
"Word output (from flash) time (nanoseconds)");
149 MODULE_PARM_DESC(do_delays,
"Simulate NAND delays using busy-waits if not zero");
152 MODULE_PARM_DESC(parts,
"Partition sizes (in erase blocks) separated by commas");
154 MODULE_PARM_DESC(badblocks,
"Erase blocks that are initially marked bad, separated by commas");
155 MODULE_PARM_DESC(weakblocks,
"Weak erase blocks [: remaining erase cycles (defaults to 3)]"
156 " separated by commas e.g. 113:2 means eb 113"
157 " can be erased only twice before failing");
159 " separated by commas e.g. 1401:2 means page 1401"
160 " can be written only twice before failing");
161 MODULE_PARM_DESC(bitflips,
"Maximum number of random bit flips per page (zero by default)");
162 MODULE_PARM_DESC(gravepages,
"Pages that lose data [: maximum reads (defaults to 3)]"
163 " separated by commas e.g. 1401:2 means page 1401"
164 " can be read only twice before failing");
165 MODULE_PARM_DESC(rptwear,
"Number of erases between reporting wear, if not zero");
166 MODULE_PARM_DESC(overridesize,
"Specifies the NAND Flash size overriding the ID bytes. "
167 "The size is specified in erase blocks and as the exponent of a power of two"
168 " e.g. 5 means a size of 32 erase blocks");
169 MODULE_PARM_DESC(cache_file,
"File to use to cache nand pages instead of memory");
170 MODULE_PARM_DESC(bbt,
"0 OOB, 1 BBT with marker in OOB, 2 BBT with marker in data area");
172 "be correctable in 512-byte blocks");
175 #define NS_LARGEST_PAGE_SIZE 4096
178 #define NS_OUTPUT_PREFIX "[nandsim]"
181 #define NS_LOG(args...) \
182 do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0)
183 #define NS_DBG(args...) \
184 do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
185 #define NS_WARN(args...) \
186 do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warning: " args); } while(0)
187 #define NS_ERR(args...) \
188 do { printk(KERN_ERR NS_OUTPUT_PREFIX " error: " args); } while(0)
189 #define NS_INFO(args...) \
190 do { printk(KERN_INFO NS_OUTPUT_PREFIX " " args); } while(0)
193 #define NS_UDELAY(us) \
194 do { if (do_delays) udelay(us); } while(0)
195 #define NS_MDELAY(us) \
196 do { if (do_delays) mdelay(us); } while(0)
199 #define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
202 #define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
205 #define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
208 #define NS_RAW_OFFSET(ns) \
209 (((ns)->regs.row << (ns)->geom.pgshift) + ((ns)->regs.row * (ns)->geom.oobsz) + (ns)->regs.column)
212 #define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
215 #define STATE_CMD_READ0 0x00000001
216 #define STATE_CMD_READ1 0x00000002
217 #define STATE_CMD_READSTART 0x00000003
218 #define STATE_CMD_PAGEPROG 0x00000004
219 #define STATE_CMD_READOOB 0x00000005
220 #define STATE_CMD_ERASE1 0x00000006
221 #define STATE_CMD_STATUS 0x00000007
222 #define STATE_CMD_STATUS_M 0x00000008
223 #define STATE_CMD_SEQIN 0x00000009
224 #define STATE_CMD_READID 0x0000000A
225 #define STATE_CMD_ERASE2 0x0000000B
226 #define STATE_CMD_RESET 0x0000000C
227 #define STATE_CMD_RNDOUT 0x0000000D
228 #define STATE_CMD_RNDOUTSTART 0x0000000E
229 #define STATE_CMD_MASK 0x0000000F
232 #define STATE_ADDR_PAGE 0x00000010
233 #define STATE_ADDR_SEC 0x00000020
234 #define STATE_ADDR_COLUMN 0x00000030
235 #define STATE_ADDR_ZERO 0x00000040
236 #define STATE_ADDR_MASK 0x00000070
239 #define STATE_DATAIN 0x00000100
240 #define STATE_DATAIN_MASK 0x00000100
242 #define STATE_DATAOUT 0x00001000
243 #define STATE_DATAOUT_ID 0x00002000
244 #define STATE_DATAOUT_STATUS 0x00003000
245 #define STATE_DATAOUT_STATUS_M 0x00004000
246 #define STATE_DATAOUT_MASK 0x00007000
249 #define STATE_READY 0x00000000
252 #define STATE_UNKNOWN 0x10000000
255 #define ACTION_CPY 0x00100000
256 #define ACTION_PRGPAGE 0x00200000
257 #define ACTION_SECERASE 0x00300000
258 #define ACTION_ZEROOFF 0x00400000
259 #define ACTION_HALFOFF 0x00500000
260 #define ACTION_OOBOFF 0x00600000
261 #define ACTION_MASK 0x00700000
263 #define NS_OPER_NUM 13
264 #define NS_OPER_STATES 6
266 #define OPT_ANY 0xFFFFFFFF
267 #define OPT_PAGE256 0x00000001
268 #define OPT_PAGE512 0x00000002
269 #define OPT_PAGE2048 0x00000008
270 #define OPT_SMARTMEDIA 0x00000010
271 #define OPT_PAGE512_8BIT 0x00000040
272 #define OPT_PAGE4096 0x00000080
273 #define OPT_LARGEPAGE (OPT_PAGE2048 | OPT_PAGE4096)
274 #define OPT_SMALLPAGE (OPT_PAGE256 | OPT_PAGE512)
277 #define NS_STATE(x) ((x) & ~ACTION_MASK)
284 #define NS_MAX_PREVSTATES 1
287 #define NS_MAX_HELD_PAGES 16
374 static struct nandsim_operations {
442 static unsigned long *erase_block_wear =
NULL;
443 static unsigned int wear_eb_count = 0;
444 static unsigned long total_wear = 0;
445 static unsigned int rptwear_cnt = 0;
456 static int alloc_device(
struct nandsim *
ns)
464 return PTR_ERR(cfile);
465 if (!cfile->
f_op || (!cfile->
f_op->read && !cfile->
f_op->aio_read)) {
466 NS_ERR(
"alloc_device: cache file not readable\n");
470 if (!cfile->
f_op->write && !cfile->
f_op->aio_write) {
471 NS_ERR(
"alloc_device: cache file not writeable\n");
477 NS_ERR(
"alloc_device: unable to allocate pages written array\n");
483 NS_ERR(
"alloc_device: unable to allocate file buf\n");
493 NS_ERR(
"alloc_device: unable to allocate page array\n");
496 for (i = 0; i < ns->
geom.pgnum; i++) {
502 NS_ERR(
"cache_create: unable to create kmem_cache\n");
518 static void free_device(
struct nandsim *ns)
530 for (i = 0; i < ns->
geom.pgnum; i++) {
540 static char *get_partition_name(
int i)
543 sprintf(buf,
"NAND simulator partition %d", i);
552 static int init_nandsim(
struct mtd_info *mtd)
561 NS_ERR(
"init_nandsim: nandsim is already initialized\n");
575 ns->
geom.pgnum = div_u64(ns->
geom.totsz, ns->
geom.pgsz);
584 if (ns->
geom.pgsz == 256) {
587 else if (ns->
geom.pgsz == 512) {
591 }
else if (ns->
geom.pgsz == 2048) {
593 }
else if (ns->
geom.pgsz == 4096) {
596 NS_ERR(
"init_nandsim: unknown page size %u\n", ns->
geom.pgsz);
601 if (ns->
geom.totsz <= (32 << 20)) {
602 ns->
geom.pgaddrbytes = 3;
603 ns->
geom.secaddrbytes = 2;
605 ns->
geom.pgaddrbytes = 4;
606 ns->
geom.secaddrbytes = 3;
609 if (ns->
geom.totsz <= (128 << 20)) {
610 ns->
geom.pgaddrbytes = 4;
611 ns->
geom.secaddrbytes = 2;
613 ns->
geom.pgaddrbytes = 5;
614 ns->
geom.secaddrbytes = 3;
620 NS_ERR(
"too many partitions.\n");
624 remains = ns->
geom.totsz;
626 for (i = 0; i < parts_num; ++
i) {
629 if (!part_sz || part_sz > remains) {
630 NS_ERR(
"bad partition size.\n");
643 NS_ERR(
"too many partitions.\n");
660 NS_WARN(
"16-bit flashes support wasn't tested\n");
662 printk(
"flash size: %llu MiB\n",
663 (
unsigned long long)ns->
geom.totsz >> 20);
664 printk(
"page size: %u bytes\n", ns->
geom.pgsz);
665 printk(
"OOB area size: %u bytes\n", ns->
geom.oobsz);
666 printk(
"sector size: %u KiB\n", ns->
geom.secsz >> 10);
668 printk(
"pages per sector: %u\n", ns->
geom.pgsec);
670 printk(
"bits in sector size: %u\n", ns->
geom.secshift);
671 printk(
"bits in page size: %u\n", ns->
geom.pgshift);
672 printk(
"bits in OOB size: %u\n", ns->
geom.oobshift);
673 printk(
"flash size with OOB: %llu KiB\n",
674 (
unsigned long long)ns->
geom.totszoob >> 10);
675 printk(
"page address bytes: %u\n", ns->
geom.pgaddrbytes);
676 printk(
"sector address bytes: %u\n", ns->
geom.secaddrbytes);
679 if ((ret = alloc_device(ns)) != 0)
685 NS_ERR(
"init_nandsim: unable to allocate %u bytes for the internal buffer\n",
703 static void free_nandsim(
struct nandsim *ns)
715 unsigned int erase_block_no;
722 zero_ok = (*w ==
'0' ? 1 : 0);
724 if (!zero_ok && !erase_block_no) {
725 NS_ERR(
"invalid badblocks.\n");
728 offset = erase_block_no * ns->
geom.secsz;
730 NS_ERR(
"invalid badblocks.\n");
739 static int parse_weakblocks(
void)
743 unsigned int erase_block_no;
744 unsigned int max_erases;
751 zero_ok = (*w ==
'0' ? 1 : 0);
753 if (!zero_ok && !erase_block_no) {
754 NS_ERR(
"invalid weakblocks.\n");
766 NS_ERR(
"unable to allocate memory.\n");
771 list_add(&wb->
list, &weak_blocks);
776 static int erase_error(
unsigned int erase_block_no)
781 if (wb->erase_block_no == erase_block_no) {
790 static int parse_weakpages(
void)
794 unsigned int page_no;
795 unsigned int max_writes;
802 zero_ok = (*w ==
'0' ? 1 : 0);
804 if (!zero_ok && !page_no) {
805 NS_ERR(
"invalid weakpagess.\n");
817 NS_ERR(
"unable to allocate memory.\n");
822 list_add(&wp->
list, &weak_pages);
827 static int write_error(
unsigned int page_no)
832 if (wp->page_no == page_no) {
841 static int parse_gravepages(
void)
846 unsigned int max_reads;
853 zero_ok = (*g ==
'0' ? 1 : 0);
855 if (!zero_ok && !page_no) {
856 NS_ERR(
"invalid gravepagess.\n");
868 NS_ERR(
"unable to allocate memory.\n");
873 list_add(&gp->
list, &grave_pages);
878 static int read_error(
unsigned int page_no)
883 if (gp->page_no == page_no) {
892 static void free_lists(
void)
907 kfree(erase_block_wear);
910 static int setup_wear_reporting(
struct mtd_info *mtd)
917 mem = wear_eb_count *
sizeof(
unsigned long);
918 if (mem /
sizeof(
unsigned long) != wear_eb_count) {
919 NS_ERR(
"Too many erase blocks for wear reporting\n");
923 if (!erase_block_wear) {
924 NS_ERR(
"Too many erase blocks for wear reporting\n");
930 static void update_wear(
unsigned int erase_block_no)
932 unsigned long wmin = -1, wmax = 0,
avg;
933 unsigned long deciles[10], decile_max[10], tot = 0;
936 if (!erase_block_wear)
940 NS_ERR(
"Erase counter total overflow\n");
941 erase_block_wear[erase_block_no] += 1;
942 if (erase_block_wear[erase_block_no] == 0)
943 NS_ERR(
"Erase counter overflow for erase block %u\n", erase_block_no);
945 if (rptwear_cnt < rptwear)
949 for (i = 0; i < wear_eb_count; ++
i) {
950 unsigned long wear = erase_block_wear[
i];
957 for (i = 0; i < 9; ++
i) {
959 decile_max[
i] = (wmax * (i + 1) + 5) / 10;
962 decile_max[9] = wmax;
963 for (i = 0; i < wear_eb_count; ++
i) {
965 unsigned long wear = erase_block_wear[
i];
966 for (d = 0; d < 10; ++
d)
967 if (wear <= decile_max[d]) {
972 avg = tot / wear_eb_count;
974 NS_INFO(
"*** Wear Report ***\n");
975 NS_INFO(
"Total numbers of erases: %lu\n", tot);
976 NS_INFO(
"Number of erase blocks: %u\n", wear_eb_count);
977 NS_INFO(
"Average number of erases: %lu\n",
avg);
978 NS_INFO(
"Maximum number of erases: %lu\n", wmax);
979 NS_INFO(
"Minimum number of erases: %lu\n", wmin);
980 for (i = 0; i < 10; ++
i) {
981 unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
982 if (from > decile_max[i])
984 NS_INFO(
"Number of ebs with erase counts from %lu to %lu : %lu\n",
989 NS_INFO(
"*** End of Wear Report ***\n");
999 return "STATE_CMD_READ0";
1001 return "STATE_CMD_READ1";
1003 return "STATE_CMD_PAGEPROG";
1005 return "STATE_CMD_READOOB";
1007 return "STATE_CMD_READSTART";
1009 return "STATE_CMD_ERASE1";
1011 return "STATE_CMD_STATUS";
1013 return "STATE_CMD_STATUS_M";
1015 return "STATE_CMD_SEQIN";
1017 return "STATE_CMD_READID";
1019 return "STATE_CMD_ERASE2";
1021 return "STATE_CMD_RESET";
1023 return "STATE_CMD_RNDOUT";
1025 return "STATE_CMD_RNDOUTSTART";
1027 return "STATE_ADDR_PAGE";
1029 return "STATE_ADDR_SEC";
1031 return "STATE_ADDR_ZERO";
1033 return "STATE_ADDR_COLUMN";
1035 return "STATE_DATAIN";
1037 return "STATE_DATAOUT";
1039 return "STATE_DATAOUT_ID";
1041 return "STATE_DATAOUT_STATUS";
1043 return "STATE_DATAOUT_STATUS_M";
1045 return "STATE_READY";
1047 return "STATE_UNKNOWN";
1050 NS_ERR(
"get_state_name: unknown state, BUG\n");
1059 static int check_command(
int cmd)
1120 NS_ERR(
"get_state_by_command: unknown command, BUG\n");
1131 if (ns->
regs.count < (ns->
geom.pgaddrbytes - ns->
geom.secaddrbytes))
1132 ns->
regs.column |= (byte << 8 * ns->
regs.count);
1134 ns->
regs.row |= (byte << 8 * (ns->
regs.count -
1135 ns->
geom.pgaddrbytes +
1136 ns->
geom.secaddrbytes));
1147 NS_DBG(
"switch_to_ready_state: switch to %s state\n", get_state_name(
STATE_READY));
1158 ns->
regs.column = 0;
1213 if (!(ns->
options & ops[i].reqopts))
1227 && (ns->
options & ops[idx].reqopts)) {
1238 if (opsfound == 1) {
1240 ns->
op = &ops[
idx].states[0];
1256 NS_DBG(
"find_operation: operation found, index: %d, state: %s, nxstate %s\n",
1257 idx, get_state_name(ns->
state), get_state_name(ns->
nxstate));
1261 if (opsfound == 0) {
1264 NS_DBG(
"find_operation: no operation found, try again with state %s\n",
1265 get_state_name(ns->
state));
1267 return find_operation(ns, 0);
1270 NS_DBG(
"find_operation: no operations found\n");
1277 NS_DBG(
"find_operation: BUG, operation must be known if address is input\n");
1281 NS_DBG(
"find_operation: there is still ambiguity\n");
1288 static void put_pages(
struct nandsim *ns)
1304 end_index = (pos + count - 1) >> PAGE_CACHE_SHIFT;
1308 for (index = start_index; index <= end_index; index++) {
1327 static int set_memalloc(
void)
1335 static void clear_memalloc(
int memalloc)
1341 static ssize_t read_file(
struct nandsim *ns,
struct file *file,
void *
buf,
size_t count, loff_t *pos)
1347 err = get_pages(ns, file, count, *pos);
1352 memalloc = set_memalloc();
1353 tx =
vfs_read(file, (
char __user *)buf, count, pos);
1354 clear_memalloc(memalloc);
1360 static ssize_t write_file(
struct nandsim *ns,
struct file *file,
void *buf,
size_t count, loff_t *pos)
1366 err = get_pages(ns, file, count, *pos);
1371 memalloc = set_memalloc();
1372 tx =
vfs_write(file, (
char __user *)buf, count, pos);
1373 clear_memalloc(memalloc);
1392 return NS_GET_PAGE(ns)->
byte + ns->
regs.column + ns->
regs.off;
1397 unsigned int page_no = ns->
regs.row;
1399 if (read_error(page_no)) {
1402 for (i = 0; i < num; ++
i)
1404 NS_WARN(
"simulating read error in page %u\n", page_no);
1412 if (bitflips &&
random32() < (1 << 22)) {
1418 ns->
buf.
byte[pos / 8] ^= (1 << (pos % 8));
1419 NS_WARN(
"read_page: flipping bit %d in page %d "
1420 "reading from %d ecc: corrected=%u failed=%u\n",
1430 static void read_page(
struct nandsim *ns,
int num)
1436 NS_DBG(
"read_page: page %d not written\n", ns->
regs.row);
1442 NS_DBG(
"read_page: page %d written, reading from %d\n",
1446 pos = (loff_t)ns->
regs.row * ns->
geom.pgszoob + ns->
regs.column + ns->
regs.off;
1447 tx = read_file(ns, ns->
cfile, ns->
buf.
byte, num, &pos);
1449 NS_ERR(
"read_page: read error for page %d ret %ld\n", ns->
regs.row, (
long)tx);
1457 mypage = NS_GET_PAGE(ns);
1459 NS_DBG(
"read_page: page %d not allocated\n", ns->
regs.row);
1462 NS_DBG(
"read_page: page %d allocated, reading from %d\n",
1474 static void erase_sector(
struct nandsim *ns)
1480 for (i = 0; i < ns->
geom.pgsec; i++)
1482 NS_DBG(
"erase_sector: freeing page %d\n", ns->
regs.row + i);
1488 mypage = NS_GET_PAGE(ns);
1489 for (i = 0; i < ns->
geom.pgsec; i++) {
1491 NS_DBG(
"erase_sector: freeing page %d\n", ns->
regs.row+i);
1502 static int prog_page(
struct nandsim *ns,
int num)
1513 NS_DBG(
"prog_page: writing page %d\n", ns->
regs.row);
1515 off = (loff_t)ns->
regs.row * ns->
geom.pgszoob + ns->
regs.column + ns->
regs.off;
1522 tx = read_file(ns, ns->
cfile, pg_off, num, &pos);
1524 NS_ERR(
"prog_page: read error for page %d ret %ld\n", ns->
regs.row, (
long)tx);
1528 for (i = 0; i < num; i++)
1531 pos = (loff_t)ns->
regs.row * ns->
geom.pgszoob;
1533 if (tx != ns->
geom.pgszoob) {
1534 NS_ERR(
"prog_page: write error for page %d ret %ld\n", ns->
regs.row, (
long)tx);
1540 tx = write_file(ns, ns->
cfile, pg_off, num, &pos);
1542 NS_ERR(
"prog_page: write error for page %d ret %ld\n", ns->
regs.row, (
long)tx);
1549 mypage = NS_GET_PAGE(ns);
1551 NS_DBG(
"prog_page: allocating page %d\n", ns->
regs.row);
1560 NS_ERR(
"prog_page: error allocating memory for page %d\n", ns->
regs.row);
1566 pg_off = NS_PAGE_BYTE_OFF(ns);
1567 for (i = 0; i < num; i++)
1581 int busdiv = ns->
busw == 8 ? 1 : 2;
1582 unsigned int erase_block_no, page_no;
1588 NS_WARN(
"do_state_action: wrong page number (%#x)\n", ns->
regs.row);
1600 if (ns->
regs.column >= (ns->
geom.pgszoob - ns->
regs.off)) {
1601 NS_ERR(
"do_state_action: column number is too large\n");
1604 num = ns->
geom.pgszoob - ns->
regs.off - ns->
regs.column;
1607 NS_DBG(
"do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
1610 if (ns->
regs.off == 0)
1612 else if (ns->
regs.off < ns->
geom.pgsz)
1613 NS_LOG(
"read page %d (second half)\n", ns->
regs.row);
1615 NS_LOG(
"read OOB of page %d\n", ns->
regs.row);
1628 NS_ERR(
"do_state_action: device is write-protected, ignore sector erase\n");
1633 || (ns->
regs.row & ~(ns->
geom.secsz - 1))) {
1634 NS_ERR(
"do_state_action: wrong sector address (%#x)\n", ns->
regs.row);
1639 8 * (ns->
geom.pgaddrbytes - ns->
geom.secaddrbytes)) | ns->
regs.column;
1640 ns->
regs.column = 0;
1642 erase_block_no = ns->
regs.row >> (ns->
geom.secshift - ns->
geom.pgshift);
1644 NS_DBG(
"do_state_action: erase sector at address %#x, off = %d\n",
1646 NS_LOG(
"erase sector %u\n", erase_block_no);
1652 if (erase_block_wear)
1653 update_wear(erase_block_no);
1655 if (erase_error(erase_block_no)) {
1656 NS_WARN(
"simulating erase failure in erase block %u\n", erase_block_no);
1668 NS_WARN(
"do_state_action: device is write-protected, programm\n");
1672 num = ns->
geom.pgszoob - ns->
regs.off - ns->
regs.column;
1673 if (num != ns->
regs.count) {
1674 NS_ERR(
"do_state_action: too few bytes were input (%d instead of %d)\n",
1675 ns->
regs.count, num);
1679 if (prog_page(ns, num) == -1)
1682 page_no = ns->
regs.row;
1684 NS_DBG(
"do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
1691 if (write_error(page_no)) {
1692 NS_WARN(
"simulating write failure in page %u\n", page_no);
1699 NS_DBG(
"do_state_action: set internal offset to 0\n");
1705 NS_ERR(
"do_state_action: BUG! can't skip half of page for non-512"
1706 "byte page size 8x chips\n");
1709 NS_DBG(
"do_state_action: set internal offset to %d\n", ns->
geom.pgsz/2);
1714 NS_DBG(
"do_state_action: set internal offset to %d\n", ns->
geom.pgsz);
1719 NS_DBG(
"do_state_action: BUG! unknown action\n");
1728 static void switch_state(
struct nandsim *ns)
1740 NS_DBG(
"switch_state: operation is known, switch to the next state, "
1741 "state: %s, nxstate: %s\n",
1742 get_state_name(ns->
state), get_state_name(ns->
nxstate));
1760 ns->
state = get_state_by_command(ns->
regs.command);
1762 NS_DBG(
"switch_state: operation is unknown, try to find it\n");
1764 if (find_operation(ns, 0) != 0)
1775 NS_DBG(
"switch_state: double the column number for 16x device\n");
1776 ns->
regs.column <<= 1;
1788 && ns->
regs.count != ns->
regs.num) {
1789 NS_WARN(
"switch_state: not all bytes were processed, %d left\n",
1794 NS_DBG(
"switch_state: operation complete, switch to STATE_READY state\n");
1796 switch_to_ready_state(ns, status);
1808 NS_DBG(
"switch_state: the next state is data I/O, switch, "
1809 "state: %s, nxstate: %s\n",
1810 get_state_name(ns->
state), get_state_name(ns->
nxstate));
1832 NS_ERR(
"switch_state: BUG! unknown data state\n");
1845 ns->
regs.num = ns->
geom.pgaddrbytes;
1849 ns->
regs.num = ns->
geom.secaddrbytes;
1858 ns->
regs.num = ns->
geom.pgaddrbytes - ns->
geom.secaddrbytes;
1862 NS_ERR(
"switch_state: BUG! unknown address state\n");
1880 if (!ns->
lines.ce) {
1881 NS_ERR(
"read_byte: chip is disabled, return %#x\n", (
uint)outb);
1885 NS_ERR(
"read_byte: ALE or CLE pin is high, return %#x\n", (
uint)outb);
1889 NS_WARN(
"read_byte: unexpected data output cycle, state is %s "
1890 "return %#x\n", get_state_name(ns->
state), (
uint)outb);
1896 NS_DBG(
"read_byte: return %#x status\n", ns->
regs.status);
1897 return ns->
regs.status;
1901 if (ns->
regs.count == ns->
regs.num) {
1902 NS_WARN(
"read_byte: no more data to output, return %#x\n", (
uint)outb);
1908 if (ns->
busw == 8) {
1910 ns->
regs.count += 1;
1913 ns->
regs.count += 2;
1917 NS_DBG(
"read_byte: read ID byte %d, total = %d\n", ns->
regs.count, ns->
regs.num);
1918 outb = ns->
ids[ns->
regs.count];
1919 ns->
regs.count += 1;
1925 if (ns->
regs.count == ns->
regs.num) {
1926 NS_DBG(
"read_byte: all bytes were read\n");
1935 static void ns_nand_write_byte(
struct mtd_info *mtd,
u_char byte)
1940 if (!ns->
lines.ce) {
1941 NS_ERR(
"write_byte: chip is disabled, ignore write\n");
1945 NS_ERR(
"write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
1949 if (ns->
lines.cle == 1) {
1961 if (check_command(byte)) {
1962 NS_ERR(
"write_byte: unknown command %#x\n", (
uint)byte);
1969 int row = ns->
regs.row;
1986 NS_WARN(
"write_byte: command (%#x) wasn't expected, expected state is %s, "
1987 "ignore previous states\n", (
uint)byte, get_state_name(ns->
nxstate));
1992 NS_DBG(
"command byte corresponding to %s state accepted\n",
1993 get_state_name(get_state_by_command(byte)));
1997 }
else if (ns->
lines.ale == 1) {
2004 NS_DBG(
"write_byte: operation isn't known yet, identify it\n");
2006 if (find_operation(ns, 1) < 0)
2017 ns->
regs.num = ns->
geom.pgaddrbytes;
2020 ns->
regs.num = ns->
geom.secaddrbytes;
2032 NS_ERR(
"write_byte: address (%#x) isn't expected, expected state is %s, "
2033 "switch to STATE_READY\n", (
uint)byte, get_state_name(ns->
nxstate));
2039 if (ns->
regs.count == ns->
regs.num) {
2040 NS_ERR(
"write_byte: no more address bytes expected\n");
2045 accept_addr_byte(ns, byte);
2047 ns->
regs.count += 1;
2049 NS_DBG(
"write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
2052 if (ns->
regs.count == ns->
regs.num) {
2053 NS_DBG(
"address (%#x, %#x) is accepted\n", ns->
regs.row, ns->
regs.column);
2064 NS_ERR(
"write_byte: data input (%#x) isn't expected, state is %s, "
2065 "switch to %s\n", (
uint)byte,
2072 if (ns->
regs.count == ns->
regs.num) {
2073 NS_WARN(
"write_byte: %u input bytes has already been accepted, ignore write\n",
2078 if (ns->
busw == 8) {
2080 ns->
regs.count += 1;
2083 ns->
regs.count += 2;
2090 static void ns_hwcontrol(
struct mtd_info *mtd,
int cmd,
unsigned int bitmask)
2099 ns_nand_write_byte(mtd, cmd);
2102 static int ns_device_ready(
struct mtd_info *mtd)
2104 NS_DBG(
"device_ready\n");
2117 static void ns_nand_write_buf(
struct mtd_info *mtd,
const u_char *buf,
int len)
2123 NS_ERR(
"write_buf: data input isn't expected, state is %s, "
2124 "switch to STATE_READY\n", get_state_name(ns->
state));
2130 if (ns->
regs.count + len > ns->
regs.num) {
2131 NS_ERR(
"write_buf: too many input bytes\n");
2137 ns->
regs.count += len;
2139 if (ns->
regs.count == ns->
regs.num) {
2140 NS_DBG(
"write_buf: %d bytes were written\n", ns->
regs.count);
2144 static void ns_nand_read_buf(
struct mtd_info *mtd,
u_char *buf,
int len)
2149 if (!ns->
lines.ce) {
2150 NS_ERR(
"read_buf: chip is disabled\n");
2154 NS_ERR(
"read_buf: ALE or CLE pin is high\n");
2158 NS_WARN(
"read_buf: unexpected data output cycle, current state is %s\n",
2159 get_state_name(ns->
state));
2166 for (i = 0; i < len; i++)
2173 if (ns->
regs.count + len > ns->
regs.num) {
2174 NS_ERR(
"read_buf: too many bytes to read\n");
2180 ns->
regs.count += len;
2182 if (ns->
regs.count == ns->
regs.num) {
2193 static int __init ns_init_module(
void)
2208 NS_ERR(
"unable to allocate core structures.\n");
2212 nsmtd->
priv = (
void *)chip;
2213 nand = (
struct nandsim *)(chip + 1);
2214 chip->
priv = (
void *)nand;
2238 NS_ERR(
"bbt has to be 0..2\n");
2246 if (third_id_byte != 0xFF || fourth_id_byte != 0xFF)
2247 nand->
geom.idbytes = 4;
2249 nand->
geom.idbytes = 2;
2253 nand->
ids[0] = first_id_byte;
2254 nand->
ids[1] = second_id_byte;
2255 nand->
ids[2] = third_id_byte;
2256 nand->
ids[3] = fourth_id_byte;
2264 if ((retval = parse_weakblocks()) != 0)
2267 if ((retval = parse_weakpages()) != 0)
2270 if ((retval = parse_gravepages()) != 0)
2275 NS_ERR(
"cannot scan NAND Simulator device\n");
2282 unsigned int eccsteps, eccbytes;
2283 if (!mtd_nand_has_bch()) {
2284 NS_ERR(
"BCH ECC support is disabled\n");
2290 eccbytes = (bch*13+7)/8;
2292 if ((nsmtd->
oobsize < 64) || !eccsteps) {
2293 NS_ERR(
"bch not available on small page devices\n");
2297 if ((eccbytes*eccsteps+2) > nsmtd->
oobsize) {
2298 NS_ERR(
"invalid bch value %u\n", bch);
2303 chip->
ecc.size = 512;
2304 chip->
ecc.bytes = eccbytes;
2305 NS_INFO(
"using %u-bit/%u bytes BCH ECC\n", bch, chip->
ecc.size);
2310 NS_ERR(
"can't register NAND Simulator\n");
2318 if (new_size >> overridesize != nsmtd->
erasesize) {
2319 NS_ERR(
"overridesize is too big\n");
2324 nsmtd->
size = new_size;
2330 if ((retval = setup_wear_reporting(nsmtd)) != 0)
2333 if ((retval = init_nandsim(nsmtd)) != 0)
2339 if ((retval = parse_badblocks(nand, nsmtd)) != 0)
2367 static void __exit ns_cleanup_module(
void)