blob: 2ab827428f18011073e4d4ec57073793640750d5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * NAND flash simulator.
3 *
4 * Author: Artem B. Bityuckiy <dedekind@oktetlabs.ru>, <dedekind@infradead.org>
5 *
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00006 * Copyright (C) 2004 Nokia Corporation
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
25
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <linux/init.h>
27#include <linux/types.h>
28#include <linux/module.h>
29#include <linux/moduleparam.h>
30#include <linux/vmalloc.h>
Herton Ronaldo Krzesinski596fd462012-05-16 16:21:52 -030031#include <linux/math64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#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>
Ivan Djelicfc2ff592011-03-11 11:05:34 +010037#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038#include <linux/mtd/partitions.h>
39#include <linux/delay.h>
Adrian Hunter2b77a0e2007-03-19 12:46:43 +020040#include <linux/list.h>
Adrian Hunter514087e72007-03-19 12:47:45 +020041#include <linux/random.h>
Randy Dunlapa5cce422008-12-17 12:46:17 -080042#include <linux/sched.h>
Adrian Huntera9fc8992008-11-12 16:06:07 +020043#include <linux/fs.h>
44#include <linux/pagemap.h>
Ezequiel Garcia5346c272012-12-03 10:31:40 -030045#include <linux/seq_file.h>
46#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
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
Ben Hutchingse99e90a2010-01-29 20:58:08 +000086#ifndef CONFIG_NANDSIM_MAX_PARTS
87#define CONFIG_NANDSIM_MAX_PARTS 32
88#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static uint first_id_byte = CONFIG_NANDSIM_FIRST_ID_BYTE;
91static uint second_id_byte = CONFIG_NANDSIM_SECOND_ID_BYTE;
92static uint third_id_byte = CONFIG_NANDSIM_THIRD_ID_BYTE;
93static uint fourth_id_byte = CONFIG_NANDSIM_FOURTH_ID_BYTE;
94static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
95static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
96static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
97static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
98static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
99static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
100static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
101static uint log = CONFIG_NANDSIM_LOG;
102static uint dbg = CONFIG_NANDSIM_DBG;
Ben Hutchingse99e90a2010-01-29 20:58:08 +0000103static unsigned long parts[CONFIG_NANDSIM_MAX_PARTS];
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200104static unsigned int parts_num;
Adrian Hunter514087e72007-03-19 12:47:45 +0200105static char *badblocks = NULL;
106static char *weakblocks = NULL;
107static char *weakpages = NULL;
108static unsigned int bitflips = 0;
109static char *gravepages = NULL;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +0200110static unsigned int overridesize = 0;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200111static char *cache_file = NULL;
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +0200112static unsigned int bbt;
Ivan Djelicfc2ff592011-03-11 11:05:34 +0100113static unsigned int bch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
115module_param(first_id_byte, uint, 0400);
116module_param(second_id_byte, uint, 0400);
117module_param(third_id_byte, uint, 0400);
118module_param(fourth_id_byte, uint, 0400);
119module_param(access_delay, uint, 0400);
120module_param(programm_delay, uint, 0400);
121module_param(erase_delay, uint, 0400);
122module_param(output_cycle, uint, 0400);
123module_param(input_cycle, uint, 0400);
124module_param(bus_width, uint, 0400);
125module_param(do_delays, uint, 0400);
126module_param(log, uint, 0400);
127module_param(dbg, uint, 0400);
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200128module_param_array(parts, ulong, &parts_num, 0400);
Adrian Hunter514087e72007-03-19 12:47:45 +0200129module_param(badblocks, charp, 0400);
130module_param(weakblocks, charp, 0400);
131module_param(weakpages, charp, 0400);
132module_param(bitflips, uint, 0400);
133module_param(gravepages, charp, 0400);
Adrian Huntera5ac8ae2007-03-19 12:49:11 +0200134module_param(overridesize, uint, 0400);
Adrian Huntera9fc8992008-11-12 16:06:07 +0200135module_param(cache_file, charp, 0400);
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +0200136module_param(bbt, uint, 0400);
Ivan Djelicfc2ff592011-03-11 11:05:34 +0100137module_param(bch, uint, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
Adrian Huntera5ac8ae2007-03-19 12:49:11 +0200139MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID)");
141MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command");
142MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command");
Adrian Huntera9fc8992008-11-12 16:06:07 +0200143MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
145MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
Andrey Yurovsky6029a3a2009-12-17 12:31:20 -0800146MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
147MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
149MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
150MODULE_PARM_DESC(log, "Perform logging if not zero");
151MODULE_PARM_DESC(dbg, "Output debug information if not zero");
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200152MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas");
Adrian Hunter514087e72007-03-19 12:47:45 +0200153/* Page and erase block positions for the following parameters are independent of any partitions */
154MODULE_PARM_DESC(badblocks, "Erase blocks that are initially marked bad, separated by commas");
155MODULE_PARM_DESC(weakblocks, "Weak erase blocks [: remaining erase cycles (defaults to 3)]"
156 " separated by commas e.g. 113:2 means eb 113"
157 " can be erased only twice before failing");
158MODULE_PARM_DESC(weakpages, "Weak pages [: maximum writes (defaults to 3)]"
159 " separated by commas e.g. 1401:2 means page 1401"
160 " can be written only twice before failing");
161MODULE_PARM_DESC(bitflips, "Maximum number of random bit flips per page (zero by default)");
162MODULE_PARM_DESC(gravepages, "Pages that lose data [: maximum reads (defaults to 3)]"
163 " separated by commas e.g. 1401:2 means page 1401"
164 " can be read only twice before failing");
Adrian Huntera5ac8ae2007-03-19 12:49:11 +0200165MODULE_PARM_DESC(overridesize, "Specifies the NAND Flash size overriding the ID bytes. "
166 "The size is specified in erase blocks and as the exponent of a power of two"
167 " e.g. 5 means a size of 32 erase blocks");
Adrian Huntera9fc8992008-11-12 16:06:07 +0200168MODULE_PARM_DESC(cache_file, "File to use to cache nand pages instead of memory");
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +0200169MODULE_PARM_DESC(bbt, "0 OOB, 1 BBT with marker in OOB, 2 BBT with marker in data area");
Ivan Djelicfc2ff592011-03-11 11:05:34 +0100170MODULE_PARM_DESC(bch, "Enable BCH ecc and set how many bits should "
171 "be correctable in 512-byte blocks");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
173/* The largest possible page size */
Sebastian Andrzej Siewior75352662009-11-29 19:07:57 +0100174#define NS_LARGEST_PAGE_SIZE 4096
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176/* The prefix for simulator output */
177#define NS_OUTPUT_PREFIX "[nandsim]"
178
179/* Simulator's output macros (logging, debugging, warning, error) */
180#define NS_LOG(args...) \
181 do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0)
182#define NS_DBG(args...) \
183 do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
184#define NS_WARN(args...) \
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200185 do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warning: " args); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186#define NS_ERR(args...) \
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200187 do { printk(KERN_ERR NS_OUTPUT_PREFIX " error: " args); } while(0)
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200188#define NS_INFO(args...) \
189 do { printk(KERN_INFO NS_OUTPUT_PREFIX " " args); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
191/* Busy-wait delay macros (microseconds, milliseconds) */
192#define NS_UDELAY(us) \
193 do { if (do_delays) udelay(us); } while(0)
194#define NS_MDELAY(us) \
195 do { if (do_delays) mdelay(us); } while(0)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197/* Is the nandsim structure initialized ? */
198#define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
199
200/* Good operation completion status */
201#define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
202
203/* Operation failed completion status */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000204#define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206/* Calculate the page offset in flash RAM image by (row, column) address */
207#define NS_RAW_OFFSET(ns) \
208 (((ns)->regs.row << (ns)->geom.pgshift) + ((ns)->regs.row * (ns)->geom.oobsz) + (ns)->regs.column)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210/* Calculate the OOB offset in flash RAM image by (row, column) address */
211#define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
212
213/* After a command is input, the simulator goes to one of the following states */
214#define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */
215#define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */
Artem Bityutskiy4a0c50c2006-12-06 21:52:32 +0200216#define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
srimugunthandaf05ec2010-11-13 12:46:05 +0200217#define STATE_CMD_PAGEPROG 0x00000004 /* start page program */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218#define STATE_CMD_READOOB 0x00000005 /* read OOB area */
219#define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
220#define STATE_CMD_STATUS 0x00000007 /* read status */
221#define STATE_CMD_STATUS_M 0x00000008 /* read multi-plane status (isn't implemented) */
srimugunthandaf05ec2010-11-13 12:46:05 +0200222#define STATE_CMD_SEQIN 0x00000009 /* sequential data input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define STATE_CMD_READID 0x0000000A /* read ID */
224#define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
225#define STATE_CMD_RESET 0x0000000C /* reset */
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300226#define STATE_CMD_RNDOUT 0x0000000D /* random output command */
227#define STATE_CMD_RNDOUTSTART 0x0000000E /* random output start command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228#define STATE_CMD_MASK 0x0000000F /* command states mask */
229
Joe Perches8e87d782008-02-03 17:22:34 +0200230/* After an address is input, the simulator goes to one of these states */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231#define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
232#define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300233#define STATE_ADDR_COLUMN 0x00000030 /* column address was accepted */
234#define STATE_ADDR_ZERO 0x00000040 /* one byte zero address was accepted */
235#define STATE_ADDR_MASK 0x00000070 /* address states mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
srimugunthandaf05ec2010-11-13 12:46:05 +0200237/* During data input/output the simulator is in these states */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238#define STATE_DATAIN 0x00000100 /* waiting for data input */
239#define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
240
241#define STATE_DATAOUT 0x00001000 /* waiting for page data output */
242#define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */
243#define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */
244#define STATE_DATAOUT_STATUS_M 0x00004000 /* waiting for multi-plane status output */
245#define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
246
247/* Previous operation is done, ready to accept new requests */
248#define STATE_READY 0x00000000
249
250/* This state is used to mark that the next state isn't known yet */
251#define STATE_UNKNOWN 0x10000000
252
253/* Simulator's actions bit masks */
254#define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */
srimugunthandaf05ec2010-11-13 12:46:05 +0200255#define ACTION_PRGPAGE 0x00200000 /* program the internal buffer to flash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256#define ACTION_SECERASE 0x00300000 /* erase sector */
257#define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */
258#define ACTION_HALFOFF 0x00500000 /* add to address half of page */
259#define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */
260#define ACTION_MASK 0x00700000 /* action mask */
261
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300262#define NS_OPER_NUM 13 /* Number of operations supported by the simulator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263#define NS_OPER_STATES 6 /* Maximum number of states in operation */
264
265#define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
266#define OPT_PAGE256 0x00000001 /* 256-byte page chips */
267#define OPT_PAGE512 0x00000002 /* 512-byte page chips */
268#define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
269#define OPT_SMARTMEDIA 0x00000010 /* SmartMedia technology chips */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270#define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
Sebastian Andrzej Siewior75352662009-11-29 19:07:57 +0100271#define OPT_PAGE4096 0x00000080 /* 4096-byte page chips */
272#define OPT_LARGEPAGE (OPT_PAGE2048 | OPT_PAGE4096) /* 2048 & 4096-byte page chips */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273#define OPT_SMALLPAGE (OPT_PAGE256 | OPT_PAGE512) /* 256 and 512-byte page chips */
274
srimugunthandaf05ec2010-11-13 12:46:05 +0200275/* Remove action bits from state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276#define NS_STATE(x) ((x) & ~ACTION_MASK)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000277
278/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 * Maximum previous states which need to be saved. Currently saving is
srimugunthandaf05ec2010-11-13 12:46:05 +0200280 * only needed for page program operation with preceded read command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 * (which is only valid for 512-byte pages).
282 */
283#define NS_MAX_PREVSTATES 1
284
Adrian Huntera9fc8992008-11-12 16:06:07 +0200285/* Maximum page cache pages needed to read or write a NAND page to the cache_file */
286#define NS_MAX_HELD_PAGES 16
287
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300288struct nandsim_debug_info {
289 struct dentry *dfs_root;
290 struct dentry *dfs_wear_report;
291};
292
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000293/*
Vijay Kumard086d432006-10-08 22:02:31 +0530294 * A union to represent flash memory contents and flash buffer.
295 */
296union ns_mem {
297 u_char *byte; /* for byte access */
298 uint16_t *word; /* for 16-bit word access */
299};
300
301/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 * The structure which describes all the internal simulator data.
303 */
304struct nandsim {
Ben Hutchingse99e90a2010-01-29 20:58:08 +0000305 struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200306 unsigned int nbparts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
308 uint busw; /* flash chip bus width (8 or 16) */
309 u_char ids[4]; /* chip's ID bytes */
310 uint32_t options; /* chip's characteristic bits */
311 uint32_t state; /* current chip state */
312 uint32_t nxstate; /* next expected state */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 uint32_t *op; /* current operation, NULL operations isn't known yet */
315 uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
316 uint16_t npstates; /* number of previous states saved */
317 uint16_t stateidx; /* current state index */
318
Vijay Kumard086d432006-10-08 22:02:31 +0530319 /* The simulated NAND flash pages array */
320 union ns_mem *pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000322 /* Slab allocator for nand pages */
323 struct kmem_cache *nand_pages_slab;
324
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 /* Internal buffer of page + OOB size bytes */
Vijay Kumard086d432006-10-08 22:02:31 +0530326 union ns_mem buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327
328 /* NAND flash "geometry" */
Artem Bityutskiy0bfa4df2010-04-01 15:22:50 +0300329 struct {
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300330 uint64_t totsz; /* total flash size, bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 uint32_t secsz; /* flash sector (erase block) size, bytes */
332 uint pgsz; /* NAND flash page size, bytes */
333 uint oobsz; /* page OOB area size, bytes */
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300334 uint64_t totszoob; /* total flash size including OOB, bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 uint pgszoob; /* page size including OOB , bytes*/
336 uint secszoob; /* sector size including OOB, bytes */
337 uint pgnum; /* total number of pages */
338 uint pgsec; /* number of pages per sector */
339 uint secshift; /* bits number in sector size */
340 uint pgshift; /* bits number in page size */
341 uint oobshift; /* bits number in OOB size */
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 */
Artem Bityutskiy0bfa4df2010-04-01 15:22:50 +0300348 struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 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 */
Artem Bityutskiy0bfa4df2010-04-01 15:22:50 +0300359 struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 int ce; /* chip Enable */
361 int cle; /* command Latch Enable */
362 int ale; /* address Latch Enable */
363 int wp; /* write Protect */
364 } lines;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200365
366 /* Fields needed when using a cache file */
367 struct file *cfile; /* Open file */
368 unsigned char *pages_written; /* Which pages have been written */
369 void *file_buf;
370 struct page *held_pages[NS_MAX_HELD_PAGES];
371 int held_cnt;
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300372
373 struct nandsim_debug_info dbg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374};
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}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200393 /* Program page starting from the beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
395 STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200396 /* Program page starting from the beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
398 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200399 /* Program page starting from the second half */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
401 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200402 /* Program OOB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 {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}},
409 /* Read multi-plane status */
410 {OPT_SMARTMEDIA, {STATE_CMD_STATUS_M, STATE_DATAOUT_STATUS_M, STATE_READY}},
411 /* Read ID */
412 {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
413 /* Large page devices read page */
414 {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300415 STATE_DATAOUT, STATE_READY}},
416 /* Large page devices random page read */
417 {OPT_LARGEPAGE, {STATE_CMD_RNDOUT, STATE_ADDR_COLUMN, STATE_CMD_RNDOUTSTART | ACTION_CPY,
418 STATE_DATAOUT, STATE_READY}},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419};
420
Adrian Hunter514087e72007-03-19 12:47:45 +0200421struct weak_block {
422 struct list_head list;
423 unsigned int erase_block_no;
424 unsigned int max_erases;
425 unsigned int erases_done;
426};
427
428static LIST_HEAD(weak_blocks);
429
430struct weak_page {
431 struct list_head list;
432 unsigned int page_no;
433 unsigned int max_writes;
434 unsigned int writes_done;
435};
436
437static LIST_HEAD(weak_pages);
438
439struct grave_page {
440 struct list_head list;
441 unsigned int page_no;
442 unsigned int max_reads;
443 unsigned int reads_done;
444};
445
446static LIST_HEAD(grave_pages);
447
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200448static unsigned long *erase_block_wear = NULL;
449static unsigned int wear_eb_count = 0;
450static unsigned long total_wear = 0;
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452/* MTD structure for NAND controller */
453static struct mtd_info *nsmtd;
454
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300455static int nandsim_debugfs_show(struct seq_file *m, void *private)
456{
457 unsigned long wmin = -1, wmax = 0, avg;
458 unsigned long deciles[10], decile_max[10], tot = 0;
459 unsigned int i;
460
461 /* Calc wear stats */
462 for (i = 0; i < wear_eb_count; ++i) {
463 unsigned long wear = erase_block_wear[i];
464 if (wear < wmin)
465 wmin = wear;
466 if (wear > wmax)
467 wmax = wear;
468 tot += wear;
469 }
470
471 for (i = 0; i < 9; ++i) {
472 deciles[i] = 0;
473 decile_max[i] = (wmax * (i + 1) + 5) / 10;
474 }
475 deciles[9] = 0;
476 decile_max[9] = wmax;
477 for (i = 0; i < wear_eb_count; ++i) {
478 int d;
479 unsigned long wear = erase_block_wear[i];
480 for (d = 0; d < 10; ++d)
481 if (wear <= decile_max[d]) {
482 deciles[d] += 1;
483 break;
484 }
485 }
486 avg = tot / wear_eb_count;
487
488 /* Output wear report */
489 seq_printf(m, "Total numbers of erases: %lu\n", tot);
490 seq_printf(m, "Number of erase blocks: %u\n", wear_eb_count);
491 seq_printf(m, "Average number of erases: %lu\n", avg);
492 seq_printf(m, "Maximum number of erases: %lu\n", wmax);
493 seq_printf(m, "Minimum number of erases: %lu\n", wmin);
494 for (i = 0; i < 10; ++i) {
495 unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
496 if (from > decile_max[i])
497 continue;
498 seq_printf(m, "Number of ebs with erase counts from %lu to %lu : %lu\n",
499 from,
500 decile_max[i],
501 deciles[i]);
502 }
503
504 return 0;
505}
506
507static int nandsim_debugfs_open(struct inode *inode, struct file *file)
508{
509 return single_open(file, nandsim_debugfs_show, inode->i_private);
510}
511
512static const struct file_operations dfs_fops = {
513 .open = nandsim_debugfs_open,
514 .read = seq_read,
515 .llseek = seq_lseek,
516 .release = single_release,
517};
518
519/**
520 * nandsim_debugfs_create - initialize debugfs
521 * @dev: nandsim device description object
522 *
523 * This function creates all debugfs files for UBI device @ubi. Returns zero in
524 * case of success and a negative error code in case of failure.
525 */
526static int nandsim_debugfs_create(struct nandsim *dev)
527{
528 struct nandsim_debug_info *dbg = &dev->dbg;
529 struct dentry *dent;
530 int err;
531
532 if (!IS_ENABLED(CONFIG_DEBUG_FS))
533 return 0;
534
535 dent = debugfs_create_dir("nandsim", NULL);
536 if (IS_ERR_OR_NULL(dent)) {
537 int err = dent ? -ENODEV : PTR_ERR(dent);
538
539 NS_ERR("cannot create \"nandsim\" debugfs directory, err %d\n",
540 err);
541 return err;
542 }
543 dbg->dfs_root = dent;
544
545 dent = debugfs_create_file("wear_report", S_IRUSR,
546 dbg->dfs_root, dev, &dfs_fops);
547 if (IS_ERR_OR_NULL(dent))
548 goto out_remove;
549 dbg->dfs_wear_report = dent;
550
551 return 0;
552
553out_remove:
554 debugfs_remove_recursive(dbg->dfs_root);
555 err = dent ? PTR_ERR(dent) : -ENODEV;
556 return err;
557}
558
559/**
560 * nandsim_debugfs_remove - destroy all debugfs files
561 */
562static void nandsim_debugfs_remove(struct nandsim *ns)
563{
564 if (IS_ENABLED(CONFIG_DEBUG_FS))
565 debugfs_remove_recursive(ns->dbg.dfs_root);
566}
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568/*
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000569 * Allocate array of page pointers, create slab allocation for an array
570 * and initialize the array by NULL pointers.
Vijay Kumard086d432006-10-08 22:02:31 +0530571 *
572 * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
573 */
Vijay Kumara5602142006-10-14 21:33:34 +0530574static int alloc_device(struct nandsim *ns)
Vijay Kumard086d432006-10-08 22:02:31 +0530575{
Adrian Huntera9fc8992008-11-12 16:06:07 +0200576 struct file *cfile;
577 int i, err;
578
579 if (cache_file) {
580 cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
581 if (IS_ERR(cfile))
582 return PTR_ERR(cfile);
583 if (!cfile->f_op || (!cfile->f_op->read && !cfile->f_op->aio_read)) {
584 NS_ERR("alloc_device: cache file not readable\n");
585 err = -EINVAL;
586 goto err_close;
587 }
588 if (!cfile->f_op->write && !cfile->f_op->aio_write) {
589 NS_ERR("alloc_device: cache file not writeable\n");
590 err = -EINVAL;
591 goto err_close;
592 }
Joe Perches309b5e42010-11-04 20:07:40 -0700593 ns->pages_written = vzalloc(ns->geom.pgnum);
Adrian Huntera9fc8992008-11-12 16:06:07 +0200594 if (!ns->pages_written) {
595 NS_ERR("alloc_device: unable to allocate pages written array\n");
596 err = -ENOMEM;
597 goto err_close;
598 }
599 ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
600 if (!ns->file_buf) {
601 NS_ERR("alloc_device: unable to allocate file buf\n");
602 err = -ENOMEM;
603 goto err_free;
604 }
605 ns->cfile = cfile;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200606 return 0;
607 }
Vijay Kumard086d432006-10-08 22:02:31 +0530608
609 ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
610 if (!ns->pages) {
Adrian Huntera9fc8992008-11-12 16:06:07 +0200611 NS_ERR("alloc_device: unable to allocate page array\n");
Vijay Kumard086d432006-10-08 22:02:31 +0530612 return -ENOMEM;
613 }
614 for (i = 0; i < ns->geom.pgnum; i++) {
615 ns->pages[i].byte = NULL;
616 }
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000617 ns->nand_pages_slab = kmem_cache_create("nandsim",
618 ns->geom.pgszoob, 0, 0, NULL);
619 if (!ns->nand_pages_slab) {
620 NS_ERR("cache_create: unable to create kmem_cache\n");
621 return -ENOMEM;
622 }
Vijay Kumard086d432006-10-08 22:02:31 +0530623
624 return 0;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200625
626err_free:
627 vfree(ns->pages_written);
628err_close:
629 filp_close(cfile, NULL);
630 return err;
Vijay Kumard086d432006-10-08 22:02:31 +0530631}
632
633/*
634 * Free any allocated pages, and free the array of page pointers.
635 */
Vijay Kumara5602142006-10-14 21:33:34 +0530636static void free_device(struct nandsim *ns)
Vijay Kumard086d432006-10-08 22:02:31 +0530637{
638 int i;
639
Adrian Huntera9fc8992008-11-12 16:06:07 +0200640 if (ns->cfile) {
641 kfree(ns->file_buf);
642 vfree(ns->pages_written);
643 filp_close(ns->cfile, NULL);
644 return;
645 }
646
Vijay Kumard086d432006-10-08 22:02:31 +0530647 if (ns->pages) {
648 for (i = 0; i < ns->geom.pgnum; i++) {
649 if (ns->pages[i].byte)
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000650 kmem_cache_free(ns->nand_pages_slab,
651 ns->pages[i].byte);
Vijay Kumard086d432006-10-08 22:02:31 +0530652 }
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000653 kmem_cache_destroy(ns->nand_pages_slab);
Vijay Kumard086d432006-10-08 22:02:31 +0530654 vfree(ns->pages);
655 }
656}
657
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200658static char *get_partition_name(int i)
659{
660 char buf[64];
661 sprintf(buf, "NAND simulator partition %d", i);
662 return kstrdup(buf, GFP_KERNEL);
663}
664
Vijay Kumard086d432006-10-08 22:02:31 +0530665/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 * Initialize the nandsim structure.
667 *
668 * RETURNS: 0 if success, -ERRNO if failure.
669 */
Vijay Kumara5602142006-10-14 21:33:34 +0530670static int init_nandsim(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Kulikov Vasiliy7b8516b2010-06-29 14:15:17 +0400672 struct nand_chip *chip = mtd->priv;
673 struct nandsim *ns = chip->priv;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200674 int i, ret = 0;
David Woodhouse0f07a0b2008-12-10 14:01:46 +0000675 uint64_t remains;
676 uint64_t next_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677
678 if (NS_IS_INITIALIZED(ns)) {
679 NS_ERR("init_nandsim: nandsim is already initialized\n");
680 return -EIO;
681 }
682
683 /* Force mtd to not do delays */
684 chip->chip_delay = 0;
685
686 /* Initialize the NAND flash parameters */
687 ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
688 ns->geom.totsz = mtd->size;
Joern Engel28318772006-05-22 23:18:05 +0200689 ns->geom.pgsz = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 ns->geom.oobsz = mtd->oobsize;
691 ns->geom.secsz = mtd->erasesize;
692 ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
Herton Ronaldo Krzesinski596fd462012-05-16 16:21:52 -0300693 ns->geom.pgnum = div_u64(ns->geom.totsz, ns->geom.pgsz);
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300694 ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 ns->geom.secshift = ffs(ns->geom.secsz) - 1;
696 ns->geom.pgshift = chip->page_shift;
697 ns->geom.oobshift = ffs(ns->geom.oobsz) - 1;
698 ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
699 ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
700 ns->options = 0;
701
702 if (ns->geom.pgsz == 256) {
703 ns->options |= OPT_PAGE256;
704 }
705 else if (ns->geom.pgsz == 512) {
Brian Norris831d3162012-05-01 17:12:53 -0700706 ns->options |= OPT_PAGE512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (ns->busw == 8)
708 ns->options |= OPT_PAGE512_8BIT;
709 } else if (ns->geom.pgsz == 2048) {
710 ns->options |= OPT_PAGE2048;
Sebastian Andrzej Siewior75352662009-11-29 19:07:57 +0100711 } else if (ns->geom.pgsz == 4096) {
712 ns->options |= OPT_PAGE4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 } else {
714 NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
715 return -EIO;
716 }
717
718 if (ns->options & OPT_SMALLPAGE) {
Adrian Hunteraf3decc2008-05-30 15:56:18 +0300719 if (ns->geom.totsz <= (32 << 20)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 ns->geom.pgaddrbytes = 3;
721 ns->geom.secaddrbytes = 2;
722 } else {
723 ns->geom.pgaddrbytes = 4;
724 ns->geom.secaddrbytes = 3;
725 }
726 } else {
727 if (ns->geom.totsz <= (128 << 20)) {
Artem Bityutskiy4a0c50c2006-12-06 21:52:32 +0200728 ns->geom.pgaddrbytes = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 ns->geom.secaddrbytes = 2;
730 } else {
731 ns->geom.pgaddrbytes = 5;
732 ns->geom.secaddrbytes = 3;
733 }
734 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000735
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200736 /* Fill the partition_info structure */
737 if (parts_num > ARRAY_SIZE(ns->partitions)) {
738 NS_ERR("too many partitions.\n");
739 ret = -EINVAL;
740 goto error;
741 }
742 remains = ns->geom.totsz;
743 next_offset = 0;
744 for (i = 0; i < parts_num; ++i) {
David Woodhouse0f07a0b2008-12-10 14:01:46 +0000745 uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300746
747 if (!part_sz || part_sz > remains) {
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200748 NS_ERR("bad partition size.\n");
749 ret = -EINVAL;
750 goto error;
751 }
752 ns->partitions[i].name = get_partition_name(i);
753 ns->partitions[i].offset = next_offset;
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300754 ns->partitions[i].size = part_sz;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200755 next_offset += ns->partitions[i].size;
756 remains -= ns->partitions[i].size;
757 }
758 ns->nbparts = parts_num;
759 if (remains) {
760 if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
761 NS_ERR("too many partitions.\n");
762 ret = -EINVAL;
763 goto error;
764 }
765 ns->partitions[i].name = get_partition_name(i);
766 ns->partitions[i].offset = next_offset;
767 ns->partitions[i].size = remains;
768 ns->nbparts += 1;
769 }
770
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 /* Detect how many ID bytes the NAND chip outputs */
772 for (i = 0; nand_flash_ids[i].name != NULL; i++) {
773 if (second_id_byte != nand_flash_ids[i].id)
774 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777 if (ns->busw == 16)
778 NS_WARN("16-bit flashes support wasn't tested\n");
779
Andrew Mortone4c094a2008-07-30 12:35:04 -0700780 printk("flash size: %llu MiB\n",
781 (unsigned long long)ns->geom.totsz >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 printk("page size: %u bytes\n", ns->geom.pgsz);
783 printk("OOB area size: %u bytes\n", ns->geom.oobsz);
784 printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
785 printk("pages number: %u\n", ns->geom.pgnum);
786 printk("pages per sector: %u\n", ns->geom.pgsec);
787 printk("bus width: %u\n", ns->busw);
788 printk("bits in sector size: %u\n", ns->geom.secshift);
789 printk("bits in page size: %u\n", ns->geom.pgshift);
Andrew Mortone4c094a2008-07-30 12:35:04 -0700790 printk("bits in OOB size: %u\n", ns->geom.oobshift);
791 printk("flash size with OOB: %llu KiB\n",
792 (unsigned long long)ns->geom.totszoob >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
794 printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
795 printk("options: %#x\n", ns->options);
796
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200797 if ((ret = alloc_device(ns)) != 0)
Vijay Kumard086d432006-10-08 22:02:31 +0530798 goto error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799
800 /* Allocate / initialize the internal buffer */
801 ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
802 if (!ns->buf.byte) {
803 NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
804 ns->geom.pgszoob);
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200805 ret = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 goto error;
807 }
808 memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
809
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 return 0;
811
812error:
Vijay Kumard086d432006-10-08 22:02:31 +0530813 free_device(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200815 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
818/*
819 * Free the nandsim structure.
820 */
Vijay Kumara5602142006-10-14 21:33:34 +0530821static void free_nandsim(struct nandsim *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 kfree(ns->buf.byte);
Vijay Kumard086d432006-10-08 22:02:31 +0530824 free_device(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 return;
827}
828
Adrian Hunter514087e72007-03-19 12:47:45 +0200829static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
830{
831 char *w;
832 int zero_ok;
833 unsigned int erase_block_no;
834 loff_t offset;
835
836 if (!badblocks)
837 return 0;
838 w = badblocks;
839 do {
840 zero_ok = (*w == '0' ? 1 : 0);
841 erase_block_no = simple_strtoul(w, &w, 0);
842 if (!zero_ok && !erase_block_no) {
843 NS_ERR("invalid badblocks.\n");
844 return -EINVAL;
845 }
846 offset = erase_block_no * ns->geom.secsz;
Artem Bityutskiy5942ddb2011-12-23 19:37:38 +0200847 if (mtd_block_markbad(mtd, offset)) {
Adrian Hunter514087e72007-03-19 12:47:45 +0200848 NS_ERR("invalid badblocks.\n");
849 return -EINVAL;
850 }
851 if (*w == ',')
852 w += 1;
853 } while (*w);
854 return 0;
855}
856
857static int parse_weakblocks(void)
858{
859 char *w;
860 int zero_ok;
861 unsigned int erase_block_no;
862 unsigned int max_erases;
863 struct weak_block *wb;
864
865 if (!weakblocks)
866 return 0;
867 w = weakblocks;
868 do {
869 zero_ok = (*w == '0' ? 1 : 0);
870 erase_block_no = simple_strtoul(w, &w, 0);
871 if (!zero_ok && !erase_block_no) {
872 NS_ERR("invalid weakblocks.\n");
873 return -EINVAL;
874 }
875 max_erases = 3;
876 if (*w == ':') {
877 w += 1;
878 max_erases = simple_strtoul(w, &w, 0);
879 }
880 if (*w == ',')
881 w += 1;
882 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
883 if (!wb) {
884 NS_ERR("unable to allocate memory.\n");
885 return -ENOMEM;
886 }
887 wb->erase_block_no = erase_block_no;
888 wb->max_erases = max_erases;
889 list_add(&wb->list, &weak_blocks);
890 } while (*w);
891 return 0;
892}
893
894static int erase_error(unsigned int erase_block_no)
895{
896 struct weak_block *wb;
897
898 list_for_each_entry(wb, &weak_blocks, list)
899 if (wb->erase_block_no == erase_block_no) {
900 if (wb->erases_done >= wb->max_erases)
901 return 1;
902 wb->erases_done += 1;
903 return 0;
904 }
905 return 0;
906}
907
908static int parse_weakpages(void)
909{
910 char *w;
911 int zero_ok;
912 unsigned int page_no;
913 unsigned int max_writes;
914 struct weak_page *wp;
915
916 if (!weakpages)
917 return 0;
918 w = weakpages;
919 do {
920 zero_ok = (*w == '0' ? 1 : 0);
921 page_no = simple_strtoul(w, &w, 0);
922 if (!zero_ok && !page_no) {
923 NS_ERR("invalid weakpagess.\n");
924 return -EINVAL;
925 }
926 max_writes = 3;
927 if (*w == ':') {
928 w += 1;
929 max_writes = simple_strtoul(w, &w, 0);
930 }
931 if (*w == ',')
932 w += 1;
933 wp = kzalloc(sizeof(*wp), GFP_KERNEL);
934 if (!wp) {
935 NS_ERR("unable to allocate memory.\n");
936 return -ENOMEM;
937 }
938 wp->page_no = page_no;
939 wp->max_writes = max_writes;
940 list_add(&wp->list, &weak_pages);
941 } while (*w);
942 return 0;
943}
944
945static int write_error(unsigned int page_no)
946{
947 struct weak_page *wp;
948
949 list_for_each_entry(wp, &weak_pages, list)
950 if (wp->page_no == page_no) {
951 if (wp->writes_done >= wp->max_writes)
952 return 1;
953 wp->writes_done += 1;
954 return 0;
955 }
956 return 0;
957}
958
959static int parse_gravepages(void)
960{
961 char *g;
962 int zero_ok;
963 unsigned int page_no;
964 unsigned int max_reads;
965 struct grave_page *gp;
966
967 if (!gravepages)
968 return 0;
969 g = gravepages;
970 do {
971 zero_ok = (*g == '0' ? 1 : 0);
972 page_no = simple_strtoul(g, &g, 0);
973 if (!zero_ok && !page_no) {
974 NS_ERR("invalid gravepagess.\n");
975 return -EINVAL;
976 }
977 max_reads = 3;
978 if (*g == ':') {
979 g += 1;
980 max_reads = simple_strtoul(g, &g, 0);
981 }
982 if (*g == ',')
983 g += 1;
984 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
985 if (!gp) {
986 NS_ERR("unable to allocate memory.\n");
987 return -ENOMEM;
988 }
989 gp->page_no = page_no;
990 gp->max_reads = max_reads;
991 list_add(&gp->list, &grave_pages);
992 } while (*g);
993 return 0;
994}
995
996static int read_error(unsigned int page_no)
997{
998 struct grave_page *gp;
999
1000 list_for_each_entry(gp, &grave_pages, list)
1001 if (gp->page_no == page_no) {
1002 if (gp->reads_done >= gp->max_reads)
1003 return 1;
1004 gp->reads_done += 1;
1005 return 0;
1006 }
1007 return 0;
1008}
1009
1010static void free_lists(void)
1011{
1012 struct list_head *pos, *n;
1013 list_for_each_safe(pos, n, &weak_blocks) {
1014 list_del(pos);
1015 kfree(list_entry(pos, struct weak_block, list));
1016 }
1017 list_for_each_safe(pos, n, &weak_pages) {
1018 list_del(pos);
1019 kfree(list_entry(pos, struct weak_page, list));
1020 }
1021 list_for_each_safe(pos, n, &grave_pages) {
1022 list_del(pos);
1023 kfree(list_entry(pos, struct grave_page, list));
1024 }
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001025 kfree(erase_block_wear);
1026}
1027
1028static int setup_wear_reporting(struct mtd_info *mtd)
1029{
1030 size_t mem;
1031
Herton Ronaldo Krzesinski596fd462012-05-16 16:21:52 -03001032 wear_eb_count = div_u64(mtd->size, mtd->erasesize);
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001033 mem = wear_eb_count * sizeof(unsigned long);
1034 if (mem / sizeof(unsigned long) != wear_eb_count) {
1035 NS_ERR("Too many erase blocks for wear reporting\n");
1036 return -ENOMEM;
1037 }
1038 erase_block_wear = kzalloc(mem, GFP_KERNEL);
1039 if (!erase_block_wear) {
1040 NS_ERR("Too many erase blocks for wear reporting\n");
1041 return -ENOMEM;
1042 }
1043 return 0;
1044}
1045
1046static void update_wear(unsigned int erase_block_no)
1047{
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001048 if (!erase_block_wear)
1049 return;
1050 total_wear += 1;
Ezequiel Garcia5346c272012-12-03 10:31:40 -03001051 /*
1052 * TODO: Notify this through a debugfs entry,
1053 * instead of showing an error message.
1054 */
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001055 if (total_wear == 0)
1056 NS_ERR("Erase counter total overflow\n");
1057 erase_block_wear[erase_block_no] += 1;
1058 if (erase_block_wear[erase_block_no] == 0)
1059 NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
Adrian Hunter514087e72007-03-19 12:47:45 +02001060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062/*
1063 * Returns the string representation of 'state' state.
1064 */
Vijay Kumara5602142006-10-14 21:33:34 +05301065static char *get_state_name(uint32_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066{
1067 switch (NS_STATE(state)) {
1068 case STATE_CMD_READ0:
1069 return "STATE_CMD_READ0";
1070 case STATE_CMD_READ1:
1071 return "STATE_CMD_READ1";
1072 case STATE_CMD_PAGEPROG:
1073 return "STATE_CMD_PAGEPROG";
1074 case STATE_CMD_READOOB:
1075 return "STATE_CMD_READOOB";
1076 case STATE_CMD_READSTART:
1077 return "STATE_CMD_READSTART";
1078 case STATE_CMD_ERASE1:
1079 return "STATE_CMD_ERASE1";
1080 case STATE_CMD_STATUS:
1081 return "STATE_CMD_STATUS";
1082 case STATE_CMD_STATUS_M:
1083 return "STATE_CMD_STATUS_M";
1084 case STATE_CMD_SEQIN:
1085 return "STATE_CMD_SEQIN";
1086 case STATE_CMD_READID:
1087 return "STATE_CMD_READID";
1088 case STATE_CMD_ERASE2:
1089 return "STATE_CMD_ERASE2";
1090 case STATE_CMD_RESET:
1091 return "STATE_CMD_RESET";
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001092 case STATE_CMD_RNDOUT:
1093 return "STATE_CMD_RNDOUT";
1094 case STATE_CMD_RNDOUTSTART:
1095 return "STATE_CMD_RNDOUTSTART";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 case STATE_ADDR_PAGE:
1097 return "STATE_ADDR_PAGE";
1098 case STATE_ADDR_SEC:
1099 return "STATE_ADDR_SEC";
1100 case STATE_ADDR_ZERO:
1101 return "STATE_ADDR_ZERO";
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001102 case STATE_ADDR_COLUMN:
1103 return "STATE_ADDR_COLUMN";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 case STATE_DATAIN:
1105 return "STATE_DATAIN";
1106 case STATE_DATAOUT:
1107 return "STATE_DATAOUT";
1108 case STATE_DATAOUT_ID:
1109 return "STATE_DATAOUT_ID";
1110 case STATE_DATAOUT_STATUS:
1111 return "STATE_DATAOUT_STATUS";
1112 case STATE_DATAOUT_STATUS_M:
1113 return "STATE_DATAOUT_STATUS_M";
1114 case STATE_READY:
1115 return "STATE_READY";
1116 case STATE_UNKNOWN:
1117 return "STATE_UNKNOWN";
1118 }
1119
1120 NS_ERR("get_state_name: unknown state, BUG\n");
1121 return NULL;
1122}
1123
1124/*
1125 * Check if command is valid.
1126 *
1127 * RETURNS: 1 if wrong command, 0 if right.
1128 */
Vijay Kumara5602142006-10-14 21:33:34 +05301129static int check_command(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130{
1131 switch (cmd) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001132
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 case NAND_CMD_READ0:
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001134 case NAND_CMD_READ1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 case NAND_CMD_READSTART:
1136 case NAND_CMD_PAGEPROG:
1137 case NAND_CMD_READOOB:
1138 case NAND_CMD_ERASE1:
1139 case NAND_CMD_STATUS:
1140 case NAND_CMD_SEQIN:
1141 case NAND_CMD_READID:
1142 case NAND_CMD_ERASE2:
1143 case NAND_CMD_RESET:
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001144 case NAND_CMD_RNDOUT:
1145 case NAND_CMD_RNDOUTSTART:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 return 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 case NAND_CMD_STATUS_MULTI:
1149 default:
1150 return 1;
1151 }
1152}
1153
1154/*
1155 * Returns state after command is accepted by command number.
1156 */
Vijay Kumara5602142006-10-14 21:33:34 +05301157static uint32_t get_state_by_command(unsigned command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158{
1159 switch (command) {
1160 case NAND_CMD_READ0:
1161 return STATE_CMD_READ0;
1162 case NAND_CMD_READ1:
1163 return STATE_CMD_READ1;
1164 case NAND_CMD_PAGEPROG:
1165 return STATE_CMD_PAGEPROG;
1166 case NAND_CMD_READSTART:
1167 return STATE_CMD_READSTART;
1168 case NAND_CMD_READOOB:
1169 return STATE_CMD_READOOB;
1170 case NAND_CMD_ERASE1:
1171 return STATE_CMD_ERASE1;
1172 case NAND_CMD_STATUS:
1173 return STATE_CMD_STATUS;
1174 case NAND_CMD_STATUS_MULTI:
1175 return STATE_CMD_STATUS_M;
1176 case NAND_CMD_SEQIN:
1177 return STATE_CMD_SEQIN;
1178 case NAND_CMD_READID:
1179 return STATE_CMD_READID;
1180 case NAND_CMD_ERASE2:
1181 return STATE_CMD_ERASE2;
1182 case NAND_CMD_RESET:
1183 return STATE_CMD_RESET;
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001184 case NAND_CMD_RNDOUT:
1185 return STATE_CMD_RNDOUT;
1186 case NAND_CMD_RNDOUTSTART:
1187 return STATE_CMD_RNDOUTSTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189
1190 NS_ERR("get_state_by_command: unknown command, BUG\n");
1191 return 0;
1192}
1193
1194/*
1195 * Move an address byte to the correspondent internal register.
1196 */
Vijay Kumara5602142006-10-14 21:33:34 +05301197static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198{
1199 uint byte = (uint)bt;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001200
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
1202 ns->regs.column |= (byte << 8 * ns->regs.count);
1203 else {
1204 ns->regs.row |= (byte << 8 * (ns->regs.count -
1205 ns->geom.pgaddrbytes +
1206 ns->geom.secaddrbytes));
1207 }
1208
1209 return;
1210}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212/*
1213 * Switch to STATE_READY state.
1214 */
Vijay Kumara5602142006-10-14 21:33:34 +05301215static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216{
1217 NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
1218
1219 ns->state = STATE_READY;
1220 ns->nxstate = STATE_UNKNOWN;
1221 ns->op = NULL;
1222 ns->npstates = 0;
1223 ns->stateidx = 0;
1224 ns->regs.num = 0;
1225 ns->regs.count = 0;
1226 ns->regs.off = 0;
1227 ns->regs.row = 0;
1228 ns->regs.column = 0;
1229 ns->regs.status = status;
1230}
1231
1232/*
1233 * If the operation isn't known yet, try to find it in the global array
1234 * of supported operations.
1235 *
1236 * Operation can be unknown because of the following.
srimugunthandaf05ec2010-11-13 12:46:05 +02001237 * 1. New command was accepted and this is the first call to find the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 * correspondent states chain. In this case ns->npstates = 0;
srimugunthandaf05ec2010-11-13 12:46:05 +02001239 * 2. There are several operations which begin with the same command(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 * (for example program from the second half and read from the
1241 * second half operations both begin with the READ1 command). In this
1242 * case the ns->pstates[] array contains previous states.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001243 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 * Thus, the function tries to find operation containing the following
1245 * states (if the 'flag' parameter is 0):
1246 * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
1247 *
1248 * If (one and only one) matching operation is found, it is accepted (
1249 * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
1250 * zeroed).
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001251 *
srimugunthandaf05ec2010-11-13 12:46:05 +02001252 * If there are several matches, the current state is pushed to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253 * ns->pstates.
1254 *
1255 * The operation can be unknown only while commands are input to the chip.
1256 * As soon as address command is accepted, the operation must be known.
1257 * In such situation the function is called with 'flag' != 0, and the
1258 * operation is searched using the following pattern:
1259 * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001260 *
srimugunthandaf05ec2010-11-13 12:46:05 +02001261 * It is supposed that this pattern must either match one operation or
Linus Torvalds1da177e2005-04-16 15:20:36 -07001262 * none. There can't be ambiguity in that case.
1263 *
srimugunthandaf05ec2010-11-13 12:46:05 +02001264 * If no matches found, the function does the following:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 * 1. if there are saved states present, try to ignore them and search
1266 * again only using the last command. If nothing was found, switch
1267 * to the STATE_READY state.
1268 * 2. if there are no saved states, switch to the STATE_READY state.
1269 *
1270 * RETURNS: -2 - no matched operations found.
1271 * -1 - several matches.
1272 * 0 - operation is found.
1273 */
Vijay Kumara5602142006-10-14 21:33:34 +05301274static int find_operation(struct nandsim *ns, uint32_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275{
1276 int opsfound = 0;
1277 int i, j, idx = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001278
Linus Torvalds1da177e2005-04-16 15:20:36 -07001279 for (i = 0; i < NS_OPER_NUM; i++) {
1280
1281 int found = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001282
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 if (!(ns->options & ops[i].reqopts))
1284 /* Ignore operations we can't perform */
1285 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001286
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 if (flag) {
1288 if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
1289 continue;
1290 } else {
1291 if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
1292 continue;
1293 }
1294
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001295 for (j = 0; j < ns->npstates; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
1297 && (ns->options & ops[idx].reqopts)) {
1298 found = 0;
1299 break;
1300 }
1301
1302 if (found) {
1303 idx = i;
1304 opsfound += 1;
1305 }
1306 }
1307
1308 if (opsfound == 1) {
1309 /* Exact match */
1310 ns->op = &ops[idx].states[0];
1311 if (flag) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001312 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 * In this case the find_operation function was
1314 * called when address has just began input. But it isn't
1315 * yet fully input and the current state must
1316 * not be one of STATE_ADDR_*, but the STATE_ADDR_*
1317 * state must be the next state (ns->nxstate).
1318 */
1319 ns->stateidx = ns->npstates - 1;
1320 } else {
1321 ns->stateidx = ns->npstates;
1322 }
1323 ns->npstates = 0;
1324 ns->state = ns->op[ns->stateidx];
1325 ns->nxstate = ns->op[ns->stateidx + 1];
1326 NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
1327 idx, get_state_name(ns->state), get_state_name(ns->nxstate));
1328 return 0;
1329 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 if (opsfound == 0) {
1332 /* Nothing was found. Try to ignore previous commands (if any) and search again */
1333 if (ns->npstates != 0) {
1334 NS_DBG("find_operation: no operation found, try again with state %s\n",
1335 get_state_name(ns->state));
1336 ns->npstates = 0;
1337 return find_operation(ns, 0);
1338
1339 }
1340 NS_DBG("find_operation: no operations found\n");
1341 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1342 return -2;
1343 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001344
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345 if (flag) {
1346 /* This shouldn't happen */
1347 NS_DBG("find_operation: BUG, operation must be known if address is input\n");
1348 return -2;
1349 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001350
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 NS_DBG("find_operation: there is still ambiguity\n");
1352
1353 ns->pstates[ns->npstates++] = ns->state;
1354
1355 return -1;
1356}
1357
Adrian Huntera9fc8992008-11-12 16:06:07 +02001358static void put_pages(struct nandsim *ns)
1359{
1360 int i;
1361
1362 for (i = 0; i < ns->held_cnt; i++)
1363 page_cache_release(ns->held_pages[i]);
1364}
1365
1366/* Get page cache pages in advance to provide NOFS memory allocation */
1367static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
1368{
1369 pgoff_t index, start_index, end_index;
1370 struct page *page;
1371 struct address_space *mapping = file->f_mapping;
1372
1373 start_index = pos >> PAGE_CACHE_SHIFT;
1374 end_index = (pos + count - 1) >> PAGE_CACHE_SHIFT;
1375 if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
1376 return -EINVAL;
1377 ns->held_cnt = 0;
1378 for (index = start_index; index <= end_index; index++) {
1379 page = find_get_page(mapping, index);
1380 if (page == NULL) {
1381 page = find_or_create_page(mapping, index, GFP_NOFS);
1382 if (page == NULL) {
1383 write_inode_now(mapping->host, 1);
1384 page = find_or_create_page(mapping, index, GFP_NOFS);
1385 }
1386 if (page == NULL) {
1387 put_pages(ns);
1388 return -ENOMEM;
1389 }
1390 unlock_page(page);
1391 }
1392 ns->held_pages[ns->held_cnt++] = page;
1393 }
1394 return 0;
1395}
1396
1397static int set_memalloc(void)
1398{
1399 if (current->flags & PF_MEMALLOC)
1400 return 0;
1401 current->flags |= PF_MEMALLOC;
1402 return 1;
1403}
1404
1405static void clear_memalloc(int memalloc)
1406{
1407 if (memalloc)
1408 current->flags &= ~PF_MEMALLOC;
1409}
1410
1411static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t *pos)
1412{
1413 mm_segment_t old_fs;
1414 ssize_t tx;
1415 int err, memalloc;
1416
1417 err = get_pages(ns, file, count, *pos);
1418 if (err)
1419 return err;
1420 old_fs = get_fs();
1421 set_fs(get_ds());
1422 memalloc = set_memalloc();
1423 tx = vfs_read(file, (char __user *)buf, count, pos);
1424 clear_memalloc(memalloc);
1425 set_fs(old_fs);
1426 put_pages(ns);
1427 return tx;
1428}
1429
1430static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t *pos)
1431{
1432 mm_segment_t old_fs;
1433 ssize_t tx;
1434 int err, memalloc;
1435
1436 err = get_pages(ns, file, count, *pos);
1437 if (err)
1438 return err;
1439 old_fs = get_fs();
1440 set_fs(get_ds());
1441 memalloc = set_memalloc();
1442 tx = vfs_write(file, (char __user *)buf, count, pos);
1443 clear_memalloc(memalloc);
1444 set_fs(old_fs);
1445 put_pages(ns);
1446 return tx;
1447}
1448
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449/*
Vijay Kumard086d432006-10-08 22:02:31 +05301450 * Returns a pointer to the current page.
1451 */
1452static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
1453{
1454 return &(ns->pages[ns->regs.row]);
1455}
1456
1457/*
1458 * Retuns a pointer to the current byte, within the current page.
1459 */
1460static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
1461{
1462 return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
1463}
1464
Adrian Huntera9fc8992008-11-12 16:06:07 +02001465int do_read_error(struct nandsim *ns, int num)
1466{
1467 unsigned int page_no = ns->regs.row;
1468
1469 if (read_error(page_no)) {
1470 int i;
1471 memset(ns->buf.byte, 0xFF, num);
1472 for (i = 0; i < num; ++i)
1473 ns->buf.byte[i] = random32();
1474 NS_WARN("simulating read error in page %u\n", page_no);
1475 return 1;
1476 }
1477 return 0;
1478}
1479
1480void do_bit_flips(struct nandsim *ns, int num)
1481{
1482 if (bitflips && random32() < (1 << 22)) {
1483 int flips = 1;
1484 if (bitflips > 1)
1485 flips = (random32() % (int) bitflips) + 1;
1486 while (flips--) {
1487 int pos = random32() % (num * 8);
1488 ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
1489 NS_WARN("read_page: flipping bit %d in page %d "
1490 "reading from %d ecc: corrected=%u failed=%u\n",
1491 pos, ns->regs.row, ns->regs.column + ns->regs.off,
1492 nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
1493 }
1494 }
1495}
1496
Vijay Kumard086d432006-10-08 22:02:31 +05301497/*
1498 * Fill the NAND buffer with data read from the specified page.
1499 */
1500static void read_page(struct nandsim *ns, int num)
1501{
1502 union ns_mem *mypage;
1503
Adrian Huntera9fc8992008-11-12 16:06:07 +02001504 if (ns->cfile) {
1505 if (!ns->pages_written[ns->regs.row]) {
1506 NS_DBG("read_page: page %d not written\n", ns->regs.row);
1507 memset(ns->buf.byte, 0xFF, num);
1508 } else {
1509 loff_t pos;
1510 ssize_t tx;
1511
1512 NS_DBG("read_page: page %d written, reading from %d\n",
1513 ns->regs.row, ns->regs.column + ns->regs.off);
1514 if (do_read_error(ns, num))
1515 return;
1516 pos = (loff_t)ns->regs.row * ns->geom.pgszoob + ns->regs.column + ns->regs.off;
1517 tx = read_file(ns, ns->cfile, ns->buf.byte, num, &pos);
1518 if (tx != num) {
1519 NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1520 return;
1521 }
1522 do_bit_flips(ns, num);
1523 }
1524 return;
1525 }
1526
Vijay Kumard086d432006-10-08 22:02:31 +05301527 mypage = NS_GET_PAGE(ns);
1528 if (mypage->byte == NULL) {
1529 NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
1530 memset(ns->buf.byte, 0xFF, num);
1531 } else {
1532 NS_DBG("read_page: page %d allocated, reading from %d\n",
1533 ns->regs.row, ns->regs.column + ns->regs.off);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001534 if (do_read_error(ns, num))
Adrian Hunter514087e72007-03-19 12:47:45 +02001535 return;
Vijay Kumard086d432006-10-08 22:02:31 +05301536 memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001537 do_bit_flips(ns, num);
Vijay Kumard086d432006-10-08 22:02:31 +05301538 }
1539}
1540
1541/*
1542 * Erase all pages in the specified sector.
1543 */
1544static void erase_sector(struct nandsim *ns)
1545{
1546 union ns_mem *mypage;
1547 int i;
1548
Adrian Huntera9fc8992008-11-12 16:06:07 +02001549 if (ns->cfile) {
1550 for (i = 0; i < ns->geom.pgsec; i++)
1551 if (ns->pages_written[ns->regs.row + i]) {
1552 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
1553 ns->pages_written[ns->regs.row + i] = 0;
1554 }
1555 return;
1556 }
1557
Vijay Kumard086d432006-10-08 22:02:31 +05301558 mypage = NS_GET_PAGE(ns);
1559 for (i = 0; i < ns->geom.pgsec; i++) {
1560 if (mypage->byte != NULL) {
1561 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
Alexey Korolev8a4c2492008-11-13 13:40:38 +00001562 kmem_cache_free(ns->nand_pages_slab, mypage->byte);
Vijay Kumard086d432006-10-08 22:02:31 +05301563 mypage->byte = NULL;
1564 }
1565 mypage++;
1566 }
1567}
1568
1569/*
1570 * Program the specified page with the contents from the NAND buffer.
1571 */
1572static int prog_page(struct nandsim *ns, int num)
1573{
Artem Bityutskiy82810b7b62006-10-20 11:23:56 +03001574 int i;
Vijay Kumard086d432006-10-08 22:02:31 +05301575 union ns_mem *mypage;
1576 u_char *pg_off;
1577
Adrian Huntera9fc8992008-11-12 16:06:07 +02001578 if (ns->cfile) {
1579 loff_t off, pos;
1580 ssize_t tx;
1581 int all;
1582
1583 NS_DBG("prog_page: writing page %d\n", ns->regs.row);
1584 pg_off = ns->file_buf + ns->regs.column + ns->regs.off;
1585 off = (loff_t)ns->regs.row * ns->geom.pgszoob + ns->regs.column + ns->regs.off;
1586 if (!ns->pages_written[ns->regs.row]) {
1587 all = 1;
1588 memset(ns->file_buf, 0xff, ns->geom.pgszoob);
1589 } else {
1590 all = 0;
1591 pos = off;
1592 tx = read_file(ns, ns->cfile, pg_off, num, &pos);
1593 if (tx != num) {
1594 NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1595 return -1;
1596 }
1597 }
1598 for (i = 0; i < num; i++)
1599 pg_off[i] &= ns->buf.byte[i];
1600 if (all) {
1601 pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
1602 tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, &pos);
1603 if (tx != ns->geom.pgszoob) {
1604 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1605 return -1;
1606 }
1607 ns->pages_written[ns->regs.row] = 1;
1608 } else {
1609 pos = off;
1610 tx = write_file(ns, ns->cfile, pg_off, num, &pos);
1611 if (tx != num) {
1612 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1613 return -1;
1614 }
1615 }
1616 return 0;
1617 }
1618
Vijay Kumard086d432006-10-08 22:02:31 +05301619 mypage = NS_GET_PAGE(ns);
1620 if (mypage->byte == NULL) {
1621 NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
Artem Bityutskiy98b830d2007-08-28 20:33:32 +03001622 /*
1623 * We allocate memory with GFP_NOFS because a flash FS may
1624 * utilize this. If it is holding an FS lock, then gets here,
Alexey Korolev8a4c2492008-11-13 13:40:38 +00001625 * then kernel memory alloc runs writeback which goes to the FS
1626 * again and deadlocks. This was seen in practice.
Artem Bityutskiy98b830d2007-08-28 20:33:32 +03001627 */
Alexey Korolev8a4c2492008-11-13 13:40:38 +00001628 mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
Vijay Kumard086d432006-10-08 22:02:31 +05301629 if (mypage->byte == NULL) {
1630 NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
1631 return -1;
1632 }
1633 memset(mypage->byte, 0xFF, ns->geom.pgszoob);
1634 }
1635
1636 pg_off = NS_PAGE_BYTE_OFF(ns);
Artem Bityutskiy82810b7b62006-10-20 11:23:56 +03001637 for (i = 0; i < num; i++)
1638 pg_off[i] &= ns->buf.byte[i];
Vijay Kumard086d432006-10-08 22:02:31 +05301639
1640 return 0;
1641}
1642
1643/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644 * If state has any action bit, perform this action.
1645 *
1646 * RETURNS: 0 if success, -1 if error.
1647 */
Vijay Kumara5602142006-10-14 21:33:34 +05301648static int do_state_action(struct nandsim *ns, uint32_t action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649{
Vijay Kumard086d432006-10-08 22:02:31 +05301650 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 int busdiv = ns->busw == 8 ? 1 : 2;
Adrian Hunter514087e72007-03-19 12:47:45 +02001652 unsigned int erase_block_no, page_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
1654 action &= ACTION_MASK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 /* Check that page address input is correct */
1657 if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
1658 NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
1659 return -1;
1660 }
1661
1662 switch (action) {
1663
1664 case ACTION_CPY:
1665 /*
1666 * Copy page data to the internal buffer.
1667 */
1668
1669 /* Column shouldn't be very large */
1670 if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
1671 NS_ERR("do_state_action: column number is too large\n");
1672 break;
1673 }
1674 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
Vijay Kumard086d432006-10-08 22:02:31 +05301675 read_page(ns, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
1678 num, NS_RAW_OFFSET(ns) + ns->regs.off);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001679
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (ns->regs.off == 0)
1681 NS_LOG("read page %d\n", ns->regs.row);
1682 else if (ns->regs.off < ns->geom.pgsz)
1683 NS_LOG("read page %d (second half)\n", ns->regs.row);
1684 else
1685 NS_LOG("read OOB of page %d\n", ns->regs.row);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 NS_UDELAY(access_delay);
1688 NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
1689
1690 break;
1691
1692 case ACTION_SECERASE:
1693 /*
1694 * Erase sector.
1695 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001696
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 if (ns->lines.wp) {
1698 NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
1699 return -1;
1700 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
1703 || (ns->regs.row & ~(ns->geom.secsz - 1))) {
1704 NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
1705 return -1;
1706 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001707
Linus Torvalds1da177e2005-04-16 15:20:36 -07001708 ns->regs.row = (ns->regs.row <<
1709 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
1710 ns->regs.column = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001711
Adrian Hunter514087e72007-03-19 12:47:45 +02001712 erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
1713
Linus Torvalds1da177e2005-04-16 15:20:36 -07001714 NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
1715 ns->regs.row, NS_RAW_OFFSET(ns));
Adrian Hunter514087e72007-03-19 12:47:45 +02001716 NS_LOG("erase sector %u\n", erase_block_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001717
Vijay Kumard086d432006-10-08 22:02:31 +05301718 erase_sector(ns);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001719
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 NS_MDELAY(erase_delay);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001721
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001722 if (erase_block_wear)
1723 update_wear(erase_block_no);
1724
Adrian Hunter514087e72007-03-19 12:47:45 +02001725 if (erase_error(erase_block_no)) {
1726 NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
1727 return -1;
1728 }
1729
Linus Torvalds1da177e2005-04-16 15:20:36 -07001730 break;
1731
1732 case ACTION_PRGPAGE:
1733 /*
srimugunthandaf05ec2010-11-13 12:46:05 +02001734 * Program page - move internal buffer data to the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 */
1736
1737 if (ns->lines.wp) {
1738 NS_WARN("do_state_action: device is write-protected, programm\n");
1739 return -1;
1740 }
1741
1742 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1743 if (num != ns->regs.count) {
1744 NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
1745 ns->regs.count, num);
1746 return -1;
1747 }
1748
Vijay Kumard086d432006-10-08 22:02:31 +05301749 if (prog_page(ns, num) == -1)
1750 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001751
Adrian Hunter514087e72007-03-19 12:47:45 +02001752 page_no = ns->regs.row;
1753
Linus Torvalds1da177e2005-04-16 15:20:36 -07001754 NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
1755 num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
1756 NS_LOG("programm page %d\n", ns->regs.row);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001757
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 NS_UDELAY(programm_delay);
1759 NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001760
Adrian Hunter514087e72007-03-19 12:47:45 +02001761 if (write_error(page_no)) {
1762 NS_WARN("simulating write failure in page %u\n", page_no);
1763 return -1;
1764 }
1765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 case ACTION_ZEROOFF:
1769 NS_DBG("do_state_action: set internal offset to 0\n");
1770 ns->regs.off = 0;
1771 break;
1772
1773 case ACTION_HALFOFF:
1774 if (!(ns->options & OPT_PAGE512_8BIT)) {
1775 NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
1776 "byte page size 8x chips\n");
1777 return -1;
1778 }
1779 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
1780 ns->regs.off = ns->geom.pgsz/2;
1781 break;
1782
1783 case ACTION_OOBOFF:
1784 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
1785 ns->regs.off = ns->geom.pgsz;
1786 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001787
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 default:
1789 NS_DBG("do_state_action: BUG! unknown action\n");
1790 }
1791
1792 return 0;
1793}
1794
1795/*
1796 * Switch simulator's state.
1797 */
Vijay Kumara5602142006-10-14 21:33:34 +05301798static void switch_state(struct nandsim *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001799{
1800 if (ns->op) {
1801 /*
1802 * The current operation have already been identified.
1803 * Just follow the states chain.
1804 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001805
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 ns->stateidx += 1;
1807 ns->state = ns->nxstate;
1808 ns->nxstate = ns->op[ns->stateidx + 1];
1809
1810 NS_DBG("switch_state: operation is known, switch to the next state, "
1811 "state: %s, nxstate: %s\n",
1812 get_state_name(ns->state), get_state_name(ns->nxstate));
1813
1814 /* See, whether we need to do some action */
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 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 } else {
1821 /*
1822 * We don't yet know which operation we perform.
1823 * Try to identify it.
1824 */
1825
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001826 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 * The only event causing the switch_state function to
1828 * be called with yet unknown operation is new command.
1829 */
1830 ns->state = get_state_by_command(ns->regs.command);
1831
1832 NS_DBG("switch_state: operation is unknown, try to find it\n");
1833
1834 if (find_operation(ns, 0) != 0)
1835 return;
1836
1837 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1838 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1839 return;
1840 }
1841 }
1842
1843 /* For 16x devices column means the page offset in words */
1844 if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
1845 NS_DBG("switch_state: double the column number for 16x device\n");
1846 ns->regs.column <<= 1;
1847 }
1848
1849 if (NS_STATE(ns->nxstate) == STATE_READY) {
1850 /*
1851 * The current state is the last. Return to STATE_READY
1852 */
1853
1854 u_char status = NS_STATUS_OK(ns);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001855
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 /* In case of data states, see if all bytes were input/output */
1857 if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
1858 && ns->regs.count != ns->regs.num) {
1859 NS_WARN("switch_state: not all bytes were processed, %d left\n",
1860 ns->regs.num - ns->regs.count);
1861 status = NS_STATUS_FAILED(ns);
1862 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
1865
1866 switch_to_ready_state(ns, status);
1867
1868 return;
1869 } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001870 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * If the next state is data input/output, switch to it now
1872 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001873
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 ns->state = ns->nxstate;
1875 ns->nxstate = ns->op[++ns->stateidx + 1];
1876 ns->regs.num = ns->regs.count = 0;
1877
1878 NS_DBG("switch_state: the next state is data I/O, switch, "
1879 "state: %s, nxstate: %s\n",
1880 get_state_name(ns->state), get_state_name(ns->nxstate));
1881
1882 /*
1883 * Set the internal register to the count of bytes which
1884 * are expected to be input or output
1885 */
1886 switch (NS_STATE(ns->state)) {
1887 case STATE_DATAIN:
1888 case STATE_DATAOUT:
1889 ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1890 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 case STATE_DATAOUT_ID:
1893 ns->regs.num = ns->geom.idbytes;
1894 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001895
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896 case STATE_DATAOUT_STATUS:
1897 case STATE_DATAOUT_STATUS_M:
1898 ns->regs.count = ns->regs.num = 0;
1899 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001900
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 default:
1902 NS_ERR("switch_state: BUG! unknown data state\n");
1903 }
1904
1905 } else if (ns->nxstate & STATE_ADDR_MASK) {
1906 /*
1907 * If the next state is address input, set the internal
1908 * register to the number of expected address bytes
1909 */
1910
1911 ns->regs.count = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001912
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 switch (NS_STATE(ns->nxstate)) {
1914 case STATE_ADDR_PAGE:
1915 ns->regs.num = ns->geom.pgaddrbytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001916
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 break;
1918 case STATE_ADDR_SEC:
1919 ns->regs.num = ns->geom.secaddrbytes;
1920 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 case STATE_ADDR_ZERO:
1923 ns->regs.num = 1;
1924 break;
1925
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001926 case STATE_ADDR_COLUMN:
1927 /* Column address is always 2 bytes */
1928 ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
1929 break;
1930
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931 default:
1932 NS_ERR("switch_state: BUG! unknown address state\n");
1933 }
1934 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001935 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936 * Just reset internal counters.
1937 */
1938
1939 ns->regs.num = 0;
1940 ns->regs.count = 0;
1941 }
1942}
1943
Vijay Kumara5602142006-10-14 21:33:34 +05301944static u_char ns_nand_read_byte(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001945{
Kulikov Vasiliy7b8516b2010-06-29 14:15:17 +04001946 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 u_char outb = 0x00;
1948
1949 /* Sanity and correctness checks */
1950 if (!ns->lines.ce) {
1951 NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
1952 return outb;
1953 }
1954 if (ns->lines.ale || ns->lines.cle) {
1955 NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
1956 return outb;
1957 }
1958 if (!(ns->state & STATE_DATAOUT_MASK)) {
1959 NS_WARN("read_byte: unexpected data output cycle, state is %s "
1960 "return %#x\n", get_state_name(ns->state), (uint)outb);
1961 return outb;
1962 }
1963
1964 /* Status register may be read as many times as it is wanted */
1965 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
1966 NS_DBG("read_byte: return %#x status\n", ns->regs.status);
1967 return ns->regs.status;
1968 }
1969
1970 /* Check if there is any data in the internal buffer which may be read */
1971 if (ns->regs.count == ns->regs.num) {
1972 NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
1973 return outb;
1974 }
1975
1976 switch (NS_STATE(ns->state)) {
1977 case STATE_DATAOUT:
1978 if (ns->busw == 8) {
1979 outb = ns->buf.byte[ns->regs.count];
1980 ns->regs.count += 1;
1981 } else {
1982 outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
1983 ns->regs.count += 2;
1984 }
1985 break;
1986 case STATE_DATAOUT_ID:
1987 NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
1988 outb = ns->ids[ns->regs.count];
1989 ns->regs.count += 1;
1990 break;
1991 default:
1992 BUG();
1993 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001994
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995 if (ns->regs.count == ns->regs.num) {
1996 NS_DBG("read_byte: all bytes were read\n");
1997
Brian Norris831d3162012-05-01 17:12:53 -07001998 if (NS_STATE(ns->nxstate) == STATE_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 switch_state(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002000 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002001
Linus Torvalds1da177e2005-04-16 15:20:36 -07002002 return outb;
2003}
2004
Vijay Kumara5602142006-10-14 21:33:34 +05302005static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006{
Kulikov Vasiliy7b8516b2010-06-29 14:15:17 +04002007 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002008
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 /* Sanity and correctness checks */
2010 if (!ns->lines.ce) {
2011 NS_ERR("write_byte: chip is disabled, ignore write\n");
2012 return;
2013 }
2014 if (ns->lines.ale && ns->lines.cle) {
2015 NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
2016 return;
2017 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002018
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 if (ns->lines.cle == 1) {
2020 /*
2021 * The byte written is a command.
2022 */
2023
2024 if (byte == NAND_CMD_RESET) {
2025 NS_LOG("reset chip\n");
2026 switch_to_ready_state(ns, NS_STATUS_OK(ns));
2027 return;
2028 }
2029
Artem Bityutskiy74216be2008-07-30 11:18:42 +03002030 /* Check that the command byte is correct */
2031 if (check_command(byte)) {
2032 NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
2033 return;
2034 }
2035
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
2037 || NS_STATE(ns->state) == STATE_DATAOUT_STATUS_M
Artem Bityutskiy74216be2008-07-30 11:18:42 +03002038 || NS_STATE(ns->state) == STATE_DATAOUT) {
2039 int row = ns->regs.row;
2040
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041 switch_state(ns);
Artem Bityutskiy74216be2008-07-30 11:18:42 +03002042 if (byte == NAND_CMD_RNDOUT)
2043 ns->regs.row = row;
2044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045
2046 /* Check if chip is expecting command */
2047 if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
Adrian Hunter9359ea462008-11-12 16:06:40 +02002048 /* Do not warn if only 2 id bytes are read */
2049 if (!(ns->regs.command == NAND_CMD_READID &&
2050 NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
2051 /*
2052 * We are in situation when something else (not command)
2053 * was expected but command was input. In this case ignore
2054 * previous command(s)/state(s) and accept the last one.
2055 */
2056 NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
2057 "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
2058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2060 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 NS_DBG("command byte corresponding to %s state accepted\n",
2063 get_state_name(get_state_by_command(byte)));
2064 ns->regs.command = byte;
2065 switch_state(ns);
2066
2067 } else if (ns->lines.ale == 1) {
2068 /*
2069 * The byte written is an address.
2070 */
2071
2072 if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
2073
2074 NS_DBG("write_byte: operation isn't known yet, identify it\n");
2075
2076 if (find_operation(ns, 1) < 0)
2077 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002078
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
2080 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2081 return;
2082 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002083
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 ns->regs.count = 0;
2085 switch (NS_STATE(ns->nxstate)) {
2086 case STATE_ADDR_PAGE:
2087 ns->regs.num = ns->geom.pgaddrbytes;
2088 break;
2089 case STATE_ADDR_SEC:
2090 ns->regs.num = ns->geom.secaddrbytes;
2091 break;
2092 case STATE_ADDR_ZERO:
2093 ns->regs.num = 1;
2094 break;
2095 default:
2096 BUG();
2097 }
2098 }
2099
2100 /* Check that chip is expecting address */
2101 if (!(ns->nxstate & STATE_ADDR_MASK)) {
2102 NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
2103 "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
2104 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2105 return;
2106 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002107
Linus Torvalds1da177e2005-04-16 15:20:36 -07002108 /* Check if this is expected byte */
2109 if (ns->regs.count == ns->regs.num) {
2110 NS_ERR("write_byte: no more address bytes expected\n");
2111 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2112 return;
2113 }
2114
2115 accept_addr_byte(ns, byte);
2116
2117 ns->regs.count += 1;
2118
2119 NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
2120 (uint)byte, ns->regs.count, ns->regs.num);
2121
2122 if (ns->regs.count == ns->regs.num) {
2123 NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
2124 switch_state(ns);
2125 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002126
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 } else {
2128 /*
2129 * The byte written is an input data.
2130 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002131
Linus Torvalds1da177e2005-04-16 15:20:36 -07002132 /* Check that chip is expecting data input */
2133 if (!(ns->state & STATE_DATAIN_MASK)) {
2134 NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
2135 "switch to %s\n", (uint)byte,
2136 get_state_name(ns->state), get_state_name(STATE_READY));
2137 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2138 return;
2139 }
2140
2141 /* Check if this is expected byte */
2142 if (ns->regs.count == ns->regs.num) {
2143 NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
2144 ns->regs.num);
2145 return;
2146 }
2147
2148 if (ns->busw == 8) {
2149 ns->buf.byte[ns->regs.count] = byte;
2150 ns->regs.count += 1;
2151 } else {
2152 ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
2153 ns->regs.count += 2;
2154 }
2155 }
2156
2157 return;
2158}
2159
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02002160static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
2161{
2162 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
2163
2164 ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
2165 ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
2166 ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
2167
2168 if (cmd != NAND_CMD_NONE)
2169 ns_nand_write_byte(mtd, cmd);
2170}
2171
Vijay Kumara5602142006-10-14 21:33:34 +05302172static int ns_device_ready(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002173{
2174 NS_DBG("device_ready\n");
2175 return 1;
2176}
2177
Vijay Kumara5602142006-10-14 21:33:34 +05302178static uint16_t ns_nand_read_word(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
2180 struct nand_chip *chip = (struct nand_chip *)mtd->priv;
2181
2182 NS_DBG("read_word\n");
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002183
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
2185}
2186
Vijay Kumara5602142006-10-14 21:33:34 +05302187static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188{
Kulikov Vasiliy7b8516b2010-06-29 14:15:17 +04002189 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190
2191 /* Check that chip is expecting data input */
2192 if (!(ns->state & STATE_DATAIN_MASK)) {
2193 NS_ERR("write_buf: data input isn't expected, state is %s, "
2194 "switch to STATE_READY\n", get_state_name(ns->state));
2195 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2196 return;
2197 }
2198
2199 /* Check if these are expected bytes */
2200 if (ns->regs.count + len > ns->regs.num) {
2201 NS_ERR("write_buf: too many input bytes\n");
2202 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2203 return;
2204 }
2205
2206 memcpy(ns->buf.byte + ns->regs.count, buf, len);
2207 ns->regs.count += len;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002208
Linus Torvalds1da177e2005-04-16 15:20:36 -07002209 if (ns->regs.count == ns->regs.num) {
2210 NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
2211 }
2212}
2213
Vijay Kumara5602142006-10-14 21:33:34 +05302214static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002215{
Kulikov Vasiliy7b8516b2010-06-29 14:15:17 +04002216 struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217
2218 /* Sanity and correctness checks */
2219 if (!ns->lines.ce) {
2220 NS_ERR("read_buf: chip is disabled\n");
2221 return;
2222 }
2223 if (ns->lines.ale || ns->lines.cle) {
2224 NS_ERR("read_buf: ALE or CLE pin is high\n");
2225 return;
2226 }
2227 if (!(ns->state & STATE_DATAOUT_MASK)) {
2228 NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
2229 get_state_name(ns->state));
2230 return;
2231 }
2232
2233 if (NS_STATE(ns->state) != STATE_DATAOUT) {
2234 int i;
2235
2236 for (i = 0; i < len; i++)
2237 buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
2238
2239 return;
2240 }
2241
2242 /* Check if these are expected bytes */
2243 if (ns->regs.count + len > ns->regs.num) {
2244 NS_ERR("read_buf: too many bytes to read\n");
2245 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2246 return;
2247 }
2248
2249 memcpy(buf, ns->buf.byte + ns->regs.count, len);
2250 ns->regs.count += len;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002251
Linus Torvalds1da177e2005-04-16 15:20:36 -07002252 if (ns->regs.count == ns->regs.num) {
Brian Norris831d3162012-05-01 17:12:53 -07002253 if (NS_STATE(ns->nxstate) == STATE_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254 switch_state(ns);
2255 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return;
2258}
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002261 * Module initialization function
2262 */
Adrian Bunk2b9175c2005-11-29 14:49:38 +00002263static int __init ns_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264{
2265 struct nand_chip *chip;
2266 struct nandsim *nand;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002267 int retval = -ENOMEM, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002268
2269 if (bus_width != 8 && bus_width != 16) {
2270 NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
2271 return -EINVAL;
2272 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002273
Linus Torvalds1da177e2005-04-16 15:20:36 -07002274 /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
Burman Yan95b93a02006-11-15 21:10:29 +02002275 nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002276 + sizeof(struct nandsim), GFP_KERNEL);
2277 if (!nsmtd) {
2278 NS_ERR("unable to allocate core structures.\n");
2279 return -ENOMEM;
2280 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002281 chip = (struct nand_chip *)(nsmtd + 1);
2282 nsmtd->priv = (void *)chip;
2283 nand = (struct nandsim *)(chip + 1);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002284 chip->priv = (void *)nand;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285
2286 /*
2287 * Register simulator's callbacks.
2288 */
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02002289 chip->cmd_ctrl = ns_hwcontrol;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002290 chip->read_byte = ns_nand_read_byte;
2291 chip->dev_ready = ns_device_ready;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002292 chip->write_buf = ns_nand_write_buf;
2293 chip->read_buf = ns_nand_read_buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 chip->read_word = ns_nand_read_word;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002295 chip->ecc.mode = NAND_ECC_SOFT;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002296 /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
2297 /* and 'badblocks' parameters to work */
Artem B. Bityuckiy51502282005-03-19 15:33:59 +00002298 chip->options |= NAND_SKIP_BBTSCAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002299
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002300 switch (bbt) {
2301 case 2:
Brian Norrisa40f7342011-05-31 16:31:22 -07002302 chip->bbt_options |= NAND_BBT_NO_OOB;
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002303 case 1:
Brian Norrisbb9ebd42011-05-31 16:31:23 -07002304 chip->bbt_options |= NAND_BBT_USE_FLASH;
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002305 case 0:
2306 break;
2307 default:
2308 NS_ERR("bbt has to be 0..2\n");
2309 retval = -EINVAL;
2310 goto error;
2311 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002312 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002313 * Perform minimum nandsim structure initialization to handle
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002314 * the initial ID read command correctly
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 */
2316 if (third_id_byte != 0xFF || fourth_id_byte != 0xFF)
2317 nand->geom.idbytes = 4;
2318 else
2319 nand->geom.idbytes = 2;
2320 nand->regs.status = NS_STATUS_OK(nand);
2321 nand->nxstate = STATE_UNKNOWN;
2322 nand->options |= OPT_PAGE256; /* temporary value */
2323 nand->ids[0] = first_id_byte;
2324 nand->ids[1] = second_id_byte;
2325 nand->ids[2] = third_id_byte;
2326 nand->ids[3] = fourth_id_byte;
2327 if (bus_width == 16) {
2328 nand->busw = 16;
2329 chip->options |= NAND_BUSWIDTH_16;
2330 }
2331
David Woodhouse552d9202006-05-14 01:20:46 +01002332 nsmtd->owner = THIS_MODULE;
2333
Adrian Hunter514087e72007-03-19 12:47:45 +02002334 if ((retval = parse_weakblocks()) != 0)
2335 goto error;
2336
2337 if ((retval = parse_weakpages()) != 0)
2338 goto error;
2339
2340 if ((retval = parse_gravepages()) != 0)
2341 goto error;
2342
Ivan Djelicfc2ff592011-03-11 11:05:34 +01002343 retval = nand_scan_ident(nsmtd, 1, NULL);
2344 if (retval) {
2345 NS_ERR("cannot scan NAND Simulator device\n");
2346 if (retval > 0)
2347 retval = -ENXIO;
2348 goto error;
2349 }
2350
2351 if (bch) {
2352 unsigned int eccsteps, eccbytes;
2353 if (!mtd_nand_has_bch()) {
2354 NS_ERR("BCH ECC support is disabled\n");
2355 retval = -EINVAL;
2356 goto error;
2357 }
2358 /* use 512-byte ecc blocks */
2359 eccsteps = nsmtd->writesize/512;
2360 eccbytes = (bch*13+7)/8;
2361 /* do not bother supporting small page devices */
2362 if ((nsmtd->oobsize < 64) || !eccsteps) {
2363 NS_ERR("bch not available on small page devices\n");
2364 retval = -EINVAL;
2365 goto error;
2366 }
2367 if ((eccbytes*eccsteps+2) > nsmtd->oobsize) {
2368 NS_ERR("invalid bch value %u\n", bch);
2369 retval = -EINVAL;
2370 goto error;
2371 }
2372 chip->ecc.mode = NAND_ECC_SOFT_BCH;
2373 chip->ecc.size = 512;
2374 chip->ecc.bytes = eccbytes;
2375 NS_INFO("using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
2376 }
2377
2378 retval = nand_scan_tail(nsmtd);
2379 if (retval) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002380 NS_ERR("can't register NAND Simulator\n");
2381 if (retval > 0)
2382 retval = -ENXIO;
2383 goto error;
2384 }
2385
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002386 if (overridesize) {
David Woodhouse0f07a0b2008-12-10 14:01:46 +00002387 uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002388 if (new_size >> overridesize != nsmtd->erasesize) {
2389 NS_ERR("overridesize is too big\n");
Richard Genoudbb0a13a2012-09-12 14:26:26 +02002390 retval = -EINVAL;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002391 goto err_exit;
2392 }
2393 /* N.B. This relies on nand_scan not doing anything with the size before we change it */
2394 nsmtd->size = new_size;
2395 chip->chipsize = new_size;
Adrian Hunter6eda7a52008-05-30 15:56:26 +03002396 chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
Adrian Hunter07293b22008-05-30 15:56:23 +03002397 chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002398 }
2399
Adrian Hunter57aa6b52007-03-19 12:40:41 +02002400 if ((retval = setup_wear_reporting(nsmtd)) != 0)
2401 goto err_exit;
2402
Ezequiel Garcia5346c272012-12-03 10:31:40 -03002403 if ((retval = nandsim_debugfs_create(nand)) != 0)
2404 goto err_exit;
2405
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002406 if ((retval = init_nandsim(nsmtd)) != 0)
2407 goto err_exit;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002408
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002409 if ((retval = nand_default_bbt(nsmtd)) != 0)
Adrian Hunter514087e72007-03-19 12:47:45 +02002410 goto err_exit;
2411
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002412 if ((retval = parse_badblocks(nand, nsmtd)) != 0)
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002413 goto err_exit;
Artem B. Bityuckiy51502282005-03-19 15:33:59 +00002414
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002415 /* Register NAND partitions */
Jamie Ilesee0e87b2011-05-23 10:23:40 +01002416 retval = mtd_device_register(nsmtd, &nand->partitions[0],
2417 nand->nbparts);
2418 if (retval != 0)
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002419 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420
2421 return 0;
2422
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002423err_exit:
2424 free_nandsim(nand);
2425 nand_release(nsmtd);
2426 for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
2427 kfree(nand->partitions[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002428error:
2429 kfree(nsmtd);
Adrian Hunter514087e72007-03-19 12:47:45 +02002430 free_lists();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
2432 return retval;
2433}
2434
2435module_init(ns_init_module);
2436
2437/*
2438 * Module clean-up function
2439 */
2440static void __exit ns_cleanup_module(void)
2441{
Kulikov Vasiliy7b8516b2010-06-29 14:15:17 +04002442 struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002443 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002444
Ezequiel Garcia5346c272012-12-03 10:31:40 -03002445 nandsim_debugfs_remove(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 free_nandsim(ns); /* Free nandsim private resources */
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002447 nand_release(nsmtd); /* Unregister driver */
2448 for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
2449 kfree(ns->partitions[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 kfree(nsmtd); /* Free other structures */
Adrian Hunter514087e72007-03-19 12:47:45 +02002451 free_lists();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002452}
2453
2454module_exit(ns_cleanup_module);
2455
2456MODULE_LICENSE ("GPL");
2457MODULE_AUTHOR ("Artem B. Bityuckiy");
2458MODULE_DESCRIPTION ("The NAND flash simulator");