mtd: nandsim: fix free of NULL pointer
[deliverable/linux.git] / drivers / mtd / nand / nandsim.c
CommitLineData
1da177e4
LT
1/*
2 * NAND flash simulator.
3 *
4 * Author: Artem B. Bityuckiy <dedekind@oktetlabs.ru>, <dedekind@infradead.org>
5 *
61b03bd7 6 * Copyright (C) 2004 Nokia Corporation
1da177e4
LT
7 *
8 * Note: NS means "NAND Simulator".
9 * Note: Input means input TO flash chip, output means output FROM chip.
10 *
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
14 * version.
15 *
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.
20 *
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
1da177e4
LT
24 */
25
1da177e4
LT
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>
596fd462 31#include <linux/math64.h>
1da177e4
LT
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>
fc2ff592 37#include <linux/mtd/nand_bch.h>
1da177e4
LT
38#include <linux/mtd/partitions.h>
39#include <linux/delay.h>
2b77a0ed 40#include <linux/list.h>
514087e7 41#include <linux/random.h>
a5cce42f 42#include <linux/sched.h>
a9fc8991
AH
43#include <linux/fs.h>
44#include <linux/pagemap.h>
5346c27c
EG
45#include <linux/seq_file.h>
46#include <linux/debugfs.h>
1da177e4
LT
47
48/* Default simulator parameters values */
49#if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
50 !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
51 !defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
52 !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
53#define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
54#define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
55#define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF /* No byte */
56#define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */
57#endif
58
59#ifndef CONFIG_NANDSIM_ACCESS_DELAY
60#define CONFIG_NANDSIM_ACCESS_DELAY 25
61#endif
62#ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
63#define CONFIG_NANDSIM_PROGRAMM_DELAY 200
64#endif
65#ifndef CONFIG_NANDSIM_ERASE_DELAY
66#define CONFIG_NANDSIM_ERASE_DELAY 2
67#endif
68#ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
69#define CONFIG_NANDSIM_OUTPUT_CYCLE 40
70#endif
71#ifndef CONFIG_NANDSIM_INPUT_CYCLE
72#define CONFIG_NANDSIM_INPUT_CYCLE 50
73#endif
74#ifndef CONFIG_NANDSIM_BUS_WIDTH
75#define CONFIG_NANDSIM_BUS_WIDTH 8
76#endif
77#ifndef CONFIG_NANDSIM_DO_DELAYS
78#define CONFIG_NANDSIM_DO_DELAYS 0
79#endif
80#ifndef CONFIG_NANDSIM_LOG
81#define CONFIG_NANDSIM_LOG 0
82#endif
83#ifndef CONFIG_NANDSIM_DBG
84#define CONFIG_NANDSIM_DBG 0
85#endif
e99e90ae
BH
86#ifndef CONFIG_NANDSIM_MAX_PARTS
87#define CONFIG_NANDSIM_MAX_PARTS 32
88#endif
1da177e4 89
1da177e4
LT
90static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
91static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
92static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
93static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
94static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
95static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
96static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
97static uint log = CONFIG_NANDSIM_LOG;
98static uint dbg = CONFIG_NANDSIM_DBG;
e99e90ae 99static unsigned long parts[CONFIG_NANDSIM_MAX_PARTS];
2b77a0ed 100static unsigned int parts_num;
514087e7
AH
101static char *badblocks = NULL;
102static char *weakblocks = NULL;
103static char *weakpages = NULL;
104static unsigned int bitflips = 0;
105static char *gravepages = NULL;
a5ac8aeb 106static unsigned int overridesize = 0;
a9fc8991 107static char *cache_file = NULL;
ce85b79f 108static unsigned int bbt;
fc2ff592 109static unsigned int bch;
b00358a5
AM
110static u_char id_bytes[8] = {
111 [0] = CONFIG_NANDSIM_FIRST_ID_BYTE,
112 [1] = CONFIG_NANDSIM_SECOND_ID_BYTE,
113 [2] = CONFIG_NANDSIM_THIRD_ID_BYTE,
114 [3] = CONFIG_NANDSIM_FOURTH_ID_BYTE,
115 [4 ... 7] = 0xFF,
116};
1da177e4 117
b00358a5
AM
118module_param_array(id_bytes, byte, NULL, 0400);
119module_param_named(first_id_byte, id_bytes[0], byte, 0400);
120module_param_named(second_id_byte, id_bytes[1], byte, 0400);
121module_param_named(third_id_byte, id_bytes[2], byte, 0400);
122module_param_named(fourth_id_byte, id_bytes[3], byte, 0400);
1da177e4
LT
123module_param(access_delay, uint, 0400);
124module_param(programm_delay, uint, 0400);
125module_param(erase_delay, uint, 0400);
126module_param(output_cycle, uint, 0400);
127module_param(input_cycle, uint, 0400);
128module_param(bus_width, uint, 0400);
129module_param(do_delays, uint, 0400);
130module_param(log, uint, 0400);
131module_param(dbg, uint, 0400);
2b77a0ed 132module_param_array(parts, ulong, &parts_num, 0400);
514087e7
AH
133module_param(badblocks, charp, 0400);
134module_param(weakblocks, charp, 0400);
135module_param(weakpages, charp, 0400);
136module_param(bitflips, uint, 0400);
137module_param(gravepages, charp, 0400);
a5ac8aeb 138module_param(overridesize, uint, 0400);
a9fc8991 139module_param(cache_file, charp, 0400);
ce85b79f 140module_param(bbt, uint, 0400);
fc2ff592 141module_param(bch, uint, 0400);
1da177e4 142
b00358a5
AM
143MODULE_PARM_DESC(id_bytes, "The ID bytes returned by NAND Flash 'read ID' command");
144MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID) (obsolete)");
145MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID) (obsolete)");
146MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command (obsolete)");
147MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command (obsolete)");
a9fc8991 148MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
1da177e4
LT
149MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
150MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
6029a3a4
AY
151MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
152MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
1da177e4
LT
153MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
154MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
155MODULE_PARM_DESC(log, "Perform logging if not zero");
156MODULE_PARM_DESC(dbg, "Output debug information if not zero");
2b77a0ed 157MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas");
514087e7
AH
158/* Page and erase block positions for the following parameters are independent of any partitions */
159MODULE_PARM_DESC(badblocks, "Erase blocks that are initially marked bad, separated by commas");
160MODULE_PARM_DESC(weakblocks, "Weak erase blocks [: remaining erase cycles (defaults to 3)]"
161 " separated by commas e.g. 113:2 means eb 113"
162 " can be erased only twice before failing");
163MODULE_PARM_DESC(weakpages, "Weak pages [: maximum writes (defaults to 3)]"
164 " separated by commas e.g. 1401:2 means page 1401"
165 " can be written only twice before failing");
166MODULE_PARM_DESC(bitflips, "Maximum number of random bit flips per page (zero by default)");
167MODULE_PARM_DESC(gravepages, "Pages that lose data [: maximum reads (defaults to 3)]"
168 " separated by commas e.g. 1401:2 means page 1401"
169 " can be read only twice before failing");
a5ac8aeb
AH
170MODULE_PARM_DESC(overridesize, "Specifies the NAND Flash size overriding the ID bytes. "
171 "The size is specified in erase blocks and as the exponent of a power of two"
172 " e.g. 5 means a size of 32 erase blocks");
a9fc8991 173MODULE_PARM_DESC(cache_file, "File to use to cache nand pages instead of memory");
ce85b79f 174MODULE_PARM_DESC(bbt, "0 OOB, 1 BBT with marker in OOB, 2 BBT with marker in data area");
fc2ff592
ID
175MODULE_PARM_DESC(bch, "Enable BCH ecc and set how many bits should "
176 "be correctable in 512-byte blocks");
1da177e4
LT
177
178/* The largest possible page size */
75352662 179#define NS_LARGEST_PAGE_SIZE 4096
61b03bd7 180
1da177e4
LT
181/* The prefix for simulator output */
182#define NS_OUTPUT_PREFIX "[nandsim]"
183
184/* Simulator's output macros (logging, debugging, warning, error) */
185#define NS_LOG(args...) \
186 do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0)
187#define NS_DBG(args...) \
188 do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
189#define NS_WARN(args...) \
2b77a0ed 190 do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warning: " args); } while(0)
1da177e4 191#define NS_ERR(args...) \
2b77a0ed 192 do { printk(KERN_ERR NS_OUTPUT_PREFIX " error: " args); } while(0)
57aa6b54
AH
193#define NS_INFO(args...) \
194 do { printk(KERN_INFO NS_OUTPUT_PREFIX " " args); } while(0)
1da177e4
LT
195
196/* Busy-wait delay macros (microseconds, milliseconds) */
197#define NS_UDELAY(us) \
198 do { if (do_delays) udelay(us); } while(0)
199#define NS_MDELAY(us) \
200 do { if (do_delays) mdelay(us); } while(0)
61b03bd7 201
1da177e4
LT
202/* Is the nandsim structure initialized ? */
203#define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
204
205/* Good operation completion status */
206#define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
207
208/* Operation failed completion status */
61b03bd7 209#define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
1da177e4
LT
210
211/* Calculate the page offset in flash RAM image by (row, column) address */
212#define NS_RAW_OFFSET(ns) \
3b8b8fa1 213 (((ns)->regs.row * (ns)->geom.pgszoob) + (ns)->regs.column)
61b03bd7 214
1da177e4
LT
215/* Calculate the OOB offset in flash RAM image by (row, column) address */
216#define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
217
218/* After a command is input, the simulator goes to one of the following states */
219#define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */
220#define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */
4a0c50c0 221#define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
daf05ec0 222#define STATE_CMD_PAGEPROG 0x00000004 /* start page program */
1da177e4
LT
223#define STATE_CMD_READOOB 0x00000005 /* read OOB area */
224#define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
225#define STATE_CMD_STATUS 0x00000007 /* read status */
daf05ec0 226#define STATE_CMD_SEQIN 0x00000009 /* sequential data input */
1da177e4
LT
227#define STATE_CMD_READID 0x0000000A /* read ID */
228#define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
229#define STATE_CMD_RESET 0x0000000C /* reset */
74216be4
AB
230#define STATE_CMD_RNDOUT 0x0000000D /* random output command */
231#define STATE_CMD_RNDOUTSTART 0x0000000E /* random output start command */
1da177e4
LT
232#define STATE_CMD_MASK 0x0000000F /* command states mask */
233
8e87d782 234/* After an address is input, the simulator goes to one of these states */
1da177e4
LT
235#define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
236#define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
74216be4
AB
237#define STATE_ADDR_COLUMN 0x00000030 /* column address was accepted */
238#define STATE_ADDR_ZERO 0x00000040 /* one byte zero address was accepted */
239#define STATE_ADDR_MASK 0x00000070 /* address states mask */
1da177e4 240
daf05ec0 241/* During data input/output the simulator is in these states */
1da177e4
LT
242#define STATE_DATAIN 0x00000100 /* waiting for data input */
243#define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
244
245#define STATE_DATAOUT 0x00001000 /* waiting for page data output */
246#define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */
247#define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */
1da177e4
LT
248#define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
249
250/* Previous operation is done, ready to accept new requests */
251#define STATE_READY 0x00000000
252
253/* This state is used to mark that the next state isn't known yet */
254#define STATE_UNKNOWN 0x10000000
255
256/* Simulator's actions bit masks */
257#define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */
daf05ec0 258#define ACTION_PRGPAGE 0x00200000 /* program the internal buffer to flash */
1da177e4
LT
259#define ACTION_SECERASE 0x00300000 /* erase sector */
260#define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */
261#define ACTION_HALFOFF 0x00500000 /* add to address half of page */
262#define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */
263#define ACTION_MASK 0x00700000 /* action mask */
264
74216be4 265#define NS_OPER_NUM 13 /* Number of operations supported by the simulator */
1da177e4
LT
266#define NS_OPER_STATES 6 /* Maximum number of states in operation */
267
268#define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
1da177e4
LT
269#define OPT_PAGE512 0x00000002 /* 512-byte page chips */
270#define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
1da177e4 271#define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
75352662
SAS
272#define OPT_PAGE4096 0x00000080 /* 4096-byte page chips */
273#define OPT_LARGEPAGE (OPT_PAGE2048 | OPT_PAGE4096) /* 2048 & 4096-byte page chips */
51148f1f 274#define OPT_SMALLPAGE (OPT_PAGE512) /* 512-byte page chips */
1da177e4 275
daf05ec0 276/* Remove action bits from state */
1da177e4 277#define NS_STATE(x) ((x) & ~ACTION_MASK)
61b03bd7
TG
278
279/*
1da177e4 280 * Maximum previous states which need to be saved. Currently saving is
daf05ec0 281 * only needed for page program operation with preceded read command
1da177e4
LT
282 * (which is only valid for 512-byte pages).
283 */
284#define NS_MAX_PREVSTATES 1
285
a9fc8991
AH
286/* Maximum page cache pages needed to read or write a NAND page to the cache_file */
287#define NS_MAX_HELD_PAGES 16
288
5346c27c
EG
289struct nandsim_debug_info {
290 struct dentry *dfs_root;
291 struct dentry *dfs_wear_report;
292};
293
d086d436
VK
294/*
295 * A union to represent flash memory contents and flash buffer.
296 */
297union ns_mem {
298 u_char *byte; /* for byte access */
299 uint16_t *word; /* for 16-bit word access */
300};
301
61b03bd7 302/*
1da177e4
LT
303 * The structure which describes all the internal simulator data.
304 */
305struct nandsim {
e99e90ae 306 struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
2b77a0ed 307 unsigned int nbparts;
1da177e4
LT
308
309 uint busw; /* flash chip bus width (8 or 16) */
b00358a5 310 u_char ids[8]; /* chip's ID bytes */
1da177e4
LT
311 uint32_t options; /* chip's characteristic bits */
312 uint32_t state; /* current chip state */
313 uint32_t nxstate; /* next expected state */
61b03bd7 314
1da177e4
LT
315 uint32_t *op; /* current operation, NULL operations isn't known yet */
316 uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
317 uint16_t npstates; /* number of previous states saved */
318 uint16_t stateidx; /* current state index */
319
d086d436
VK
320 /* The simulated NAND flash pages array */
321 union ns_mem *pages;
1da177e4 322
8a4c2495
AK
323 /* Slab allocator for nand pages */
324 struct kmem_cache *nand_pages_slab;
325
1da177e4 326 /* Internal buffer of page + OOB size bytes */
d086d436 327 union ns_mem buf;
1da177e4
LT
328
329 /* NAND flash "geometry" */
0bfa4df2 330 struct {
6eda7a55 331 uint64_t totsz; /* total flash size, bytes */
1da177e4
LT
332 uint32_t secsz; /* flash sector (erase block) size, bytes */
333 uint pgsz; /* NAND flash page size, bytes */
334 uint oobsz; /* page OOB area size, bytes */
6eda7a55 335 uint64_t totszoob; /* total flash size including OOB, bytes */
1da177e4
LT
336 uint pgszoob; /* page size including OOB , bytes*/
337 uint secszoob; /* sector size including OOB, bytes */
338 uint pgnum; /* total number of pages */
339 uint pgsec; /* number of pages per sector */
340 uint secshift; /* bits number in sector size */
341 uint pgshift; /* bits number in page size */
1da177e4
LT
342 uint pgaddrbytes; /* bytes per page address */
343 uint secaddrbytes; /* bytes per sector address */
344 uint idbytes; /* the number ID bytes that this chip outputs */
345 } geom;
346
347 /* NAND flash internal registers */
0bfa4df2 348 struct {
1da177e4
LT
349 unsigned command; /* the command register */
350 u_char status; /* the status register */
351 uint row; /* the page number */
352 uint column; /* the offset within page */
353 uint count; /* internal counter */
354 uint num; /* number of bytes which must be processed */
355 uint off; /* fixed page offset */
356 } regs;
357
358 /* NAND flash lines state */
0bfa4df2 359 struct {
1da177e4
LT
360 int ce; /* chip Enable */
361 int cle; /* command Latch Enable */
362 int ale; /* address Latch Enable */
363 int wp; /* write Protect */
364 } lines;
a9fc8991
AH
365
366 /* Fields needed when using a cache file */
367 struct file *cfile; /* Open file */
08efe91a 368 unsigned long *pages_written; /* Which pages have been written */
a9fc8991
AH
369 void *file_buf;
370 struct page *held_pages[NS_MAX_HELD_PAGES];
371 int held_cnt;
5346c27c
EG
372
373 struct nandsim_debug_info dbg;
1da177e4
LT
374};
375
376/*
377 * Operations array. To perform any operation the simulator must pass
378 * through the correspondent states chain.
379 */
380static struct nandsim_operations {
381 uint32_t reqopts; /* options which are required to perform the operation */
382 uint32_t states[NS_OPER_STATES]; /* operation's states */
383} ops[NS_OPER_NUM] = {
384 /* Read page + OOB from the beginning */
385 {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
386 STATE_DATAOUT, STATE_READY}},
387 /* Read page + OOB from the second half */
388 {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
389 STATE_DATAOUT, STATE_READY}},
390 /* Read OOB */
391 {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
392 STATE_DATAOUT, STATE_READY}},
daf05ec0 393 /* Program page starting from the beginning */
1da177e4
LT
394 {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
395 STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
daf05ec0 396 /* Program page starting from the beginning */
1da177e4
LT
397 {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
398 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
daf05ec0 399 /* Program page starting from the second half */
1da177e4
LT
400 {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
401 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
daf05ec0 402 /* Program OOB */
1da177e4
LT
403 {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
404 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
405 /* Erase sector */
406 {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}},
407 /* Read status */
408 {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}},
1da177e4
LT
409 /* Read ID */
410 {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
411 /* Large page devices read page */
412 {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
74216be4
AB
413 STATE_DATAOUT, STATE_READY}},
414 /* Large page devices random page read */
415 {OPT_LARGEPAGE, {STATE_CMD_RNDOUT, STATE_ADDR_COLUMN, STATE_CMD_RNDOUTSTART | ACTION_CPY,
416 STATE_DATAOUT, STATE_READY}},
1da177e4
LT
417};
418
514087e7
AH
419struct weak_block {
420 struct list_head list;
421 unsigned int erase_block_no;
422 unsigned int max_erases;
423 unsigned int erases_done;
424};
425
426static LIST_HEAD(weak_blocks);
427
428struct weak_page {
429 struct list_head list;
430 unsigned int page_no;
431 unsigned int max_writes;
432 unsigned int writes_done;
433};
434
435static LIST_HEAD(weak_pages);
436
437struct grave_page {
438 struct list_head list;
439 unsigned int page_no;
440 unsigned int max_reads;
441 unsigned int reads_done;
442};
443
444static LIST_HEAD(grave_pages);
445
57aa6b54
AH
446static unsigned long *erase_block_wear = NULL;
447static unsigned int wear_eb_count = 0;
448static unsigned long total_wear = 0;
57aa6b54 449
1da177e4
LT
450/* MTD structure for NAND controller */
451static struct mtd_info *nsmtd;
452
5346c27c
EG
453static int nandsim_debugfs_show(struct seq_file *m, void *private)
454{
455 unsigned long wmin = -1, wmax = 0, avg;
456 unsigned long deciles[10], decile_max[10], tot = 0;
457 unsigned int i;
458
459 /* Calc wear stats */
460 for (i = 0; i < wear_eb_count; ++i) {
461 unsigned long wear = erase_block_wear[i];
462 if (wear < wmin)
463 wmin = wear;
464 if (wear > wmax)
465 wmax = wear;
466 tot += wear;
467 }
468
469 for (i = 0; i < 9; ++i) {
470 deciles[i] = 0;
471 decile_max[i] = (wmax * (i + 1) + 5) / 10;
472 }
473 deciles[9] = 0;
474 decile_max[9] = wmax;
475 for (i = 0; i < wear_eb_count; ++i) {
476 int d;
477 unsigned long wear = erase_block_wear[i];
478 for (d = 0; d < 10; ++d)
479 if (wear <= decile_max[d]) {
480 deciles[d] += 1;
481 break;
482 }
483 }
484 avg = tot / wear_eb_count;
485
486 /* Output wear report */
487 seq_printf(m, "Total numbers of erases: %lu\n", tot);
488 seq_printf(m, "Number of erase blocks: %u\n", wear_eb_count);
489 seq_printf(m, "Average number of erases: %lu\n", avg);
490 seq_printf(m, "Maximum number of erases: %lu\n", wmax);
491 seq_printf(m, "Minimum number of erases: %lu\n", wmin);
492 for (i = 0; i < 10; ++i) {
493 unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
494 if (from > decile_max[i])
495 continue;
496 seq_printf(m, "Number of ebs with erase counts from %lu to %lu : %lu\n",
497 from,
498 decile_max[i],
499 deciles[i]);
500 }
501
502 return 0;
503}
504
505static int nandsim_debugfs_open(struct inode *inode, struct file *file)
506{
507 return single_open(file, nandsim_debugfs_show, inode->i_private);
508}
509
510static const struct file_operations dfs_fops = {
511 .open = nandsim_debugfs_open,
512 .read = seq_read,
513 .llseek = seq_lseek,
514 .release = single_release,
515};
516
517/**
518 * nandsim_debugfs_create - initialize debugfs
519 * @dev: nandsim device description object
520 *
521 * This function creates all debugfs files for UBI device @ubi. Returns zero in
522 * case of success and a negative error code in case of failure.
523 */
524static int nandsim_debugfs_create(struct nandsim *dev)
525{
526 struct nandsim_debug_info *dbg = &dev->dbg;
527 struct dentry *dent;
528 int err;
529
530 if (!IS_ENABLED(CONFIG_DEBUG_FS))
531 return 0;
532
533 dent = debugfs_create_dir("nandsim", NULL);
534 if (IS_ERR_OR_NULL(dent)) {
535 int err = dent ? -ENODEV : PTR_ERR(dent);
536
537 NS_ERR("cannot create \"nandsim\" debugfs directory, err %d\n",
538 err);
539 return err;
540 }
541 dbg->dfs_root = dent;
542
543 dent = debugfs_create_file("wear_report", S_IRUSR,
544 dbg->dfs_root, dev, &dfs_fops);
545 if (IS_ERR_OR_NULL(dent))
546 goto out_remove;
547 dbg->dfs_wear_report = dent;
548
549 return 0;
550
551out_remove:
552 debugfs_remove_recursive(dbg->dfs_root);
553 err = dent ? PTR_ERR(dent) : -ENODEV;
554 return err;
555}
556
557/**
558 * nandsim_debugfs_remove - destroy all debugfs files
559 */
560static void nandsim_debugfs_remove(struct nandsim *ns)
561{
562 if (IS_ENABLED(CONFIG_DEBUG_FS))
563 debugfs_remove_recursive(ns->dbg.dfs_root);
564}
565
d086d436 566/*
8a4c2495
AK
567 * Allocate array of page pointers, create slab allocation for an array
568 * and initialize the array by NULL pointers.
d086d436
VK
569 *
570 * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
571 */
a5602146 572static int alloc_device(struct nandsim *ns)
d086d436 573{
a9fc8991
AH
574 struct file *cfile;
575 int i, err;
576
577 if (cache_file) {
578 cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
579 if (IS_ERR(cfile))
580 return PTR_ERR(cfile);
7f7f25e8 581 if (!(cfile->f_mode & FMODE_CAN_READ)) {
a9fc8991
AH
582 NS_ERR("alloc_device: cache file not readable\n");
583 err = -EINVAL;
584 goto err_close;
585 }
7f7f25e8 586 if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
a9fc8991
AH
587 NS_ERR("alloc_device: cache file not writeable\n");
588 err = -EINVAL;
589 goto err_close;
590 }
08efe91a
AM
591 ns->pages_written = vzalloc(BITS_TO_LONGS(ns->geom.pgnum) *
592 sizeof(unsigned long));
a9fc8991
AH
593 if (!ns->pages_written) {
594 NS_ERR("alloc_device: unable to allocate pages written array\n");
595 err = -ENOMEM;
596 goto err_close;
597 }
598 ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
599 if (!ns->file_buf) {
600 NS_ERR("alloc_device: unable to allocate file buf\n");
601 err = -ENOMEM;
602 goto err_free;
603 }
604 ns->cfile = cfile;
a9fc8991
AH
605 return 0;
606 }
d086d436
VK
607
608 ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
609 if (!ns->pages) {
a9fc8991 610 NS_ERR("alloc_device: unable to allocate page array\n");
d086d436
VK
611 return -ENOMEM;
612 }
613 for (i = 0; i < ns->geom.pgnum; i++) {
614 ns->pages[i].byte = NULL;
615 }
8a4c2495
AK
616 ns->nand_pages_slab = kmem_cache_create("nandsim",
617 ns->geom.pgszoob, 0, 0, NULL);
618 if (!ns->nand_pages_slab) {
619 NS_ERR("cache_create: unable to create kmem_cache\n");
620 return -ENOMEM;
621 }
d086d436
VK
622
623 return 0;
a9fc8991
AH
624
625err_free:
626 vfree(ns->pages_written);
627err_close:
628 filp_close(cfile, NULL);
629 return err;
d086d436
VK
630}
631
632/*
633 * Free any allocated pages, and free the array of page pointers.
634 */
a5602146 635static void free_device(struct nandsim *ns)
d086d436
VK
636{
637 int i;
638
a9fc8991
AH
639 if (ns->cfile) {
640 kfree(ns->file_buf);
641 vfree(ns->pages_written);
642 filp_close(ns->cfile, NULL);
643 return;
644 }
645
d086d436
VK
646 if (ns->pages) {
647 for (i = 0; i < ns->geom.pgnum; i++) {
648 if (ns->pages[i].byte)
8a4c2495
AK
649 kmem_cache_free(ns->nand_pages_slab,
650 ns->pages[i].byte);
d086d436 651 }
ec7478fa 652 if (ns->nand_pages_slab)
653 kmem_cache_destroy(ns->nand_pages_slab);
d086d436
VK
654 vfree(ns->pages);
655 }
656}
657
2b77a0ed
AH
658static char *get_partition_name(int i)
659{
f03a5729 660 return kasprintf(GFP_KERNEL, "NAND simulator partition %d", i);
2b77a0ed
AH
661}
662
1da177e4
LT
663/*
664 * Initialize the nandsim structure.
665 *
666 * RETURNS: 0 if success, -ERRNO if failure.
667 */
a5602146 668static int init_nandsim(struct mtd_info *mtd)
1da177e4 669{
7b8516b7
KV
670 struct nand_chip *chip = mtd->priv;
671 struct nandsim *ns = chip->priv;
2b77a0ed 672 int i, ret = 0;
0f07a0be
DW
673 uint64_t remains;
674 uint64_t next_offset;
1da177e4
LT
675
676 if (NS_IS_INITIALIZED(ns)) {
677 NS_ERR("init_nandsim: nandsim is already initialized\n");
678 return -EIO;
679 }
680
681 /* Force mtd to not do delays */
682 chip->chip_delay = 0;
683
684 /* Initialize the NAND flash parameters */
685 ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
686 ns->geom.totsz = mtd->size;
28318776 687 ns->geom.pgsz = mtd->writesize;
1da177e4
LT
688 ns->geom.oobsz = mtd->oobsize;
689 ns->geom.secsz = mtd->erasesize;
690 ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
596fd462 691 ns->geom.pgnum = div_u64(ns->geom.totsz, ns->geom.pgsz);
6eda7a55 692 ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz;
1da177e4
LT
693 ns->geom.secshift = ffs(ns->geom.secsz) - 1;
694 ns->geom.pgshift = chip->page_shift;
1da177e4
LT
695 ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
696 ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
697 ns->options = 0;
698
51148f1f 699 if (ns->geom.pgsz == 512) {
831d316b 700 ns->options |= OPT_PAGE512;
1da177e4
LT
701 if (ns->busw == 8)
702 ns->options |= OPT_PAGE512_8BIT;
703 } else if (ns->geom.pgsz == 2048) {
704 ns->options |= OPT_PAGE2048;
75352662
SAS
705 } else if (ns->geom.pgsz == 4096) {
706 ns->options |= OPT_PAGE4096;
1da177e4
LT
707 } else {
708 NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
709 return -EIO;
710 }
711
712 if (ns->options & OPT_SMALLPAGE) {
af3deccf 713 if (ns->geom.totsz <= (32 << 20)) {
1da177e4
LT
714 ns->geom.pgaddrbytes = 3;
715 ns->geom.secaddrbytes = 2;
716 } else {
717 ns->geom.pgaddrbytes = 4;
718 ns->geom.secaddrbytes = 3;
719 }
720 } else {
721 if (ns->geom.totsz <= (128 << 20)) {
4a0c50c0 722 ns->geom.pgaddrbytes = 4;
1da177e4
LT
723 ns->geom.secaddrbytes = 2;
724 } else {
725 ns->geom.pgaddrbytes = 5;
726 ns->geom.secaddrbytes = 3;
727 }
728 }
61b03bd7 729
2b77a0ed
AH
730 /* Fill the partition_info structure */
731 if (parts_num > ARRAY_SIZE(ns->partitions)) {
732 NS_ERR("too many partitions.\n");
733 ret = -EINVAL;
734 goto error;
735 }
736 remains = ns->geom.totsz;
737 next_offset = 0;
738 for (i = 0; i < parts_num; ++i) {
0f07a0be 739 uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
6eda7a55
AH
740
741 if (!part_sz || part_sz > remains) {
2b77a0ed
AH
742 NS_ERR("bad partition size.\n");
743 ret = -EINVAL;
744 goto error;
745 }
746 ns->partitions[i].name = get_partition_name(i);
641c7925
RW
747 if (!ns->partitions[i].name) {
748 NS_ERR("unable to allocate memory.\n");
749 ret = -ENOMEM;
750 goto error;
751 }
2b77a0ed 752 ns->partitions[i].offset = next_offset;
6eda7a55 753 ns->partitions[i].size = part_sz;
2b77a0ed
AH
754 next_offset += ns->partitions[i].size;
755 remains -= ns->partitions[i].size;
756 }
757 ns->nbparts = parts_num;
758 if (remains) {
759 if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
760 NS_ERR("too many partitions.\n");
761 ret = -EINVAL;
762 goto error;
763 }
764 ns->partitions[i].name = get_partition_name(i);
641c7925
RW
765 if (!ns->partitions[i].name) {
766 NS_ERR("unable to allocate memory.\n");
767 ret = -ENOMEM;
768 goto error;
769 }
2b77a0ed
AH
770 ns->partitions[i].offset = next_offset;
771 ns->partitions[i].size = remains;
772 ns->nbparts += 1;
773 }
774
1da177e4
LT
775 if (ns->busw == 16)
776 NS_WARN("16-bit flashes support wasn't tested\n");
777
e4c094a5
AM
778 printk("flash size: %llu MiB\n",
779 (unsigned long long)ns->geom.totsz >> 20);
1da177e4
LT
780 printk("page size: %u bytes\n", ns->geom.pgsz);
781 printk("OOB area size: %u bytes\n", ns->geom.oobsz);
782 printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
783 printk("pages number: %u\n", ns->geom.pgnum);
784 printk("pages per sector: %u\n", ns->geom.pgsec);
785 printk("bus width: %u\n", ns->busw);
786 printk("bits in sector size: %u\n", ns->geom.secshift);
787 printk("bits in page size: %u\n", ns->geom.pgshift);
2f3b07a7 788 printk("bits in OOB size: %u\n", ffs(ns->geom.oobsz) - 1);
e4c094a5
AM
789 printk("flash size with OOB: %llu KiB\n",
790 (unsigned long long)ns->geom.totszoob >> 10);
1da177e4
LT
791 printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
792 printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
793 printk("options: %#x\n", ns->options);
794
2b77a0ed 795 if ((ret = alloc_device(ns)) != 0)
d086d436 796 goto error;
1da177e4
LT
797
798 /* Allocate / initialize the internal buffer */
799 ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
800 if (!ns->buf.byte) {
801 NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
802 ns->geom.pgszoob);
2b77a0ed 803 ret = -ENOMEM;
1da177e4
LT
804 goto error;
805 }
806 memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
807
1da177e4
LT
808 return 0;
809
810error:
d086d436 811 free_device(ns);
1da177e4 812
2b77a0ed 813 return ret;
1da177e4
LT
814}
815
816/*
817 * Free the nandsim structure.
818 */
a5602146 819static void free_nandsim(struct nandsim *ns)
1da177e4
LT
820{
821 kfree(ns->buf.byte);
d086d436 822 free_device(ns);
1da177e4
LT
823
824 return;
825}
826
514087e7
AH
827static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
828{
829 char *w;
830 int zero_ok;
831 unsigned int erase_block_no;
832 loff_t offset;
833
834 if (!badblocks)
835 return 0;
836 w = badblocks;
837 do {
838 zero_ok = (*w == '0' ? 1 : 0);
839 erase_block_no = simple_strtoul(w, &w, 0);
840 if (!zero_ok && !erase_block_no) {
841 NS_ERR("invalid badblocks.\n");
842 return -EINVAL;
843 }
b033e1aa 844 offset = (loff_t)erase_block_no * ns->geom.secsz;
5942ddbc 845 if (mtd_block_markbad(mtd, offset)) {
514087e7
AH
846 NS_ERR("invalid badblocks.\n");
847 return -EINVAL;
848 }
849 if (*w == ',')
850 w += 1;
851 } while (*w);
852 return 0;
853}
854
855static int parse_weakblocks(void)
856{
857 char *w;
858 int zero_ok;
859 unsigned int erase_block_no;
860 unsigned int max_erases;
861 struct weak_block *wb;
862
863 if (!weakblocks)
864 return 0;
865 w = weakblocks;
866 do {
867 zero_ok = (*w == '0' ? 1 : 0);
868 erase_block_no = simple_strtoul(w, &w, 0);
869 if (!zero_ok && !erase_block_no) {
870 NS_ERR("invalid weakblocks.\n");
871 return -EINVAL;
872 }
873 max_erases = 3;
874 if (*w == ':') {
875 w += 1;
876 max_erases = simple_strtoul(w, &w, 0);
877 }
878 if (*w == ',')
879 w += 1;
880 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
881 if (!wb) {
882 NS_ERR("unable to allocate memory.\n");
883 return -ENOMEM;
884 }
885 wb->erase_block_no = erase_block_no;
886 wb->max_erases = max_erases;
887 list_add(&wb->list, &weak_blocks);
888 } while (*w);
889 return 0;
890}
891
892static int erase_error(unsigned int erase_block_no)
893{
894 struct weak_block *wb;
895
896 list_for_each_entry(wb, &weak_blocks, list)
897 if (wb->erase_block_no == erase_block_no) {
898 if (wb->erases_done >= wb->max_erases)
899 return 1;
900 wb->erases_done += 1;
901 return 0;
902 }
903 return 0;
904}
905
906static int parse_weakpages(void)
907{
908 char *w;
909 int zero_ok;
910 unsigned int page_no;
911 unsigned int max_writes;
912 struct weak_page *wp;
913
914 if (!weakpages)
915 return 0;
916 w = weakpages;
917 do {
918 zero_ok = (*w == '0' ? 1 : 0);
919 page_no = simple_strtoul(w, &w, 0);
920 if (!zero_ok && !page_no) {
921 NS_ERR("invalid weakpagess.\n");
922 return -EINVAL;
923 }
924 max_writes = 3;
925 if (*w == ':') {
926 w += 1;
927 max_writes = simple_strtoul(w, &w, 0);
928 }
929 if (*w == ',')
930 w += 1;
931 wp = kzalloc(sizeof(*wp), GFP_KERNEL);
932 if (!wp) {
933 NS_ERR("unable to allocate memory.\n");
934 return -ENOMEM;
935 }
936 wp->page_no = page_no;
937 wp->max_writes = max_writes;
938 list_add(&wp->list, &weak_pages);
939 } while (*w);
940 return 0;
941}
942
943static int write_error(unsigned int page_no)
944{
945 struct weak_page *wp;
946
947 list_for_each_entry(wp, &weak_pages, list)
948 if (wp->page_no == page_no) {
949 if (wp->writes_done >= wp->max_writes)
950 return 1;
951 wp->writes_done += 1;
952 return 0;
953 }
954 return 0;
955}
956
957static int parse_gravepages(void)
958{
959 char *g;
960 int zero_ok;
961 unsigned int page_no;
962 unsigned int max_reads;
963 struct grave_page *gp;
964
965 if (!gravepages)
966 return 0;
967 g = gravepages;
968 do {
969 zero_ok = (*g == '0' ? 1 : 0);
970 page_no = simple_strtoul(g, &g, 0);
971 if (!zero_ok && !page_no) {
972 NS_ERR("invalid gravepagess.\n");
973 return -EINVAL;
974 }
975 max_reads = 3;
976 if (*g == ':') {
977 g += 1;
978 max_reads = simple_strtoul(g, &g, 0);
979 }
980 if (*g == ',')
981 g += 1;
982 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
983 if (!gp) {
984 NS_ERR("unable to allocate memory.\n");
985 return -ENOMEM;
986 }
987 gp->page_no = page_no;
988 gp->max_reads = max_reads;
989 list_add(&gp->list, &grave_pages);
990 } while (*g);
991 return 0;
992}
993
994static int read_error(unsigned int page_no)
995{
996 struct grave_page *gp;
997
998 list_for_each_entry(gp, &grave_pages, list)
999 if (gp->page_no == page_no) {
1000 if (gp->reads_done >= gp->max_reads)
1001 return 1;
1002 gp->reads_done += 1;
1003 return 0;
1004 }
1005 return 0;
1006}
1007
1008static void free_lists(void)
1009{
1010 struct list_head *pos, *n;
1011 list_for_each_safe(pos, n, &weak_blocks) {
1012 list_del(pos);
1013 kfree(list_entry(pos, struct weak_block, list));
1014 }
1015 list_for_each_safe(pos, n, &weak_pages) {
1016 list_del(pos);
1017 kfree(list_entry(pos, struct weak_page, list));
1018 }
1019 list_for_each_safe(pos, n, &grave_pages) {
1020 list_del(pos);
1021 kfree(list_entry(pos, struct grave_page, list));
1022 }
57aa6b54
AH
1023 kfree(erase_block_wear);
1024}
1025
1026static int setup_wear_reporting(struct mtd_info *mtd)
1027{
1028 size_t mem;
1029
596fd462 1030 wear_eb_count = div_u64(mtd->size, mtd->erasesize);
57aa6b54
AH
1031 mem = wear_eb_count * sizeof(unsigned long);
1032 if (mem / sizeof(unsigned long) != wear_eb_count) {
1033 NS_ERR("Too many erase blocks for wear reporting\n");
1034 return -ENOMEM;
1035 }
1036 erase_block_wear = kzalloc(mem, GFP_KERNEL);
1037 if (!erase_block_wear) {
1038 NS_ERR("Too many erase blocks for wear reporting\n");
1039 return -ENOMEM;
1040 }
1041 return 0;
1042}
1043
1044static void update_wear(unsigned int erase_block_no)
1045{
57aa6b54
AH
1046 if (!erase_block_wear)
1047 return;
1048 total_wear += 1;
5346c27c
EG
1049 /*
1050 * TODO: Notify this through a debugfs entry,
1051 * instead of showing an error message.
1052 */
57aa6b54
AH
1053 if (total_wear == 0)
1054 NS_ERR("Erase counter total overflow\n");
1055 erase_block_wear[erase_block_no] += 1;
1056 if (erase_block_wear[erase_block_no] == 0)
1057 NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
514087e7
AH
1058}
1059
1da177e4
LT
1060/*
1061 * Returns the string representation of 'state' state.
1062 */
a5602146 1063static char *get_state_name(uint32_t state)
1da177e4
LT
1064{
1065 switch (NS_STATE(state)) {
1066 case STATE_CMD_READ0:
1067 return "STATE_CMD_READ0";
1068 case STATE_CMD_READ1:
1069 return "STATE_CMD_READ1";
1070 case STATE_CMD_PAGEPROG:
1071 return "STATE_CMD_PAGEPROG";
1072 case STATE_CMD_READOOB:
1073 return "STATE_CMD_READOOB";
1074 case STATE_CMD_READSTART:
1075 return "STATE_CMD_READSTART";
1076 case STATE_CMD_ERASE1:
1077 return "STATE_CMD_ERASE1";
1078 case STATE_CMD_STATUS:
1079 return "STATE_CMD_STATUS";
1da177e4
LT
1080 case STATE_CMD_SEQIN:
1081 return "STATE_CMD_SEQIN";
1082 case STATE_CMD_READID:
1083 return "STATE_CMD_READID";
1084 case STATE_CMD_ERASE2:
1085 return "STATE_CMD_ERASE2";
1086 case STATE_CMD_RESET:
1087 return "STATE_CMD_RESET";
74216be4
AB
1088 case STATE_CMD_RNDOUT:
1089 return "STATE_CMD_RNDOUT";
1090 case STATE_CMD_RNDOUTSTART:
1091 return "STATE_CMD_RNDOUTSTART";
1da177e4
LT
1092 case STATE_ADDR_PAGE:
1093 return "STATE_ADDR_PAGE";
1094 case STATE_ADDR_SEC:
1095 return "STATE_ADDR_SEC";
1096 case STATE_ADDR_ZERO:
1097 return "STATE_ADDR_ZERO";
74216be4
AB
1098 case STATE_ADDR_COLUMN:
1099 return "STATE_ADDR_COLUMN";
1da177e4
LT
1100 case STATE_DATAIN:
1101 return "STATE_DATAIN";
1102 case STATE_DATAOUT:
1103 return "STATE_DATAOUT";
1104 case STATE_DATAOUT_ID:
1105 return "STATE_DATAOUT_ID";
1106 case STATE_DATAOUT_STATUS:
1107 return "STATE_DATAOUT_STATUS";
1da177e4
LT
1108 case STATE_READY:
1109 return "STATE_READY";
1110 case STATE_UNKNOWN:
1111 return "STATE_UNKNOWN";
1112 }
1113
1114 NS_ERR("get_state_name: unknown state, BUG\n");
1115 return NULL;
1116}
1117
1118/*
1119 * Check if command is valid.
1120 *
1121 * RETURNS: 1 if wrong command, 0 if right.
1122 */
a5602146 1123static int check_command(int cmd)
1da177e4
LT
1124{
1125 switch (cmd) {
61b03bd7 1126
1da177e4 1127 case NAND_CMD_READ0:
74216be4 1128 case NAND_CMD_READ1:
1da177e4
LT
1129 case NAND_CMD_READSTART:
1130 case NAND_CMD_PAGEPROG:
1131 case NAND_CMD_READOOB:
1132 case NAND_CMD_ERASE1:
1133 case NAND_CMD_STATUS:
1134 case NAND_CMD_SEQIN:
1135 case NAND_CMD_READID:
1136 case NAND_CMD_ERASE2:
1137 case NAND_CMD_RESET:
74216be4
AB
1138 case NAND_CMD_RNDOUT:
1139 case NAND_CMD_RNDOUTSTART:
1da177e4 1140 return 0;
61b03bd7 1141
1da177e4
LT
1142 default:
1143 return 1;
1144 }
1145}
1146
1147/*
1148 * Returns state after command is accepted by command number.
1149 */
a5602146 1150static uint32_t get_state_by_command(unsigned command)
1da177e4
LT
1151{
1152 switch (command) {
1153 case NAND_CMD_READ0:
1154 return STATE_CMD_READ0;
1155 case NAND_CMD_READ1:
1156 return STATE_CMD_READ1;
1157 case NAND_CMD_PAGEPROG:
1158 return STATE_CMD_PAGEPROG;
1159 case NAND_CMD_READSTART:
1160 return STATE_CMD_READSTART;
1161 case NAND_CMD_READOOB:
1162 return STATE_CMD_READOOB;
1163 case NAND_CMD_ERASE1:
1164 return STATE_CMD_ERASE1;
1165 case NAND_CMD_STATUS:
1166 return STATE_CMD_STATUS;
1da177e4
LT
1167 case NAND_CMD_SEQIN:
1168 return STATE_CMD_SEQIN;
1169 case NAND_CMD_READID:
1170 return STATE_CMD_READID;
1171 case NAND_CMD_ERASE2:
1172 return STATE_CMD_ERASE2;
1173 case NAND_CMD_RESET:
1174 return STATE_CMD_RESET;
74216be4
AB
1175 case NAND_CMD_RNDOUT:
1176 return STATE_CMD_RNDOUT;
1177 case NAND_CMD_RNDOUTSTART:
1178 return STATE_CMD_RNDOUTSTART;
1da177e4
LT
1179 }
1180
1181 NS_ERR("get_state_by_command: unknown command, BUG\n");
1182 return 0;
1183}
1184
1185/*
1186 * Move an address byte to the correspondent internal register.
1187 */
a5602146 1188static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
1da177e4
LT
1189{
1190 uint byte = (uint)bt;
61b03bd7 1191
1da177e4
LT
1192 if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
1193 ns->regs.column |= (byte << 8 * ns->regs.count);
1194 else {
1195 ns->regs.row |= (byte << 8 * (ns->regs.count -
1196 ns->geom.pgaddrbytes +
1197 ns->geom.secaddrbytes));
1198 }
1199
1200 return;
1201}
61b03bd7 1202
1da177e4
LT
1203/*
1204 * Switch to STATE_READY state.
1205 */
a5602146 1206static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
1da177e4
LT
1207{
1208 NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
1209
1210 ns->state = STATE_READY;
1211 ns->nxstate = STATE_UNKNOWN;
1212 ns->op = NULL;
1213 ns->npstates = 0;
1214 ns->stateidx = 0;
1215 ns->regs.num = 0;
1216 ns->regs.count = 0;
1217 ns->regs.off = 0;
1218 ns->regs.row = 0;
1219 ns->regs.column = 0;
1220 ns->regs.status = status;
1221}
1222
1223/*
1224 * If the operation isn't known yet, try to find it in the global array
1225 * of supported operations.
1226 *
1227 * Operation can be unknown because of the following.
daf05ec0 1228 * 1. New command was accepted and this is the first call to find the
1da177e4 1229 * correspondent states chain. In this case ns->npstates = 0;
daf05ec0 1230 * 2. There are several operations which begin with the same command(s)
1da177e4
LT
1231 * (for example program from the second half and read from the
1232 * second half operations both begin with the READ1 command). In this
1233 * case the ns->pstates[] array contains previous states.
61b03bd7 1234 *
1da177e4
LT
1235 * Thus, the function tries to find operation containing the following
1236 * states (if the 'flag' parameter is 0):
1237 * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
1238 *
1239 * If (one and only one) matching operation is found, it is accepted (
1240 * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
1241 * zeroed).
61b03bd7 1242 *
daf05ec0 1243 * If there are several matches, the current state is pushed to the
1da177e4
LT
1244 * ns->pstates.
1245 *
1246 * The operation can be unknown only while commands are input to the chip.
1247 * As soon as address command is accepted, the operation must be known.
1248 * In such situation the function is called with 'flag' != 0, and the
1249 * operation is searched using the following pattern:
1250 * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
61b03bd7 1251 *
daf05ec0 1252 * It is supposed that this pattern must either match one operation or
1da177e4
LT
1253 * none. There can't be ambiguity in that case.
1254 *
daf05ec0 1255 * If no matches found, the function does the following:
1da177e4
LT
1256 * 1. if there are saved states present, try to ignore them and search
1257 * again only using the last command. If nothing was found, switch
1258 * to the STATE_READY state.
1259 * 2. if there are no saved states, switch to the STATE_READY state.
1260 *
1261 * RETURNS: -2 - no matched operations found.
1262 * -1 - several matches.
1263 * 0 - operation is found.
1264 */
a5602146 1265static int find_operation(struct nandsim *ns, uint32_t flag)
1da177e4
LT
1266{
1267 int opsfound = 0;
1268 int i, j, idx = 0;
61b03bd7 1269
1da177e4
LT
1270 for (i = 0; i < NS_OPER_NUM; i++) {
1271
1272 int found = 1;
61b03bd7 1273
1da177e4
LT
1274 if (!(ns->options & ops[i].reqopts))
1275 /* Ignore operations we can't perform */
1276 continue;
61b03bd7 1277
1da177e4
LT
1278 if (flag) {
1279 if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
1280 continue;
1281 } else {
1282 if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
1283 continue;
1284 }
1285
61b03bd7 1286 for (j = 0; j < ns->npstates; j++)
1da177e4
LT
1287 if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
1288 && (ns->options & ops[idx].reqopts)) {
1289 found = 0;
1290 break;
1291 }
1292
1293 if (found) {
1294 idx = i;
1295 opsfound += 1;
1296 }
1297 }
1298
1299 if (opsfound == 1) {
1300 /* Exact match */
1301 ns->op = &ops[idx].states[0];
1302 if (flag) {
61b03bd7 1303 /*
1da177e4
LT
1304 * In this case the find_operation function was
1305 * called when address has just began input. But it isn't
1306 * yet fully input and the current state must
1307 * not be one of STATE_ADDR_*, but the STATE_ADDR_*
1308 * state must be the next state (ns->nxstate).
1309 */
1310 ns->stateidx = ns->npstates - 1;
1311 } else {
1312 ns->stateidx = ns->npstates;
1313 }
1314 ns->npstates = 0;
1315 ns->state = ns->op[ns->stateidx];
1316 ns->nxstate = ns->op[ns->stateidx + 1];
1317 NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
1318 idx, get_state_name(ns->state), get_state_name(ns->nxstate));
1319 return 0;
1320 }
61b03bd7 1321
1da177e4
LT
1322 if (opsfound == 0) {
1323 /* Nothing was found. Try to ignore previous commands (if any) and search again */
1324 if (ns->npstates != 0) {
1325 NS_DBG("find_operation: no operation found, try again with state %s\n",
1326 get_state_name(ns->state));
1327 ns->npstates = 0;
1328 return find_operation(ns, 0);
1329
1330 }
1331 NS_DBG("find_operation: no operations found\n");
1332 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1333 return -2;
1334 }
61b03bd7 1335
1da177e4
LT
1336 if (flag) {
1337 /* This shouldn't happen */
1338 NS_DBG("find_operation: BUG, operation must be known if address is input\n");
1339 return -2;
1340 }
61b03bd7 1341
1da177e4
LT
1342 NS_DBG("find_operation: there is still ambiguity\n");
1343
1344 ns->pstates[ns->npstates++] = ns->state;
1345
1346 return -1;
1347}
1348
a9fc8991
AH
1349static void put_pages(struct nandsim *ns)
1350{
1351 int i;
1352
1353 for (i = 0; i < ns->held_cnt; i++)
1354 page_cache_release(ns->held_pages[i]);
1355}
1356
1357/* Get page cache pages in advance to provide NOFS memory allocation */
1358static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
1359{
1360 pgoff_t index, start_index, end_index;
1361 struct page *page;
1362 struct address_space *mapping = file->f_mapping;
1363
1364 start_index = pos >> PAGE_CACHE_SHIFT;
1365 end_index = (pos + count - 1) >> PAGE_CACHE_SHIFT;
1366 if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
1367 return -EINVAL;
1368 ns->held_cnt = 0;
1369 for (index = start_index; index <= end_index; index++) {
1370 page = find_get_page(mapping, index);
1371 if (page == NULL) {
1372 page = find_or_create_page(mapping, index, GFP_NOFS);
1373 if (page == NULL) {
1374 write_inode_now(mapping->host, 1);
1375 page = find_or_create_page(mapping, index, GFP_NOFS);
1376 }
1377 if (page == NULL) {
1378 put_pages(ns);
1379 return -ENOMEM;
1380 }
1381 unlock_page(page);
1382 }
1383 ns->held_pages[ns->held_cnt++] = page;
1384 }
1385 return 0;
1386}
1387
1388static int set_memalloc(void)
1389{
1390 if (current->flags & PF_MEMALLOC)
1391 return 0;
1392 current->flags |= PF_MEMALLOC;
1393 return 1;
1394}
1395
1396static void clear_memalloc(int memalloc)
1397{
1398 if (memalloc)
1399 current->flags &= ~PF_MEMALLOC;
1400}
1401
7bb307e8 1402static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
a9fc8991 1403{
a9fc8991
AH
1404 ssize_t tx;
1405 int err, memalloc;
1406
7bb307e8 1407 err = get_pages(ns, file, count, pos);
a9fc8991
AH
1408 if (err)
1409 return err;
a9fc8991 1410 memalloc = set_memalloc();
7bb307e8 1411 tx = kernel_read(file, pos, buf, count);
a9fc8991 1412 clear_memalloc(memalloc);
a9fc8991
AH
1413 put_pages(ns);
1414 return tx;
1415}
1416
7bb307e8 1417static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
a9fc8991 1418{
a9fc8991
AH
1419 ssize_t tx;
1420 int err, memalloc;
1421
7bb307e8 1422 err = get_pages(ns, file, count, pos);
a9fc8991
AH
1423 if (err)
1424 return err;
a9fc8991 1425 memalloc = set_memalloc();
7bb307e8 1426 tx = kernel_write(file, buf, count, pos);
a9fc8991 1427 clear_memalloc(memalloc);
a9fc8991
AH
1428 put_pages(ns);
1429 return tx;
1430}
1431
d086d436
VK
1432/*
1433 * Returns a pointer to the current page.
1434 */
1435static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
1436{
1437 return &(ns->pages[ns->regs.row]);
1438}
1439
1440/*
1441 * Retuns a pointer to the current byte, within the current page.
1442 */
1443static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
1444{
1445 return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
1446}
1447
b2b263f2 1448static int do_read_error(struct nandsim *ns, int num)
a9fc8991
AH
1449{
1450 unsigned int page_no = ns->regs.row;
1451
1452 if (read_error(page_no)) {
7e45bf83 1453 prandom_bytes(ns->buf.byte, num);
a9fc8991
AH
1454 NS_WARN("simulating read error in page %u\n", page_no);
1455 return 1;
1456 }
1457 return 0;
1458}
1459
b2b263f2 1460static void do_bit_flips(struct nandsim *ns, int num)
a9fc8991 1461{
aca662a3 1462 if (bitflips && prandom_u32() < (1 << 22)) {
a9fc8991
AH
1463 int flips = 1;
1464 if (bitflips > 1)
aca662a3 1465 flips = (prandom_u32() % (int) bitflips) + 1;
a9fc8991 1466 while (flips--) {
aca662a3 1467 int pos = prandom_u32() % (num * 8);
a9fc8991
AH
1468 ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
1469 NS_WARN("read_page: flipping bit %d in page %d "
1470 "reading from %d ecc: corrected=%u failed=%u\n",
1471 pos, ns->regs.row, ns->regs.column + ns->regs.off,
1472 nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
1473 }
1474 }
1475}
1476
d086d436
VK
1477/*
1478 * Fill the NAND buffer with data read from the specified page.
1479 */
1480static void read_page(struct nandsim *ns, int num)
1481{
1482 union ns_mem *mypage;
1483
a9fc8991 1484 if (ns->cfile) {
08efe91a 1485 if (!test_bit(ns->regs.row, ns->pages_written)) {
a9fc8991
AH
1486 NS_DBG("read_page: page %d not written\n", ns->regs.row);
1487 memset(ns->buf.byte, 0xFF, num);
1488 } else {
1489 loff_t pos;
1490 ssize_t tx;
1491
1492 NS_DBG("read_page: page %d written, reading from %d\n",
1493 ns->regs.row, ns->regs.column + ns->regs.off);
1494 if (do_read_error(ns, num))
1495 return;
6d07fcf7 1496 pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
7bb307e8 1497 tx = read_file(ns, ns->cfile, ns->buf.byte, num, pos);
a9fc8991
AH
1498 if (tx != num) {
1499 NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1500 return;
1501 }
1502 do_bit_flips(ns, num);
1503 }
1504 return;
1505 }
1506
d086d436
VK
1507 mypage = NS_GET_PAGE(ns);
1508 if (mypage->byte == NULL) {
1509 NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
1510 memset(ns->buf.byte, 0xFF, num);
1511 } else {
1512 NS_DBG("read_page: page %d allocated, reading from %d\n",
1513 ns->regs.row, ns->regs.column + ns->regs.off);
a9fc8991 1514 if (do_read_error(ns, num))
514087e7 1515 return;
d086d436 1516 memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
a9fc8991 1517 do_bit_flips(ns, num);
d086d436
VK
1518 }
1519}
1520
1521/*
1522 * Erase all pages in the specified sector.
1523 */
1524static void erase_sector(struct nandsim *ns)
1525{
1526 union ns_mem *mypage;
1527 int i;
1528
a9fc8991
AH
1529 if (ns->cfile) {
1530 for (i = 0; i < ns->geom.pgsec; i++)
08efe91a
AM
1531 if (__test_and_clear_bit(ns->regs.row + i,
1532 ns->pages_written)) {
a9fc8991 1533 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
a9fc8991
AH
1534 }
1535 return;
1536 }
1537
d086d436
VK
1538 mypage = NS_GET_PAGE(ns);
1539 for (i = 0; i < ns->geom.pgsec; i++) {
1540 if (mypage->byte != NULL) {
1541 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
8a4c2495 1542 kmem_cache_free(ns->nand_pages_slab, mypage->byte);
d086d436
VK
1543 mypage->byte = NULL;
1544 }
1545 mypage++;
1546 }
1547}
1548
1549/*
1550 * Program the specified page with the contents from the NAND buffer.
1551 */
1552static int prog_page(struct nandsim *ns, int num)
1553{
82810b7b 1554 int i;
d086d436
VK
1555 union ns_mem *mypage;
1556 u_char *pg_off;
1557
a9fc8991 1558 if (ns->cfile) {
7bb307e8 1559 loff_t off;
a9fc8991
AH
1560 ssize_t tx;
1561 int all;
1562
1563 NS_DBG("prog_page: writing page %d\n", ns->regs.row);
1564 pg_off = ns->file_buf + ns->regs.column + ns->regs.off;
6d07fcf7 1565 off = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
08efe91a 1566 if (!test_bit(ns->regs.row, ns->pages_written)) {
a9fc8991
AH
1567 all = 1;
1568 memset(ns->file_buf, 0xff, ns->geom.pgszoob);
1569 } else {
1570 all = 0;
7bb307e8 1571 tx = read_file(ns, ns->cfile, pg_off, num, off);
a9fc8991
AH
1572 if (tx != num) {
1573 NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1574 return -1;
1575 }
1576 }
1577 for (i = 0; i < num; i++)
1578 pg_off[i] &= ns->buf.byte[i];
1579 if (all) {
7bb307e8
AV
1580 loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
1581 tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, pos);
a9fc8991
AH
1582 if (tx != ns->geom.pgszoob) {
1583 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1584 return -1;
1585 }
08efe91a 1586 __set_bit(ns->regs.row, ns->pages_written);
a9fc8991 1587 } else {
7bb307e8 1588 tx = write_file(ns, ns->cfile, pg_off, num, off);
a9fc8991
AH
1589 if (tx != num) {
1590 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1591 return -1;
1592 }
1593 }
1594 return 0;
1595 }
1596
d086d436
VK
1597 mypage = NS_GET_PAGE(ns);
1598 if (mypage->byte == NULL) {
1599 NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
98b830d2
AB
1600 /*
1601 * We allocate memory with GFP_NOFS because a flash FS may
1602 * utilize this. If it is holding an FS lock, then gets here,
8a4c2495
AK
1603 * then kernel memory alloc runs writeback which goes to the FS
1604 * again and deadlocks. This was seen in practice.
98b830d2 1605 */
8a4c2495 1606 mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
d086d436
VK
1607 if (mypage->byte == NULL) {
1608 NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
1609 return -1;
1610 }
1611 memset(mypage->byte, 0xFF, ns->geom.pgszoob);
1612 }
1613
1614 pg_off = NS_PAGE_BYTE_OFF(ns);
82810b7b
AB
1615 for (i = 0; i < num; i++)
1616 pg_off[i] &= ns->buf.byte[i];
d086d436
VK
1617
1618 return 0;
1619}
1620
1da177e4
LT
1621/*
1622 * If state has any action bit, perform this action.
1623 *
1624 * RETURNS: 0 if success, -1 if error.
1625 */
a5602146 1626static int do_state_action(struct nandsim *ns, uint32_t action)
1da177e4 1627{
d086d436 1628 int num;
1da177e4 1629 int busdiv = ns->busw == 8 ? 1 : 2;
514087e7 1630 unsigned int erase_block_no, page_no;
1da177e4
LT
1631
1632 action &= ACTION_MASK;
61b03bd7 1633
1da177e4
LT
1634 /* Check that page address input is correct */
1635 if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
1636 NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
1637 return -1;
1638 }
1639
1640 switch (action) {
1641
1642 case ACTION_CPY:
1643 /*
1644 * Copy page data to the internal buffer.
1645 */
1646
1647 /* Column shouldn't be very large */
1648 if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
1649 NS_ERR("do_state_action: column number is too large\n");
1650 break;
1651 }
1652 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
d086d436 1653 read_page(ns, num);
1da177e4
LT
1654
1655 NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
1656 num, NS_RAW_OFFSET(ns) + ns->regs.off);
61b03bd7 1657
1da177e4
LT
1658 if (ns->regs.off == 0)
1659 NS_LOG("read page %d\n", ns->regs.row);
1660 else if (ns->regs.off < ns->geom.pgsz)
1661 NS_LOG("read page %d (second half)\n", ns->regs.row);
1662 else
1663 NS_LOG("read OOB of page %d\n", ns->regs.row);
61b03bd7 1664
1da177e4
LT
1665 NS_UDELAY(access_delay);
1666 NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
1667
1668 break;
1669
1670 case ACTION_SECERASE:
1671 /*
1672 * Erase sector.
1673 */
61b03bd7 1674
1da177e4
LT
1675 if (ns->lines.wp) {
1676 NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
1677 return -1;
1678 }
61b03bd7 1679
1da177e4
LT
1680 if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
1681 || (ns->regs.row & ~(ns->geom.secsz - 1))) {
1682 NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
1683 return -1;
1684 }
61b03bd7 1685
1da177e4
LT
1686 ns->regs.row = (ns->regs.row <<
1687 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
1688 ns->regs.column = 0;
61b03bd7 1689
514087e7
AH
1690 erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
1691
1da177e4
LT
1692 NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
1693 ns->regs.row, NS_RAW_OFFSET(ns));
514087e7 1694 NS_LOG("erase sector %u\n", erase_block_no);
1da177e4 1695
d086d436 1696 erase_sector(ns);
61b03bd7 1697
1da177e4 1698 NS_MDELAY(erase_delay);
61b03bd7 1699
57aa6b54
AH
1700 if (erase_block_wear)
1701 update_wear(erase_block_no);
1702
514087e7
AH
1703 if (erase_error(erase_block_no)) {
1704 NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
1705 return -1;
1706 }
1707
1da177e4
LT
1708 break;
1709
1710 case ACTION_PRGPAGE:
1711 /*
daf05ec0 1712 * Program page - move internal buffer data to the page.
1da177e4
LT
1713 */
1714
1715 if (ns->lines.wp) {
1716 NS_WARN("do_state_action: device is write-protected, programm\n");
1717 return -1;
1718 }
1719
1720 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1721 if (num != ns->regs.count) {
1722 NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
1723 ns->regs.count, num);
1724 return -1;
1725 }
1726
d086d436
VK
1727 if (prog_page(ns, num) == -1)
1728 return -1;
1da177e4 1729
514087e7
AH
1730 page_no = ns->regs.row;
1731
1da177e4
LT
1732 NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
1733 num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
1734 NS_LOG("programm page %d\n", ns->regs.row);
61b03bd7 1735
1da177e4
LT
1736 NS_UDELAY(programm_delay);
1737 NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
61b03bd7 1738
514087e7
AH
1739 if (write_error(page_no)) {
1740 NS_WARN("simulating write failure in page %u\n", page_no);
1741 return -1;
1742 }
1743
1da177e4 1744 break;
61b03bd7 1745
1da177e4
LT
1746 case ACTION_ZEROOFF:
1747 NS_DBG("do_state_action: set internal offset to 0\n");
1748 ns->regs.off = 0;
1749 break;
1750
1751 case ACTION_HALFOFF:
1752 if (!(ns->options & OPT_PAGE512_8BIT)) {
1753 NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
1754 "byte page size 8x chips\n");
1755 return -1;
1756 }
1757 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
1758 ns->regs.off = ns->geom.pgsz/2;
1759 break;
1760
1761 case ACTION_OOBOFF:
1762 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
1763 ns->regs.off = ns->geom.pgsz;
1764 break;
61b03bd7 1765
1da177e4
LT
1766 default:
1767 NS_DBG("do_state_action: BUG! unknown action\n");
1768 }
1769
1770 return 0;
1771}
1772
1773/*
1774 * Switch simulator's state.
1775 */
a5602146 1776static void switch_state(struct nandsim *ns)
1da177e4
LT
1777{
1778 if (ns->op) {
1779 /*
1780 * The current operation have already been identified.
1781 * Just follow the states chain.
1782 */
61b03bd7 1783
1da177e4
LT
1784 ns->stateidx += 1;
1785 ns->state = ns->nxstate;
1786 ns->nxstate = ns->op[ns->stateidx + 1];
1787
1788 NS_DBG("switch_state: operation is known, switch to the next state, "
1789 "state: %s, nxstate: %s\n",
1790 get_state_name(ns->state), get_state_name(ns->nxstate));
1791
1792 /* See, whether we need to do some action */
1793 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1794 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1795 return;
1796 }
61b03bd7 1797
1da177e4
LT
1798 } else {
1799 /*
1800 * We don't yet know which operation we perform.
1801 * Try to identify it.
1802 */
1803
61b03bd7 1804 /*
1da177e4
LT
1805 * The only event causing the switch_state function to
1806 * be called with yet unknown operation is new command.
1807 */
1808 ns->state = get_state_by_command(ns->regs.command);
1809
1810 NS_DBG("switch_state: operation is unknown, try to find it\n");
1811
1812 if (find_operation(ns, 0) != 0)
1813 return;
1814
1815 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1816 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1817 return;
1818 }
1819 }
1820
1821 /* For 16x devices column means the page offset in words */
1822 if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
1823 NS_DBG("switch_state: double the column number for 16x device\n");
1824 ns->regs.column <<= 1;
1825 }
1826
1827 if (NS_STATE(ns->nxstate) == STATE_READY) {
1828 /*
1829 * The current state is the last. Return to STATE_READY
1830 */
1831
1832 u_char status = NS_STATUS_OK(ns);
61b03bd7 1833
1da177e4
LT
1834 /* In case of data states, see if all bytes were input/output */
1835 if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
1836 && ns->regs.count != ns->regs.num) {
1837 NS_WARN("switch_state: not all bytes were processed, %d left\n",
1838 ns->regs.num - ns->regs.count);
1839 status = NS_STATUS_FAILED(ns);
1840 }
61b03bd7 1841
1da177e4
LT
1842 NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
1843
1844 switch_to_ready_state(ns, status);
1845
1846 return;
1847 } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
61b03bd7 1848 /*
1da177e4
LT
1849 * If the next state is data input/output, switch to it now
1850 */
61b03bd7 1851
1da177e4
LT
1852 ns->state = ns->nxstate;
1853 ns->nxstate = ns->op[++ns->stateidx + 1];
1854 ns->regs.num = ns->regs.count = 0;
1855
1856 NS_DBG("switch_state: the next state is data I/O, switch, "
1857 "state: %s, nxstate: %s\n",
1858 get_state_name(ns->state), get_state_name(ns->nxstate));
1859
1860 /*
1861 * Set the internal register to the count of bytes which
1862 * are expected to be input or output
1863 */
1864 switch (NS_STATE(ns->state)) {
1865 case STATE_DATAIN:
1866 case STATE_DATAOUT:
1867 ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1868 break;
61b03bd7 1869
1da177e4
LT
1870 case STATE_DATAOUT_ID:
1871 ns->regs.num = ns->geom.idbytes;
1872 break;
61b03bd7 1873
1da177e4 1874 case STATE_DATAOUT_STATUS:
1da177e4
LT
1875 ns->regs.count = ns->regs.num = 0;
1876 break;
61b03bd7 1877
1da177e4
LT
1878 default:
1879 NS_ERR("switch_state: BUG! unknown data state\n");
1880 }
1881
1882 } else if (ns->nxstate & STATE_ADDR_MASK) {
1883 /*
1884 * If the next state is address input, set the internal
1885 * register to the number of expected address bytes
1886 */
1887
1888 ns->regs.count = 0;
61b03bd7 1889
1da177e4
LT
1890 switch (NS_STATE(ns->nxstate)) {
1891 case STATE_ADDR_PAGE:
1892 ns->regs.num = ns->geom.pgaddrbytes;
61b03bd7 1893
1da177e4
LT
1894 break;
1895 case STATE_ADDR_SEC:
1896 ns->regs.num = ns->geom.secaddrbytes;
1897 break;
61b03bd7 1898
1da177e4
LT
1899 case STATE_ADDR_ZERO:
1900 ns->regs.num = 1;
1901 break;
1902
74216be4
AB
1903 case STATE_ADDR_COLUMN:
1904 /* Column address is always 2 bytes */
1905 ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
1906 break;
1907
1da177e4
LT
1908 default:
1909 NS_ERR("switch_state: BUG! unknown address state\n");
1910 }
1911 } else {
61b03bd7 1912 /*
1da177e4
LT
1913 * Just reset internal counters.
1914 */
1915
1916 ns->regs.num = 0;
1917 ns->regs.count = 0;
1918 }
1919}
1920
a5602146 1921static u_char ns_nand_read_byte(struct mtd_info *mtd)
1da177e4 1922{
7b8516b7 1923 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
1da177e4
LT
1924 u_char outb = 0x00;
1925
1926 /* Sanity and correctness checks */
1927 if (!ns->lines.ce) {
1928 NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
1929 return outb;
1930 }
1931 if (ns->lines.ale || ns->lines.cle) {
1932 NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
1933 return outb;
1934 }
1935 if (!(ns->state & STATE_DATAOUT_MASK)) {
1936 NS_WARN("read_byte: unexpected data output cycle, state is %s "
1937 "return %#x\n", get_state_name(ns->state), (uint)outb);
1938 return outb;
1939 }
1940
1941 /* Status register may be read as many times as it is wanted */
1942 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
1943 NS_DBG("read_byte: return %#x status\n", ns->regs.status);
1944 return ns->regs.status;
1945 }
1946
1947 /* Check if there is any data in the internal buffer which may be read */
1948 if (ns->regs.count == ns->regs.num) {
1949 NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
1950 return outb;
1951 }
1952
1953 switch (NS_STATE(ns->state)) {
1954 case STATE_DATAOUT:
1955 if (ns->busw == 8) {
1956 outb = ns->buf.byte[ns->regs.count];
1957 ns->regs.count += 1;
1958 } else {
1959 outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
1960 ns->regs.count += 2;
1961 }
1962 break;
1963 case STATE_DATAOUT_ID:
1964 NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
1965 outb = ns->ids[ns->regs.count];
1966 ns->regs.count += 1;
1967 break;
1968 default:
1969 BUG();
1970 }
61b03bd7 1971
1da177e4
LT
1972 if (ns->regs.count == ns->regs.num) {
1973 NS_DBG("read_byte: all bytes were read\n");
1974
831d316b 1975 if (NS_STATE(ns->nxstate) == STATE_READY)
1da177e4 1976 switch_state(ns);
1da177e4 1977 }
61b03bd7 1978
1da177e4
LT
1979 return outb;
1980}
1981
a5602146 1982static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
1da177e4 1983{
7b8516b7 1984 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
61b03bd7 1985
1da177e4
LT
1986 /* Sanity and correctness checks */
1987 if (!ns->lines.ce) {
1988 NS_ERR("write_byte: chip is disabled, ignore write\n");
1989 return;
1990 }
1991 if (ns->lines.ale && ns->lines.cle) {
1992 NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
1993 return;
1994 }
61b03bd7 1995
1da177e4
LT
1996 if (ns->lines.cle == 1) {
1997 /*
1998 * The byte written is a command.
1999 */
2000
2001 if (byte == NAND_CMD_RESET) {
2002 NS_LOG("reset chip\n");
2003 switch_to_ready_state(ns, NS_STATUS_OK(ns));
2004 return;
2005 }
2006
74216be4
AB
2007 /* Check that the command byte is correct */
2008 if (check_command(byte)) {
2009 NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
2010 return;
2011 }
2012
1da177e4 2013 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
74216be4
AB
2014 || NS_STATE(ns->state) == STATE_DATAOUT) {
2015 int row = ns->regs.row;
2016
1da177e4 2017 switch_state(ns);
74216be4
AB
2018 if (byte == NAND_CMD_RNDOUT)
2019 ns->regs.row = row;
2020 }
1da177e4
LT
2021
2022 /* Check if chip is expecting command */
2023 if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
9359ea46
AH
2024 /* Do not warn if only 2 id bytes are read */
2025 if (!(ns->regs.command == NAND_CMD_READID &&
2026 NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
2027 /*
2028 * We are in situation when something else (not command)
2029 * was expected but command was input. In this case ignore
2030 * previous command(s)/state(s) and accept the last one.
2031 */
2032 NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
2033 "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
2034 }
1da177e4
LT
2035 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2036 }
61b03bd7 2037
1da177e4
LT
2038 NS_DBG("command byte corresponding to %s state accepted\n",
2039 get_state_name(get_state_by_command(byte)));
2040 ns->regs.command = byte;
2041 switch_state(ns);
2042
2043 } else if (ns->lines.ale == 1) {
2044 /*
2045 * The byte written is an address.
2046 */
2047
2048 if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
2049
2050 NS_DBG("write_byte: operation isn't known yet, identify it\n");
2051
2052 if (find_operation(ns, 1) < 0)
2053 return;
61b03bd7 2054
1da177e4
LT
2055 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
2056 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2057 return;
2058 }
61b03bd7 2059
1da177e4
LT
2060 ns->regs.count = 0;
2061 switch (NS_STATE(ns->nxstate)) {
2062 case STATE_ADDR_PAGE:
2063 ns->regs.num = ns->geom.pgaddrbytes;
2064 break;
2065 case STATE_ADDR_SEC:
2066 ns->regs.num = ns->geom.secaddrbytes;
2067 break;
2068 case STATE_ADDR_ZERO:
2069 ns->regs.num = 1;
2070 break;
2071 default:
2072 BUG();
2073 }
2074 }
2075
2076 /* Check that chip is expecting address */
2077 if (!(ns->nxstate & STATE_ADDR_MASK)) {
2078 NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
2079 "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
2080 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2081 return;
2082 }
61b03bd7 2083
1da177e4
LT
2084 /* Check if this is expected byte */
2085 if (ns->regs.count == ns->regs.num) {
2086 NS_ERR("write_byte: no more address bytes expected\n");
2087 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2088 return;
2089 }
2090
2091 accept_addr_byte(ns, byte);
2092
2093 ns->regs.count += 1;
2094
2095 NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
2096 (uint)byte, ns->regs.count, ns->regs.num);
2097
2098 if (ns->regs.count == ns->regs.num) {
2099 NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
2100 switch_state(ns);
2101 }
61b03bd7 2102
1da177e4
LT
2103 } else {
2104 /*
2105 * The byte written is an input data.
2106 */
61b03bd7 2107
1da177e4
LT
2108 /* Check that chip is expecting data input */
2109 if (!(ns->state & STATE_DATAIN_MASK)) {
2110 NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
2111 "switch to %s\n", (uint)byte,
2112 get_state_name(ns->state), get_state_name(STATE_READY));
2113 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2114 return;
2115 }
2116
2117 /* Check if this is expected byte */
2118 if (ns->regs.count == ns->regs.num) {
2119 NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
2120 ns->regs.num);
2121 return;
2122 }
2123
2124 if (ns->busw == 8) {
2125 ns->buf.byte[ns->regs.count] = byte;
2126 ns->regs.count += 1;
2127 } else {
2128 ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
2129 ns->regs.count += 2;
2130 }
2131 }
2132
2133 return;
2134}
2135
7abd3ef9
TG
2136static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
2137{
2138 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
2139
2140 ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
2141 ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
2142 ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
2143
2144 if (cmd != NAND_CMD_NONE)
2145 ns_nand_write_byte(mtd, cmd);
2146}
2147
a5602146 2148static int ns_device_ready(struct mtd_info *mtd)
1da177e4
LT
2149{
2150 NS_DBG("device_ready\n");
2151 return 1;
2152}
2153
a5602146 2154static uint16_t ns_nand_read_word(struct mtd_info *mtd)
1da177e4
LT
2155{
2156 struct nand_chip *chip = (struct nand_chip *)mtd->priv;
2157
2158 NS_DBG("read_word\n");
61b03bd7 2159
1da177e4
LT
2160 return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
2161}
2162
a5602146 2163static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
1da177e4 2164{
7b8516b7 2165 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
1da177e4
LT
2166
2167 /* Check that chip is expecting data input */
2168 if (!(ns->state & STATE_DATAIN_MASK)) {
2169 NS_ERR("write_buf: data input isn't expected, state is %s, "
2170 "switch to STATE_READY\n", get_state_name(ns->state));
2171 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2172 return;
2173 }
2174
2175 /* Check if these are expected bytes */
2176 if (ns->regs.count + len > ns->regs.num) {
2177 NS_ERR("write_buf: too many input bytes\n");
2178 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2179 return;
2180 }
2181
2182 memcpy(ns->buf.byte + ns->regs.count, buf, len);
2183 ns->regs.count += len;
61b03bd7 2184
1da177e4
LT
2185 if (ns->regs.count == ns->regs.num) {
2186 NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
2187 }
2188}
2189
a5602146 2190static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
1da177e4 2191{
7b8516b7 2192 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
1da177e4
LT
2193
2194 /* Sanity and correctness checks */
2195 if (!ns->lines.ce) {
2196 NS_ERR("read_buf: chip is disabled\n");
2197 return;
2198 }
2199 if (ns->lines.ale || ns->lines.cle) {
2200 NS_ERR("read_buf: ALE or CLE pin is high\n");
2201 return;
2202 }
2203 if (!(ns->state & STATE_DATAOUT_MASK)) {
2204 NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
2205 get_state_name(ns->state));
2206 return;
2207 }
2208
2209 if (NS_STATE(ns->state) != STATE_DATAOUT) {
2210 int i;
2211
2212 for (i = 0; i < len; i++)
2213 buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
2214
2215 return;
2216 }
2217
2218 /* Check if these are expected bytes */
2219 if (ns->regs.count + len > ns->regs.num) {
2220 NS_ERR("read_buf: too many bytes to read\n");
2221 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2222 return;
2223 }
2224
2225 memcpy(buf, ns->buf.byte + ns->regs.count, len);
2226 ns->regs.count += len;
61b03bd7 2227
1da177e4 2228 if (ns->regs.count == ns->regs.num) {
831d316b 2229 if (NS_STATE(ns->nxstate) == STATE_READY)
1da177e4
LT
2230 switch_state(ns);
2231 }
61b03bd7 2232
1da177e4
LT
2233 return;
2234}
2235
1da177e4
LT
2236/*
2237 * Module initialization function
2238 */
2b9175c1 2239static int __init ns_init_module(void)
1da177e4
LT
2240{
2241 struct nand_chip *chip;
2242 struct nandsim *nand;
2b77a0ed 2243 int retval = -ENOMEM, i;
1da177e4
LT
2244
2245 if (bus_width != 8 && bus_width != 16) {
2246 NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
2247 return -EINVAL;
2248 }
61b03bd7 2249
1da177e4 2250 /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
95b93a0c 2251 nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
1da177e4
LT
2252 + sizeof(struct nandsim), GFP_KERNEL);
2253 if (!nsmtd) {
2254 NS_ERR("unable to allocate core structures.\n");
2255 return -ENOMEM;
2256 }
1da177e4
LT
2257 chip = (struct nand_chip *)(nsmtd + 1);
2258 nsmtd->priv = (void *)chip;
2259 nand = (struct nandsim *)(chip + 1);
61b03bd7 2260 chip->priv = (void *)nand;
1da177e4
LT
2261
2262 /*
2263 * Register simulator's callbacks.
2264 */
7abd3ef9 2265 chip->cmd_ctrl = ns_hwcontrol;
1da177e4
LT
2266 chip->read_byte = ns_nand_read_byte;
2267 chip->dev_ready = ns_device_ready;
1da177e4
LT
2268 chip->write_buf = ns_nand_write_buf;
2269 chip->read_buf = ns_nand_read_buf;
1da177e4 2270 chip->read_word = ns_nand_read_word;
6dfc6d25 2271 chip->ecc.mode = NAND_ECC_SOFT;
a5ac8aeb
AH
2272 /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
2273 /* and 'badblocks' parameters to work */
51502287 2274 chip->options |= NAND_SKIP_BBTSCAN;
1da177e4 2275
ce85b79f
SAS
2276 switch (bbt) {
2277 case 2:
a40f7341 2278 chip->bbt_options |= NAND_BBT_NO_OOB;
ce85b79f 2279 case 1:
bb9ebd4e 2280 chip->bbt_options |= NAND_BBT_USE_FLASH;
ce85b79f
SAS
2281 case 0:
2282 break;
2283 default:
2284 NS_ERR("bbt has to be 0..2\n");
2285 retval = -EINVAL;
2286 goto error;
2287 }
61b03bd7 2288 /*
1da177e4 2289 * Perform minimum nandsim structure initialization to handle
61b03bd7 2290 * the initial ID read command correctly
1da177e4 2291 */
b00358a5
AM
2292 if (id_bytes[6] != 0xFF || id_bytes[7] != 0xFF)
2293 nand->geom.idbytes = 8;
2294 else if (id_bytes[4] != 0xFF || id_bytes[5] != 0xFF)
2295 nand->geom.idbytes = 6;
2296 else if (id_bytes[2] != 0xFF || id_bytes[3] != 0xFF)
1da177e4
LT
2297 nand->geom.idbytes = 4;
2298 else
2299 nand->geom.idbytes = 2;
2300 nand->regs.status = NS_STATUS_OK(nand);
2301 nand->nxstate = STATE_UNKNOWN;
51148f1f 2302 nand->options |= OPT_PAGE512; /* temporary value */
b00358a5 2303 memcpy(nand->ids, id_bytes, sizeof(nand->ids));
1da177e4
LT
2304 if (bus_width == 16) {
2305 nand->busw = 16;
2306 chip->options |= NAND_BUSWIDTH_16;
2307 }
2308
552d9205
DW
2309 nsmtd->owner = THIS_MODULE;
2310
514087e7
AH
2311 if ((retval = parse_weakblocks()) != 0)
2312 goto error;
2313
2314 if ((retval = parse_weakpages()) != 0)
2315 goto error;
2316
2317 if ((retval = parse_gravepages()) != 0)
2318 goto error;
2319
fc2ff592
ID
2320 retval = nand_scan_ident(nsmtd, 1, NULL);
2321 if (retval) {
2322 NS_ERR("cannot scan NAND Simulator device\n");
2323 if (retval > 0)
2324 retval = -ENXIO;
2325 goto error;
2326 }
2327
2328 if (bch) {
2329 unsigned int eccsteps, eccbytes;
2330 if (!mtd_nand_has_bch()) {
2331 NS_ERR("BCH ECC support is disabled\n");
2332 retval = -EINVAL;
2333 goto error;
2334 }
2335 /* use 512-byte ecc blocks */
2336 eccsteps = nsmtd->writesize/512;
2337 eccbytes = (bch*13+7)/8;
2338 /* do not bother supporting small page devices */
2339 if ((nsmtd->oobsize < 64) || !eccsteps) {
2340 NS_ERR("bch not available on small page devices\n");
2341 retval = -EINVAL;
2342 goto error;
2343 }
2344 if ((eccbytes*eccsteps+2) > nsmtd->oobsize) {
2345 NS_ERR("invalid bch value %u\n", bch);
2346 retval = -EINVAL;
2347 goto error;
2348 }
2349 chip->ecc.mode = NAND_ECC_SOFT_BCH;
2350 chip->ecc.size = 512;
e0377cde 2351 chip->ecc.strength = bch;
fc2ff592
ID
2352 chip->ecc.bytes = eccbytes;
2353 NS_INFO("using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
2354 }
2355
2356 retval = nand_scan_tail(nsmtd);
2357 if (retval) {
1da177e4
LT
2358 NS_ERR("can't register NAND Simulator\n");
2359 if (retval > 0)
2360 retval = -ENXIO;
2361 goto error;
2362 }
2363
a5ac8aeb 2364 if (overridesize) {
0f07a0be 2365 uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
a5ac8aeb
AH
2366 if (new_size >> overridesize != nsmtd->erasesize) {
2367 NS_ERR("overridesize is too big\n");
bb0a13a1 2368 retval = -EINVAL;
a5ac8aeb
AH
2369 goto err_exit;
2370 }
2371 /* N.B. This relies on nand_scan not doing anything with the size before we change it */
2372 nsmtd->size = new_size;
2373 chip->chipsize = new_size;
6eda7a55 2374 chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
07293b20 2375 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
a5ac8aeb
AH
2376 }
2377
57aa6b54
AH
2378 if ((retval = setup_wear_reporting(nsmtd)) != 0)
2379 goto err_exit;
2380
5346c27c
EG
2381 if ((retval = nandsim_debugfs_create(nand)) != 0)
2382 goto err_exit;
2383
2b77a0ed
AH
2384 if ((retval = init_nandsim(nsmtd)) != 0)
2385 goto err_exit;
61b03bd7 2386
4fd18ae4 2387 if ((retval = chip->scan_bbt(nsmtd)) != 0)
514087e7
AH
2388 goto err_exit;
2389
ce85b79f 2390 if ((retval = parse_badblocks(nand, nsmtd)) != 0)
2b77a0ed 2391 goto err_exit;
51502287 2392
2b77a0ed 2393 /* Register NAND partitions */
ee0e87b1
JI
2394 retval = mtd_device_register(nsmtd, &nand->partitions[0],
2395 nand->nbparts);
2396 if (retval != 0)
2b77a0ed 2397 goto err_exit;
1da177e4
LT
2398
2399 return 0;
2400
2b77a0ed
AH
2401err_exit:
2402 free_nandsim(nand);
2403 nand_release(nsmtd);
2404 for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
2405 kfree(nand->partitions[i].name);
1da177e4
LT
2406error:
2407 kfree(nsmtd);
514087e7 2408 free_lists();
1da177e4
LT
2409
2410 return retval;
2411}
2412
2413module_init(ns_init_module);
2414
2415/*
2416 * Module clean-up function
2417 */
2418static void __exit ns_cleanup_module(void)
2419{
7b8516b7 2420 struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
2b77a0ed 2421 int i;
1da177e4 2422
5346c27c 2423 nandsim_debugfs_remove(ns);
1da177e4 2424 free_nandsim(ns); /* Free nandsim private resources */
2b77a0ed
AH
2425 nand_release(nsmtd); /* Unregister driver */
2426 for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
2427 kfree(ns->partitions[i].name);
1da177e4 2428 kfree(nsmtd); /* Free other structures */
514087e7 2429 free_lists();
1da177e4
LT
2430}
2431
2432module_exit(ns_cleanup_module);
2433
2434MODULE_LICENSE ("GPL");
2435MODULE_AUTHOR ("Artem B. Bityuckiy");
2436MODULE_DESCRIPTION ("The NAND flash simulator");
This page took 0.64876 seconds and 5 git commands to generate.