2 * NAND flash simulator.
4 * Author: Artem B. Bityuckiy <dedekind@oktetlabs.ru>, <dedekind@infradead.org>
6 * Copyright (C) 2004 Nokia Corporation
8 * Note: NS means "NAND Simulator".
9 * Note: Input means input TO flash chip, output means output FROM chip.
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2, or (at your option) any later
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
19 * Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
26 #include <linux/init.h>
27 #include <linux/types.h>
28 #include <linux/module.h>
29 #include <linux/moduleparam.h>
30 #include <linux/vmalloc.h>
31 #include <linux/math64.h>
32 #include <linux/slab.h>
33 #include <linux/errno.h>
34 #include <linux/string.h>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/nand.h>
37 #include <linux/mtd/nand_bch.h>
38 #include <linux/mtd/partitions.h>
39 #include <linux/delay.h>
40 #include <linux/list.h>
41 #include <linux/random.h>
42 #include <linux/sched.h>
43 #include <linux/sched/mm.h>
45 #include <linux/pagemap.h>
46 #include <linux/seq_file.h>
47 #include <linux/debugfs.h>
49 /* Default simulator parameters values */
50 #if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
51 !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
52 !defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
53 !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
54 #define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
55 #define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
56 #define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF /* No byte */
57 #define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */
60 #ifndef CONFIG_NANDSIM_ACCESS_DELAY
61 #define CONFIG_NANDSIM_ACCESS_DELAY 25
63 #ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
64 #define CONFIG_NANDSIM_PROGRAMM_DELAY 200
66 #ifndef CONFIG_NANDSIM_ERASE_DELAY
67 #define CONFIG_NANDSIM_ERASE_DELAY 2
69 #ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
70 #define CONFIG_NANDSIM_OUTPUT_CYCLE 40
72 #ifndef CONFIG_NANDSIM_INPUT_CYCLE
73 #define CONFIG_NANDSIM_INPUT_CYCLE 50
75 #ifndef CONFIG_NANDSIM_BUS_WIDTH
76 #define CONFIG_NANDSIM_BUS_WIDTH 8
78 #ifndef CONFIG_NANDSIM_DO_DELAYS
79 #define CONFIG_NANDSIM_DO_DELAYS 0
81 #ifndef CONFIG_NANDSIM_LOG
82 #define CONFIG_NANDSIM_LOG 0
84 #ifndef CONFIG_NANDSIM_DBG
85 #define CONFIG_NANDSIM_DBG 0
87 #ifndef CONFIG_NANDSIM_MAX_PARTS
88 #define CONFIG_NANDSIM_MAX_PARTS 32
91 static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
92 static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
93 static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
94 static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
95 static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
96 static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
97 static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
98 static uint log = CONFIG_NANDSIM_LOG;
99 static uint dbg = CONFIG_NANDSIM_DBG;
100 static unsigned long parts[CONFIG_NANDSIM_MAX_PARTS];
101 static unsigned int parts_num;
102 static char *badblocks = NULL;
103 static char *weakblocks = NULL;
104 static char *weakpages = NULL;
105 static unsigned int bitflips = 0;
106 static char *gravepages = NULL;
107 static unsigned int overridesize = 0;
108 static char *cache_file = NULL;
109 static unsigned int bbt;
110 static unsigned int bch;
111 static u_char id_bytes[8] = {
112 [0] = CONFIG_NANDSIM_FIRST_ID_BYTE,
113 [1] = CONFIG_NANDSIM_SECOND_ID_BYTE,
114 [2] = CONFIG_NANDSIM_THIRD_ID_BYTE,
115 [3] = CONFIG_NANDSIM_FOURTH_ID_BYTE,
119 module_param_array(id_bytes, byte, NULL, 0400);
120 module_param_named(first_id_byte, id_bytes[0], byte, 0400);
121 module_param_named(second_id_byte, id_bytes[1], byte, 0400);
122 module_param_named(third_id_byte, id_bytes[2], byte, 0400);
123 module_param_named(fourth_id_byte, id_bytes[3], byte, 0400);
124 module_param(access_delay, uint, 0400);
125 module_param(programm_delay, uint, 0400);
126 module_param(erase_delay, uint, 0400);
127 module_param(output_cycle, uint, 0400);
128 module_param(input_cycle, uint, 0400);
129 module_param(bus_width, uint, 0400);
130 module_param(do_delays, uint, 0400);
131 module_param(log, uint, 0400);
132 module_param(dbg, uint, 0400);
133 module_param_array(parts, ulong, &parts_num, 0400);
134 module_param(badblocks, charp, 0400);
135 module_param(weakblocks, charp, 0400);
136 module_param(weakpages, charp, 0400);
137 module_param(bitflips, uint, 0400);
138 module_param(gravepages, charp, 0400);
139 module_param(overridesize, uint, 0400);
140 module_param(cache_file, charp, 0400);
141 module_param(bbt, uint, 0400);
142 module_param(bch, uint, 0400);
144 MODULE_PARM_DESC(id_bytes, "The ID bytes returned by NAND Flash 'read ID' command");
145 MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID) (obsolete)");
146 MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID) (obsolete)");
147 MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command (obsolete)");
148 MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command (obsolete)");
149 MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
150 MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
151 MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
152 MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
153 MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
154 MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
155 MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
156 MODULE_PARM_DESC(log, "Perform logging if not zero");
157 MODULE_PARM_DESC(dbg, "Output debug information if not zero");
158 MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas");
159 /* Page and erase block positions for the following parameters are independent of any partitions */
160 MODULE_PARM_DESC(badblocks, "Erase blocks that are initially marked bad, separated by commas");
161 MODULE_PARM_DESC(weakblocks, "Weak erase blocks [: remaining erase cycles (defaults to 3)]"
162 " separated by commas e.g. 113:2 means eb 113"
163 " can be erased only twice before failing");
164 MODULE_PARM_DESC(weakpages, "Weak pages [: maximum writes (defaults to 3)]"
165 " separated by commas e.g. 1401:2 means page 1401"
166 " can be written only twice before failing");
167 MODULE_PARM_DESC(bitflips, "Maximum number of random bit flips per page (zero by default)");
168 MODULE_PARM_DESC(gravepages, "Pages that lose data [: maximum reads (defaults to 3)]"
169 " separated by commas e.g. 1401:2 means page 1401"
170 " can be read only twice before failing");
171 MODULE_PARM_DESC(overridesize, "Specifies the NAND Flash size overriding the ID bytes. "
172 "The size is specified in erase blocks and as the exponent of a power of two"
173 " e.g. 5 means a size of 32 erase blocks");
174 MODULE_PARM_DESC(cache_file, "File to use to cache nand pages instead of memory");
175 MODULE_PARM_DESC(bbt, "0 OOB, 1 BBT with marker in OOB, 2 BBT with marker in data area");
176 MODULE_PARM_DESC(bch, "Enable BCH ecc and set how many bits should "
177 "be correctable in 512-byte blocks");
179 /* The largest possible page size */
180 #define NS_LARGEST_PAGE_SIZE 4096
182 /* The prefix for simulator output */
183 #define NS_OUTPUT_PREFIX "[nandsim]"
185 /* Simulator's output macros (logging, debugging, warning, error) */
186 #define NS_LOG(args...) \
187 do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0)
188 #define NS_DBG(args...) \
189 do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
190 #define NS_WARN(args...) \
191 do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warning: " args); } while(0)
192 #define NS_ERR(args...) \
193 do { printk(KERN_ERR NS_OUTPUT_PREFIX " error: " args); } while(0)
194 #define NS_INFO(args...) \
195 do { printk(KERN_INFO NS_OUTPUT_PREFIX " " args); } while(0)
197 /* Busy-wait delay macros (microseconds, milliseconds) */
198 #define NS_UDELAY(us) \
199 do { if (do_delays) udelay(us); } while(0)
200 #define NS_MDELAY(us) \
201 do { if (do_delays) mdelay(us); } while(0)
203 /* Is the nandsim structure initialized ? */
204 #define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
206 /* Good operation completion status */
207 #define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
209 /* Operation failed completion status */
210 #define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
212 /* Calculate the page offset in flash RAM image by (row, column) address */
213 #define NS_RAW_OFFSET(ns) \
214 (((ns)->regs.row * (ns)->geom.pgszoob) + (ns)->regs.column)
216 /* Calculate the OOB offset in flash RAM image by (row, column) address */
217 #define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
219 /* After a command is input, the simulator goes to one of the following states */
220 #define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */
221 #define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */
222 #define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
223 #define STATE_CMD_PAGEPROG 0x00000004 /* start page program */
224 #define STATE_CMD_READOOB 0x00000005 /* read OOB area */
225 #define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
226 #define STATE_CMD_STATUS 0x00000007 /* read status */
227 #define STATE_CMD_SEQIN 0x00000009 /* sequential data input */
228 #define STATE_CMD_READID 0x0000000A /* read ID */
229 #define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
230 #define STATE_CMD_RESET 0x0000000C /* reset */
231 #define STATE_CMD_RNDOUT 0x0000000D /* random output command */
232 #define STATE_CMD_RNDOUTSTART 0x0000000E /* random output start command */
233 #define STATE_CMD_MASK 0x0000000F /* command states mask */
235 /* After an address is input, the simulator goes to one of these states */
236 #define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
237 #define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
238 #define STATE_ADDR_COLUMN 0x00000030 /* column address was accepted */
239 #define STATE_ADDR_ZERO 0x00000040 /* one byte zero address was accepted */
240 #define STATE_ADDR_MASK 0x00000070 /* address states mask */
242 /* During data input/output the simulator is in these states */
243 #define STATE_DATAIN 0x00000100 /* waiting for data input */
244 #define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
246 #define STATE_DATAOUT 0x00001000 /* waiting for page data output */
247 #define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */
248 #define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */
249 #define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
251 /* Previous operation is done, ready to accept new requests */
252 #define STATE_READY 0x00000000
254 /* This state is used to mark that the next state isn't known yet */
255 #define STATE_UNKNOWN 0x10000000
257 /* Simulator's actions bit masks */
258 #define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */
259 #define ACTION_PRGPAGE 0x00200000 /* program the internal buffer to flash */
260 #define ACTION_SECERASE 0x00300000 /* erase sector */
261 #define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */
262 #define ACTION_HALFOFF 0x00500000 /* add to address half of page */
263 #define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */
264 #define ACTION_MASK 0x00700000 /* action mask */
266 #define NS_OPER_NUM 13 /* Number of operations supported by the simulator */
267 #define NS_OPER_STATES 6 /* Maximum number of states in operation */
269 #define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
270 #define OPT_PAGE512 0x00000002 /* 512-byte page chips */
271 #define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
272 #define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
273 #define OPT_PAGE4096 0x00000080 /* 4096-byte page chips */
274 #define OPT_LARGEPAGE (OPT_PAGE2048 | OPT_PAGE4096) /* 2048 & 4096-byte page chips */
275 #define OPT_SMALLPAGE (OPT_PAGE512) /* 512-byte page chips */
277 /* Remove action bits from state */
278 #define NS_STATE(x) ((x) & ~ACTION_MASK)
281 * Maximum previous states which need to be saved. Currently saving is
282 * only needed for page program operation with preceded read command
283 * (which is only valid for 512-byte pages).
285 #define NS_MAX_PREVSTATES 1
287 /* Maximum page cache pages needed to read or write a NAND page to the cache_file */
288 #define NS_MAX_HELD_PAGES 16
290 struct nandsim_debug_info {
291 struct dentry *dfs_root;
292 struct dentry *dfs_wear_report;
296 * A union to represent flash memory contents and flash buffer.
299 u_char *byte; /* for byte access */
300 uint16_t *word; /* for 16-bit word access */
304 * The structure which describes all the internal simulator data.
307 struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
308 unsigned int nbparts;
310 uint busw; /* flash chip bus width (8 or 16) */
311 u_char ids[8]; /* chip's ID bytes */
312 uint32_t options; /* chip's characteristic bits */
313 uint32_t state; /* current chip state */
314 uint32_t nxstate; /* next expected state */
316 uint32_t *op; /* current operation, NULL operations isn't known yet */
317 uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
318 uint16_t npstates; /* number of previous states saved */
319 uint16_t stateidx; /* current state index */
321 /* The simulated NAND flash pages array */
324 /* Slab allocator for nand pages */
325 struct kmem_cache *nand_pages_slab;
327 /* Internal buffer of page + OOB size bytes */
330 /* NAND flash "geometry" */
332 uint64_t totsz; /* total flash size, bytes */
333 uint32_t secsz; /* flash sector (erase block) size, bytes */
334 uint pgsz; /* NAND flash page size, bytes */
335 uint oobsz; /* page OOB area size, bytes */
336 uint64_t totszoob; /* total flash size including OOB, bytes */
337 uint pgszoob; /* page size including OOB , bytes*/
338 uint secszoob; /* sector size including OOB, bytes */
339 uint pgnum; /* total number of pages */
340 uint pgsec; /* number of pages per sector */
341 uint secshift; /* bits number in sector size */
342 uint pgshift; /* bits number in page size */
343 uint pgaddrbytes; /* bytes per page address */
344 uint secaddrbytes; /* bytes per sector address */
345 uint idbytes; /* the number ID bytes that this chip outputs */
348 /* NAND flash internal registers */
350 unsigned command; /* the command register */
351 u_char status; /* the status register */
352 uint row; /* the page number */
353 uint column; /* the offset within page */
354 uint count; /* internal counter */
355 uint num; /* number of bytes which must be processed */
356 uint off; /* fixed page offset */
359 /* NAND flash lines state */
361 int ce; /* chip Enable */
362 int cle; /* command Latch Enable */
363 int ale; /* address Latch Enable */
364 int wp; /* write Protect */
367 /* Fields needed when using a cache file */
368 struct file *cfile; /* Open file */
369 unsigned long *pages_written; /* Which pages have been written */
371 struct page *held_pages[NS_MAX_HELD_PAGES];
374 struct nandsim_debug_info dbg;
378 * Operations array. To perform any operation the simulator must pass
379 * through the correspondent states chain.
381 static struct nandsim_operations {
382 uint32_t reqopts; /* options which are required to perform the operation */
383 uint32_t states[NS_OPER_STATES]; /* operation's states */
384 } ops[NS_OPER_NUM] = {
385 /* Read page + OOB from the beginning */
386 {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
387 STATE_DATAOUT, STATE_READY}},
388 /* Read page + OOB from the second half */
389 {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
390 STATE_DATAOUT, STATE_READY}},
392 {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
393 STATE_DATAOUT, STATE_READY}},
394 /* Program page starting from the beginning */
395 {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
396 STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
397 /* Program page starting from the beginning */
398 {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
399 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
400 /* Program page starting from the second half */
401 {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
402 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
404 {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
405 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
407 {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}},
409 {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}},
411 {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
412 /* Large page devices read page */
413 {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
414 STATE_DATAOUT, STATE_READY}},
415 /* Large page devices random page read */
416 {OPT_LARGEPAGE, {STATE_CMD_RNDOUT, STATE_ADDR_COLUMN, STATE_CMD_RNDOUTSTART | ACTION_CPY,
417 STATE_DATAOUT, STATE_READY}},
421 struct list_head list;
422 unsigned int erase_block_no;
423 unsigned int max_erases;
424 unsigned int erases_done;
427 static LIST_HEAD(weak_blocks);
430 struct list_head list;
431 unsigned int page_no;
432 unsigned int max_writes;
433 unsigned int writes_done;
436 static LIST_HEAD(weak_pages);
439 struct list_head list;
440 unsigned int page_no;
441 unsigned int max_reads;
442 unsigned int reads_done;
445 static LIST_HEAD(grave_pages);
447 static unsigned long *erase_block_wear = NULL;
448 static unsigned int wear_eb_count = 0;
449 static unsigned long total_wear = 0;
451 /* MTD structure for NAND controller */
452 static struct mtd_info *nsmtd;
454 static int nandsim_debugfs_show(struct seq_file *m, void *private)
456 unsigned long wmin = -1, wmax = 0, avg;
457 unsigned long deciles[10], decile_max[10], tot = 0;
460 /* Calc wear stats */
461 for (i = 0; i < wear_eb_count; ++i) {
462 unsigned long wear = erase_block_wear[i];
470 for (i = 0; i < 9; ++i) {
472 decile_max[i] = (wmax * (i + 1) + 5) / 10;
475 decile_max[9] = wmax;
476 for (i = 0; i < wear_eb_count; ++i) {
478 unsigned long wear = erase_block_wear[i];
479 for (d = 0; d < 10; ++d)
480 if (wear <= decile_max[d]) {
485 avg = tot / wear_eb_count;
487 /* Output wear report */
488 seq_printf(m, "Total numbers of erases: %lu\n", tot);
489 seq_printf(m, "Number of erase blocks: %u\n", wear_eb_count);
490 seq_printf(m, "Average number of erases: %lu\n", avg);
491 seq_printf(m, "Maximum number of erases: %lu\n", wmax);
492 seq_printf(m, "Minimum number of erases: %lu\n", wmin);
493 for (i = 0; i < 10; ++i) {
494 unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
495 if (from > decile_max[i])
497 seq_printf(m, "Number of ebs with erase counts from %lu to %lu : %lu\n",
506 static int nandsim_debugfs_open(struct inode *inode, struct file *file)
508 return single_open(file, nandsim_debugfs_show, inode->i_private);
511 static const struct file_operations dfs_fops = {
512 .open = nandsim_debugfs_open,
515 .release = single_release,
519 * nandsim_debugfs_create - initialize debugfs
520 * @dev: nandsim device description object
522 * This function creates all debugfs files for UBI device @ubi. Returns zero in
523 * case of success and a negative error code in case of failure.
525 static int nandsim_debugfs_create(struct nandsim *dev)
527 struct nandsim_debug_info *dbg = &dev->dbg;
530 if (!IS_ENABLED(CONFIG_DEBUG_FS))
533 dent = debugfs_create_dir("nandsim", NULL);
535 NS_ERR("cannot create \"nandsim\" debugfs directory\n");
538 dbg->dfs_root = dent;
540 dent = debugfs_create_file("wear_report", S_IRUSR,
541 dbg->dfs_root, dev, &dfs_fops);
544 dbg->dfs_wear_report = dent;
549 debugfs_remove_recursive(dbg->dfs_root);
554 * nandsim_debugfs_remove - destroy all debugfs files
556 static void nandsim_debugfs_remove(struct nandsim *ns)
558 if (IS_ENABLED(CONFIG_DEBUG_FS))
559 debugfs_remove_recursive(ns->dbg.dfs_root);
563 * Allocate array of page pointers, create slab allocation for an array
564 * and initialize the array by NULL pointers.
566 * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
568 static int __init alloc_device(struct nandsim *ns)
574 cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
576 return PTR_ERR(cfile);
577 if (!(cfile->f_mode & FMODE_CAN_READ)) {
578 NS_ERR("alloc_device: cache file not readable\n");
582 if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
583 NS_ERR("alloc_device: cache file not writeable\n");
587 ns->pages_written = vzalloc(BITS_TO_LONGS(ns->geom.pgnum) *
588 sizeof(unsigned long));
589 if (!ns->pages_written) {
590 NS_ERR("alloc_device: unable to allocate pages written array\n");
594 ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
596 NS_ERR("alloc_device: unable to allocate file buf\n");
604 ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
606 NS_ERR("alloc_device: unable to allocate page array\n");
609 for (i = 0; i < ns->geom.pgnum; i++) {
610 ns->pages[i].byte = NULL;
612 ns->nand_pages_slab = kmem_cache_create("nandsim",
613 ns->geom.pgszoob, 0, 0, NULL);
614 if (!ns->nand_pages_slab) {
615 NS_ERR("cache_create: unable to create kmem_cache\n");
622 vfree(ns->pages_written);
624 filp_close(cfile, NULL);
629 * Free any allocated pages, and free the array of page pointers.
631 static void free_device(struct nandsim *ns)
637 vfree(ns->pages_written);
638 filp_close(ns->cfile, NULL);
643 for (i = 0; i < ns->geom.pgnum; i++) {
644 if (ns->pages[i].byte)
645 kmem_cache_free(ns->nand_pages_slab,
648 kmem_cache_destroy(ns->nand_pages_slab);
653 static char __init *get_partition_name(int i)
655 return kasprintf(GFP_KERNEL, "NAND simulator partition %d", i);
659 * Initialize the nandsim structure.
661 * RETURNS: 0 if success, -ERRNO if failure.
663 static int __init init_nandsim(struct mtd_info *mtd)
665 struct nand_chip *chip = mtd_to_nand(mtd);
666 struct nandsim *ns = nand_get_controller_data(chip);
669 uint64_t next_offset;
671 if (NS_IS_INITIALIZED(ns)) {
672 NS_ERR("init_nandsim: nandsim is already initialized\n");
676 /* Force mtd to not do delays */
677 chip->chip_delay = 0;
679 /* Initialize the NAND flash parameters */
680 ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
681 ns->geom.totsz = mtd->size;
682 ns->geom.pgsz = mtd->writesize;
683 ns->geom.oobsz = mtd->oobsize;
684 ns->geom.secsz = mtd->erasesize;
685 ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
686 ns->geom.pgnum = div_u64(ns->geom.totsz, ns->geom.pgsz);
687 ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz;
688 ns->geom.secshift = ffs(ns->geom.secsz) - 1;
689 ns->geom.pgshift = chip->page_shift;
690 ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
691 ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
694 if (ns->geom.pgsz == 512) {
695 ns->options |= OPT_PAGE512;
697 ns->options |= OPT_PAGE512_8BIT;
698 } else if (ns->geom.pgsz == 2048) {
699 ns->options |= OPT_PAGE2048;
700 } else if (ns->geom.pgsz == 4096) {
701 ns->options |= OPT_PAGE4096;
703 NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
707 if (ns->options & OPT_SMALLPAGE) {
708 if (ns->geom.totsz <= (32 << 20)) {
709 ns->geom.pgaddrbytes = 3;
710 ns->geom.secaddrbytes = 2;
712 ns->geom.pgaddrbytes = 4;
713 ns->geom.secaddrbytes = 3;
716 if (ns->geom.totsz <= (128 << 20)) {
717 ns->geom.pgaddrbytes = 4;
718 ns->geom.secaddrbytes = 2;
720 ns->geom.pgaddrbytes = 5;
721 ns->geom.secaddrbytes = 3;
725 /* Fill the partition_info structure */
726 if (parts_num > ARRAY_SIZE(ns->partitions)) {
727 NS_ERR("too many partitions.\n");
730 remains = ns->geom.totsz;
732 for (i = 0; i < parts_num; ++i) {
733 uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
735 if (!part_sz || part_sz > remains) {
736 NS_ERR("bad partition size.\n");
739 ns->partitions[i].name = get_partition_name(i);
740 if (!ns->partitions[i].name) {
741 NS_ERR("unable to allocate memory.\n");
744 ns->partitions[i].offset = next_offset;
745 ns->partitions[i].size = part_sz;
746 next_offset += ns->partitions[i].size;
747 remains -= ns->partitions[i].size;
749 ns->nbparts = parts_num;
751 if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
752 NS_ERR("too many partitions.\n");
755 ns->partitions[i].name = get_partition_name(i);
756 if (!ns->partitions[i].name) {
757 NS_ERR("unable to allocate memory.\n");
760 ns->partitions[i].offset = next_offset;
761 ns->partitions[i].size = remains;
766 NS_WARN("16-bit flashes support wasn't tested\n");
768 printk("flash size: %llu MiB\n",
769 (unsigned long long)ns->geom.totsz >> 20);
770 printk("page size: %u bytes\n", ns->geom.pgsz);
771 printk("OOB area size: %u bytes\n", ns->geom.oobsz);
772 printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
773 printk("pages number: %u\n", ns->geom.pgnum);
774 printk("pages per sector: %u\n", ns->geom.pgsec);
775 printk("bus width: %u\n", ns->busw);
776 printk("bits in sector size: %u\n", ns->geom.secshift);
777 printk("bits in page size: %u\n", ns->geom.pgshift);
778 printk("bits in OOB size: %u\n", ffs(ns->geom.oobsz) - 1);
779 printk("flash size with OOB: %llu KiB\n",
780 (unsigned long long)ns->geom.totszoob >> 10);
781 printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
782 printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
783 printk("options: %#x\n", ns->options);
785 if ((ret = alloc_device(ns)) != 0)
788 /* Allocate / initialize the internal buffer */
789 ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
791 NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
795 memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
801 * Free the nandsim structure.
803 static void free_nandsim(struct nandsim *ns)
811 static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
815 unsigned int erase_block_no;
822 zero_ok = (*w == '0' ? 1 : 0);
823 erase_block_no = simple_strtoul(w, &w, 0);
824 if (!zero_ok && !erase_block_no) {
825 NS_ERR("invalid badblocks.\n");
828 offset = (loff_t)erase_block_no * ns->geom.secsz;
829 if (mtd_block_markbad(mtd, offset)) {
830 NS_ERR("invalid badblocks.\n");
839 static int parse_weakblocks(void)
843 unsigned int erase_block_no;
844 unsigned int max_erases;
845 struct weak_block *wb;
851 zero_ok = (*w == '0' ? 1 : 0);
852 erase_block_no = simple_strtoul(w, &w, 0);
853 if (!zero_ok && !erase_block_no) {
854 NS_ERR("invalid weakblocks.\n");
860 max_erases = simple_strtoul(w, &w, 0);
864 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
866 NS_ERR("unable to allocate memory.\n");
869 wb->erase_block_no = erase_block_no;
870 wb->max_erases = max_erases;
871 list_add(&wb->list, &weak_blocks);
876 static int erase_error(unsigned int erase_block_no)
878 struct weak_block *wb;
880 list_for_each_entry(wb, &weak_blocks, list)
881 if (wb->erase_block_no == erase_block_no) {
882 if (wb->erases_done >= wb->max_erases)
884 wb->erases_done += 1;
890 static int parse_weakpages(void)
894 unsigned int page_no;
895 unsigned int max_writes;
896 struct weak_page *wp;
902 zero_ok = (*w == '0' ? 1 : 0);
903 page_no = simple_strtoul(w, &w, 0);
904 if (!zero_ok && !page_no) {
905 NS_ERR("invalid weakpagess.\n");
911 max_writes = simple_strtoul(w, &w, 0);
915 wp = kzalloc(sizeof(*wp), GFP_KERNEL);
917 NS_ERR("unable to allocate memory.\n");
920 wp->page_no = page_no;
921 wp->max_writes = max_writes;
922 list_add(&wp->list, &weak_pages);
927 static int write_error(unsigned int page_no)
929 struct weak_page *wp;
931 list_for_each_entry(wp, &weak_pages, list)
932 if (wp->page_no == page_no) {
933 if (wp->writes_done >= wp->max_writes)
935 wp->writes_done += 1;
941 static int parse_gravepages(void)
945 unsigned int page_no;
946 unsigned int max_reads;
947 struct grave_page *gp;
953 zero_ok = (*g == '0' ? 1 : 0);
954 page_no = simple_strtoul(g, &g, 0);
955 if (!zero_ok && !page_no) {
956 NS_ERR("invalid gravepagess.\n");
962 max_reads = simple_strtoul(g, &g, 0);
966 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
968 NS_ERR("unable to allocate memory.\n");
971 gp->page_no = page_no;
972 gp->max_reads = max_reads;
973 list_add(&gp->list, &grave_pages);
978 static int read_error(unsigned int page_no)
980 struct grave_page *gp;
982 list_for_each_entry(gp, &grave_pages, list)
983 if (gp->page_no == page_no) {
984 if (gp->reads_done >= gp->max_reads)
992 static void free_lists(void)
994 struct list_head *pos, *n;
995 list_for_each_safe(pos, n, &weak_blocks) {
997 kfree(list_entry(pos, struct weak_block, list));
999 list_for_each_safe(pos, n, &weak_pages) {
1001 kfree(list_entry(pos, struct weak_page, list));
1003 list_for_each_safe(pos, n, &grave_pages) {
1005 kfree(list_entry(pos, struct grave_page, list));
1007 kfree(erase_block_wear);
1010 static int setup_wear_reporting(struct mtd_info *mtd)
1014 wear_eb_count = div_u64(mtd->size, mtd->erasesize);
1015 mem = wear_eb_count * sizeof(unsigned long);
1016 if (mem / sizeof(unsigned long) != wear_eb_count) {
1017 NS_ERR("Too many erase blocks for wear reporting\n");
1020 erase_block_wear = kzalloc(mem, GFP_KERNEL);
1021 if (!erase_block_wear) {
1022 NS_ERR("Too many erase blocks for wear reporting\n");
1028 static void update_wear(unsigned int erase_block_no)
1030 if (!erase_block_wear)
1034 * TODO: Notify this through a debugfs entry,
1035 * instead of showing an error message.
1037 if (total_wear == 0)
1038 NS_ERR("Erase counter total overflow\n");
1039 erase_block_wear[erase_block_no] += 1;
1040 if (erase_block_wear[erase_block_no] == 0)
1041 NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
1045 * Returns the string representation of 'state' state.
1047 static char *get_state_name(uint32_t state)
1049 switch (NS_STATE(state)) {
1050 case STATE_CMD_READ0:
1051 return "STATE_CMD_READ0";
1052 case STATE_CMD_READ1:
1053 return "STATE_CMD_READ1";
1054 case STATE_CMD_PAGEPROG:
1055 return "STATE_CMD_PAGEPROG";
1056 case STATE_CMD_READOOB:
1057 return "STATE_CMD_READOOB";
1058 case STATE_CMD_READSTART:
1059 return "STATE_CMD_READSTART";
1060 case STATE_CMD_ERASE1:
1061 return "STATE_CMD_ERASE1";
1062 case STATE_CMD_STATUS:
1063 return "STATE_CMD_STATUS";
1064 case STATE_CMD_SEQIN:
1065 return "STATE_CMD_SEQIN";
1066 case STATE_CMD_READID:
1067 return "STATE_CMD_READID";
1068 case STATE_CMD_ERASE2:
1069 return "STATE_CMD_ERASE2";
1070 case STATE_CMD_RESET:
1071 return "STATE_CMD_RESET";
1072 case STATE_CMD_RNDOUT:
1073 return "STATE_CMD_RNDOUT";
1074 case STATE_CMD_RNDOUTSTART:
1075 return "STATE_CMD_RNDOUTSTART";
1076 case STATE_ADDR_PAGE:
1077 return "STATE_ADDR_PAGE";
1078 case STATE_ADDR_SEC:
1079 return "STATE_ADDR_SEC";
1080 case STATE_ADDR_ZERO:
1081 return "STATE_ADDR_ZERO";
1082 case STATE_ADDR_COLUMN:
1083 return "STATE_ADDR_COLUMN";
1085 return "STATE_DATAIN";
1087 return "STATE_DATAOUT";
1088 case STATE_DATAOUT_ID:
1089 return "STATE_DATAOUT_ID";
1090 case STATE_DATAOUT_STATUS:
1091 return "STATE_DATAOUT_STATUS";
1093 return "STATE_READY";
1095 return "STATE_UNKNOWN";
1098 NS_ERR("get_state_name: unknown state, BUG\n");
1103 * Check if command is valid.
1105 * RETURNS: 1 if wrong command, 0 if right.
1107 static int check_command(int cmd)
1111 case NAND_CMD_READ0:
1112 case NAND_CMD_READ1:
1113 case NAND_CMD_READSTART:
1114 case NAND_CMD_PAGEPROG:
1115 case NAND_CMD_READOOB:
1116 case NAND_CMD_ERASE1:
1117 case NAND_CMD_STATUS:
1118 case NAND_CMD_SEQIN:
1119 case NAND_CMD_READID:
1120 case NAND_CMD_ERASE2:
1121 case NAND_CMD_RESET:
1122 case NAND_CMD_RNDOUT:
1123 case NAND_CMD_RNDOUTSTART:
1132 * Returns state after command is accepted by command number.
1134 static uint32_t get_state_by_command(unsigned command)
1137 case NAND_CMD_READ0:
1138 return STATE_CMD_READ0;
1139 case NAND_CMD_READ1:
1140 return STATE_CMD_READ1;
1141 case NAND_CMD_PAGEPROG:
1142 return STATE_CMD_PAGEPROG;
1143 case NAND_CMD_READSTART:
1144 return STATE_CMD_READSTART;
1145 case NAND_CMD_READOOB:
1146 return STATE_CMD_READOOB;
1147 case NAND_CMD_ERASE1:
1148 return STATE_CMD_ERASE1;
1149 case NAND_CMD_STATUS:
1150 return STATE_CMD_STATUS;
1151 case NAND_CMD_SEQIN:
1152 return STATE_CMD_SEQIN;
1153 case NAND_CMD_READID:
1154 return STATE_CMD_READID;
1155 case NAND_CMD_ERASE2:
1156 return STATE_CMD_ERASE2;
1157 case NAND_CMD_RESET:
1158 return STATE_CMD_RESET;
1159 case NAND_CMD_RNDOUT:
1160 return STATE_CMD_RNDOUT;
1161 case NAND_CMD_RNDOUTSTART:
1162 return STATE_CMD_RNDOUTSTART;
1165 NS_ERR("get_state_by_command: unknown command, BUG\n");
1170 * Move an address byte to the correspondent internal register.
1172 static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
1174 uint byte = (uint)bt;
1176 if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
1177 ns->regs.column |= (byte << 8 * ns->regs.count);
1179 ns->regs.row |= (byte << 8 * (ns->regs.count -
1180 ns->geom.pgaddrbytes +
1181 ns->geom.secaddrbytes));
1188 * Switch to STATE_READY state.
1190 static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
1192 NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
1194 ns->state = STATE_READY;
1195 ns->nxstate = STATE_UNKNOWN;
1203 ns->regs.column = 0;
1204 ns->regs.status = status;
1208 * If the operation isn't known yet, try to find it in the global array
1209 * of supported operations.
1211 * Operation can be unknown because of the following.
1212 * 1. New command was accepted and this is the first call to find the
1213 * correspondent states chain. In this case ns->npstates = 0;
1214 * 2. There are several operations which begin with the same command(s)
1215 * (for example program from the second half and read from the
1216 * second half operations both begin with the READ1 command). In this
1217 * case the ns->pstates[] array contains previous states.
1219 * Thus, the function tries to find operation containing the following
1220 * states (if the 'flag' parameter is 0):
1221 * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
1223 * If (one and only one) matching operation is found, it is accepted (
1224 * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
1227 * If there are several matches, the current state is pushed to the
1230 * The operation can be unknown only while commands are input to the chip.
1231 * As soon as address command is accepted, the operation must be known.
1232 * In such situation the function is called with 'flag' != 0, and the
1233 * operation is searched using the following pattern:
1234 * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
1236 * It is supposed that this pattern must either match one operation or
1237 * none. There can't be ambiguity in that case.
1239 * If no matches found, the function does the following:
1240 * 1. if there are saved states present, try to ignore them and search
1241 * again only using the last command. If nothing was found, switch
1242 * to the STATE_READY state.
1243 * 2. if there are no saved states, switch to the STATE_READY state.
1245 * RETURNS: -2 - no matched operations found.
1246 * -1 - several matches.
1247 * 0 - operation is found.
1249 static int find_operation(struct nandsim *ns, uint32_t flag)
1254 for (i = 0; i < NS_OPER_NUM; i++) {
1258 if (!(ns->options & ops[i].reqopts))
1259 /* Ignore operations we can't perform */
1263 if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
1266 if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
1270 for (j = 0; j < ns->npstates; j++)
1271 if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
1272 && (ns->options & ops[idx].reqopts)) {
1283 if (opsfound == 1) {
1285 ns->op = &ops[idx].states[0];
1288 * In this case the find_operation function was
1289 * called when address has just began input. But it isn't
1290 * yet fully input and the current state must
1291 * not be one of STATE_ADDR_*, but the STATE_ADDR_*
1292 * state must be the next state (ns->nxstate).
1294 ns->stateidx = ns->npstates - 1;
1296 ns->stateidx = ns->npstates;
1299 ns->state = ns->op[ns->stateidx];
1300 ns->nxstate = ns->op[ns->stateidx + 1];
1301 NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
1302 idx, get_state_name(ns->state), get_state_name(ns->nxstate));
1306 if (opsfound == 0) {
1307 /* Nothing was found. Try to ignore previous commands (if any) and search again */
1308 if (ns->npstates != 0) {
1309 NS_DBG("find_operation: no operation found, try again with state %s\n",
1310 get_state_name(ns->state));
1312 return find_operation(ns, 0);
1315 NS_DBG("find_operation: no operations found\n");
1316 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1321 /* This shouldn't happen */
1322 NS_DBG("find_operation: BUG, operation must be known if address is input\n");
1326 NS_DBG("find_operation: there is still ambiguity\n");
1328 ns->pstates[ns->npstates++] = ns->state;
1333 static void put_pages(struct nandsim *ns)
1337 for (i = 0; i < ns->held_cnt; i++)
1338 put_page(ns->held_pages[i]);
1341 /* Get page cache pages in advance to provide NOFS memory allocation */
1342 static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
1344 pgoff_t index, start_index, end_index;
1346 struct address_space *mapping = file->f_mapping;
1348 start_index = pos >> PAGE_SHIFT;
1349 end_index = (pos + count - 1) >> PAGE_SHIFT;
1350 if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
1353 for (index = start_index; index <= end_index; index++) {
1354 page = find_get_page(mapping, index);
1356 page = find_or_create_page(mapping, index, GFP_NOFS);
1358 write_inode_now(mapping->host, 1);
1359 page = find_or_create_page(mapping, index, GFP_NOFS);
1367 ns->held_pages[ns->held_cnt++] = page;
1372 static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
1376 unsigned int noreclaim_flag;
1378 err = get_pages(ns, file, count, pos);
1381 noreclaim_flag = memalloc_noreclaim_save();
1382 tx = kernel_read(file, pos, buf, count);
1383 memalloc_noreclaim_restore(noreclaim_flag);
1388 static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
1392 unsigned int noreclaim_flag;
1394 err = get_pages(ns, file, count, pos);
1397 noreclaim_flag = memalloc_noreclaim_save();
1398 tx = kernel_write(file, buf, count, pos);
1399 memalloc_noreclaim_restore(noreclaim_flag);
1405 * Returns a pointer to the current page.
1407 static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
1409 return &(ns->pages[ns->regs.row]);
1413 * Retuns a pointer to the current byte, within the current page.
1415 static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
1417 return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
1420 static int do_read_error(struct nandsim *ns, int num)
1422 unsigned int page_no = ns->regs.row;
1424 if (read_error(page_no)) {
1425 prandom_bytes(ns->buf.byte, num);
1426 NS_WARN("simulating read error in page %u\n", page_no);
1432 static void do_bit_flips(struct nandsim *ns, int num)
1434 if (bitflips && prandom_u32() < (1 << 22)) {
1437 flips = (prandom_u32() % (int) bitflips) + 1;
1439 int pos = prandom_u32() % (num * 8);
1440 ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
1441 NS_WARN("read_page: flipping bit %d in page %d "
1442 "reading from %d ecc: corrected=%u failed=%u\n",
1443 pos, ns->regs.row, ns->regs.column + ns->regs.off,
1444 nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
1450 * Fill the NAND buffer with data read from the specified page.
1452 static void read_page(struct nandsim *ns, int num)
1454 union ns_mem *mypage;
1457 if (!test_bit(ns->regs.row, ns->pages_written)) {
1458 NS_DBG("read_page: page %d not written\n", ns->regs.row);
1459 memset(ns->buf.byte, 0xFF, num);
1464 NS_DBG("read_page: page %d written, reading from %d\n",
1465 ns->regs.row, ns->regs.column + ns->regs.off);
1466 if (do_read_error(ns, num))
1468 pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
1469 tx = read_file(ns, ns->cfile, ns->buf.byte, num, pos);
1471 NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1474 do_bit_flips(ns, num);
1479 mypage = NS_GET_PAGE(ns);
1480 if (mypage->byte == NULL) {
1481 NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
1482 memset(ns->buf.byte, 0xFF, num);
1484 NS_DBG("read_page: page %d allocated, reading from %d\n",
1485 ns->regs.row, ns->regs.column + ns->regs.off);
1486 if (do_read_error(ns, num))
1488 memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
1489 do_bit_flips(ns, num);
1494 * Erase all pages in the specified sector.
1496 static void erase_sector(struct nandsim *ns)
1498 union ns_mem *mypage;
1502 for (i = 0; i < ns->geom.pgsec; i++)
1503 if (__test_and_clear_bit(ns->regs.row + i,
1504 ns->pages_written)) {
1505 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
1510 mypage = NS_GET_PAGE(ns);
1511 for (i = 0; i < ns->geom.pgsec; i++) {
1512 if (mypage->byte != NULL) {
1513 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
1514 kmem_cache_free(ns->nand_pages_slab, mypage->byte);
1515 mypage->byte = NULL;
1522 * Program the specified page with the contents from the NAND buffer.
1524 static int prog_page(struct nandsim *ns, int num)
1527 union ns_mem *mypage;
1535 NS_DBG("prog_page: writing page %d\n", ns->regs.row);
1536 pg_off = ns->file_buf + ns->regs.column + ns->regs.off;
1537 off = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
1538 if (!test_bit(ns->regs.row, ns->pages_written)) {
1540 memset(ns->file_buf, 0xff, ns->geom.pgszoob);
1543 tx = read_file(ns, ns->cfile, pg_off, num, off);
1545 NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1549 for (i = 0; i < num; i++)
1550 pg_off[i] &= ns->buf.byte[i];
1552 loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
1553 tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, pos);
1554 if (tx != ns->geom.pgszoob) {
1555 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1558 __set_bit(ns->regs.row, ns->pages_written);
1560 tx = write_file(ns, ns->cfile, pg_off, num, off);
1562 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1569 mypage = NS_GET_PAGE(ns);
1570 if (mypage->byte == NULL) {
1571 NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
1573 * We allocate memory with GFP_NOFS because a flash FS may
1574 * utilize this. If it is holding an FS lock, then gets here,
1575 * then kernel memory alloc runs writeback which goes to the FS
1576 * again and deadlocks. This was seen in practice.
1578 mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
1579 if (mypage->byte == NULL) {
1580 NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
1583 memset(mypage->byte, 0xFF, ns->geom.pgszoob);
1586 pg_off = NS_PAGE_BYTE_OFF(ns);
1587 for (i = 0; i < num; i++)
1588 pg_off[i] &= ns->buf.byte[i];
1594 * If state has any action bit, perform this action.
1596 * RETURNS: 0 if success, -1 if error.
1598 static int do_state_action(struct nandsim *ns, uint32_t action)
1601 int busdiv = ns->busw == 8 ? 1 : 2;
1602 unsigned int erase_block_no, page_no;
1604 action &= ACTION_MASK;
1606 /* Check that page address input is correct */
1607 if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
1608 NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
1616 * Copy page data to the internal buffer.
1619 /* Column shouldn't be very large */
1620 if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
1621 NS_ERR("do_state_action: column number is too large\n");
1624 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1627 NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
1628 num, NS_RAW_OFFSET(ns) + ns->regs.off);
1630 if (ns->regs.off == 0)
1631 NS_LOG("read page %d\n", ns->regs.row);
1632 else if (ns->regs.off < ns->geom.pgsz)
1633 NS_LOG("read page %d (second half)\n", ns->regs.row);
1635 NS_LOG("read OOB of page %d\n", ns->regs.row);
1637 NS_UDELAY(access_delay);
1638 NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
1642 case ACTION_SECERASE:
1648 NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
1652 if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
1653 || (ns->regs.row & ~(ns->geom.secsz - 1))) {
1654 NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
1658 ns->regs.row = (ns->regs.row <<
1659 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
1660 ns->regs.column = 0;
1662 erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
1664 NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
1665 ns->regs.row, NS_RAW_OFFSET(ns));
1666 NS_LOG("erase sector %u\n", erase_block_no);
1670 NS_MDELAY(erase_delay);
1672 if (erase_block_wear)
1673 update_wear(erase_block_no);
1675 if (erase_error(erase_block_no)) {
1676 NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
1682 case ACTION_PRGPAGE:
1684 * Program page - move internal buffer data to the page.
1688 NS_WARN("do_state_action: device is write-protected, programm\n");
1692 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1693 if (num != ns->regs.count) {
1694 NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
1695 ns->regs.count, num);
1699 if (prog_page(ns, num) == -1)
1702 page_no = ns->regs.row;
1704 NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
1705 num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
1706 NS_LOG("programm page %d\n", ns->regs.row);
1708 NS_UDELAY(programm_delay);
1709 NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
1711 if (write_error(page_no)) {
1712 NS_WARN("simulating write failure in page %u\n", page_no);
1718 case ACTION_ZEROOFF:
1719 NS_DBG("do_state_action: set internal offset to 0\n");
1723 case ACTION_HALFOFF:
1724 if (!(ns->options & OPT_PAGE512_8BIT)) {
1725 NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
1726 "byte page size 8x chips\n");
1729 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
1730 ns->regs.off = ns->geom.pgsz/2;
1734 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
1735 ns->regs.off = ns->geom.pgsz;
1739 NS_DBG("do_state_action: BUG! unknown action\n");
1746 * Switch simulator's state.
1748 static void switch_state(struct nandsim *ns)
1752 * The current operation have already been identified.
1753 * Just follow the states chain.
1757 ns->state = ns->nxstate;
1758 ns->nxstate = ns->op[ns->stateidx + 1];
1760 NS_DBG("switch_state: operation is known, switch to the next state, "
1761 "state: %s, nxstate: %s\n",
1762 get_state_name(ns->state), get_state_name(ns->nxstate));
1764 /* See, whether we need to do some action */
1765 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1766 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1772 * We don't yet know which operation we perform.
1773 * Try to identify it.
1777 * The only event causing the switch_state function to
1778 * be called with yet unknown operation is new command.
1780 ns->state = get_state_by_command(ns->regs.command);
1782 NS_DBG("switch_state: operation is unknown, try to find it\n");
1784 if (find_operation(ns, 0) != 0)
1787 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1788 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1793 /* For 16x devices column means the page offset in words */
1794 if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
1795 NS_DBG("switch_state: double the column number for 16x device\n");
1796 ns->regs.column <<= 1;
1799 if (NS_STATE(ns->nxstate) == STATE_READY) {
1801 * The current state is the last. Return to STATE_READY
1804 u_char status = NS_STATUS_OK(ns);
1806 /* In case of data states, see if all bytes were input/output */
1807 if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
1808 && ns->regs.count != ns->regs.num) {
1809 NS_WARN("switch_state: not all bytes were processed, %d left\n",
1810 ns->regs.num - ns->regs.count);
1811 status = NS_STATUS_FAILED(ns);
1814 NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
1816 switch_to_ready_state(ns, status);
1819 } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
1821 * If the next state is data input/output, switch to it now
1824 ns->state = ns->nxstate;
1825 ns->nxstate = ns->op[++ns->stateidx + 1];
1826 ns->regs.num = ns->regs.count = 0;
1828 NS_DBG("switch_state: the next state is data I/O, switch, "
1829 "state: %s, nxstate: %s\n",
1830 get_state_name(ns->state), get_state_name(ns->nxstate));
1833 * Set the internal register to the count of bytes which
1834 * are expected to be input or output
1836 switch (NS_STATE(ns->state)) {
1839 ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1842 case STATE_DATAOUT_ID:
1843 ns->regs.num = ns->geom.idbytes;
1846 case STATE_DATAOUT_STATUS:
1847 ns->regs.count = ns->regs.num = 0;
1851 NS_ERR("switch_state: BUG! unknown data state\n");
1854 } else if (ns->nxstate & STATE_ADDR_MASK) {
1856 * If the next state is address input, set the internal
1857 * register to the number of expected address bytes
1862 switch (NS_STATE(ns->nxstate)) {
1863 case STATE_ADDR_PAGE:
1864 ns->regs.num = ns->geom.pgaddrbytes;
1867 case STATE_ADDR_SEC:
1868 ns->regs.num = ns->geom.secaddrbytes;
1871 case STATE_ADDR_ZERO:
1875 case STATE_ADDR_COLUMN:
1876 /* Column address is always 2 bytes */
1877 ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
1881 NS_ERR("switch_state: BUG! unknown address state\n");
1885 * Just reset internal counters.
1893 static u_char ns_nand_read_byte(struct mtd_info *mtd)
1895 struct nand_chip *chip = mtd_to_nand(mtd);
1896 struct nandsim *ns = nand_get_controller_data(chip);
1899 /* Sanity and correctness checks */
1900 if (!ns->lines.ce) {
1901 NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
1904 if (ns->lines.ale || ns->lines.cle) {
1905 NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
1908 if (!(ns->state & STATE_DATAOUT_MASK)) {
1909 NS_WARN("read_byte: unexpected data output cycle, state is %s "
1910 "return %#x\n", get_state_name(ns->state), (uint)outb);
1914 /* Status register may be read as many times as it is wanted */
1915 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
1916 NS_DBG("read_byte: return %#x status\n", ns->regs.status);
1917 return ns->regs.status;
1920 /* Check if there is any data in the internal buffer which may be read */
1921 if (ns->regs.count == ns->regs.num) {
1922 NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
1926 switch (NS_STATE(ns->state)) {
1928 if (ns->busw == 8) {
1929 outb = ns->buf.byte[ns->regs.count];
1930 ns->regs.count += 1;
1932 outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
1933 ns->regs.count += 2;
1936 case STATE_DATAOUT_ID:
1937 NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
1938 outb = ns->ids[ns->regs.count];
1939 ns->regs.count += 1;
1945 if (ns->regs.count == ns->regs.num) {
1946 NS_DBG("read_byte: all bytes were read\n");
1948 if (NS_STATE(ns->nxstate) == STATE_READY)
1955 static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
1957 struct nand_chip *chip = mtd_to_nand(mtd);
1958 struct nandsim *ns = nand_get_controller_data(chip);
1960 /* Sanity and correctness checks */
1961 if (!ns->lines.ce) {
1962 NS_ERR("write_byte: chip is disabled, ignore write\n");
1965 if (ns->lines.ale && ns->lines.cle) {
1966 NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
1970 if (ns->lines.cle == 1) {
1972 * The byte written is a command.
1975 if (byte == NAND_CMD_RESET) {
1976 NS_LOG("reset chip\n");
1977 switch_to_ready_state(ns, NS_STATUS_OK(ns));
1981 /* Check that the command byte is correct */
1982 if (check_command(byte)) {
1983 NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
1987 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
1988 || NS_STATE(ns->state) == STATE_DATAOUT) {
1989 int row = ns->regs.row;
1992 if (byte == NAND_CMD_RNDOUT)
1996 /* Check if chip is expecting command */
1997 if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
1998 /* Do not warn if only 2 id bytes are read */
1999 if (!(ns->regs.command == NAND_CMD_READID &&
2000 NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
2002 * We are in situation when something else (not command)
2003 * was expected but command was input. In this case ignore
2004 * previous command(s)/state(s) and accept the last one.
2006 NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
2007 "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
2009 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2012 NS_DBG("command byte corresponding to %s state accepted\n",
2013 get_state_name(get_state_by_command(byte)));
2014 ns->regs.command = byte;
2017 } else if (ns->lines.ale == 1) {
2019 * The byte written is an address.
2022 if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
2024 NS_DBG("write_byte: operation isn't known yet, identify it\n");
2026 if (find_operation(ns, 1) < 0)
2029 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
2030 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2035 switch (NS_STATE(ns->nxstate)) {
2036 case STATE_ADDR_PAGE:
2037 ns->regs.num = ns->geom.pgaddrbytes;
2039 case STATE_ADDR_SEC:
2040 ns->regs.num = ns->geom.secaddrbytes;
2042 case STATE_ADDR_ZERO:
2050 /* Check that chip is expecting address */
2051 if (!(ns->nxstate & STATE_ADDR_MASK)) {
2052 NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
2053 "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
2054 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2058 /* Check if this is expected byte */
2059 if (ns->regs.count == ns->regs.num) {
2060 NS_ERR("write_byte: no more address bytes expected\n");
2061 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2065 accept_addr_byte(ns, byte);
2067 ns->regs.count += 1;
2069 NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
2070 (uint)byte, ns->regs.count, ns->regs.num);
2072 if (ns->regs.count == ns->regs.num) {
2073 NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
2079 * The byte written is an input data.
2082 /* Check that chip is expecting data input */
2083 if (!(ns->state & STATE_DATAIN_MASK)) {
2084 NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
2085 "switch to %s\n", (uint)byte,
2086 get_state_name(ns->state), get_state_name(STATE_READY));
2087 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2091 /* Check if this is expected byte */
2092 if (ns->regs.count == ns->regs.num) {
2093 NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
2098 if (ns->busw == 8) {
2099 ns->buf.byte[ns->regs.count] = byte;
2100 ns->regs.count += 1;
2102 ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
2103 ns->regs.count += 2;
2110 static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
2112 struct nand_chip *chip = mtd_to_nand(mtd);
2113 struct nandsim *ns = nand_get_controller_data(chip);
2115 ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
2116 ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
2117 ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
2119 if (cmd != NAND_CMD_NONE)
2120 ns_nand_write_byte(mtd, cmd);
2123 static int ns_device_ready(struct mtd_info *mtd)
2125 NS_DBG("device_ready\n");
2129 static uint16_t ns_nand_read_word(struct mtd_info *mtd)
2131 struct nand_chip *chip = mtd_to_nand(mtd);
2133 NS_DBG("read_word\n");
2135 return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
2138 static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
2140 struct nand_chip *chip = mtd_to_nand(mtd);
2141 struct nandsim *ns = nand_get_controller_data(chip);
2143 /* Check that chip is expecting data input */
2144 if (!(ns->state & STATE_DATAIN_MASK)) {
2145 NS_ERR("write_buf: data input isn't expected, state is %s, "
2146 "switch to STATE_READY\n", get_state_name(ns->state));
2147 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2151 /* Check if these are expected bytes */
2152 if (ns->regs.count + len > ns->regs.num) {
2153 NS_ERR("write_buf: too many input bytes\n");
2154 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2158 memcpy(ns->buf.byte + ns->regs.count, buf, len);
2159 ns->regs.count += len;
2161 if (ns->regs.count == ns->regs.num) {
2162 NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
2166 static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
2168 struct nand_chip *chip = mtd_to_nand(mtd);
2169 struct nandsim *ns = nand_get_controller_data(chip);
2171 /* Sanity and correctness checks */
2172 if (!ns->lines.ce) {
2173 NS_ERR("read_buf: chip is disabled\n");
2176 if (ns->lines.ale || ns->lines.cle) {
2177 NS_ERR("read_buf: ALE or CLE pin is high\n");
2180 if (!(ns->state & STATE_DATAOUT_MASK)) {
2181 NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
2182 get_state_name(ns->state));
2186 if (NS_STATE(ns->state) != STATE_DATAOUT) {
2189 for (i = 0; i < len; i++)
2190 buf[i] = mtd_to_nand(mtd)->read_byte(mtd);
2195 /* Check if these are expected bytes */
2196 if (ns->regs.count + len > ns->regs.num) {
2197 NS_ERR("read_buf: too many bytes to read\n");
2198 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2202 memcpy(buf, ns->buf.byte + ns->regs.count, len);
2203 ns->regs.count += len;
2205 if (ns->regs.count == ns->regs.num) {
2206 if (NS_STATE(ns->nxstate) == STATE_READY)
2214 * Module initialization function
2216 static int __init ns_init_module(void)
2218 struct nand_chip *chip;
2219 struct nandsim *nand;
2220 int retval = -ENOMEM, i;
2222 if (bus_width != 8 && bus_width != 16) {
2223 NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
2227 /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
2228 chip = kzalloc(sizeof(struct nand_chip) + sizeof(struct nandsim),
2231 NS_ERR("unable to allocate core structures.\n");
2234 nsmtd = nand_to_mtd(chip);
2235 nand = (struct nandsim *)(chip + 1);
2236 nand_set_controller_data(chip, (void *)nand);
2239 * Register simulator's callbacks.
2241 chip->cmd_ctrl = ns_hwcontrol;
2242 chip->read_byte = ns_nand_read_byte;
2243 chip->dev_ready = ns_device_ready;
2244 chip->write_buf = ns_nand_write_buf;
2245 chip->read_buf = ns_nand_read_buf;
2246 chip->read_word = ns_nand_read_word;
2247 chip->ecc.mode = NAND_ECC_SOFT;
2248 chip->ecc.algo = NAND_ECC_HAMMING;
2249 /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
2250 /* and 'badblocks' parameters to work */
2251 chip->options |= NAND_SKIP_BBTSCAN;
2255 chip->bbt_options |= NAND_BBT_NO_OOB;
2257 chip->bbt_options |= NAND_BBT_USE_FLASH;
2261 NS_ERR("bbt has to be 0..2\n");
2266 * Perform minimum nandsim structure initialization to handle
2267 * the initial ID read command correctly
2269 if (id_bytes[6] != 0xFF || id_bytes[7] != 0xFF)
2270 nand->geom.idbytes = 8;
2271 else if (id_bytes[4] != 0xFF || id_bytes[5] != 0xFF)
2272 nand->geom.idbytes = 6;
2273 else if (id_bytes[2] != 0xFF || id_bytes[3] != 0xFF)
2274 nand->geom.idbytes = 4;
2276 nand->geom.idbytes = 2;
2277 nand->regs.status = NS_STATUS_OK(nand);
2278 nand->nxstate = STATE_UNKNOWN;
2279 nand->options |= OPT_PAGE512; /* temporary value */
2280 memcpy(nand->ids, id_bytes, sizeof(nand->ids));
2281 if (bus_width == 16) {
2283 chip->options |= NAND_BUSWIDTH_16;
2286 nsmtd->owner = THIS_MODULE;
2288 if ((retval = parse_weakblocks()) != 0)
2291 if ((retval = parse_weakpages()) != 0)
2294 if ((retval = parse_gravepages()) != 0)
2297 retval = nand_scan_ident(nsmtd, 1, NULL);
2299 NS_ERR("cannot scan NAND Simulator device\n");
2304 unsigned int eccsteps, eccbytes;
2305 if (!mtd_nand_has_bch()) {
2306 NS_ERR("BCH ECC support is disabled\n");
2310 /* use 512-byte ecc blocks */
2311 eccsteps = nsmtd->writesize/512;
2312 eccbytes = (bch*13+7)/8;
2313 /* do not bother supporting small page devices */
2314 if ((nsmtd->oobsize < 64) || !eccsteps) {
2315 NS_ERR("bch not available on small page devices\n");
2319 if ((eccbytes*eccsteps+2) > nsmtd->oobsize) {
2320 NS_ERR("invalid bch value %u\n", bch);
2324 chip->ecc.mode = NAND_ECC_SOFT;
2325 chip->ecc.algo = NAND_ECC_BCH;
2326 chip->ecc.size = 512;
2327 chip->ecc.strength = bch;
2328 chip->ecc.bytes = eccbytes;
2329 NS_INFO("using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
2332 retval = nand_scan_tail(nsmtd);
2334 NS_ERR("can't register NAND Simulator\n");
2339 uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
2340 if (new_size >> overridesize != nsmtd->erasesize) {
2341 NS_ERR("overridesize is too big\n");
2345 /* N.B. This relies on nand_scan not doing anything with the size before we change it */
2346 nsmtd->size = new_size;
2347 chip->chipsize = new_size;
2348 chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
2349 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
2352 if ((retval = setup_wear_reporting(nsmtd)) != 0)
2355 if ((retval = nandsim_debugfs_create(nand)) != 0)
2358 if ((retval = init_nandsim(nsmtd)) != 0)
2361 if ((retval = chip->scan_bbt(nsmtd)) != 0)
2364 if ((retval = parse_badblocks(nand, nsmtd)) != 0)
2367 /* Register NAND partitions */
2368 retval = mtd_device_register(nsmtd, &nand->partitions[0],
2377 nand_release(nsmtd);
2378 for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
2379 kfree(nand->partitions[i].name);
2387 module_init(ns_init_module);
2390 * Module clean-up function
2392 static void __exit ns_cleanup_module(void)
2394 struct nand_chip *chip = mtd_to_nand(nsmtd);
2395 struct nandsim *ns = nand_get_controller_data(chip);
2398 nandsim_debugfs_remove(ns);
2399 free_nandsim(ns); /* Free nandsim private resources */
2400 nand_release(nsmtd); /* Unregister driver */
2401 for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
2402 kfree(ns->partitions[i].name);
2403 kfree(mtd_to_nand(nsmtd)); /* Free other structures */
2407 module_exit(ns_cleanup_module);
2409 MODULE_LICENSE ("GPL");
2410 MODULE_AUTHOR ("Artem B. Bityuckiy");
2411 MODULE_DESCRIPTION ("The NAND flash simulator");