blob: 0f86dd283769b447653cb41620ee20e53cc7ee9a [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
Shreeya Patel63fa37f2018-02-22 22:01:22 +053026#define pr_fmt(fmt) "[nandsim]" fmt
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include <linux/init.h>
29#include <linux/types.h>
30#include <linux/module.h>
31#include <linux/moduleparam.h>
32#include <linux/vmalloc.h>
Herton Ronaldo Krzesinski596fd462012-05-16 16:21:52 -030033#include <linux/math64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034#include <linux/slab.h>
35#include <linux/errno.h>
36#include <linux/string.h>
37#include <linux/mtd/mtd.h>
Boris Brezillond4092d72017-08-04 17:29:10 +020038#include <linux/mtd/rawnand.h>
Ivan Djelicfc2ff592011-03-11 11:05:34 +010039#include <linux/mtd/nand_bch.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <linux/mtd/partitions.h>
41#include <linux/delay.h>
Adrian Hunter2b77a0e2007-03-19 12:46:43 +020042#include <linux/list.h>
Adrian Hunter514087e72007-03-19 12:47:45 +020043#include <linux/random.h>
Randy Dunlapa5cce422008-12-17 12:46:17 -080044#include <linux/sched.h>
Vlastimil Babkadcbe8212017-05-08 15:59:57 -070045#include <linux/sched/mm.h>
Adrian Huntera9fc8992008-11-12 16:06:07 +020046#include <linux/fs.h>
47#include <linux/pagemap.h>
Ezequiel Garcia5346c272012-12-03 10:31:40 -030048#include <linux/seq_file.h>
49#include <linux/debugfs.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
51/* Default simulator parameters values */
52#if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
53 !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
54 !defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
55 !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
56#define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
57#define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
58#define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF /* No byte */
59#define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */
60#endif
61
62#ifndef CONFIG_NANDSIM_ACCESS_DELAY
63#define CONFIG_NANDSIM_ACCESS_DELAY 25
64#endif
65#ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
66#define CONFIG_NANDSIM_PROGRAMM_DELAY 200
67#endif
68#ifndef CONFIG_NANDSIM_ERASE_DELAY
69#define CONFIG_NANDSIM_ERASE_DELAY 2
70#endif
71#ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
72#define CONFIG_NANDSIM_OUTPUT_CYCLE 40
73#endif
74#ifndef CONFIG_NANDSIM_INPUT_CYCLE
75#define CONFIG_NANDSIM_INPUT_CYCLE 50
76#endif
77#ifndef CONFIG_NANDSIM_BUS_WIDTH
78#define CONFIG_NANDSIM_BUS_WIDTH 8
79#endif
80#ifndef CONFIG_NANDSIM_DO_DELAYS
81#define CONFIG_NANDSIM_DO_DELAYS 0
82#endif
83#ifndef CONFIG_NANDSIM_LOG
84#define CONFIG_NANDSIM_LOG 0
85#endif
86#ifndef CONFIG_NANDSIM_DBG
87#define CONFIG_NANDSIM_DBG 0
88#endif
Ben Hutchingse99e90a2010-01-29 20:58:08 +000089#ifndef CONFIG_NANDSIM_MAX_PARTS
90#define CONFIG_NANDSIM_MAX_PARTS 32
91#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -070092
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
94static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
95static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
96static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
97static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
98static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
99static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
100static uint log = CONFIG_NANDSIM_LOG;
101static uint dbg = CONFIG_NANDSIM_DBG;
Ben Hutchingse99e90a2010-01-29 20:58:08 +0000102static unsigned long parts[CONFIG_NANDSIM_MAX_PARTS];
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200103static unsigned int parts_num;
Adrian Hunter514087e72007-03-19 12:47:45 +0200104static char *badblocks = NULL;
105static char *weakblocks = NULL;
106static char *weakpages = NULL;
107static unsigned int bitflips = 0;
108static char *gravepages = NULL;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +0200109static unsigned int overridesize = 0;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200110static char *cache_file = NULL;
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +0200111static unsigned int bbt;
Ivan Djelicfc2ff592011-03-11 11:05:34 +0100112static unsigned int bch;
Akinobu Mitab00358a2014-10-23 08:41:44 +0900113static u_char id_bytes[8] = {
114 [0] = CONFIG_NANDSIM_FIRST_ID_BYTE,
115 [1] = CONFIG_NANDSIM_SECOND_ID_BYTE,
116 [2] = CONFIG_NANDSIM_THIRD_ID_BYTE,
117 [3] = CONFIG_NANDSIM_FOURTH_ID_BYTE,
118 [4 ... 7] = 0xFF,
119};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Akinobu Mitab00358a2014-10-23 08:41:44 +0900121module_param_array(id_bytes, byte, NULL, 0400);
122module_param_named(first_id_byte, id_bytes[0], byte, 0400);
123module_param_named(second_id_byte, id_bytes[1], byte, 0400);
124module_param_named(third_id_byte, id_bytes[2], byte, 0400);
125module_param_named(fourth_id_byte, id_bytes[3], byte, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126module_param(access_delay, uint, 0400);
127module_param(programm_delay, uint, 0400);
128module_param(erase_delay, uint, 0400);
129module_param(output_cycle, uint, 0400);
130module_param(input_cycle, uint, 0400);
131module_param(bus_width, uint, 0400);
132module_param(do_delays, uint, 0400);
133module_param(log, uint, 0400);
134module_param(dbg, uint, 0400);
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200135module_param_array(parts, ulong, &parts_num, 0400);
Adrian Hunter514087e72007-03-19 12:47:45 +0200136module_param(badblocks, charp, 0400);
137module_param(weakblocks, charp, 0400);
138module_param(weakpages, charp, 0400);
139module_param(bitflips, uint, 0400);
140module_param(gravepages, charp, 0400);
Adrian Huntera5ac8ae2007-03-19 12:49:11 +0200141module_param(overridesize, uint, 0400);
Adrian Huntera9fc8992008-11-12 16:06:07 +0200142module_param(cache_file, charp, 0400);
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +0200143module_param(bbt, uint, 0400);
Ivan Djelicfc2ff592011-03-11 11:05:34 +0100144module_param(bch, uint, 0400);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145
Akinobu Mitab00358a2014-10-23 08:41:44 +0900146MODULE_PARM_DESC(id_bytes, "The ID bytes returned by NAND Flash 'read ID' command");
147MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID) (obsolete)");
148MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID) (obsolete)");
149MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command (obsolete)");
150MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command (obsolete)");
Adrian Huntera9fc8992008-11-12 16:06:07 +0200151MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
153MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
Andrey Yurovsky6029a3a2009-12-17 12:31:20 -0800154MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
155MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
157MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
158MODULE_PARM_DESC(log, "Perform logging if not zero");
159MODULE_PARM_DESC(dbg, "Output debug information if not zero");
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200160MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas");
Adrian Hunter514087e72007-03-19 12:47:45 +0200161/* Page and erase block positions for the following parameters are independent of any partitions */
162MODULE_PARM_DESC(badblocks, "Erase blocks that are initially marked bad, separated by commas");
163MODULE_PARM_DESC(weakblocks, "Weak erase blocks [: remaining erase cycles (defaults to 3)]"
164 " separated by commas e.g. 113:2 means eb 113"
165 " can be erased only twice before failing");
166MODULE_PARM_DESC(weakpages, "Weak pages [: maximum writes (defaults to 3)]"
167 " separated by commas e.g. 1401:2 means page 1401"
168 " can be written only twice before failing");
169MODULE_PARM_DESC(bitflips, "Maximum number of random bit flips per page (zero by default)");
170MODULE_PARM_DESC(gravepages, "Pages that lose data [: maximum reads (defaults to 3)]"
171 " separated by commas e.g. 1401:2 means page 1401"
172 " can be read only twice before failing");
Adrian Huntera5ac8ae2007-03-19 12:49:11 +0200173MODULE_PARM_DESC(overridesize, "Specifies the NAND Flash size overriding the ID bytes. "
174 "The size is specified in erase blocks and as the exponent of a power of two"
175 " e.g. 5 means a size of 32 erase blocks");
Adrian Huntera9fc8992008-11-12 16:06:07 +0200176MODULE_PARM_DESC(cache_file, "File to use to cache nand pages instead of memory");
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +0200177MODULE_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 +0100178MODULE_PARM_DESC(bch, "Enable BCH ecc and set how many bits should "
179 "be correctable in 512-byte blocks");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180
181/* The largest possible page size */
Sebastian Andrzej Siewior75352662009-11-29 19:07:57 +0100182#define NS_LARGEST_PAGE_SIZE 4096
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184/* Simulator's output macros (logging, debugging, warning, error) */
185#define NS_LOG(args...) \
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530186 do { if (log) pr_debug(" log: " args); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187#define NS_DBG(args...) \
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530188 do { if (dbg) pr_debug(" debug: " args); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189#define NS_WARN(args...) \
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530190 do { pr_warn(" warning: " args); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191#define NS_ERR(args...) \
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530192 do { pr_err(" error: " args); } while(0)
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200193#define NS_INFO(args...) \
Shreeya Patel63fa37f2018-02-22 22:01:22 +0530194 do { pr_info(" " args); } while(0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
196/* Busy-wait delay macros (microseconds, milliseconds) */
197#define NS_UDELAY(us) \
198 do { if (do_delays) udelay(us); } while(0)
199#define NS_MDELAY(us) \
200 do { if (do_delays) mdelay(us); } while(0)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202/* Is the nandsim structure initialized ? */
203#define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
204
205/* Good operation completion status */
206#define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
207
208/* Operation failed completion status */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000209#define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211/* Calculate the page offset in flash RAM image by (row, column) address */
212#define NS_RAW_OFFSET(ns) \
Akinobu Mita3b8b8fa2013-07-28 11:21:55 +0900213 (((ns)->regs.row * (ns)->geom.pgszoob) + (ns)->regs.column)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215/* Calculate the OOB offset in flash RAM image by (row, column) address */
216#define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
217
218/* After a command is input, the simulator goes to one of the following states */
219#define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */
220#define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */
Artem Bityutskiy4a0c50c2006-12-06 21:52:32 +0200221#define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
srimugunthandaf05ec2010-11-13 12:46:05 +0200222#define STATE_CMD_PAGEPROG 0x00000004 /* start page program */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223#define STATE_CMD_READOOB 0x00000005 /* read OOB area */
224#define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
225#define STATE_CMD_STATUS 0x00000007 /* read status */
srimugunthandaf05ec2010-11-13 12:46:05 +0200226#define STATE_CMD_SEQIN 0x00000009 /* sequential data input */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227#define STATE_CMD_READID 0x0000000A /* read ID */
228#define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
229#define STATE_CMD_RESET 0x0000000C /* reset */
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300230#define STATE_CMD_RNDOUT 0x0000000D /* random output command */
231#define STATE_CMD_RNDOUTSTART 0x0000000E /* random output start command */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232#define STATE_CMD_MASK 0x0000000F /* command states mask */
233
Joe Perches8e87d782008-02-03 17:22:34 +0200234/* After an address is input, the simulator goes to one of these states */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
236#define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300237#define STATE_ADDR_COLUMN 0x00000030 /* column address was accepted */
238#define STATE_ADDR_ZERO 0x00000040 /* one byte zero address was accepted */
239#define STATE_ADDR_MASK 0x00000070 /* address states mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
srimugunthandaf05ec2010-11-13 12:46:05 +0200241/* During data input/output the simulator is in these states */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242#define STATE_DATAIN 0x00000100 /* waiting for data input */
243#define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
244
245#define STATE_DATAOUT 0x00001000 /* waiting for page data output */
246#define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */
247#define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248#define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
249
250/* Previous operation is done, ready to accept new requests */
251#define STATE_READY 0x00000000
252
253/* This state is used to mark that the next state isn't known yet */
254#define STATE_UNKNOWN 0x10000000
255
256/* Simulator's actions bit masks */
257#define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */
srimugunthandaf05ec2010-11-13 12:46:05 +0200258#define ACTION_PRGPAGE 0x00200000 /* program the internal buffer to flash */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259#define ACTION_SECERASE 0x00300000 /* erase sector */
260#define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */
261#define ACTION_HALFOFF 0x00500000 /* add to address half of page */
262#define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */
263#define ACTION_MASK 0x00700000 /* action mask */
264
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300265#define NS_OPER_NUM 13 /* Number of operations supported by the simulator */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266#define NS_OPER_STATES 6 /* Maximum number of states in operation */
267
268#define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269#define OPT_PAGE512 0x00000002 /* 512-byte page chips */
270#define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271#define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
Sebastian Andrzej Siewior75352662009-11-29 19:07:57 +0100272#define OPT_PAGE4096 0x00000080 /* 4096-byte page chips */
273#define OPT_LARGEPAGE (OPT_PAGE2048 | OPT_PAGE4096) /* 2048 & 4096-byte page chips */
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200274#define OPT_SMALLPAGE (OPT_PAGE512) /* 512-byte page chips */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
srimugunthandaf05ec2010-11-13 12:46:05 +0200276/* Remove action bits from state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277#define NS_STATE(x) ((x) & ~ACTION_MASK)
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000278
279/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 * Maximum previous states which need to be saved. Currently saving is
srimugunthandaf05ec2010-11-13 12:46:05 +0200281 * only needed for page program operation with preceded read command
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 * (which is only valid for 512-byte pages).
283 */
284#define NS_MAX_PREVSTATES 1
285
Adrian Huntera9fc8992008-11-12 16:06:07 +0200286/* Maximum page cache pages needed to read or write a NAND page to the cache_file */
287#define NS_MAX_HELD_PAGES 16
288
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000289/*
Vijay Kumard086d432006-10-08 22:02:31 +0530290 * A union to represent flash memory contents and flash buffer.
291 */
292union ns_mem {
293 u_char *byte; /* for byte access */
294 uint16_t *word; /* for 16-bit word access */
295};
296
297/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 * The structure which describes all the internal simulator data.
299 */
300struct nandsim {
Ben Hutchingse99e90a2010-01-29 20:58:08 +0000301 struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200302 unsigned int nbparts;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 uint busw; /* flash chip bus width (8 or 16) */
Akinobu Mitab00358a2014-10-23 08:41:44 +0900305 u_char ids[8]; /* chip's ID bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 uint32_t options; /* chip's characteristic bits */
307 uint32_t state; /* current chip state */
308 uint32_t nxstate; /* next expected state */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000309
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 uint32_t *op; /* current operation, NULL operations isn't known yet */
311 uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
312 uint16_t npstates; /* number of previous states saved */
313 uint16_t stateidx; /* current state index */
314
Vijay Kumard086d432006-10-08 22:02:31 +0530315 /* The simulated NAND flash pages array */
316 union ns_mem *pages;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000318 /* Slab allocator for nand pages */
319 struct kmem_cache *nand_pages_slab;
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 /* Internal buffer of page + OOB size bytes */
Vijay Kumard086d432006-10-08 22:02:31 +0530322 union ns_mem buf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
324 /* NAND flash "geometry" */
Artem Bityutskiy0bfa4df2010-04-01 15:22:50 +0300325 struct {
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300326 uint64_t totsz; /* total flash size, bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 uint32_t secsz; /* flash sector (erase block) size, bytes */
328 uint pgsz; /* NAND flash page size, bytes */
329 uint oobsz; /* page OOB area size, bytes */
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300330 uint64_t totszoob; /* total flash size including OOB, bytes */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 uint pgszoob; /* page size including OOB , bytes*/
332 uint secszoob; /* sector size including OOB, bytes */
333 uint pgnum; /* total number of pages */
334 uint pgsec; /* number of pages per sector */
335 uint secshift; /* bits number in sector size */
336 uint pgshift; /* bits number in page size */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 uint pgaddrbytes; /* bytes per page address */
338 uint secaddrbytes; /* bytes per sector address */
339 uint idbytes; /* the number ID bytes that this chip outputs */
340 } geom;
341
342 /* NAND flash internal registers */
Artem Bityutskiy0bfa4df2010-04-01 15:22:50 +0300343 struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 unsigned command; /* the command register */
345 u_char status; /* the status register */
346 uint row; /* the page number */
347 uint column; /* the offset within page */
348 uint count; /* internal counter */
349 uint num; /* number of bytes which must be processed */
350 uint off; /* fixed page offset */
351 } regs;
352
353 /* NAND flash lines state */
Artem Bityutskiy0bfa4df2010-04-01 15:22:50 +0300354 struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 int ce; /* chip Enable */
356 int cle; /* command Latch Enable */
357 int ale; /* address Latch Enable */
358 int wp; /* write Protect */
359 } lines;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200360
361 /* Fields needed when using a cache file */
362 struct file *cfile; /* Open file */
Akinobu Mita08efe912013-07-28 11:21:53 +0900363 unsigned long *pages_written; /* Which pages have been written */
Adrian Huntera9fc8992008-11-12 16:06:07 +0200364 void *file_buf;
365 struct page *held_pages[NS_MAX_HELD_PAGES];
366 int held_cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367};
368
369/*
370 * Operations array. To perform any operation the simulator must pass
371 * through the correspondent states chain.
372 */
373static struct nandsim_operations {
374 uint32_t reqopts; /* options which are required to perform the operation */
375 uint32_t states[NS_OPER_STATES]; /* operation's states */
376} ops[NS_OPER_NUM] = {
377 /* Read page + OOB from the beginning */
378 {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
379 STATE_DATAOUT, STATE_READY}},
380 /* Read page + OOB from the second half */
381 {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
382 STATE_DATAOUT, STATE_READY}},
383 /* Read OOB */
384 {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
385 STATE_DATAOUT, STATE_READY}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200386 /* Program page starting from the beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
388 STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200389 /* Program page starting from the beginning */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
391 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200392 /* Program page starting from the second half */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
394 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
srimugunthandaf05ec2010-11-13 12:46:05 +0200395 /* Program OOB */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
397 STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
398 /* Erase sector */
399 {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}},
400 /* Read status */
401 {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /* Read ID */
403 {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
404 /* Large page devices read page */
405 {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
Artem Bityutskiy74216be2008-07-30 11:18:42 +0300406 STATE_DATAOUT, STATE_READY}},
407 /* Large page devices random page read */
408 {OPT_LARGEPAGE, {STATE_CMD_RNDOUT, STATE_ADDR_COLUMN, STATE_CMD_RNDOUTSTART | ACTION_CPY,
409 STATE_DATAOUT, STATE_READY}},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410};
411
Adrian Hunter514087e72007-03-19 12:47:45 +0200412struct weak_block {
413 struct list_head list;
414 unsigned int erase_block_no;
415 unsigned int max_erases;
416 unsigned int erases_done;
417};
418
419static LIST_HEAD(weak_blocks);
420
421struct weak_page {
422 struct list_head list;
423 unsigned int page_no;
424 unsigned int max_writes;
425 unsigned int writes_done;
426};
427
428static LIST_HEAD(weak_pages);
429
430struct grave_page {
431 struct list_head list;
432 unsigned int page_no;
433 unsigned int max_reads;
434 unsigned int reads_done;
435};
436
437static LIST_HEAD(grave_pages);
438
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200439static unsigned long *erase_block_wear = NULL;
440static unsigned int wear_eb_count = 0;
441static unsigned long total_wear = 0;
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200442
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443/* MTD structure for NAND controller */
444static struct mtd_info *nsmtd;
445
Yangtao Lic78f59d2018-12-02 03:33:58 -0500446static int nandsim_show(struct seq_file *m, void *private)
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300447{
448 unsigned long wmin = -1, wmax = 0, avg;
449 unsigned long deciles[10], decile_max[10], tot = 0;
450 unsigned int i;
451
452 /* Calc wear stats */
453 for (i = 0; i < wear_eb_count; ++i) {
454 unsigned long wear = erase_block_wear[i];
455 if (wear < wmin)
456 wmin = wear;
457 if (wear > wmax)
458 wmax = wear;
459 tot += wear;
460 }
461
462 for (i = 0; i < 9; ++i) {
463 deciles[i] = 0;
464 decile_max[i] = (wmax * (i + 1) + 5) / 10;
465 }
466 deciles[9] = 0;
467 decile_max[9] = wmax;
468 for (i = 0; i < wear_eb_count; ++i) {
469 int d;
470 unsigned long wear = erase_block_wear[i];
471 for (d = 0; d < 10; ++d)
472 if (wear <= decile_max[d]) {
473 deciles[d] += 1;
474 break;
475 }
476 }
477 avg = tot / wear_eb_count;
478
479 /* Output wear report */
480 seq_printf(m, "Total numbers of erases: %lu\n", tot);
481 seq_printf(m, "Number of erase blocks: %u\n", wear_eb_count);
482 seq_printf(m, "Average number of erases: %lu\n", avg);
483 seq_printf(m, "Maximum number of erases: %lu\n", wmax);
484 seq_printf(m, "Minimum number of erases: %lu\n", wmin);
485 for (i = 0; i < 10; ++i) {
486 unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
487 if (from > decile_max[i])
488 continue;
489 seq_printf(m, "Number of ebs with erase counts from %lu to %lu : %lu\n",
490 from,
491 decile_max[i],
492 deciles[i]);
493 }
494
495 return 0;
496}
Yangtao Lic78f59d2018-12-02 03:33:58 -0500497DEFINE_SHOW_ATTRIBUTE(nandsim);
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300498
499/**
500 * nandsim_debugfs_create - initialize debugfs
501 * @dev: nandsim device description object
502 *
503 * This function creates all debugfs files for UBI device @ubi. Returns zero in
504 * case of success and a negative error code in case of failure.
505 */
506static int nandsim_debugfs_create(struct nandsim *dev)
507{
Mario Rugieroe8e3edb2017-05-29 08:38:41 -0300508 struct dentry *root = nsmtd->dbg.dfs_dir;
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300509 struct dentry *dent;
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300510
Boris Brezillon15305782017-11-11 16:08:34 +0100511 /*
512 * Just skip debugfs initialization when the debugfs directory is
513 * missing.
514 */
515 if (IS_ERR_OR_NULL(root)) {
516 if (IS_ENABLED(CONFIG_DEBUG_FS) &&
517 !IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
518 NS_WARN("CONFIG_MTD_PARTITIONED_MASTER must be enabled to expose debugfs stuff\n");
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300519 return 0;
Boris Brezillon15305782017-11-11 16:08:34 +0100520 }
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300521
Mario Rugieroe8e3edb2017-05-29 08:38:41 -0300522 dent = debugfs_create_file("nandsim_wear_report", S_IRUSR,
Yangtao Lic78f59d2018-12-02 03:33:58 -0500523 root, dev, &nandsim_fops);
Mario Rugieroe8e3edb2017-05-29 08:38:41 -0300524 if (IS_ERR_OR_NULL(dent)) {
525 NS_ERR("cannot create \"nandsim_wear_report\" debugfs entry\n");
526 return -1;
527 }
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300528
529 return 0;
Ezequiel Garcia5346c272012-12-03 10:31:40 -0300530}
531
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532/*
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000533 * Allocate array of page pointers, create slab allocation for an array
534 * and initialize the array by NULL pointers.
Vijay Kumard086d432006-10-08 22:02:31 +0530535 *
536 * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
537 */
Julia Lawall77784782016-04-19 14:33:35 +0200538static int __init alloc_device(struct nandsim *ns)
Vijay Kumard086d432006-10-08 22:02:31 +0530539{
Adrian Huntera9fc8992008-11-12 16:06:07 +0200540 struct file *cfile;
541 int i, err;
542
543 if (cache_file) {
544 cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
545 if (IS_ERR(cfile))
546 return PTR_ERR(cfile);
Al Viro7f7f25e2014-02-11 17:49:24 -0500547 if (!(cfile->f_mode & FMODE_CAN_READ)) {
Adrian Huntera9fc8992008-11-12 16:06:07 +0200548 NS_ERR("alloc_device: cache file not readable\n");
549 err = -EINVAL;
550 goto err_close;
551 }
Al Viro7f7f25e2014-02-11 17:49:24 -0500552 if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
Adrian Huntera9fc8992008-11-12 16:06:07 +0200553 NS_ERR("alloc_device: cache file not writeable\n");
554 err = -EINVAL;
555 goto err_close;
556 }
Kees Cookfad953c2018-06-12 14:27:37 -0700557 ns->pages_written =
558 vzalloc(array_size(sizeof(unsigned long),
559 BITS_TO_LONGS(ns->geom.pgnum)));
Adrian Huntera9fc8992008-11-12 16:06:07 +0200560 if (!ns->pages_written) {
561 NS_ERR("alloc_device: unable to allocate pages written array\n");
562 err = -ENOMEM;
563 goto err_close;
564 }
565 ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
566 if (!ns->file_buf) {
567 NS_ERR("alloc_device: unable to allocate file buf\n");
568 err = -ENOMEM;
569 goto err_free;
570 }
571 ns->cfile = cfile;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200572 return 0;
573 }
Vijay Kumard086d432006-10-08 22:02:31 +0530574
Kees Cook42bc47b2018-06-12 14:27:11 -0700575 ns->pages = vmalloc(array_size(sizeof(union ns_mem), ns->geom.pgnum));
Vijay Kumard086d432006-10-08 22:02:31 +0530576 if (!ns->pages) {
Adrian Huntera9fc8992008-11-12 16:06:07 +0200577 NS_ERR("alloc_device: unable to allocate page array\n");
Vijay Kumard086d432006-10-08 22:02:31 +0530578 return -ENOMEM;
579 }
580 for (i = 0; i < ns->geom.pgnum; i++) {
581 ns->pages[i].byte = NULL;
582 }
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000583 ns->nand_pages_slab = kmem_cache_create("nandsim",
584 ns->geom.pgszoob, 0, 0, NULL);
585 if (!ns->nand_pages_slab) {
586 NS_ERR("cache_create: unable to create kmem_cache\n");
587 return -ENOMEM;
588 }
Vijay Kumard086d432006-10-08 22:02:31 +0530589
590 return 0;
Adrian Huntera9fc8992008-11-12 16:06:07 +0200591
592err_free:
593 vfree(ns->pages_written);
594err_close:
595 filp_close(cfile, NULL);
596 return err;
Vijay Kumard086d432006-10-08 22:02:31 +0530597}
598
599/*
600 * Free any allocated pages, and free the array of page pointers.
601 */
Vijay Kumara5602142006-10-14 21:33:34 +0530602static void free_device(struct nandsim *ns)
Vijay Kumard086d432006-10-08 22:02:31 +0530603{
604 int i;
605
Adrian Huntera9fc8992008-11-12 16:06:07 +0200606 if (ns->cfile) {
607 kfree(ns->file_buf);
608 vfree(ns->pages_written);
609 filp_close(ns->cfile, NULL);
610 return;
611 }
612
Vijay Kumard086d432006-10-08 22:02:31 +0530613 if (ns->pages) {
614 for (i = 0; i < ns->geom.pgnum; i++) {
615 if (ns->pages[i].byte)
Alexey Korolev8a4c2492008-11-13 13:40:38 +0000616 kmem_cache_free(ns->nand_pages_slab,
617 ns->pages[i].byte);
Vijay Kumard086d432006-10-08 22:02:31 +0530618 }
Julia Lawall0791a5f2015-09-13 14:14:54 +0200619 kmem_cache_destroy(ns->nand_pages_slab);
Vijay Kumard086d432006-10-08 22:02:31 +0530620 vfree(ns->pages);
621 }
622}
623
Julia Lawall77784782016-04-19 14:33:35 +0200624static char __init *get_partition_name(int i)
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200625{
Akinobu Mitaf03a5722013-07-28 11:21:54 +0900626 return kasprintf(GFP_KERNEL, "NAND simulator partition %d", i);
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200627}
628
Vijay Kumard086d432006-10-08 22:02:31 +0530629/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 * Initialize the nandsim structure.
631 *
632 * RETURNS: 0 if success, -ERRNO if failure.
633 */
Julia Lawall77784782016-04-19 14:33:35 +0200634static int __init init_nandsim(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Boris BREZILLON862eba52015-12-01 12:03:03 +0100636 struct nand_chip *chip = mtd_to_nand(mtd);
Boris BREZILLONd699ed22015-12-10 09:00:41 +0100637 struct nandsim *ns = nand_get_controller_data(chip);
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200638 int i, ret = 0;
David Woodhouse0f07a0b2008-12-10 14:01:46 +0000639 uint64_t remains;
640 uint64_t next_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (NS_IS_INITIALIZED(ns)) {
643 NS_ERR("init_nandsim: nandsim is already initialized\n");
644 return -EIO;
645 }
646
647 /* Force mtd to not do delays */
Boris Brezillon3cece3a2018-09-07 00:38:41 +0200648 chip->legacy.chip_delay = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650 /* Initialize the NAND flash parameters */
651 ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
652 ns->geom.totsz = mtd->size;
Joern Engel28318772006-05-22 23:18:05 +0200653 ns->geom.pgsz = mtd->writesize;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 ns->geom.oobsz = mtd->oobsize;
655 ns->geom.secsz = mtd->erasesize;
656 ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
Herton Ronaldo Krzesinski596fd462012-05-16 16:21:52 -0300657 ns->geom.pgnum = div_u64(ns->geom.totsz, ns->geom.pgsz);
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300658 ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 ns->geom.secshift = ffs(ns->geom.secsz) - 1;
660 ns->geom.pgshift = chip->page_shift;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
662 ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
663 ns->options = 0;
664
Artem Bityutskiy51148f12013-03-05 15:00:51 +0200665 if (ns->geom.pgsz == 512) {
Brian Norris831d3162012-05-01 17:12:53 -0700666 ns->options |= OPT_PAGE512;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 if (ns->busw == 8)
668 ns->options |= OPT_PAGE512_8BIT;
669 } else if (ns->geom.pgsz == 2048) {
670 ns->options |= OPT_PAGE2048;
Sebastian Andrzej Siewior75352662009-11-29 19:07:57 +0100671 } else if (ns->geom.pgsz == 4096) {
672 ns->options |= OPT_PAGE4096;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 } else {
674 NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
675 return -EIO;
676 }
677
678 if (ns->options & OPT_SMALLPAGE) {
Adrian Hunteraf3decc2008-05-30 15:56:18 +0300679 if (ns->geom.totsz <= (32 << 20)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 ns->geom.pgaddrbytes = 3;
681 ns->geom.secaddrbytes = 2;
682 } else {
683 ns->geom.pgaddrbytes = 4;
684 ns->geom.secaddrbytes = 3;
685 }
686 } else {
687 if (ns->geom.totsz <= (128 << 20)) {
Artem Bityutskiy4a0c50c2006-12-06 21:52:32 +0200688 ns->geom.pgaddrbytes = 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 ns->geom.secaddrbytes = 2;
690 } else {
691 ns->geom.pgaddrbytes = 5;
692 ns->geom.secaddrbytes = 3;
693 }
694 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +0000695
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200696 /* Fill the partition_info structure */
697 if (parts_num > ARRAY_SIZE(ns->partitions)) {
698 NS_ERR("too many partitions.\n");
shengyong5891a8d2015-06-25 02:23:14 +0000699 return -EINVAL;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200700 }
701 remains = ns->geom.totsz;
702 next_offset = 0;
703 for (i = 0; i < parts_num; ++i) {
David Woodhouse0f07a0b2008-12-10 14:01:46 +0000704 uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300705
706 if (!part_sz || part_sz > remains) {
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200707 NS_ERR("bad partition size.\n");
shengyong5891a8d2015-06-25 02:23:14 +0000708 return -EINVAL;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200709 }
710 ns->partitions[i].name = get_partition_name(i);
Richard Weinberger641c7922015-06-01 23:10:50 +0200711 if (!ns->partitions[i].name) {
712 NS_ERR("unable to allocate memory.\n");
shengyong5891a8d2015-06-25 02:23:14 +0000713 return -ENOMEM;
Richard Weinberger641c7922015-06-01 23:10:50 +0200714 }
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200715 ns->partitions[i].offset = next_offset;
Adrian Hunter6eda7a52008-05-30 15:56:26 +0300716 ns->partitions[i].size = part_sz;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200717 next_offset += ns->partitions[i].size;
718 remains -= ns->partitions[i].size;
719 }
720 ns->nbparts = parts_num;
721 if (remains) {
722 if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
723 NS_ERR("too many partitions.\n");
shengyong5891a8d2015-06-25 02:23:14 +0000724 return -EINVAL;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200725 }
726 ns->partitions[i].name = get_partition_name(i);
Richard Weinberger641c7922015-06-01 23:10:50 +0200727 if (!ns->partitions[i].name) {
728 NS_ERR("unable to allocate memory.\n");
shengyong5891a8d2015-06-25 02:23:14 +0000729 return -ENOMEM;
Richard Weinberger641c7922015-06-01 23:10:50 +0200730 }
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200731 ns->partitions[i].offset = next_offset;
732 ns->partitions[i].size = remains;
733 ns->nbparts += 1;
734 }
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (ns->busw == 16)
737 NS_WARN("16-bit flashes support wasn't tested\n");
738
Andrew Mortone4c094a2008-07-30 12:35:04 -0700739 printk("flash size: %llu MiB\n",
740 (unsigned long long)ns->geom.totsz >> 20);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 printk("page size: %u bytes\n", ns->geom.pgsz);
742 printk("OOB area size: %u bytes\n", ns->geom.oobsz);
743 printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
744 printk("pages number: %u\n", ns->geom.pgnum);
745 printk("pages per sector: %u\n", ns->geom.pgsec);
746 printk("bus width: %u\n", ns->busw);
747 printk("bits in sector size: %u\n", ns->geom.secshift);
748 printk("bits in page size: %u\n", ns->geom.pgshift);
Akinobu Mita2f3b07a2013-07-28 11:21:58 +0900749 printk("bits in OOB size: %u\n", ffs(ns->geom.oobsz) - 1);
Andrew Mortone4c094a2008-07-30 12:35:04 -0700750 printk("flash size with OOB: %llu KiB\n",
751 (unsigned long long)ns->geom.totszoob >> 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
753 printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
754 printk("options: %#x\n", ns->options);
755
Adrian Hunter2b77a0e2007-03-19 12:46:43 +0200756 if ((ret = alloc_device(ns)) != 0)
shengyong5891a8d2015-06-25 02:23:14 +0000757 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
759 /* Allocate / initialize the internal buffer */
760 ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
761 if (!ns->buf.byte) {
762 NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
763 ns->geom.pgszoob);
shengyong5891a8d2015-06-25 02:23:14 +0000764 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 }
766 memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769}
770
771/*
772 * Free the nandsim structure.
773 */
Vijay Kumara5602142006-10-14 21:33:34 +0530774static void free_nandsim(struct nandsim *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
776 kfree(ns->buf.byte);
Vijay Kumard086d432006-10-08 22:02:31 +0530777 free_device(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 return;
780}
781
Adrian Hunter514087e72007-03-19 12:47:45 +0200782static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
783{
784 char *w;
785 int zero_ok;
786 unsigned int erase_block_no;
787 loff_t offset;
788
789 if (!badblocks)
790 return 0;
791 w = badblocks;
792 do {
793 zero_ok = (*w == '0' ? 1 : 0);
794 erase_block_no = simple_strtoul(w, &w, 0);
795 if (!zero_ok && !erase_block_no) {
796 NS_ERR("invalid badblocks.\n");
797 return -EINVAL;
798 }
Brian Norrisb033e1a2014-07-21 19:07:44 -0700799 offset = (loff_t)erase_block_no * ns->geom.secsz;
Artem Bityutskiy5942ddb2011-12-23 19:37:38 +0200800 if (mtd_block_markbad(mtd, offset)) {
Adrian Hunter514087e72007-03-19 12:47:45 +0200801 NS_ERR("invalid badblocks.\n");
802 return -EINVAL;
803 }
804 if (*w == ',')
805 w += 1;
806 } while (*w);
807 return 0;
808}
809
810static int parse_weakblocks(void)
811{
812 char *w;
813 int zero_ok;
814 unsigned int erase_block_no;
815 unsigned int max_erases;
816 struct weak_block *wb;
817
818 if (!weakblocks)
819 return 0;
820 w = weakblocks;
821 do {
822 zero_ok = (*w == '0' ? 1 : 0);
823 erase_block_no = simple_strtoul(w, &w, 0);
824 if (!zero_ok && !erase_block_no) {
825 NS_ERR("invalid weakblocks.\n");
826 return -EINVAL;
827 }
828 max_erases = 3;
829 if (*w == ':') {
830 w += 1;
831 max_erases = simple_strtoul(w, &w, 0);
832 }
833 if (*w == ',')
834 w += 1;
835 wb = kzalloc(sizeof(*wb), GFP_KERNEL);
836 if (!wb) {
837 NS_ERR("unable to allocate memory.\n");
838 return -ENOMEM;
839 }
840 wb->erase_block_no = erase_block_no;
841 wb->max_erases = max_erases;
842 list_add(&wb->list, &weak_blocks);
843 } while (*w);
844 return 0;
845}
846
847static int erase_error(unsigned int erase_block_no)
848{
849 struct weak_block *wb;
850
851 list_for_each_entry(wb, &weak_blocks, list)
852 if (wb->erase_block_no == erase_block_no) {
853 if (wb->erases_done >= wb->max_erases)
854 return 1;
855 wb->erases_done += 1;
856 return 0;
857 }
858 return 0;
859}
860
861static int parse_weakpages(void)
862{
863 char *w;
864 int zero_ok;
865 unsigned int page_no;
866 unsigned int max_writes;
867 struct weak_page *wp;
868
869 if (!weakpages)
870 return 0;
871 w = weakpages;
872 do {
873 zero_ok = (*w == '0' ? 1 : 0);
874 page_no = simple_strtoul(w, &w, 0);
875 if (!zero_ok && !page_no) {
Colin Ian King215157f2017-02-23 11:30:48 +0000876 NS_ERR("invalid weakpages.\n");
Adrian Hunter514087e72007-03-19 12:47:45 +0200877 return -EINVAL;
878 }
879 max_writes = 3;
880 if (*w == ':') {
881 w += 1;
882 max_writes = simple_strtoul(w, &w, 0);
883 }
884 if (*w == ',')
885 w += 1;
886 wp = kzalloc(sizeof(*wp), GFP_KERNEL);
887 if (!wp) {
888 NS_ERR("unable to allocate memory.\n");
889 return -ENOMEM;
890 }
891 wp->page_no = page_no;
892 wp->max_writes = max_writes;
893 list_add(&wp->list, &weak_pages);
894 } while (*w);
895 return 0;
896}
897
898static int write_error(unsigned int page_no)
899{
900 struct weak_page *wp;
901
902 list_for_each_entry(wp, &weak_pages, list)
903 if (wp->page_no == page_no) {
904 if (wp->writes_done >= wp->max_writes)
905 return 1;
906 wp->writes_done += 1;
907 return 0;
908 }
909 return 0;
910}
911
912static int parse_gravepages(void)
913{
914 char *g;
915 int zero_ok;
916 unsigned int page_no;
917 unsigned int max_reads;
918 struct grave_page *gp;
919
920 if (!gravepages)
921 return 0;
922 g = gravepages;
923 do {
924 zero_ok = (*g == '0' ? 1 : 0);
925 page_no = simple_strtoul(g, &g, 0);
926 if (!zero_ok && !page_no) {
927 NS_ERR("invalid gravepagess.\n");
928 return -EINVAL;
929 }
930 max_reads = 3;
931 if (*g == ':') {
932 g += 1;
933 max_reads = simple_strtoul(g, &g, 0);
934 }
935 if (*g == ',')
936 g += 1;
937 gp = kzalloc(sizeof(*gp), GFP_KERNEL);
938 if (!gp) {
939 NS_ERR("unable to allocate memory.\n");
940 return -ENOMEM;
941 }
942 gp->page_no = page_no;
943 gp->max_reads = max_reads;
944 list_add(&gp->list, &grave_pages);
945 } while (*g);
946 return 0;
947}
948
949static int read_error(unsigned int page_no)
950{
951 struct grave_page *gp;
952
953 list_for_each_entry(gp, &grave_pages, list)
954 if (gp->page_no == page_no) {
955 if (gp->reads_done >= gp->max_reads)
956 return 1;
957 gp->reads_done += 1;
958 return 0;
959 }
960 return 0;
961}
962
963static void free_lists(void)
964{
965 struct list_head *pos, *n;
966 list_for_each_safe(pos, n, &weak_blocks) {
967 list_del(pos);
968 kfree(list_entry(pos, struct weak_block, list));
969 }
970 list_for_each_safe(pos, n, &weak_pages) {
971 list_del(pos);
972 kfree(list_entry(pos, struct weak_page, list));
973 }
974 list_for_each_safe(pos, n, &grave_pages) {
975 list_del(pos);
976 kfree(list_entry(pos, struct grave_page, list));
977 }
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200978 kfree(erase_block_wear);
979}
980
981static int setup_wear_reporting(struct mtd_info *mtd)
982{
983 size_t mem;
984
Herton Ronaldo Krzesinski596fd462012-05-16 16:21:52 -0300985 wear_eb_count = div_u64(mtd->size, mtd->erasesize);
Adrian Hunter57aa6b52007-03-19 12:40:41 +0200986 mem = wear_eb_count * sizeof(unsigned long);
987 if (mem / sizeof(unsigned long) != wear_eb_count) {
988 NS_ERR("Too many erase blocks for wear reporting\n");
989 return -ENOMEM;
990 }
991 erase_block_wear = kzalloc(mem, GFP_KERNEL);
992 if (!erase_block_wear) {
993 NS_ERR("Too many erase blocks for wear reporting\n");
994 return -ENOMEM;
995 }
996 return 0;
997}
998
999static void update_wear(unsigned int erase_block_no)
1000{
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001001 if (!erase_block_wear)
1002 return;
1003 total_wear += 1;
Ezequiel Garcia5346c272012-12-03 10:31:40 -03001004 /*
1005 * TODO: Notify this through a debugfs entry,
1006 * instead of showing an error message.
1007 */
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001008 if (total_wear == 0)
1009 NS_ERR("Erase counter total overflow\n");
1010 erase_block_wear[erase_block_no] += 1;
1011 if (erase_block_wear[erase_block_no] == 0)
1012 NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
Adrian Hunter514087e72007-03-19 12:47:45 +02001013}
1014
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015/*
1016 * Returns the string representation of 'state' state.
1017 */
Vijay Kumara5602142006-10-14 21:33:34 +05301018static char *get_state_name(uint32_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019{
1020 switch (NS_STATE(state)) {
1021 case STATE_CMD_READ0:
1022 return "STATE_CMD_READ0";
1023 case STATE_CMD_READ1:
1024 return "STATE_CMD_READ1";
1025 case STATE_CMD_PAGEPROG:
1026 return "STATE_CMD_PAGEPROG";
1027 case STATE_CMD_READOOB:
1028 return "STATE_CMD_READOOB";
1029 case STATE_CMD_READSTART:
1030 return "STATE_CMD_READSTART";
1031 case STATE_CMD_ERASE1:
1032 return "STATE_CMD_ERASE1";
1033 case STATE_CMD_STATUS:
1034 return "STATE_CMD_STATUS";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 case STATE_CMD_SEQIN:
1036 return "STATE_CMD_SEQIN";
1037 case STATE_CMD_READID:
1038 return "STATE_CMD_READID";
1039 case STATE_CMD_ERASE2:
1040 return "STATE_CMD_ERASE2";
1041 case STATE_CMD_RESET:
1042 return "STATE_CMD_RESET";
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001043 case STATE_CMD_RNDOUT:
1044 return "STATE_CMD_RNDOUT";
1045 case STATE_CMD_RNDOUTSTART:
1046 return "STATE_CMD_RNDOUTSTART";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 case STATE_ADDR_PAGE:
1048 return "STATE_ADDR_PAGE";
1049 case STATE_ADDR_SEC:
1050 return "STATE_ADDR_SEC";
1051 case STATE_ADDR_ZERO:
1052 return "STATE_ADDR_ZERO";
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001053 case STATE_ADDR_COLUMN:
1054 return "STATE_ADDR_COLUMN";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 case STATE_DATAIN:
1056 return "STATE_DATAIN";
1057 case STATE_DATAOUT:
1058 return "STATE_DATAOUT";
1059 case STATE_DATAOUT_ID:
1060 return "STATE_DATAOUT_ID";
1061 case STATE_DATAOUT_STATUS:
1062 return "STATE_DATAOUT_STATUS";
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 case STATE_READY:
1064 return "STATE_READY";
1065 case STATE_UNKNOWN:
1066 return "STATE_UNKNOWN";
1067 }
1068
1069 NS_ERR("get_state_name: unknown state, BUG\n");
1070 return NULL;
1071}
1072
1073/*
1074 * Check if command is valid.
1075 *
1076 * RETURNS: 1 if wrong command, 0 if right.
1077 */
Vijay Kumara5602142006-10-14 21:33:34 +05301078static int check_command(int cmd)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079{
1080 switch (cmd) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 case NAND_CMD_READ0:
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001083 case NAND_CMD_READ1:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 case NAND_CMD_READSTART:
1085 case NAND_CMD_PAGEPROG:
1086 case NAND_CMD_READOOB:
1087 case NAND_CMD_ERASE1:
1088 case NAND_CMD_STATUS:
1089 case NAND_CMD_SEQIN:
1090 case NAND_CMD_READID:
1091 case NAND_CMD_ERASE2:
1092 case NAND_CMD_RESET:
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001093 case NAND_CMD_RNDOUT:
1094 case NAND_CMD_RNDOUTSTART:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001096
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097 default:
1098 return 1;
1099 }
1100}
1101
1102/*
1103 * Returns state after command is accepted by command number.
1104 */
Vijay Kumara5602142006-10-14 21:33:34 +05301105static uint32_t get_state_by_command(unsigned command)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106{
1107 switch (command) {
1108 case NAND_CMD_READ0:
1109 return STATE_CMD_READ0;
1110 case NAND_CMD_READ1:
1111 return STATE_CMD_READ1;
1112 case NAND_CMD_PAGEPROG:
1113 return STATE_CMD_PAGEPROG;
1114 case NAND_CMD_READSTART:
1115 return STATE_CMD_READSTART;
1116 case NAND_CMD_READOOB:
1117 return STATE_CMD_READOOB;
1118 case NAND_CMD_ERASE1:
1119 return STATE_CMD_ERASE1;
1120 case NAND_CMD_STATUS:
1121 return STATE_CMD_STATUS;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122 case NAND_CMD_SEQIN:
1123 return STATE_CMD_SEQIN;
1124 case NAND_CMD_READID:
1125 return STATE_CMD_READID;
1126 case NAND_CMD_ERASE2:
1127 return STATE_CMD_ERASE2;
1128 case NAND_CMD_RESET:
1129 return STATE_CMD_RESET;
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001130 case NAND_CMD_RNDOUT:
1131 return STATE_CMD_RNDOUT;
1132 case NAND_CMD_RNDOUTSTART:
1133 return STATE_CMD_RNDOUTSTART;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134 }
1135
1136 NS_ERR("get_state_by_command: unknown command, BUG\n");
1137 return 0;
1138}
1139
1140/*
1141 * Move an address byte to the correspondent internal register.
1142 */
Vijay Kumara5602142006-10-14 21:33:34 +05301143static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144{
1145 uint byte = (uint)bt;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
1148 ns->regs.column |= (byte << 8 * ns->regs.count);
1149 else {
1150 ns->regs.row |= (byte << 8 * (ns->regs.count -
1151 ns->geom.pgaddrbytes +
1152 ns->geom.secaddrbytes));
1153 }
1154
1155 return;
1156}
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/*
1159 * Switch to STATE_READY state.
1160 */
Vijay Kumara5602142006-10-14 21:33:34 +05301161static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162{
1163 NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
1164
1165 ns->state = STATE_READY;
1166 ns->nxstate = STATE_UNKNOWN;
1167 ns->op = NULL;
1168 ns->npstates = 0;
1169 ns->stateidx = 0;
1170 ns->regs.num = 0;
1171 ns->regs.count = 0;
1172 ns->regs.off = 0;
1173 ns->regs.row = 0;
1174 ns->regs.column = 0;
1175 ns->regs.status = status;
1176}
1177
1178/*
1179 * If the operation isn't known yet, try to find it in the global array
1180 * of supported operations.
1181 *
1182 * Operation can be unknown because of the following.
srimugunthandaf05ec2010-11-13 12:46:05 +02001183 * 1. New command was accepted and this is the first call to find the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * correspondent states chain. In this case ns->npstates = 0;
srimugunthandaf05ec2010-11-13 12:46:05 +02001185 * 2. There are several operations which begin with the same command(s)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 * (for example program from the second half and read from the
1187 * second half operations both begin with the READ1 command). In this
1188 * case the ns->pstates[] array contains previous states.
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001189 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190 * Thus, the function tries to find operation containing the following
1191 * states (if the 'flag' parameter is 0):
1192 * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
1193 *
1194 * If (one and only one) matching operation is found, it is accepted (
1195 * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
1196 * zeroed).
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001197 *
srimugunthandaf05ec2010-11-13 12:46:05 +02001198 * If there are several matches, the current state is pushed to the
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 * ns->pstates.
1200 *
1201 * The operation can be unknown only while commands are input to the chip.
1202 * As soon as address command is accepted, the operation must be known.
1203 * In such situation the function is called with 'flag' != 0, and the
1204 * operation is searched using the following pattern:
1205 * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001206 *
srimugunthandaf05ec2010-11-13 12:46:05 +02001207 * It is supposed that this pattern must either match one operation or
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 * none. There can't be ambiguity in that case.
1209 *
srimugunthandaf05ec2010-11-13 12:46:05 +02001210 * If no matches found, the function does the following:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * 1. if there are saved states present, try to ignore them and search
1212 * again only using the last command. If nothing was found, switch
1213 * to the STATE_READY state.
1214 * 2. if there are no saved states, switch to the STATE_READY state.
1215 *
1216 * RETURNS: -2 - no matched operations found.
1217 * -1 - several matches.
1218 * 0 - operation is found.
1219 */
Vijay Kumara5602142006-10-14 21:33:34 +05301220static int find_operation(struct nandsim *ns, uint32_t flag)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221{
1222 int opsfound = 0;
1223 int i, j, idx = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001224
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 for (i = 0; i < NS_OPER_NUM; i++) {
1226
1227 int found = 1;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001228
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 if (!(ns->options & ops[i].reqopts))
1230 /* Ignore operations we can't perform */
1231 continue;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233 if (flag) {
1234 if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
1235 continue;
1236 } else {
1237 if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
1238 continue;
1239 }
1240
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001241 for (j = 0; j < ns->npstates; j++)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
1243 && (ns->options & ops[idx].reqopts)) {
1244 found = 0;
1245 break;
1246 }
1247
1248 if (found) {
1249 idx = i;
1250 opsfound += 1;
1251 }
1252 }
1253
1254 if (opsfound == 1) {
1255 /* Exact match */
1256 ns->op = &ops[idx].states[0];
1257 if (flag) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001258 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 * In this case the find_operation function was
1260 * called when address has just began input. But it isn't
1261 * yet fully input and the current state must
1262 * not be one of STATE_ADDR_*, but the STATE_ADDR_*
1263 * state must be the next state (ns->nxstate).
1264 */
1265 ns->stateidx = ns->npstates - 1;
1266 } else {
1267 ns->stateidx = ns->npstates;
1268 }
1269 ns->npstates = 0;
1270 ns->state = ns->op[ns->stateidx];
1271 ns->nxstate = ns->op[ns->stateidx + 1];
1272 NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
1273 idx, get_state_name(ns->state), get_state_name(ns->nxstate));
1274 return 0;
1275 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001276
Linus Torvalds1da177e2005-04-16 15:20:36 -07001277 if (opsfound == 0) {
1278 /* Nothing was found. Try to ignore previous commands (if any) and search again */
1279 if (ns->npstates != 0) {
1280 NS_DBG("find_operation: no operation found, try again with state %s\n",
1281 get_state_name(ns->state));
1282 ns->npstates = 0;
1283 return find_operation(ns, 0);
1284
1285 }
1286 NS_DBG("find_operation: no operations found\n");
1287 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1288 return -2;
1289 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001290
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 if (flag) {
1292 /* This shouldn't happen */
1293 NS_DBG("find_operation: BUG, operation must be known if address is input\n");
1294 return -2;
1295 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001296
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 NS_DBG("find_operation: there is still ambiguity\n");
1298
1299 ns->pstates[ns->npstates++] = ns->state;
1300
1301 return -1;
1302}
1303
Adrian Huntera9fc8992008-11-12 16:06:07 +02001304static void put_pages(struct nandsim *ns)
1305{
1306 int i;
1307
1308 for (i = 0; i < ns->held_cnt; i++)
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001309 put_page(ns->held_pages[i]);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001310}
1311
1312/* Get page cache pages in advance to provide NOFS memory allocation */
1313static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
1314{
1315 pgoff_t index, start_index, end_index;
1316 struct page *page;
1317 struct address_space *mapping = file->f_mapping;
1318
Kirill A. Shutemov09cbfea2016-04-01 15:29:47 +03001319 start_index = pos >> PAGE_SHIFT;
1320 end_index = (pos + count - 1) >> PAGE_SHIFT;
Adrian Huntera9fc8992008-11-12 16:06:07 +02001321 if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
1322 return -EINVAL;
1323 ns->held_cnt = 0;
1324 for (index = start_index; index <= end_index; index++) {
1325 page = find_get_page(mapping, index);
1326 if (page == NULL) {
1327 page = find_or_create_page(mapping, index, GFP_NOFS);
1328 if (page == NULL) {
1329 write_inode_now(mapping->host, 1);
1330 page = find_or_create_page(mapping, index, GFP_NOFS);
1331 }
1332 if (page == NULL) {
1333 put_pages(ns);
1334 return -ENOMEM;
1335 }
1336 unlock_page(page);
1337 }
1338 ns->held_pages[ns->held_cnt++] = page;
1339 }
1340 return 0;
1341}
1342
Al Viro7bb307e2013-02-23 14:51:48 -05001343static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
Adrian Huntera9fc8992008-11-12 16:06:07 +02001344{
Adrian Huntera9fc8992008-11-12 16:06:07 +02001345 ssize_t tx;
Vlastimil Babkadcbe8212017-05-08 15:59:57 -07001346 int err;
1347 unsigned int noreclaim_flag;
Adrian Huntera9fc8992008-11-12 16:06:07 +02001348
Al Viro7bb307e2013-02-23 14:51:48 -05001349 err = get_pages(ns, file, count, pos);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001350 if (err)
1351 return err;
Vlastimil Babkadcbe8212017-05-08 15:59:57 -07001352 noreclaim_flag = memalloc_noreclaim_save();
Christoph Hellwigbdd1d2d2017-09-01 17:39:13 +02001353 tx = kernel_read(file, buf, count, &pos);
Vlastimil Babkadcbe8212017-05-08 15:59:57 -07001354 memalloc_noreclaim_restore(noreclaim_flag);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001355 put_pages(ns);
1356 return tx;
1357}
1358
Al Viro7bb307e2013-02-23 14:51:48 -05001359static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
Adrian Huntera9fc8992008-11-12 16:06:07 +02001360{
Adrian Huntera9fc8992008-11-12 16:06:07 +02001361 ssize_t tx;
Vlastimil Babkadcbe8212017-05-08 15:59:57 -07001362 int err;
1363 unsigned int noreclaim_flag;
Adrian Huntera9fc8992008-11-12 16:06:07 +02001364
Al Viro7bb307e2013-02-23 14:51:48 -05001365 err = get_pages(ns, file, count, pos);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001366 if (err)
1367 return err;
Vlastimil Babkadcbe8212017-05-08 15:59:57 -07001368 noreclaim_flag = memalloc_noreclaim_save();
Christoph Hellwige13ec932017-09-01 17:39:14 +02001369 tx = kernel_write(file, buf, count, &pos);
Vlastimil Babkadcbe8212017-05-08 15:59:57 -07001370 memalloc_noreclaim_restore(noreclaim_flag);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001371 put_pages(ns);
1372 return tx;
1373}
1374
Linus Torvalds1da177e2005-04-16 15:20:36 -07001375/*
Vijay Kumard086d432006-10-08 22:02:31 +05301376 * Returns a pointer to the current page.
1377 */
1378static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
1379{
1380 return &(ns->pages[ns->regs.row]);
1381}
1382
1383/*
1384 * Retuns a pointer to the current byte, within the current page.
1385 */
1386static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
1387{
1388 return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
1389}
1390
Jingoo Hanb2b263f22013-08-07 16:13:32 +09001391static int do_read_error(struct nandsim *ns, int num)
Adrian Huntera9fc8992008-11-12 16:06:07 +02001392{
1393 unsigned int page_no = ns->regs.row;
1394
1395 if (read_error(page_no)) {
Akinobu Mita7e45bf82012-12-17 16:04:32 -08001396 prandom_bytes(ns->buf.byte, num);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001397 NS_WARN("simulating read error in page %u\n", page_no);
1398 return 1;
1399 }
1400 return 0;
1401}
1402
Jingoo Hanb2b263f22013-08-07 16:13:32 +09001403static void do_bit_flips(struct nandsim *ns, int num)
Adrian Huntera9fc8992008-11-12 16:06:07 +02001404{
Akinobu Mitaaca662a2013-01-03 21:19:13 +09001405 if (bitflips && prandom_u32() < (1 << 22)) {
Adrian Huntera9fc8992008-11-12 16:06:07 +02001406 int flips = 1;
1407 if (bitflips > 1)
Akinobu Mitaaca662a2013-01-03 21:19:13 +09001408 flips = (prandom_u32() % (int) bitflips) + 1;
Adrian Huntera9fc8992008-11-12 16:06:07 +02001409 while (flips--) {
Akinobu Mitaaca662a2013-01-03 21:19:13 +09001410 int pos = prandom_u32() % (num * 8);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001411 ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
1412 NS_WARN("read_page: flipping bit %d in page %d "
1413 "reading from %d ecc: corrected=%u failed=%u\n",
1414 pos, ns->regs.row, ns->regs.column + ns->regs.off,
1415 nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
1416 }
1417 }
1418}
1419
Vijay Kumard086d432006-10-08 22:02:31 +05301420/*
1421 * Fill the NAND buffer with data read from the specified page.
1422 */
1423static void read_page(struct nandsim *ns, int num)
1424{
1425 union ns_mem *mypage;
1426
Adrian Huntera9fc8992008-11-12 16:06:07 +02001427 if (ns->cfile) {
Akinobu Mita08efe912013-07-28 11:21:53 +09001428 if (!test_bit(ns->regs.row, ns->pages_written)) {
Adrian Huntera9fc8992008-11-12 16:06:07 +02001429 NS_DBG("read_page: page %d not written\n", ns->regs.row);
1430 memset(ns->buf.byte, 0xFF, num);
1431 } else {
1432 loff_t pos;
1433 ssize_t tx;
1434
1435 NS_DBG("read_page: page %d written, reading from %d\n",
1436 ns->regs.row, ns->regs.column + ns->regs.off);
1437 if (do_read_error(ns, num))
1438 return;
Akinobu Mita6d07fcf2013-07-28 11:21:56 +09001439 pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
Al Viro7bb307e2013-02-23 14:51:48 -05001440 tx = read_file(ns, ns->cfile, ns->buf.byte, num, pos);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001441 if (tx != num) {
1442 NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1443 return;
1444 }
1445 do_bit_flips(ns, num);
1446 }
1447 return;
1448 }
1449
Vijay Kumard086d432006-10-08 22:02:31 +05301450 mypage = NS_GET_PAGE(ns);
1451 if (mypage->byte == NULL) {
1452 NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
1453 memset(ns->buf.byte, 0xFF, num);
1454 } else {
1455 NS_DBG("read_page: page %d allocated, reading from %d\n",
1456 ns->regs.row, ns->regs.column + ns->regs.off);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001457 if (do_read_error(ns, num))
Adrian Hunter514087e72007-03-19 12:47:45 +02001458 return;
Vijay Kumard086d432006-10-08 22:02:31 +05301459 memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001460 do_bit_flips(ns, num);
Vijay Kumard086d432006-10-08 22:02:31 +05301461 }
1462}
1463
1464/*
1465 * Erase all pages in the specified sector.
1466 */
1467static void erase_sector(struct nandsim *ns)
1468{
1469 union ns_mem *mypage;
1470 int i;
1471
Adrian Huntera9fc8992008-11-12 16:06:07 +02001472 if (ns->cfile) {
1473 for (i = 0; i < ns->geom.pgsec; i++)
Akinobu Mita08efe912013-07-28 11:21:53 +09001474 if (__test_and_clear_bit(ns->regs.row + i,
1475 ns->pages_written)) {
Adrian Huntera9fc8992008-11-12 16:06:07 +02001476 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001477 }
1478 return;
1479 }
1480
Vijay Kumard086d432006-10-08 22:02:31 +05301481 mypage = NS_GET_PAGE(ns);
1482 for (i = 0; i < ns->geom.pgsec; i++) {
1483 if (mypage->byte != NULL) {
1484 NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
Alexey Korolev8a4c2492008-11-13 13:40:38 +00001485 kmem_cache_free(ns->nand_pages_slab, mypage->byte);
Vijay Kumard086d432006-10-08 22:02:31 +05301486 mypage->byte = NULL;
1487 }
1488 mypage++;
1489 }
1490}
1491
1492/*
1493 * Program the specified page with the contents from the NAND buffer.
1494 */
1495static int prog_page(struct nandsim *ns, int num)
1496{
Artem Bityutskiy82810b7b62006-10-20 11:23:56 +03001497 int i;
Vijay Kumard086d432006-10-08 22:02:31 +05301498 union ns_mem *mypage;
1499 u_char *pg_off;
1500
Adrian Huntera9fc8992008-11-12 16:06:07 +02001501 if (ns->cfile) {
Al Viro7bb307e2013-02-23 14:51:48 -05001502 loff_t off;
Adrian Huntera9fc8992008-11-12 16:06:07 +02001503 ssize_t tx;
1504 int all;
1505
1506 NS_DBG("prog_page: writing page %d\n", ns->regs.row);
1507 pg_off = ns->file_buf + ns->regs.column + ns->regs.off;
Akinobu Mita6d07fcf2013-07-28 11:21:56 +09001508 off = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
Akinobu Mita08efe912013-07-28 11:21:53 +09001509 if (!test_bit(ns->regs.row, ns->pages_written)) {
Adrian Huntera9fc8992008-11-12 16:06:07 +02001510 all = 1;
1511 memset(ns->file_buf, 0xff, ns->geom.pgszoob);
1512 } else {
1513 all = 0;
Al Viro7bb307e2013-02-23 14:51:48 -05001514 tx = read_file(ns, ns->cfile, pg_off, num, off);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001515 if (tx != num) {
1516 NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
1517 return -1;
1518 }
1519 }
1520 for (i = 0; i < num; i++)
1521 pg_off[i] &= ns->buf.byte[i];
1522 if (all) {
Al Viro7bb307e2013-02-23 14:51:48 -05001523 loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
1524 tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, pos);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001525 if (tx != ns->geom.pgszoob) {
1526 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1527 return -1;
1528 }
Akinobu Mita08efe912013-07-28 11:21:53 +09001529 __set_bit(ns->regs.row, ns->pages_written);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001530 } else {
Al Viro7bb307e2013-02-23 14:51:48 -05001531 tx = write_file(ns, ns->cfile, pg_off, num, off);
Adrian Huntera9fc8992008-11-12 16:06:07 +02001532 if (tx != num) {
1533 NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
1534 return -1;
1535 }
1536 }
1537 return 0;
1538 }
1539
Vijay Kumard086d432006-10-08 22:02:31 +05301540 mypage = NS_GET_PAGE(ns);
1541 if (mypage->byte == NULL) {
1542 NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
Artem Bityutskiy98b830d2007-08-28 20:33:32 +03001543 /*
1544 * We allocate memory with GFP_NOFS because a flash FS may
1545 * utilize this. If it is holding an FS lock, then gets here,
Alexey Korolev8a4c2492008-11-13 13:40:38 +00001546 * then kernel memory alloc runs writeback which goes to the FS
1547 * again and deadlocks. This was seen in practice.
Artem Bityutskiy98b830d2007-08-28 20:33:32 +03001548 */
Alexey Korolev8a4c2492008-11-13 13:40:38 +00001549 mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
Vijay Kumard086d432006-10-08 22:02:31 +05301550 if (mypage->byte == NULL) {
1551 NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
1552 return -1;
1553 }
1554 memset(mypage->byte, 0xFF, ns->geom.pgszoob);
1555 }
1556
1557 pg_off = NS_PAGE_BYTE_OFF(ns);
Artem Bityutskiy82810b7b62006-10-20 11:23:56 +03001558 for (i = 0; i < num; i++)
1559 pg_off[i] &= ns->buf.byte[i];
Vijay Kumard086d432006-10-08 22:02:31 +05301560
1561 return 0;
1562}
1563
1564/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565 * If state has any action bit, perform this action.
1566 *
1567 * RETURNS: 0 if success, -1 if error.
1568 */
Vijay Kumara5602142006-10-14 21:33:34 +05301569static int do_state_action(struct nandsim *ns, uint32_t action)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570{
Vijay Kumard086d432006-10-08 22:02:31 +05301571 int num;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int busdiv = ns->busw == 8 ? 1 : 2;
Adrian Hunter514087e72007-03-19 12:47:45 +02001573 unsigned int erase_block_no, page_no;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574
1575 action &= ACTION_MASK;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001576
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 /* Check that page address input is correct */
1578 if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
1579 NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
1580 return -1;
1581 }
1582
1583 switch (action) {
1584
1585 case ACTION_CPY:
1586 /*
1587 * Copy page data to the internal buffer.
1588 */
1589
1590 /* Column shouldn't be very large */
1591 if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
1592 NS_ERR("do_state_action: column number is too large\n");
1593 break;
1594 }
1595 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
Vijay Kumard086d432006-10-08 22:02:31 +05301596 read_page(ns, num);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597
1598 NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
1599 num, NS_RAW_OFFSET(ns) + ns->regs.off);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001600
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (ns->regs.off == 0)
1602 NS_LOG("read page %d\n", ns->regs.row);
1603 else if (ns->regs.off < ns->geom.pgsz)
1604 NS_LOG("read page %d (second half)\n", ns->regs.row);
1605 else
1606 NS_LOG("read OOB of page %d\n", ns->regs.row);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 NS_UDELAY(access_delay);
1609 NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
1610
1611 break;
1612
1613 case ACTION_SECERASE:
1614 /*
1615 * Erase sector.
1616 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001617
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (ns->lines.wp) {
1619 NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
1620 return -1;
1621 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001622
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
1624 || (ns->regs.row & ~(ns->geom.secsz - 1))) {
1625 NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
1626 return -1;
1627 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001628
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 ns->regs.row = (ns->regs.row <<
1630 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
1631 ns->regs.column = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001632
Adrian Hunter514087e72007-03-19 12:47:45 +02001633 erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
1634
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
1636 ns->regs.row, NS_RAW_OFFSET(ns));
Adrian Hunter514087e72007-03-19 12:47:45 +02001637 NS_LOG("erase sector %u\n", erase_block_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Vijay Kumard086d432006-10-08 22:02:31 +05301639 erase_sector(ns);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 NS_MDELAY(erase_delay);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001642
Adrian Hunter57aa6b52007-03-19 12:40:41 +02001643 if (erase_block_wear)
1644 update_wear(erase_block_no);
1645
Adrian Hunter514087e72007-03-19 12:47:45 +02001646 if (erase_error(erase_block_no)) {
1647 NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
1648 return -1;
1649 }
1650
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 break;
1652
1653 case ACTION_PRGPAGE:
1654 /*
srimugunthandaf05ec2010-11-13 12:46:05 +02001655 * Program page - move internal buffer data to the page.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 */
1657
1658 if (ns->lines.wp) {
1659 NS_WARN("do_state_action: device is write-protected, programm\n");
1660 return -1;
1661 }
1662
1663 num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1664 if (num != ns->regs.count) {
1665 NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
1666 ns->regs.count, num);
1667 return -1;
1668 }
1669
Vijay Kumard086d432006-10-08 22:02:31 +05301670 if (prog_page(ns, num) == -1)
1671 return -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672
Adrian Hunter514087e72007-03-19 12:47:45 +02001673 page_no = ns->regs.row;
1674
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
1676 num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
1677 NS_LOG("programm page %d\n", ns->regs.row);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001678
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679 NS_UDELAY(programm_delay);
1680 NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001681
Adrian Hunter514087e72007-03-19 12:47:45 +02001682 if (write_error(page_no)) {
1683 NS_WARN("simulating write failure in page %u\n", page_no);
1684 return -1;
1685 }
1686
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001688
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 case ACTION_ZEROOFF:
1690 NS_DBG("do_state_action: set internal offset to 0\n");
1691 ns->regs.off = 0;
1692 break;
1693
1694 case ACTION_HALFOFF:
1695 if (!(ns->options & OPT_PAGE512_8BIT)) {
1696 NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
1697 "byte page size 8x chips\n");
1698 return -1;
1699 }
1700 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
1701 ns->regs.off = ns->geom.pgsz/2;
1702 break;
1703
1704 case ACTION_OOBOFF:
1705 NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
1706 ns->regs.off = ns->geom.pgsz;
1707 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001708
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709 default:
1710 NS_DBG("do_state_action: BUG! unknown action\n");
1711 }
1712
1713 return 0;
1714}
1715
1716/*
1717 * Switch simulator's state.
1718 */
Vijay Kumara5602142006-10-14 21:33:34 +05301719static void switch_state(struct nandsim *ns)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720{
1721 if (ns->op) {
1722 /*
1723 * The current operation have already been identified.
1724 * Just follow the states chain.
1725 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ns->stateidx += 1;
1728 ns->state = ns->nxstate;
1729 ns->nxstate = ns->op[ns->stateidx + 1];
1730
1731 NS_DBG("switch_state: operation is known, switch to the next state, "
1732 "state: %s, nxstate: %s\n",
1733 get_state_name(ns->state), get_state_name(ns->nxstate));
1734
1735 /* See, whether we need to do some action */
1736 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1737 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1738 return;
1739 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001740
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 } else {
1742 /*
1743 * We don't yet know which operation we perform.
1744 * Try to identify it.
1745 */
1746
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001747 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 * The only event causing the switch_state function to
1749 * be called with yet unknown operation is new command.
1750 */
1751 ns->state = get_state_by_command(ns->regs.command);
1752
1753 NS_DBG("switch_state: operation is unknown, try to find it\n");
1754
1755 if (find_operation(ns, 0) != 0)
1756 return;
1757
1758 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1759 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1760 return;
1761 }
1762 }
1763
1764 /* For 16x devices column means the page offset in words */
1765 if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
1766 NS_DBG("switch_state: double the column number for 16x device\n");
1767 ns->regs.column <<= 1;
1768 }
1769
1770 if (NS_STATE(ns->nxstate) == STATE_READY) {
1771 /*
1772 * The current state is the last. Return to STATE_READY
1773 */
1774
1775 u_char status = NS_STATUS_OK(ns);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001776
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777 /* In case of data states, see if all bytes were input/output */
1778 if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
1779 && ns->regs.count != ns->regs.num) {
1780 NS_WARN("switch_state: not all bytes were processed, %d left\n",
1781 ns->regs.num - ns->regs.count);
1782 status = NS_STATUS_FAILED(ns);
1783 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001784
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
1786
1787 switch_to_ready_state(ns, status);
1788
1789 return;
1790 } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001791 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792 * If the next state is data input/output, switch to it now
1793 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001794
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 ns->state = ns->nxstate;
1796 ns->nxstate = ns->op[++ns->stateidx + 1];
1797 ns->regs.num = ns->regs.count = 0;
1798
1799 NS_DBG("switch_state: the next state is data I/O, switch, "
1800 "state: %s, nxstate: %s\n",
1801 get_state_name(ns->state), get_state_name(ns->nxstate));
1802
1803 /*
1804 * Set the internal register to the count of bytes which
1805 * are expected to be input or output
1806 */
1807 switch (NS_STATE(ns->state)) {
1808 case STATE_DATAIN:
1809 case STATE_DATAOUT:
1810 ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
1811 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001812
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 case STATE_DATAOUT_ID:
1814 ns->regs.num = ns->geom.idbytes;
1815 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001816
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 case STATE_DATAOUT_STATUS:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818 ns->regs.count = ns->regs.num = 0;
1819 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001820
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 default:
1822 NS_ERR("switch_state: BUG! unknown data state\n");
1823 }
1824
1825 } else if (ns->nxstate & STATE_ADDR_MASK) {
1826 /*
1827 * If the next state is address input, set the internal
1828 * register to the number of expected address bytes
1829 */
1830
1831 ns->regs.count = 0;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001832
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 switch (NS_STATE(ns->nxstate)) {
1834 case STATE_ADDR_PAGE:
1835 ns->regs.num = ns->geom.pgaddrbytes;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001836
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837 break;
1838 case STATE_ADDR_SEC:
1839 ns->regs.num = ns->geom.secaddrbytes;
1840 break;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001841
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 case STATE_ADDR_ZERO:
1843 ns->regs.num = 1;
1844 break;
1845
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001846 case STATE_ADDR_COLUMN:
1847 /* Column address is always 2 bytes */
1848 ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
1849 break;
1850
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 default:
1852 NS_ERR("switch_state: BUG! unknown address state\n");
1853 }
1854 } else {
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001855 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 * Just reset internal counters.
1857 */
1858
1859 ns->regs.num = 0;
1860 ns->regs.count = 0;
1861 }
1862}
1863
Boris Brezillon7e534322018-09-06 14:05:22 +02001864static u_char ns_nand_read_byte(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865{
Brian Norrisc66b6512016-01-07 11:06:46 -08001866 struct nandsim *ns = nand_get_controller_data(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 u_char outb = 0x00;
1868
1869 /* Sanity and correctness checks */
1870 if (!ns->lines.ce) {
1871 NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
1872 return outb;
1873 }
1874 if (ns->lines.ale || ns->lines.cle) {
1875 NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
1876 return outb;
1877 }
1878 if (!(ns->state & STATE_DATAOUT_MASK)) {
1879 NS_WARN("read_byte: unexpected data output cycle, state is %s "
1880 "return %#x\n", get_state_name(ns->state), (uint)outb);
1881 return outb;
1882 }
1883
1884 /* Status register may be read as many times as it is wanted */
1885 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
1886 NS_DBG("read_byte: return %#x status\n", ns->regs.status);
1887 return ns->regs.status;
1888 }
1889
1890 /* Check if there is any data in the internal buffer which may be read */
1891 if (ns->regs.count == ns->regs.num) {
1892 NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
1893 return outb;
1894 }
1895
1896 switch (NS_STATE(ns->state)) {
1897 case STATE_DATAOUT:
1898 if (ns->busw == 8) {
1899 outb = ns->buf.byte[ns->regs.count];
1900 ns->regs.count += 1;
1901 } else {
1902 outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
1903 ns->regs.count += 2;
1904 }
1905 break;
1906 case STATE_DATAOUT_ID:
1907 NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
1908 outb = ns->ids[ns->regs.count];
1909 ns->regs.count += 1;
1910 break;
1911 default:
1912 BUG();
1913 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001914
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 if (ns->regs.count == ns->regs.num) {
1916 NS_DBG("read_byte: all bytes were read\n");
1917
Brian Norris831d3162012-05-01 17:12:53 -07001918 if (NS_STATE(ns->nxstate) == STATE_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 switch_state(ns);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 return outb;
1923}
1924
Boris Brezillonc0739d82018-09-06 14:05:23 +02001925static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926{
Brian Norrisc66b6512016-01-07 11:06:46 -08001927 struct nandsim *ns = nand_get_controller_data(chip);
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001928
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 /* Sanity and correctness checks */
1930 if (!ns->lines.ce) {
1931 NS_ERR("write_byte: chip is disabled, ignore write\n");
1932 return;
1933 }
1934 if (ns->lines.ale && ns->lines.cle) {
1935 NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
1936 return;
1937 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001938
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939 if (ns->lines.cle == 1) {
1940 /*
1941 * The byte written is a command.
1942 */
1943
1944 if (byte == NAND_CMD_RESET) {
1945 NS_LOG("reset chip\n");
1946 switch_to_ready_state(ns, NS_STATUS_OK(ns));
1947 return;
1948 }
1949
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001950 /* Check that the command byte is correct */
1951 if (check_command(byte)) {
1952 NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
1953 return;
1954 }
1955
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956 if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001957 || NS_STATE(ns->state) == STATE_DATAOUT) {
1958 int row = ns->regs.row;
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 switch_state(ns);
Artem Bityutskiy74216be2008-07-30 11:18:42 +03001961 if (byte == NAND_CMD_RNDOUT)
1962 ns->regs.row = row;
1963 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964
1965 /* Check if chip is expecting command */
1966 if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
Adrian Hunter9359ea462008-11-12 16:06:40 +02001967 /* Do not warn if only 2 id bytes are read */
1968 if (!(ns->regs.command == NAND_CMD_READID &&
1969 NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
1970 /*
1971 * We are in situation when something else (not command)
1972 * was expected but command was input. In this case ignore
1973 * previous command(s)/state(s) and accept the last one.
1974 */
1975 NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
1976 "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
1977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
1979 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 NS_DBG("command byte corresponding to %s state accepted\n",
1982 get_state_name(get_state_by_command(byte)));
1983 ns->regs.command = byte;
1984 switch_state(ns);
1985
1986 } else if (ns->lines.ale == 1) {
1987 /*
1988 * The byte written is an address.
1989 */
1990
1991 if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
1992
1993 NS_DBG("write_byte: operation isn't known yet, identify it\n");
1994
1995 if (find_operation(ns, 1) < 0)
1996 return;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00001997
Linus Torvalds1da177e2005-04-16 15:20:36 -07001998 if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
1999 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2000 return;
2001 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002002
Linus Torvalds1da177e2005-04-16 15:20:36 -07002003 ns->regs.count = 0;
2004 switch (NS_STATE(ns->nxstate)) {
2005 case STATE_ADDR_PAGE:
2006 ns->regs.num = ns->geom.pgaddrbytes;
2007 break;
2008 case STATE_ADDR_SEC:
2009 ns->regs.num = ns->geom.secaddrbytes;
2010 break;
2011 case STATE_ADDR_ZERO:
2012 ns->regs.num = 1;
2013 break;
2014 default:
2015 BUG();
2016 }
2017 }
2018
2019 /* Check that chip is expecting address */
2020 if (!(ns->nxstate & STATE_ADDR_MASK)) {
2021 NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
2022 "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
2023 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2024 return;
2025 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002026
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 /* Check if this is expected byte */
2028 if (ns->regs.count == ns->regs.num) {
2029 NS_ERR("write_byte: no more address bytes expected\n");
2030 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2031 return;
2032 }
2033
2034 accept_addr_byte(ns, byte);
2035
2036 ns->regs.count += 1;
2037
2038 NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
2039 (uint)byte, ns->regs.count, ns->regs.num);
2040
2041 if (ns->regs.count == ns->regs.num) {
2042 NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
2043 switch_state(ns);
2044 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002045
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046 } else {
2047 /*
2048 * The byte written is an input data.
2049 */
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 /* Check that chip is expecting data input */
2052 if (!(ns->state & STATE_DATAIN_MASK)) {
2053 NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
2054 "switch to %s\n", (uint)byte,
2055 get_state_name(ns->state), get_state_name(STATE_READY));
2056 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2057 return;
2058 }
2059
2060 /* Check if this is expected byte */
2061 if (ns->regs.count == ns->regs.num) {
2062 NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
2063 ns->regs.num);
2064 return;
2065 }
2066
2067 if (ns->busw == 8) {
2068 ns->buf.byte[ns->regs.count] = byte;
2069 ns->regs.count += 1;
2070 } else {
2071 ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
2072 ns->regs.count += 2;
2073 }
2074 }
2075
2076 return;
2077}
2078
Boris Brezillon0f808c12018-09-06 14:05:26 +02002079static void ns_hwcontrol(struct nand_chip *chip, int cmd, unsigned int bitmask)
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02002080{
Brian Norrisc66b6512016-01-07 11:06:46 -08002081 struct nandsim *ns = nand_get_controller_data(chip);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02002082
2083 ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
2084 ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
2085 ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
2086
2087 if (cmd != NAND_CMD_NONE)
Boris Brezillonc0739d82018-09-06 14:05:23 +02002088 ns_nand_write_byte(chip, cmd);
Thomas Gleixner7abd3ef2006-05-23 23:25:53 +02002089}
2090
Boris Brezillon50a487e2018-09-06 14:05:27 +02002091static int ns_device_ready(struct nand_chip *chip)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092{
2093 NS_DBG("device_ready\n");
2094 return 1;
2095}
2096
Boris Brezillonc0739d82018-09-06 14:05:23 +02002097static void ns_nand_write_buf(struct nand_chip *chip, const u_char *buf,
2098 int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099{
Brian Norrisc66b6512016-01-07 11:06:46 -08002100 struct nandsim *ns = nand_get_controller_data(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101
2102 /* Check that chip is expecting data input */
2103 if (!(ns->state & STATE_DATAIN_MASK)) {
2104 NS_ERR("write_buf: data input isn't expected, state is %s, "
2105 "switch to STATE_READY\n", get_state_name(ns->state));
2106 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2107 return;
2108 }
2109
2110 /* Check if these are expected bytes */
2111 if (ns->regs.count + len > ns->regs.num) {
2112 NS_ERR("write_buf: too many input bytes\n");
2113 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2114 return;
2115 }
2116
2117 memcpy(ns->buf.byte + ns->regs.count, buf, len);
2118 ns->regs.count += len;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002119
Linus Torvalds1da177e2005-04-16 15:20:36 -07002120 if (ns->regs.count == ns->regs.num) {
2121 NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
2122 }
2123}
2124
Boris Brezillon7e534322018-09-06 14:05:22 +02002125static void ns_nand_read_buf(struct nand_chip *chip, u_char *buf, int len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126{
Brian Norrisc66b6512016-01-07 11:06:46 -08002127 struct nandsim *ns = nand_get_controller_data(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128
2129 /* Sanity and correctness checks */
2130 if (!ns->lines.ce) {
2131 NS_ERR("read_buf: chip is disabled\n");
2132 return;
2133 }
2134 if (ns->lines.ale || ns->lines.cle) {
2135 NS_ERR("read_buf: ALE or CLE pin is high\n");
2136 return;
2137 }
2138 if (!(ns->state & STATE_DATAOUT_MASK)) {
2139 NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
2140 get_state_name(ns->state));
2141 return;
2142 }
2143
2144 if (NS_STATE(ns->state) != STATE_DATAOUT) {
2145 int i;
2146
2147 for (i = 0; i < len; i++)
Boris Brezillon716bbba2018-09-07 00:38:35 +02002148 buf[i] = chip->legacy.read_byte(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149
2150 return;
2151 }
2152
2153 /* Check if these are expected bytes */
2154 if (ns->regs.count + len > ns->regs.num) {
2155 NS_ERR("read_buf: too many bytes to read\n");
2156 switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
2157 return;
2158 }
2159
2160 memcpy(buf, ns->buf.byte + ns->regs.count, len);
2161 ns->regs.count += len;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163 if (ns->regs.count == ns->regs.num) {
Brian Norris831d3162012-05-01 17:12:53 -07002164 if (NS_STATE(ns->nxstate) == STATE_READY)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 switch_state(ns);
2166 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002167
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 return;
2169}
2170
Miquel Raynal5cbad9e2018-07-20 17:15:08 +02002171static int ns_attach_chip(struct nand_chip *chip)
2172{
2173 unsigned int eccsteps, eccbytes;
2174
2175 if (!bch)
2176 return 0;
2177
2178 if (!mtd_nand_has_bch()) {
2179 NS_ERR("BCH ECC support is disabled\n");
2180 return -EINVAL;
2181 }
2182
2183 /* Use 512-byte ecc blocks */
2184 eccsteps = nsmtd->writesize / 512;
2185 eccbytes = ((bch * 13) + 7) / 8;
2186
2187 /* Do not bother supporting small page devices */
2188 if (nsmtd->oobsize < 64 || !eccsteps) {
2189 NS_ERR("BCH not available on small page devices\n");
2190 return -EINVAL;
2191 }
2192
2193 if (((eccbytes * eccsteps) + 2) > nsmtd->oobsize) {
2194 NS_ERR("Invalid BCH value %u\n", bch);
2195 return -EINVAL;
2196 }
2197
2198 chip->ecc.mode = NAND_ECC_SOFT;
2199 chip->ecc.algo = NAND_ECC_BCH;
2200 chip->ecc.size = 512;
2201 chip->ecc.strength = bch;
2202 chip->ecc.bytes = eccbytes;
2203
2204 NS_INFO("Using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
2205
2206 return 0;
2207}
2208
2209static const struct nand_controller_ops ns_controller_ops = {
2210 .attach_chip = ns_attach_chip,
2211};
2212
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 * Module initialization function
2215 */
Adrian Bunk2b9175c2005-11-29 14:49:38 +00002216static int __init ns_init_module(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217{
2218 struct nand_chip *chip;
2219 struct nandsim *nand;
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002220 int retval = -ENOMEM, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
2222 if (bus_width != 8 && bus_width != 16) {
2223 NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
2224 return -EINVAL;
2225 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002226
Linus Torvalds1da177e2005-04-16 15:20:36 -07002227 /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
Boris BREZILLONed10f162015-12-10 09:00:13 +01002228 chip = kzalloc(sizeof(struct nand_chip) + sizeof(struct nandsim),
2229 GFP_KERNEL);
2230 if (!chip) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002231 NS_ERR("unable to allocate core structures.\n");
2232 return -ENOMEM;
2233 }
Boris BREZILLONed10f162015-12-10 09:00:13 +01002234 nsmtd = nand_to_mtd(chip);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002235 nand = (struct nandsim *)(chip + 1);
Boris BREZILLONd699ed22015-12-10 09:00:41 +01002236 nand_set_controller_data(chip, (void *)nand);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002237
2238 /*
2239 * Register simulator's callbacks.
2240 */
Boris Brezillonbf6065c2018-09-07 00:38:36 +02002241 chip->legacy.cmd_ctrl = ns_hwcontrol;
Boris Brezillon716bbba2018-09-07 00:38:35 +02002242 chip->legacy.read_byte = ns_nand_read_byte;
Boris Brezillon8395b752018-09-07 00:38:37 +02002243 chip->legacy.dev_ready = ns_device_ready;
Boris Brezillon716bbba2018-09-07 00:38:35 +02002244 chip->legacy.write_buf = ns_nand_write_buf;
2245 chip->legacy.read_buf = ns_nand_read_buf;
Thomas Gleixner6dfc6d22006-05-23 12:00:46 +02002246 chip->ecc.mode = NAND_ECC_SOFT;
Rafał Miłecki8ae6bcd2016-03-23 11:19:03 +01002247 chip->ecc.algo = NAND_ECC_HAMMING;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002248 /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
2249 /* and 'badblocks' parameters to work */
Artem B. Bityuckiy51502282005-03-19 15:33:59 +00002250 chip->options |= NAND_SKIP_BBTSCAN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002252 switch (bbt) {
2253 case 2:
Gustavo A. R. Silva64f1da12019-02-08 11:49:30 -06002254 chip->bbt_options |= NAND_BBT_NO_OOB;
2255 /* fall through */
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002256 case 1:
Gustavo A. R. Silva64f1da12019-02-08 11:49:30 -06002257 chip->bbt_options |= NAND_BBT_USE_FLASH;
2258 /* fall through */
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002259 case 0:
2260 break;
2261 default:
2262 NS_ERR("bbt has to be 0..2\n");
2263 retval = -EINVAL;
2264 goto error;
2265 }
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002266 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002267 * Perform minimum nandsim structure initialization to handle
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002268 * the initial ID read command correctly
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 */
Akinobu Mitab00358a2014-10-23 08:41:44 +09002270 if (id_bytes[6] != 0xFF || id_bytes[7] != 0xFF)
2271 nand->geom.idbytes = 8;
2272 else if (id_bytes[4] != 0xFF || id_bytes[5] != 0xFF)
2273 nand->geom.idbytes = 6;
2274 else if (id_bytes[2] != 0xFF || id_bytes[3] != 0xFF)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275 nand->geom.idbytes = 4;
2276 else
2277 nand->geom.idbytes = 2;
2278 nand->regs.status = NS_STATUS_OK(nand);
2279 nand->nxstate = STATE_UNKNOWN;
Artem Bityutskiy51148f12013-03-05 15:00:51 +02002280 nand->options |= OPT_PAGE512; /* temporary value */
Akinobu Mitab00358a2014-10-23 08:41:44 +09002281 memcpy(nand->ids, id_bytes, sizeof(nand->ids));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 if (bus_width == 16) {
2283 nand->busw = 16;
2284 chip->options |= NAND_BUSWIDTH_16;
2285 }
2286
David Woodhouse552d9202006-05-14 01:20:46 +01002287 nsmtd->owner = THIS_MODULE;
2288
Adrian Hunter514087e72007-03-19 12:47:45 +02002289 if ((retval = parse_weakblocks()) != 0)
2290 goto error;
2291
2292 if ((retval = parse_weakpages()) != 0)
2293 goto error;
2294
2295 if ((retval = parse_gravepages()) != 0)
2296 goto error;
2297
Boris Brezillon7b6a9b22018-11-20 10:02:39 +01002298 chip->legacy.dummy_controller.ops = &ns_controller_ops;
Boris Brezillon00ad3782018-09-06 14:05:14 +02002299 retval = nand_scan(chip, 1);
Ivan Djelicfc2ff592011-03-11 11:05:34 +01002300 if (retval) {
Miquel Raynal5cbad9e2018-07-20 17:15:08 +02002301 NS_ERR("Could not scan NAND Simulator device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002302 goto error;
2303 }
2304
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002305 if (overridesize) {
David Woodhouse0f07a0b2008-12-10 14:01:46 +00002306 uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
Boris Brezillon629a4422018-10-25 17:10:37 +02002307 struct nand_memory_organization *memorg;
Boris Brezillon6c836d52018-10-29 11:22:16 +01002308 u64 targetsize;
Boris Brezillon629a4422018-10-25 17:10:37 +02002309
2310 memorg = nanddev_get_memorg(&chip->base);
2311
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002312 if (new_size >> overridesize != nsmtd->erasesize) {
2313 NS_ERR("overridesize is too big\n");
Richard Genoudbb0a13a2012-09-12 14:26:26 +02002314 retval = -EINVAL;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002315 goto err_exit;
2316 }
Boris Brezillon6c836d52018-10-29 11:22:16 +01002317
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002318 /* N.B. This relies on nand_scan not doing anything with the size before we change it */
2319 nsmtd->size = new_size;
Boris Brezillon629a4422018-10-25 17:10:37 +02002320 memorg->eraseblocks_per_lun = 1 << overridesize;
Boris Brezillon6c836d52018-10-29 11:22:16 +01002321 targetsize = nanddev_target_size(&chip->base);
Adrian Hunter6eda7a52008-05-30 15:56:26 +03002322 chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
Boris Brezillon6c836d52018-10-29 11:22:16 +01002323 chip->pagemask = (targetsize >> chip->page_shift) - 1;
Adrian Huntera5ac8ae2007-03-19 12:49:11 +02002324 }
2325
Adrian Hunter57aa6b52007-03-19 12:40:41 +02002326 if ((retval = setup_wear_reporting(nsmtd)) != 0)
2327 goto err_exit;
2328
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002329 if ((retval = init_nandsim(nsmtd)) != 0)
2330 goto err_exit;
Thomas Gleixner61b03bd2005-11-07 11:15:49 +00002331
Boris Brezillone80eba72018-07-05 12:27:31 +02002332 if ((retval = nand_create_bbt(chip)) != 0)
Adrian Hunter514087e72007-03-19 12:47:45 +02002333 goto err_exit;
2334
Sebastian Andrzej Siewiorce85b792010-09-29 19:43:54 +02002335 if ((retval = parse_badblocks(nand, nsmtd)) != 0)
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002336 goto err_exit;
Artem B. Bityuckiy51502282005-03-19 15:33:59 +00002337
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002338 /* Register NAND partitions */
Jamie Ilesee0e87b2011-05-23 10:23:40 +01002339 retval = mtd_device_register(nsmtd, &nand->partitions[0],
2340 nand->nbparts);
2341 if (retval != 0)
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002342 goto err_exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002343
Mario Rugieroe8e3edb2017-05-29 08:38:41 -03002344 if ((retval = nandsim_debugfs_create(nand)) != 0)
2345 goto err_exit;
2346
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 return 0;
2348
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002349err_exit:
2350 free_nandsim(nand);
Boris Brezillon59ac2762018-09-06 14:05:15 +02002351 nand_release(chip);
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002352 for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
2353 kfree(nand->partitions[i].name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354error:
Boris BREZILLONed10f162015-12-10 09:00:13 +01002355 kfree(chip);
Adrian Hunter514087e72007-03-19 12:47:45 +02002356 free_lists();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357
2358 return retval;
2359}
2360
2361module_init(ns_init_module);
2362
2363/*
2364 * Module clean-up function
2365 */
2366static void __exit ns_cleanup_module(void)
2367{
Brian Norrisc66b6512016-01-07 11:06:46 -08002368 struct nand_chip *chip = mtd_to_nand(nsmtd);
2369 struct nandsim *ns = nand_get_controller_data(chip);
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002370 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
2372 free_nandsim(ns); /* Free nandsim private resources */
Boris Brezillon59ac2762018-09-06 14:05:15 +02002373 nand_release(chip); /* Unregister driver */
Adrian Hunter2b77a0e2007-03-19 12:46:43 +02002374 for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
2375 kfree(ns->partitions[i].name);
Boris BREZILLONed10f162015-12-10 09:00:13 +01002376 kfree(mtd_to_nand(nsmtd)); /* Free other structures */
Adrian Hunter514087e72007-03-19 12:47:45 +02002377 free_lists();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002378}
2379
2380module_exit(ns_cleanup_module);
2381
2382MODULE_LICENSE ("GPL");
2383MODULE_AUTHOR ("Artem B. Bityuckiy");
2384MODULE_DESCRIPTION ("The NAND flash simulator");