1660662f8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
293db446aSBoris Brezillon /*
393db446aSBoris Brezillon * NAND flash simulator.
493db446aSBoris Brezillon *
593db446aSBoris Brezillon * Author: Artem B. Bityuckiy <dedekind@oktetlabs.ru>, <dedekind@infradead.org>
693db446aSBoris Brezillon *
793db446aSBoris Brezillon * Copyright (C) 2004 Nokia Corporation
893db446aSBoris Brezillon *
993db446aSBoris Brezillon * Note: NS means "NAND Simulator".
1093db446aSBoris Brezillon * Note: Input means input TO flash chip, output means output FROM chip.
1193db446aSBoris Brezillon */
1293db446aSBoris Brezillon
1363fa37f0SShreeya Patel #define pr_fmt(fmt) "[nandsim]" fmt
1463fa37f0SShreeya Patel
1593db446aSBoris Brezillon #include <linux/init.h>
1693db446aSBoris Brezillon #include <linux/types.h>
1793db446aSBoris Brezillon #include <linux/module.h>
1893db446aSBoris Brezillon #include <linux/moduleparam.h>
1993db446aSBoris Brezillon #include <linux/vmalloc.h>
2093db446aSBoris Brezillon #include <linux/math64.h>
2193db446aSBoris Brezillon #include <linux/slab.h>
2293db446aSBoris Brezillon #include <linux/errno.h>
2393db446aSBoris Brezillon #include <linux/string.h>
2493db446aSBoris Brezillon #include <linux/mtd/mtd.h>
2593db446aSBoris Brezillon #include <linux/mtd/rawnand.h>
2693db446aSBoris Brezillon #include <linux/mtd/partitions.h>
2793db446aSBoris Brezillon #include <linux/delay.h>
2893db446aSBoris Brezillon #include <linux/list.h>
2993db446aSBoris Brezillon #include <linux/random.h>
3093db446aSBoris Brezillon #include <linux/sched.h>
3193db446aSBoris Brezillon #include <linux/sched/mm.h>
3293db446aSBoris Brezillon #include <linux/fs.h>
3393db446aSBoris Brezillon #include <linux/pagemap.h>
3493db446aSBoris Brezillon #include <linux/seq_file.h>
3593db446aSBoris Brezillon #include <linux/debugfs.h>
3693db446aSBoris Brezillon
3793db446aSBoris Brezillon /* Default simulator parameters values */
3893db446aSBoris Brezillon #if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
3993db446aSBoris Brezillon !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
4093db446aSBoris Brezillon !defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
4193db446aSBoris Brezillon !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
4293db446aSBoris Brezillon #define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
4393db446aSBoris Brezillon #define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
4493db446aSBoris Brezillon #define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF /* No byte */
4593db446aSBoris Brezillon #define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */
4693db446aSBoris Brezillon #endif
4793db446aSBoris Brezillon
4893db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_ACCESS_DELAY
4993db446aSBoris Brezillon #define CONFIG_NANDSIM_ACCESS_DELAY 25
5093db446aSBoris Brezillon #endif
5193db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
5293db446aSBoris Brezillon #define CONFIG_NANDSIM_PROGRAMM_DELAY 200
5393db446aSBoris Brezillon #endif
5493db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_ERASE_DELAY
5593db446aSBoris Brezillon #define CONFIG_NANDSIM_ERASE_DELAY 2
5693db446aSBoris Brezillon #endif
5793db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
5893db446aSBoris Brezillon #define CONFIG_NANDSIM_OUTPUT_CYCLE 40
5993db446aSBoris Brezillon #endif
6093db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_INPUT_CYCLE
6193db446aSBoris Brezillon #define CONFIG_NANDSIM_INPUT_CYCLE 50
6293db446aSBoris Brezillon #endif
6393db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_BUS_WIDTH
6493db446aSBoris Brezillon #define CONFIG_NANDSIM_BUS_WIDTH 8
6593db446aSBoris Brezillon #endif
6693db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_DO_DELAYS
6793db446aSBoris Brezillon #define CONFIG_NANDSIM_DO_DELAYS 0
6893db446aSBoris Brezillon #endif
6993db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_LOG
7093db446aSBoris Brezillon #define CONFIG_NANDSIM_LOG 0
7193db446aSBoris Brezillon #endif
7293db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_DBG
7393db446aSBoris Brezillon #define CONFIG_NANDSIM_DBG 0
7493db446aSBoris Brezillon #endif
7593db446aSBoris Brezillon #ifndef CONFIG_NANDSIM_MAX_PARTS
7693db446aSBoris Brezillon #define CONFIG_NANDSIM_MAX_PARTS 32
7793db446aSBoris Brezillon #endif
7893db446aSBoris Brezillon
7993db446aSBoris Brezillon static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
8093db446aSBoris Brezillon static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
8193db446aSBoris Brezillon static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
8293db446aSBoris Brezillon static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
8393db446aSBoris Brezillon static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
8493db446aSBoris Brezillon static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
8593db446aSBoris Brezillon static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
8693db446aSBoris Brezillon static uint log = CONFIG_NANDSIM_LOG;
8793db446aSBoris Brezillon static uint dbg = CONFIG_NANDSIM_DBG;
8893db446aSBoris Brezillon static unsigned long parts[CONFIG_NANDSIM_MAX_PARTS];
8993db446aSBoris Brezillon static unsigned int parts_num;
9093db446aSBoris Brezillon static char *badblocks = NULL;
9193db446aSBoris Brezillon static char *weakblocks = NULL;
9293db446aSBoris Brezillon static char *weakpages = NULL;
9393db446aSBoris Brezillon static unsigned int bitflips = 0;
9493db446aSBoris Brezillon static char *gravepages = NULL;
9593db446aSBoris Brezillon static unsigned int overridesize = 0;
9693db446aSBoris Brezillon static char *cache_file = NULL;
9793db446aSBoris Brezillon static unsigned int bbt;
9893db446aSBoris Brezillon static unsigned int bch;
9993db446aSBoris Brezillon static u_char id_bytes[8] = {
10093db446aSBoris Brezillon [0] = CONFIG_NANDSIM_FIRST_ID_BYTE,
10193db446aSBoris Brezillon [1] = CONFIG_NANDSIM_SECOND_ID_BYTE,
10293db446aSBoris Brezillon [2] = CONFIG_NANDSIM_THIRD_ID_BYTE,
10393db446aSBoris Brezillon [3] = CONFIG_NANDSIM_FOURTH_ID_BYTE,
10493db446aSBoris Brezillon [4 ... 7] = 0xFF,
10593db446aSBoris Brezillon };
10693db446aSBoris Brezillon
10793db446aSBoris Brezillon module_param_array(id_bytes, byte, NULL, 0400);
10893db446aSBoris Brezillon module_param_named(first_id_byte, id_bytes[0], byte, 0400);
10993db446aSBoris Brezillon module_param_named(second_id_byte, id_bytes[1], byte, 0400);
11093db446aSBoris Brezillon module_param_named(third_id_byte, id_bytes[2], byte, 0400);
11193db446aSBoris Brezillon module_param_named(fourth_id_byte, id_bytes[3], byte, 0400);
11293db446aSBoris Brezillon module_param(access_delay, uint, 0400);
11393db446aSBoris Brezillon module_param(programm_delay, uint, 0400);
11493db446aSBoris Brezillon module_param(erase_delay, uint, 0400);
11593db446aSBoris Brezillon module_param(output_cycle, uint, 0400);
11693db446aSBoris Brezillon module_param(input_cycle, uint, 0400);
11793db446aSBoris Brezillon module_param(bus_width, uint, 0400);
11893db446aSBoris Brezillon module_param(do_delays, uint, 0400);
11993db446aSBoris Brezillon module_param(log, uint, 0400);
12093db446aSBoris Brezillon module_param(dbg, uint, 0400);
12193db446aSBoris Brezillon module_param_array(parts, ulong, &parts_num, 0400);
12293db446aSBoris Brezillon module_param(badblocks, charp, 0400);
12393db446aSBoris Brezillon module_param(weakblocks, charp, 0400);
12493db446aSBoris Brezillon module_param(weakpages, charp, 0400);
12593db446aSBoris Brezillon module_param(bitflips, uint, 0400);
12693db446aSBoris Brezillon module_param(gravepages, charp, 0400);
12793db446aSBoris Brezillon module_param(overridesize, uint, 0400);
12893db446aSBoris Brezillon module_param(cache_file, charp, 0400);
12993db446aSBoris Brezillon module_param(bbt, uint, 0400);
13093db446aSBoris Brezillon module_param(bch, uint, 0400);
13193db446aSBoris Brezillon
13293db446aSBoris Brezillon MODULE_PARM_DESC(id_bytes, "The ID bytes returned by NAND Flash 'read ID' command");
13393db446aSBoris Brezillon MODULE_PARM_DESC(first_id_byte, "The first byte returned by NAND Flash 'read ID' command (manufacturer ID) (obsolete)");
13493db446aSBoris Brezillon MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID) (obsolete)");
13593db446aSBoris Brezillon MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command (obsolete)");
13693db446aSBoris Brezillon MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command (obsolete)");
13793db446aSBoris Brezillon MODULE_PARM_DESC(access_delay, "Initial page access delay (microseconds)");
13893db446aSBoris Brezillon MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
13993db446aSBoris Brezillon MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
14093db446aSBoris Brezillon MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanoseconds)");
14193db446aSBoris Brezillon MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanoseconds)");
14293db446aSBoris Brezillon MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
14393db446aSBoris Brezillon MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
14493db446aSBoris Brezillon MODULE_PARM_DESC(log, "Perform logging if not zero");
14593db446aSBoris Brezillon MODULE_PARM_DESC(dbg, "Output debug information if not zero");
14693db446aSBoris Brezillon MODULE_PARM_DESC(parts, "Partition sizes (in erase blocks) separated by commas");
14793db446aSBoris Brezillon /* Page and erase block positions for the following parameters are independent of any partitions */
14893db446aSBoris Brezillon MODULE_PARM_DESC(badblocks, "Erase blocks that are initially marked bad, separated by commas");
14993db446aSBoris Brezillon MODULE_PARM_DESC(weakblocks, "Weak erase blocks [: remaining erase cycles (defaults to 3)]"
15093db446aSBoris Brezillon " separated by commas e.g. 113:2 means eb 113"
15193db446aSBoris Brezillon " can be erased only twice before failing");
15293db446aSBoris Brezillon MODULE_PARM_DESC(weakpages, "Weak pages [: maximum writes (defaults to 3)]"
15393db446aSBoris Brezillon " separated by commas e.g. 1401:2 means page 1401"
15493db446aSBoris Brezillon " can be written only twice before failing");
15593db446aSBoris Brezillon MODULE_PARM_DESC(bitflips, "Maximum number of random bit flips per page (zero by default)");
15693db446aSBoris Brezillon MODULE_PARM_DESC(gravepages, "Pages that lose data [: maximum reads (defaults to 3)]"
15793db446aSBoris Brezillon " separated by commas e.g. 1401:2 means page 1401"
15893db446aSBoris Brezillon " can be read only twice before failing");
15993db446aSBoris Brezillon MODULE_PARM_DESC(overridesize, "Specifies the NAND Flash size overriding the ID bytes. "
16093db446aSBoris Brezillon "The size is specified in erase blocks and as the exponent of a power of two"
16193db446aSBoris Brezillon " e.g. 5 means a size of 32 erase blocks");
16293db446aSBoris Brezillon MODULE_PARM_DESC(cache_file, "File to use to cache nand pages instead of memory");
16393db446aSBoris Brezillon MODULE_PARM_DESC(bbt, "0 OOB, 1 BBT with marker in OOB, 2 BBT with marker in data area");
16493db446aSBoris Brezillon MODULE_PARM_DESC(bch, "Enable BCH ecc and set how many bits should "
16593db446aSBoris Brezillon "be correctable in 512-byte blocks");
16693db446aSBoris Brezillon
16793db446aSBoris Brezillon /* The largest possible page size */
16893db446aSBoris Brezillon #define NS_LARGEST_PAGE_SIZE 4096
16993db446aSBoris Brezillon
17093db446aSBoris Brezillon /* Simulator's output macros (logging, debugging, warning, error) */
17193db446aSBoris Brezillon #define NS_LOG(args...) \
17263fa37f0SShreeya Patel do { if (log) pr_debug(" log: " args); } while(0)
17393db446aSBoris Brezillon #define NS_DBG(args...) \
17463fa37f0SShreeya Patel do { if (dbg) pr_debug(" debug: " args); } while(0)
17593db446aSBoris Brezillon #define NS_WARN(args...) \
17663fa37f0SShreeya Patel do { pr_warn(" warning: " args); } while(0)
17793db446aSBoris Brezillon #define NS_ERR(args...) \
17863fa37f0SShreeya Patel do { pr_err(" error: " args); } while(0)
17993db446aSBoris Brezillon #define NS_INFO(args...) \
18063fa37f0SShreeya Patel do { pr_info(" " args); } while(0)
18193db446aSBoris Brezillon
18293db446aSBoris Brezillon /* Busy-wait delay macros (microseconds, milliseconds) */
18393db446aSBoris Brezillon #define NS_UDELAY(us) \
18493db446aSBoris Brezillon do { if (do_delays) udelay(us); } while(0)
18593db446aSBoris Brezillon #define NS_MDELAY(us) \
18693db446aSBoris Brezillon do { if (do_delays) mdelay(us); } while(0)
18793db446aSBoris Brezillon
18893db446aSBoris Brezillon /* Is the nandsim structure initialized ? */
18993db446aSBoris Brezillon #define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
19093db446aSBoris Brezillon
19193db446aSBoris Brezillon /* Good operation completion status */
19293db446aSBoris Brezillon #define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
19393db446aSBoris Brezillon
19493db446aSBoris Brezillon /* Operation failed completion status */
19593db446aSBoris Brezillon #define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
19693db446aSBoris Brezillon
19793db446aSBoris Brezillon /* Calculate the page offset in flash RAM image by (row, column) address */
19893db446aSBoris Brezillon #define NS_RAW_OFFSET(ns) \
19993db446aSBoris Brezillon (((ns)->regs.row * (ns)->geom.pgszoob) + (ns)->regs.column)
20093db446aSBoris Brezillon
20193db446aSBoris Brezillon /* Calculate the OOB offset in flash RAM image by (row, column) address */
20293db446aSBoris Brezillon #define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
20393db446aSBoris Brezillon
204109cf81fSRinHizakura /* Calculate the byte shift in the next page to access */
205109cf81fSRinHizakura #define NS_PAGE_BYTE_SHIFT(ns) ((ns)->regs.column + (ns)->regs.off)
206109cf81fSRinHizakura
20793db446aSBoris Brezillon /* After a command is input, the simulator goes to one of the following states */
20893db446aSBoris Brezillon #define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */
20993db446aSBoris Brezillon #define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */
21093db446aSBoris Brezillon #define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
21193db446aSBoris Brezillon #define STATE_CMD_PAGEPROG 0x00000004 /* start page program */
21293db446aSBoris Brezillon #define STATE_CMD_READOOB 0x00000005 /* read OOB area */
21393db446aSBoris Brezillon #define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
21493db446aSBoris Brezillon #define STATE_CMD_STATUS 0x00000007 /* read status */
21593db446aSBoris Brezillon #define STATE_CMD_SEQIN 0x00000009 /* sequential data input */
21693db446aSBoris Brezillon #define STATE_CMD_READID 0x0000000A /* read ID */
21793db446aSBoris Brezillon #define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
21893db446aSBoris Brezillon #define STATE_CMD_RESET 0x0000000C /* reset */
21993db446aSBoris Brezillon #define STATE_CMD_RNDOUT 0x0000000D /* random output command */
22093db446aSBoris Brezillon #define STATE_CMD_RNDOUTSTART 0x0000000E /* random output start command */
22193db446aSBoris Brezillon #define STATE_CMD_MASK 0x0000000F /* command states mask */
22293db446aSBoris Brezillon
22393db446aSBoris Brezillon /* After an address is input, the simulator goes to one of these states */
22493db446aSBoris Brezillon #define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
22593db446aSBoris Brezillon #define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
22693db446aSBoris Brezillon #define STATE_ADDR_COLUMN 0x00000030 /* column address was accepted */
22793db446aSBoris Brezillon #define STATE_ADDR_ZERO 0x00000040 /* one byte zero address was accepted */
22893db446aSBoris Brezillon #define STATE_ADDR_MASK 0x00000070 /* address states mask */
22993db446aSBoris Brezillon
23093db446aSBoris Brezillon /* During data input/output the simulator is in these states */
23193db446aSBoris Brezillon #define STATE_DATAIN 0x00000100 /* waiting for data input */
23293db446aSBoris Brezillon #define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
23393db446aSBoris Brezillon
23493db446aSBoris Brezillon #define STATE_DATAOUT 0x00001000 /* waiting for page data output */
23593db446aSBoris Brezillon #define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */
23693db446aSBoris Brezillon #define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */
23793db446aSBoris Brezillon #define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
23893db446aSBoris Brezillon
23993db446aSBoris Brezillon /* Previous operation is done, ready to accept new requests */
24093db446aSBoris Brezillon #define STATE_READY 0x00000000
24193db446aSBoris Brezillon
24293db446aSBoris Brezillon /* This state is used to mark that the next state isn't known yet */
24393db446aSBoris Brezillon #define STATE_UNKNOWN 0x10000000
24493db446aSBoris Brezillon
24593db446aSBoris Brezillon /* Simulator's actions bit masks */
24693db446aSBoris Brezillon #define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */
24793db446aSBoris Brezillon #define ACTION_PRGPAGE 0x00200000 /* program the internal buffer to flash */
24893db446aSBoris Brezillon #define ACTION_SECERASE 0x00300000 /* erase sector */
24993db446aSBoris Brezillon #define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */
25093db446aSBoris Brezillon #define ACTION_HALFOFF 0x00500000 /* add to address half of page */
25193db446aSBoris Brezillon #define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */
25293db446aSBoris Brezillon #define ACTION_MASK 0x00700000 /* action mask */
25393db446aSBoris Brezillon
25493db446aSBoris Brezillon #define NS_OPER_NUM 13 /* Number of operations supported by the simulator */
25593db446aSBoris Brezillon #define NS_OPER_STATES 6 /* Maximum number of states in operation */
25693db446aSBoris Brezillon
25793db446aSBoris Brezillon #define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
25893db446aSBoris Brezillon #define OPT_PAGE512 0x00000002 /* 512-byte page chips */
25993db446aSBoris Brezillon #define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
26093db446aSBoris Brezillon #define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
26193db446aSBoris Brezillon #define OPT_PAGE4096 0x00000080 /* 4096-byte page chips */
26293db446aSBoris Brezillon #define OPT_LARGEPAGE (OPT_PAGE2048 | OPT_PAGE4096) /* 2048 & 4096-byte page chips */
26393db446aSBoris Brezillon #define OPT_SMALLPAGE (OPT_PAGE512) /* 512-byte page chips */
26493db446aSBoris Brezillon
26593db446aSBoris Brezillon /* Remove action bits from state */
26693db446aSBoris Brezillon #define NS_STATE(x) ((x) & ~ACTION_MASK)
26793db446aSBoris Brezillon
26893db446aSBoris Brezillon /*
26993db446aSBoris Brezillon * Maximum previous states which need to be saved. Currently saving is
27093db446aSBoris Brezillon * only needed for page program operation with preceded read command
27193db446aSBoris Brezillon * (which is only valid for 512-byte pages).
27293db446aSBoris Brezillon */
27393db446aSBoris Brezillon #define NS_MAX_PREVSTATES 1
27493db446aSBoris Brezillon
27593db446aSBoris Brezillon /* Maximum page cache pages needed to read or write a NAND page to the cache_file */
27693db446aSBoris Brezillon #define NS_MAX_HELD_PAGES 16
27793db446aSBoris Brezillon
27893db446aSBoris Brezillon /*
27993db446aSBoris Brezillon * A union to represent flash memory contents and flash buffer.
28093db446aSBoris Brezillon */
28193db446aSBoris Brezillon union ns_mem {
28293db446aSBoris Brezillon u_char *byte; /* for byte access */
28393db446aSBoris Brezillon uint16_t *word; /* for 16-bit word access */
28493db446aSBoris Brezillon };
28593db446aSBoris Brezillon
28693db446aSBoris Brezillon /*
28793db446aSBoris Brezillon * The structure which describes all the internal simulator data.
28893db446aSBoris Brezillon */
28993db446aSBoris Brezillon struct nandsim {
29074aee14cSRichard Weinberger struct nand_chip chip;
2911c14fe21SRichard Weinberger struct nand_controller base;
29293db446aSBoris Brezillon struct mtd_partition partitions[CONFIG_NANDSIM_MAX_PARTS];
29393db446aSBoris Brezillon unsigned int nbparts;
29493db446aSBoris Brezillon
29593db446aSBoris Brezillon uint busw; /* flash chip bus width (8 or 16) */
29693db446aSBoris Brezillon u_char ids[8]; /* chip's ID bytes */
29793db446aSBoris Brezillon uint32_t options; /* chip's characteristic bits */
29893db446aSBoris Brezillon uint32_t state; /* current chip state */
29993db446aSBoris Brezillon uint32_t nxstate; /* next expected state */
30093db446aSBoris Brezillon
30193db446aSBoris Brezillon uint32_t *op; /* current operation, NULL operations isn't known yet */
30293db446aSBoris Brezillon uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
30393db446aSBoris Brezillon uint16_t npstates; /* number of previous states saved */
30493db446aSBoris Brezillon uint16_t stateidx; /* current state index */
30593db446aSBoris Brezillon
30693db446aSBoris Brezillon /* The simulated NAND flash pages array */
30793db446aSBoris Brezillon union ns_mem *pages;
30893db446aSBoris Brezillon
30993db446aSBoris Brezillon /* Slab allocator for nand pages */
31093db446aSBoris Brezillon struct kmem_cache *nand_pages_slab;
31193db446aSBoris Brezillon
31293db446aSBoris Brezillon /* Internal buffer of page + OOB size bytes */
31393db446aSBoris Brezillon union ns_mem buf;
31493db446aSBoris Brezillon
31593db446aSBoris Brezillon /* NAND flash "geometry" */
31693db446aSBoris Brezillon struct {
31793db446aSBoris Brezillon uint64_t totsz; /* total flash size, bytes */
31893db446aSBoris Brezillon uint32_t secsz; /* flash sector (erase block) size, bytes */
31993db446aSBoris Brezillon uint pgsz; /* NAND flash page size, bytes */
32093db446aSBoris Brezillon uint oobsz; /* page OOB area size, bytes */
32193db446aSBoris Brezillon uint64_t totszoob; /* total flash size including OOB, bytes */
32293db446aSBoris Brezillon uint pgszoob; /* page size including OOB , bytes*/
32393db446aSBoris Brezillon uint secszoob; /* sector size including OOB, bytes */
32493db446aSBoris Brezillon uint pgnum; /* total number of pages */
32593db446aSBoris Brezillon uint pgsec; /* number of pages per sector */
32693db446aSBoris Brezillon uint secshift; /* bits number in sector size */
32793db446aSBoris Brezillon uint pgshift; /* bits number in page size */
32893db446aSBoris Brezillon uint pgaddrbytes; /* bytes per page address */
32993db446aSBoris Brezillon uint secaddrbytes; /* bytes per sector address */
33093db446aSBoris Brezillon uint idbytes; /* the number ID bytes that this chip outputs */
33193db446aSBoris Brezillon } geom;
33293db446aSBoris Brezillon
33393db446aSBoris Brezillon /* NAND flash internal registers */
33493db446aSBoris Brezillon struct {
33593db446aSBoris Brezillon unsigned command; /* the command register */
33693db446aSBoris Brezillon u_char status; /* the status register */
33793db446aSBoris Brezillon uint row; /* the page number */
33893db446aSBoris Brezillon uint column; /* the offset within page */
33993db446aSBoris Brezillon uint count; /* internal counter */
34093db446aSBoris Brezillon uint num; /* number of bytes which must be processed */
34193db446aSBoris Brezillon uint off; /* fixed page offset */
34293db446aSBoris Brezillon } regs;
34393db446aSBoris Brezillon
34493db446aSBoris Brezillon /* NAND flash lines state */
34593db446aSBoris Brezillon struct {
34693db446aSBoris Brezillon int ce; /* chip Enable */
34793db446aSBoris Brezillon int cle; /* command Latch Enable */
34893db446aSBoris Brezillon int ale; /* address Latch Enable */
34993db446aSBoris Brezillon int wp; /* write Protect */
35093db446aSBoris Brezillon } lines;
35193db446aSBoris Brezillon
35293db446aSBoris Brezillon /* Fields needed when using a cache file */
35393db446aSBoris Brezillon struct file *cfile; /* Open file */
35493db446aSBoris Brezillon unsigned long *pages_written; /* Which pages have been written */
35593db446aSBoris Brezillon void *file_buf;
35693db446aSBoris Brezillon struct page *held_pages[NS_MAX_HELD_PAGES];
35793db446aSBoris Brezillon int held_cnt;
3587f2a1736SMiquel Raynal
3597f2a1736SMiquel Raynal /* debugfs entry */
3607f2a1736SMiquel Raynal struct dentry *dent;
36193db446aSBoris Brezillon };
36293db446aSBoris Brezillon
36393db446aSBoris Brezillon /*
36493db446aSBoris Brezillon * Operations array. To perform any operation the simulator must pass
36593db446aSBoris Brezillon * through the correspondent states chain.
36693db446aSBoris Brezillon */
36793db446aSBoris Brezillon static struct nandsim_operations {
36893db446aSBoris Brezillon uint32_t reqopts; /* options which are required to perform the operation */
36993db446aSBoris Brezillon uint32_t states[NS_OPER_STATES]; /* operation's states */
37093db446aSBoris Brezillon } ops[NS_OPER_NUM] = {
37193db446aSBoris Brezillon /* Read page + OOB from the beginning */
37293db446aSBoris Brezillon {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
37393db446aSBoris Brezillon STATE_DATAOUT, STATE_READY}},
37493db446aSBoris Brezillon /* Read page + OOB from the second half */
37593db446aSBoris Brezillon {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
37693db446aSBoris Brezillon STATE_DATAOUT, STATE_READY}},
37793db446aSBoris Brezillon /* Read OOB */
37893db446aSBoris Brezillon {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
37993db446aSBoris Brezillon STATE_DATAOUT, STATE_READY}},
38093db446aSBoris Brezillon /* Program page starting from the beginning */
38193db446aSBoris Brezillon {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
38293db446aSBoris Brezillon STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
38393db446aSBoris Brezillon /* Program page starting from the beginning */
38493db446aSBoris Brezillon {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
38593db446aSBoris Brezillon STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
38693db446aSBoris Brezillon /* Program page starting from the second half */
38793db446aSBoris Brezillon {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
38893db446aSBoris Brezillon STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
38993db446aSBoris Brezillon /* Program OOB */
39093db446aSBoris Brezillon {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
39193db446aSBoris Brezillon STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
39293db446aSBoris Brezillon /* Erase sector */
39393db446aSBoris Brezillon {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}},
39493db446aSBoris Brezillon /* Read status */
39593db446aSBoris Brezillon {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}},
39693db446aSBoris Brezillon /* Read ID */
39793db446aSBoris Brezillon {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
39893db446aSBoris Brezillon /* Large page devices read page */
39993db446aSBoris Brezillon {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
40093db446aSBoris Brezillon STATE_DATAOUT, STATE_READY}},
40193db446aSBoris Brezillon /* Large page devices random page read */
40293db446aSBoris Brezillon {OPT_LARGEPAGE, {STATE_CMD_RNDOUT, STATE_ADDR_COLUMN, STATE_CMD_RNDOUTSTART | ACTION_CPY,
40393db446aSBoris Brezillon STATE_DATAOUT, STATE_READY}},
40493db446aSBoris Brezillon };
40593db446aSBoris Brezillon
40693db446aSBoris Brezillon struct weak_block {
40793db446aSBoris Brezillon struct list_head list;
40893db446aSBoris Brezillon unsigned int erase_block_no;
40993db446aSBoris Brezillon unsigned int max_erases;
41093db446aSBoris Brezillon unsigned int erases_done;
41193db446aSBoris Brezillon };
41293db446aSBoris Brezillon
41393db446aSBoris Brezillon static LIST_HEAD(weak_blocks);
41493db446aSBoris Brezillon
41593db446aSBoris Brezillon struct weak_page {
41693db446aSBoris Brezillon struct list_head list;
41793db446aSBoris Brezillon unsigned int page_no;
41893db446aSBoris Brezillon unsigned int max_writes;
41993db446aSBoris Brezillon unsigned int writes_done;
42093db446aSBoris Brezillon };
42193db446aSBoris Brezillon
42293db446aSBoris Brezillon static LIST_HEAD(weak_pages);
42393db446aSBoris Brezillon
42493db446aSBoris Brezillon struct grave_page {
42593db446aSBoris Brezillon struct list_head list;
42693db446aSBoris Brezillon unsigned int page_no;
42793db446aSBoris Brezillon unsigned int max_reads;
42893db446aSBoris Brezillon unsigned int reads_done;
42993db446aSBoris Brezillon };
43093db446aSBoris Brezillon
43193db446aSBoris Brezillon static LIST_HEAD(grave_pages);
43293db446aSBoris Brezillon
43393db446aSBoris Brezillon static unsigned long *erase_block_wear = NULL;
43493db446aSBoris Brezillon static unsigned int wear_eb_count = 0;
43593db446aSBoris Brezillon static unsigned long total_wear = 0;
43693db446aSBoris Brezillon
43793db446aSBoris Brezillon /* MTD structure for NAND controller */
43893db446aSBoris Brezillon static struct mtd_info *nsmtd;
43993db446aSBoris Brezillon
ns_show(struct seq_file * m,void * private)44088f9f3e8SMiquel Raynal static int ns_show(struct seq_file *m, void *private)
44193db446aSBoris Brezillon {
44293db446aSBoris Brezillon unsigned long wmin = -1, wmax = 0, avg;
44393db446aSBoris Brezillon unsigned long deciles[10], decile_max[10], tot = 0;
44493db446aSBoris Brezillon unsigned int i;
44593db446aSBoris Brezillon
44693db446aSBoris Brezillon /* Calc wear stats */
44793db446aSBoris Brezillon for (i = 0; i < wear_eb_count; ++i) {
44893db446aSBoris Brezillon unsigned long wear = erase_block_wear[i];
44993db446aSBoris Brezillon if (wear < wmin)
45093db446aSBoris Brezillon wmin = wear;
45193db446aSBoris Brezillon if (wear > wmax)
45293db446aSBoris Brezillon wmax = wear;
45393db446aSBoris Brezillon tot += wear;
45493db446aSBoris Brezillon }
45593db446aSBoris Brezillon
45693db446aSBoris Brezillon for (i = 0; i < 9; ++i) {
45793db446aSBoris Brezillon deciles[i] = 0;
45893db446aSBoris Brezillon decile_max[i] = (wmax * (i + 1) + 5) / 10;
45993db446aSBoris Brezillon }
46093db446aSBoris Brezillon deciles[9] = 0;
46193db446aSBoris Brezillon decile_max[9] = wmax;
46293db446aSBoris Brezillon for (i = 0; i < wear_eb_count; ++i) {
46393db446aSBoris Brezillon int d;
46493db446aSBoris Brezillon unsigned long wear = erase_block_wear[i];
46593db446aSBoris Brezillon for (d = 0; d < 10; ++d)
46693db446aSBoris Brezillon if (wear <= decile_max[d]) {
46793db446aSBoris Brezillon deciles[d] += 1;
46893db446aSBoris Brezillon break;
46993db446aSBoris Brezillon }
47093db446aSBoris Brezillon }
47193db446aSBoris Brezillon avg = tot / wear_eb_count;
47293db446aSBoris Brezillon
47393db446aSBoris Brezillon /* Output wear report */
47493db446aSBoris Brezillon seq_printf(m, "Total numbers of erases: %lu\n", tot);
47593db446aSBoris Brezillon seq_printf(m, "Number of erase blocks: %u\n", wear_eb_count);
47693db446aSBoris Brezillon seq_printf(m, "Average number of erases: %lu\n", avg);
47793db446aSBoris Brezillon seq_printf(m, "Maximum number of erases: %lu\n", wmax);
47893db446aSBoris Brezillon seq_printf(m, "Minimum number of erases: %lu\n", wmin);
47993db446aSBoris Brezillon for (i = 0; i < 10; ++i) {
48093db446aSBoris Brezillon unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
48193db446aSBoris Brezillon if (from > decile_max[i])
48293db446aSBoris Brezillon continue;
48393db446aSBoris Brezillon seq_printf(m, "Number of ebs with erase counts from %lu to %lu : %lu\n",
48493db446aSBoris Brezillon from,
48593db446aSBoris Brezillon decile_max[i],
48693db446aSBoris Brezillon deciles[i]);
48793db446aSBoris Brezillon }
48893db446aSBoris Brezillon
48993db446aSBoris Brezillon return 0;
49093db446aSBoris Brezillon }
49188f9f3e8SMiquel Raynal DEFINE_SHOW_ATTRIBUTE(ns);
49293db446aSBoris Brezillon
49393db446aSBoris Brezillon /**
49488f9f3e8SMiquel Raynal * ns_debugfs_create - initialize debugfs
4956be834c6SMiquel Raynal * @ns: nandsim device description object
49693db446aSBoris Brezillon *
49793db446aSBoris Brezillon * This function creates all debugfs files for UBI device @ubi. Returns zero in
49893db446aSBoris Brezillon * case of success and a negative error code in case of failure.
49993db446aSBoris Brezillon */
ns_debugfs_create(struct nandsim * ns)50088f9f3e8SMiquel Raynal static int ns_debugfs_create(struct nandsim *ns)
50193db446aSBoris Brezillon {
50293db446aSBoris Brezillon struct dentry *root = nsmtd->dbg.dfs_dir;
50393db446aSBoris Brezillon
50493db446aSBoris Brezillon /*
50593db446aSBoris Brezillon * Just skip debugfs initialization when the debugfs directory is
50693db446aSBoris Brezillon * missing.
50793db446aSBoris Brezillon */
50893db446aSBoris Brezillon if (IS_ERR_OR_NULL(root)) {
50993db446aSBoris Brezillon if (IS_ENABLED(CONFIG_DEBUG_FS) &&
51093db446aSBoris Brezillon !IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER))
51193db446aSBoris Brezillon NS_WARN("CONFIG_MTD_PARTITIONED_MASTER must be enabled to expose debugfs stuff\n");
51293db446aSBoris Brezillon return 0;
51393db446aSBoris Brezillon }
51493db446aSBoris Brezillon
5157f2a1736SMiquel Raynal ns->dent = debugfs_create_file("nandsim_wear_report", 0400, root, ns,
51688f9f3e8SMiquel Raynal &ns_fops);
5177f2a1736SMiquel Raynal if (IS_ERR_OR_NULL(ns->dent)) {
51893db446aSBoris Brezillon NS_ERR("cannot create \"nandsim_wear_report\" debugfs entry\n");
51993db446aSBoris Brezillon return -1;
52093db446aSBoris Brezillon }
52193db446aSBoris Brezillon
52293db446aSBoris Brezillon return 0;
52393db446aSBoris Brezillon }
52493db446aSBoris Brezillon
ns_debugfs_remove(struct nandsim * ns)525cde495f8SMiquel Raynal static void ns_debugfs_remove(struct nandsim *ns)
526cde495f8SMiquel Raynal {
527cde495f8SMiquel Raynal debugfs_remove_recursive(ns->dent);
528cde495f8SMiquel Raynal }
529cde495f8SMiquel Raynal
53093db446aSBoris Brezillon /*
53193db446aSBoris Brezillon * Allocate array of page pointers, create slab allocation for an array
53293db446aSBoris Brezillon * and initialize the array by NULL pointers.
53393db446aSBoris Brezillon *
53493db446aSBoris Brezillon * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
53593db446aSBoris Brezillon */
ns_alloc_device(struct nandsim * ns)53688f9f3e8SMiquel Raynal static int __init ns_alloc_device(struct nandsim *ns)
53793db446aSBoris Brezillon {
53893db446aSBoris Brezillon struct file *cfile;
53993db446aSBoris Brezillon int i, err;
54093db446aSBoris Brezillon
54193db446aSBoris Brezillon if (cache_file) {
54293db446aSBoris Brezillon cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
54393db446aSBoris Brezillon if (IS_ERR(cfile))
54493db446aSBoris Brezillon return PTR_ERR(cfile);
54593db446aSBoris Brezillon if (!(cfile->f_mode & FMODE_CAN_READ)) {
54693db446aSBoris Brezillon NS_ERR("alloc_device: cache file not readable\n");
54793db446aSBoris Brezillon err = -EINVAL;
548058018ebSMiquel Raynal goto err_close_filp;
54993db446aSBoris Brezillon }
55093db446aSBoris Brezillon if (!(cfile->f_mode & FMODE_CAN_WRITE)) {
55193db446aSBoris Brezillon NS_ERR("alloc_device: cache file not writeable\n");
55293db446aSBoris Brezillon err = -EINVAL;
553058018ebSMiquel Raynal goto err_close_filp;
55493db446aSBoris Brezillon }
555fad953ceSKees Cook ns->pages_written =
556fad953ceSKees Cook vzalloc(array_size(sizeof(unsigned long),
557fad953ceSKees Cook BITS_TO_LONGS(ns->geom.pgnum)));
55893db446aSBoris Brezillon if (!ns->pages_written) {
55993db446aSBoris Brezillon NS_ERR("alloc_device: unable to allocate pages written array\n");
56093db446aSBoris Brezillon err = -ENOMEM;
561058018ebSMiquel Raynal goto err_close_filp;
56293db446aSBoris Brezillon }
56393db446aSBoris Brezillon ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
56493db446aSBoris Brezillon if (!ns->file_buf) {
56593db446aSBoris Brezillon NS_ERR("alloc_device: unable to allocate file buf\n");
56693db446aSBoris Brezillon err = -ENOMEM;
567058018ebSMiquel Raynal goto err_free_pw;
56893db446aSBoris Brezillon }
56993db446aSBoris Brezillon ns->cfile = cfile;
570058018ebSMiquel Raynal
57193db446aSBoris Brezillon return 0;
572058018ebSMiquel Raynal
573058018ebSMiquel Raynal err_free_pw:
574058018ebSMiquel Raynal vfree(ns->pages_written);
575058018ebSMiquel Raynal err_close_filp:
576058018ebSMiquel Raynal filp_close(cfile, NULL);
577058018ebSMiquel Raynal
578058018ebSMiquel Raynal return err;
57993db446aSBoris Brezillon }
58093db446aSBoris Brezillon
58142bc47b3SKees Cook ns->pages = vmalloc(array_size(sizeof(union ns_mem), ns->geom.pgnum));
58293db446aSBoris Brezillon if (!ns->pages) {
58393db446aSBoris Brezillon NS_ERR("alloc_device: unable to allocate page array\n");
58493db446aSBoris Brezillon return -ENOMEM;
58593db446aSBoris Brezillon }
58693db446aSBoris Brezillon for (i = 0; i < ns->geom.pgnum; i++) {
58793db446aSBoris Brezillon ns->pages[i].byte = NULL;
58893db446aSBoris Brezillon }
58993db446aSBoris Brezillon ns->nand_pages_slab = kmem_cache_create("nandsim",
59093db446aSBoris Brezillon ns->geom.pgszoob, 0, 0, NULL);
59193db446aSBoris Brezillon if (!ns->nand_pages_slab) {
59293db446aSBoris Brezillon NS_ERR("cache_create: unable to create kmem_cache\n");
593058018ebSMiquel Raynal err = -ENOMEM;
594058018ebSMiquel Raynal goto err_free_pg;
59593db446aSBoris Brezillon }
59693db446aSBoris Brezillon
59793db446aSBoris Brezillon return 0;
59893db446aSBoris Brezillon
599058018ebSMiquel Raynal err_free_pg:
600058018ebSMiquel Raynal vfree(ns->pages);
601058018ebSMiquel Raynal
60293db446aSBoris Brezillon return err;
60393db446aSBoris Brezillon }
60493db446aSBoris Brezillon
60593db446aSBoris Brezillon /*
60693db446aSBoris Brezillon * Free any allocated pages, and free the array of page pointers.
60793db446aSBoris Brezillon */
ns_free_device(struct nandsim * ns)60888f9f3e8SMiquel Raynal static void ns_free_device(struct nandsim *ns)
60993db446aSBoris Brezillon {
61093db446aSBoris Brezillon int i;
61193db446aSBoris Brezillon
61293db446aSBoris Brezillon if (ns->cfile) {
61393db446aSBoris Brezillon kfree(ns->file_buf);
61493db446aSBoris Brezillon vfree(ns->pages_written);
61593db446aSBoris Brezillon filp_close(ns->cfile, NULL);
61693db446aSBoris Brezillon return;
61793db446aSBoris Brezillon }
61893db446aSBoris Brezillon
61993db446aSBoris Brezillon if (ns->pages) {
62093db446aSBoris Brezillon for (i = 0; i < ns->geom.pgnum; i++) {
62193db446aSBoris Brezillon if (ns->pages[i].byte)
62293db446aSBoris Brezillon kmem_cache_free(ns->nand_pages_slab,
62393db446aSBoris Brezillon ns->pages[i].byte);
62493db446aSBoris Brezillon }
62593db446aSBoris Brezillon kmem_cache_destroy(ns->nand_pages_slab);
62693db446aSBoris Brezillon vfree(ns->pages);
62793db446aSBoris Brezillon }
62893db446aSBoris Brezillon }
62993db446aSBoris Brezillon
ns_get_partition_name(int i)63088f9f3e8SMiquel Raynal static char __init *ns_get_partition_name(int i)
63193db446aSBoris Brezillon {
63293db446aSBoris Brezillon return kasprintf(GFP_KERNEL, "NAND simulator partition %d", i);
63393db446aSBoris Brezillon }
63493db446aSBoris Brezillon
63593db446aSBoris Brezillon /*
63693db446aSBoris Brezillon * Initialize the nandsim structure.
63793db446aSBoris Brezillon *
63893db446aSBoris Brezillon * RETURNS: 0 if success, -ERRNO if failure.
63993db446aSBoris Brezillon */
ns_init(struct mtd_info * mtd)64088f9f3e8SMiquel Raynal static int __init ns_init(struct mtd_info *mtd)
64193db446aSBoris Brezillon {
64293db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(mtd);
64393db446aSBoris Brezillon struct nandsim *ns = nand_get_controller_data(chip);
64493db446aSBoris Brezillon int i, ret = 0;
64593db446aSBoris Brezillon uint64_t remains;
64693db446aSBoris Brezillon uint64_t next_offset;
64793db446aSBoris Brezillon
64893db446aSBoris Brezillon if (NS_IS_INITIALIZED(ns)) {
64993db446aSBoris Brezillon NS_ERR("init_nandsim: nandsim is already initialized\n");
65093db446aSBoris Brezillon return -EIO;
65193db446aSBoris Brezillon }
65293db446aSBoris Brezillon
65393db446aSBoris Brezillon /* Initialize the NAND flash parameters */
65493db446aSBoris Brezillon ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
65593db446aSBoris Brezillon ns->geom.totsz = mtd->size;
65693db446aSBoris Brezillon ns->geom.pgsz = mtd->writesize;
65793db446aSBoris Brezillon ns->geom.oobsz = mtd->oobsize;
65893db446aSBoris Brezillon ns->geom.secsz = mtd->erasesize;
65993db446aSBoris Brezillon ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
66093db446aSBoris Brezillon ns->geom.pgnum = div_u64(ns->geom.totsz, ns->geom.pgsz);
66193db446aSBoris Brezillon ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz;
66293db446aSBoris Brezillon ns->geom.secshift = ffs(ns->geom.secsz) - 1;
66393db446aSBoris Brezillon ns->geom.pgshift = chip->page_shift;
66493db446aSBoris Brezillon ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
66593db446aSBoris Brezillon ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
66693db446aSBoris Brezillon ns->options = 0;
66793db446aSBoris Brezillon
66893db446aSBoris Brezillon if (ns->geom.pgsz == 512) {
66993db446aSBoris Brezillon ns->options |= OPT_PAGE512;
67093db446aSBoris Brezillon if (ns->busw == 8)
67193db446aSBoris Brezillon ns->options |= OPT_PAGE512_8BIT;
67293db446aSBoris Brezillon } else if (ns->geom.pgsz == 2048) {
67393db446aSBoris Brezillon ns->options |= OPT_PAGE2048;
67493db446aSBoris Brezillon } else if (ns->geom.pgsz == 4096) {
67593db446aSBoris Brezillon ns->options |= OPT_PAGE4096;
67693db446aSBoris Brezillon } else {
67793db446aSBoris Brezillon NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
67893db446aSBoris Brezillon return -EIO;
67993db446aSBoris Brezillon }
68093db446aSBoris Brezillon
68193db446aSBoris Brezillon if (ns->options & OPT_SMALLPAGE) {
68293db446aSBoris Brezillon if (ns->geom.totsz <= (32 << 20)) {
68393db446aSBoris Brezillon ns->geom.pgaddrbytes = 3;
68493db446aSBoris Brezillon ns->geom.secaddrbytes = 2;
68593db446aSBoris Brezillon } else {
68693db446aSBoris Brezillon ns->geom.pgaddrbytes = 4;
68793db446aSBoris Brezillon ns->geom.secaddrbytes = 3;
68893db446aSBoris Brezillon }
68993db446aSBoris Brezillon } else {
69093db446aSBoris Brezillon if (ns->geom.totsz <= (128 << 20)) {
69193db446aSBoris Brezillon ns->geom.pgaddrbytes = 4;
69293db446aSBoris Brezillon ns->geom.secaddrbytes = 2;
69393db446aSBoris Brezillon } else {
69493db446aSBoris Brezillon ns->geom.pgaddrbytes = 5;
69593db446aSBoris Brezillon ns->geom.secaddrbytes = 3;
69693db446aSBoris Brezillon }
69793db446aSBoris Brezillon }
69893db446aSBoris Brezillon
69993db446aSBoris Brezillon /* Fill the partition_info structure */
70093db446aSBoris Brezillon if (parts_num > ARRAY_SIZE(ns->partitions)) {
70193db446aSBoris Brezillon NS_ERR("too many partitions.\n");
70293db446aSBoris Brezillon return -EINVAL;
70393db446aSBoris Brezillon }
70493db446aSBoris Brezillon remains = ns->geom.totsz;
70593db446aSBoris Brezillon next_offset = 0;
70693db446aSBoris Brezillon for (i = 0; i < parts_num; ++i) {
70793db446aSBoris Brezillon uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
70893db446aSBoris Brezillon
70993db446aSBoris Brezillon if (!part_sz || part_sz > remains) {
71093db446aSBoris Brezillon NS_ERR("bad partition size.\n");
71193db446aSBoris Brezillon return -EINVAL;
71293db446aSBoris Brezillon }
71388f9f3e8SMiquel Raynal ns->partitions[i].name = ns_get_partition_name(i);
71493db446aSBoris Brezillon if (!ns->partitions[i].name) {
71593db446aSBoris Brezillon NS_ERR("unable to allocate memory.\n");
71693db446aSBoris Brezillon return -ENOMEM;
71793db446aSBoris Brezillon }
71893db446aSBoris Brezillon ns->partitions[i].offset = next_offset;
71993db446aSBoris Brezillon ns->partitions[i].size = part_sz;
72093db446aSBoris Brezillon next_offset += ns->partitions[i].size;
72193db446aSBoris Brezillon remains -= ns->partitions[i].size;
72293db446aSBoris Brezillon }
72393db446aSBoris Brezillon ns->nbparts = parts_num;
72493db446aSBoris Brezillon if (remains) {
72593db446aSBoris Brezillon if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
72693db446aSBoris Brezillon NS_ERR("too many partitions.\n");
72752bc51c5SMiquel Raynal ret = -EINVAL;
72852bc51c5SMiquel Raynal goto free_partition_names;
72993db446aSBoris Brezillon }
73088f9f3e8SMiquel Raynal ns->partitions[i].name = ns_get_partition_name(i);
73193db446aSBoris Brezillon if (!ns->partitions[i].name) {
73293db446aSBoris Brezillon NS_ERR("unable to allocate memory.\n");
73352bc51c5SMiquel Raynal ret = -ENOMEM;
73452bc51c5SMiquel Raynal goto free_partition_names;
73593db446aSBoris Brezillon }
73693db446aSBoris Brezillon ns->partitions[i].offset = next_offset;
73793db446aSBoris Brezillon ns->partitions[i].size = remains;
73893db446aSBoris Brezillon ns->nbparts += 1;
73993db446aSBoris Brezillon }
74093db446aSBoris Brezillon
74193db446aSBoris Brezillon if (ns->busw == 16)
74293db446aSBoris Brezillon NS_WARN("16-bit flashes support wasn't tested\n");
74393db446aSBoris Brezillon
74493db446aSBoris Brezillon printk("flash size: %llu MiB\n",
74593db446aSBoris Brezillon (unsigned long long)ns->geom.totsz >> 20);
74693db446aSBoris Brezillon printk("page size: %u bytes\n", ns->geom.pgsz);
74793db446aSBoris Brezillon printk("OOB area size: %u bytes\n", ns->geom.oobsz);
74893db446aSBoris Brezillon printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
74993db446aSBoris Brezillon printk("pages number: %u\n", ns->geom.pgnum);
75093db446aSBoris Brezillon printk("pages per sector: %u\n", ns->geom.pgsec);
75193db446aSBoris Brezillon printk("bus width: %u\n", ns->busw);
75293db446aSBoris Brezillon printk("bits in sector size: %u\n", ns->geom.secshift);
75393db446aSBoris Brezillon printk("bits in page size: %u\n", ns->geom.pgshift);
75493db446aSBoris Brezillon printk("bits in OOB size: %u\n", ffs(ns->geom.oobsz) - 1);
75593db446aSBoris Brezillon printk("flash size with OOB: %llu KiB\n",
75693db446aSBoris Brezillon (unsigned long long)ns->geom.totszoob >> 10);
75793db446aSBoris Brezillon printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
75893db446aSBoris Brezillon printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
75993db446aSBoris Brezillon printk("options: %#x\n", ns->options);
76093db446aSBoris Brezillon
761052a7a53SMiquel Raynal ret = ns_alloc_device(ns);
762052a7a53SMiquel Raynal if (ret)
76352bc51c5SMiquel Raynal goto free_partition_names;
76493db446aSBoris Brezillon
76593db446aSBoris Brezillon /* Allocate / initialize the internal buffer */
76693db446aSBoris Brezillon ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
76793db446aSBoris Brezillon if (!ns->buf.byte) {
76893db446aSBoris Brezillon NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
76993db446aSBoris Brezillon ns->geom.pgszoob);
77052bc51c5SMiquel Raynal ret = -ENOMEM;
771161246ecSMiquel Raynal goto free_device;
77293db446aSBoris Brezillon }
77393db446aSBoris Brezillon memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
77493db446aSBoris Brezillon
77593db446aSBoris Brezillon return 0;
77652bc51c5SMiquel Raynal
777161246ecSMiquel Raynal free_device:
778161246ecSMiquel Raynal ns_free_device(ns);
77952bc51c5SMiquel Raynal free_partition_names:
78052bc51c5SMiquel Raynal for (i = 0; i < ARRAY_SIZE(ns->partitions); ++i)
78152bc51c5SMiquel Raynal kfree(ns->partitions[i].name);
78252bc51c5SMiquel Raynal
78352bc51c5SMiquel Raynal return ret;
78493db446aSBoris Brezillon }
78593db446aSBoris Brezillon
78693db446aSBoris Brezillon /*
78793db446aSBoris Brezillon * Free the nandsim structure.
78893db446aSBoris Brezillon */
ns_free(struct nandsim * ns)78988f9f3e8SMiquel Raynal static void ns_free(struct nandsim *ns)
79093db446aSBoris Brezillon {
79172e840a1SMiquel Raynal int i;
79272e840a1SMiquel Raynal
79372e840a1SMiquel Raynal for (i = 0; i < ARRAY_SIZE(ns->partitions); ++i)
79472e840a1SMiquel Raynal kfree(ns->partitions[i].name);
79572e840a1SMiquel Raynal
79693db446aSBoris Brezillon kfree(ns->buf.byte);
79788f9f3e8SMiquel Raynal ns_free_device(ns);
79893db446aSBoris Brezillon
79993db446aSBoris Brezillon return;
80093db446aSBoris Brezillon }
80193db446aSBoris Brezillon
ns_parse_badblocks(struct nandsim * ns,struct mtd_info * mtd)80288f9f3e8SMiquel Raynal static int ns_parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
80393db446aSBoris Brezillon {
80493db446aSBoris Brezillon char *w;
80593db446aSBoris Brezillon int zero_ok;
80693db446aSBoris Brezillon unsigned int erase_block_no;
80793db446aSBoris Brezillon loff_t offset;
80893db446aSBoris Brezillon
80993db446aSBoris Brezillon if (!badblocks)
81093db446aSBoris Brezillon return 0;
81193db446aSBoris Brezillon w = badblocks;
81293db446aSBoris Brezillon do {
81393db446aSBoris Brezillon zero_ok = (*w == '0' ? 1 : 0);
81493db446aSBoris Brezillon erase_block_no = simple_strtoul(w, &w, 0);
81593db446aSBoris Brezillon if (!zero_ok && !erase_block_no) {
81693db446aSBoris Brezillon NS_ERR("invalid badblocks.\n");
81793db446aSBoris Brezillon return -EINVAL;
81893db446aSBoris Brezillon }
81993db446aSBoris Brezillon offset = (loff_t)erase_block_no * ns->geom.secsz;
82093db446aSBoris Brezillon if (mtd_block_markbad(mtd, offset)) {
82193db446aSBoris Brezillon NS_ERR("invalid badblocks.\n");
82293db446aSBoris Brezillon return -EINVAL;
82393db446aSBoris Brezillon }
82493db446aSBoris Brezillon if (*w == ',')
82593db446aSBoris Brezillon w += 1;
82693db446aSBoris Brezillon } while (*w);
82793db446aSBoris Brezillon return 0;
82893db446aSBoris Brezillon }
82993db446aSBoris Brezillon
ns_parse_weakblocks(void)83088f9f3e8SMiquel Raynal static int ns_parse_weakblocks(void)
83193db446aSBoris Brezillon {
83293db446aSBoris Brezillon char *w;
83393db446aSBoris Brezillon int zero_ok;
83493db446aSBoris Brezillon unsigned int erase_block_no;
83593db446aSBoris Brezillon unsigned int max_erases;
83693db446aSBoris Brezillon struct weak_block *wb;
83793db446aSBoris Brezillon
83893db446aSBoris Brezillon if (!weakblocks)
83993db446aSBoris Brezillon return 0;
84093db446aSBoris Brezillon w = weakblocks;
84193db446aSBoris Brezillon do {
84293db446aSBoris Brezillon zero_ok = (*w == '0' ? 1 : 0);
84393db446aSBoris Brezillon erase_block_no = simple_strtoul(w, &w, 0);
84493db446aSBoris Brezillon if (!zero_ok && !erase_block_no) {
84593db446aSBoris Brezillon NS_ERR("invalid weakblocks.\n");
84693db446aSBoris Brezillon return -EINVAL;
84793db446aSBoris Brezillon }
84893db446aSBoris Brezillon max_erases = 3;
84993db446aSBoris Brezillon if (*w == ':') {
85093db446aSBoris Brezillon w += 1;
85193db446aSBoris Brezillon max_erases = simple_strtoul(w, &w, 0);
85293db446aSBoris Brezillon }
85393db446aSBoris Brezillon if (*w == ',')
85493db446aSBoris Brezillon w += 1;
85593db446aSBoris Brezillon wb = kzalloc(sizeof(*wb), GFP_KERNEL);
85693db446aSBoris Brezillon if (!wb) {
85793db446aSBoris Brezillon NS_ERR("unable to allocate memory.\n");
85893db446aSBoris Brezillon return -ENOMEM;
85993db446aSBoris Brezillon }
86093db446aSBoris Brezillon wb->erase_block_no = erase_block_no;
86193db446aSBoris Brezillon wb->max_erases = max_erases;
86293db446aSBoris Brezillon list_add(&wb->list, &weak_blocks);
86393db446aSBoris Brezillon } while (*w);
86493db446aSBoris Brezillon return 0;
86593db446aSBoris Brezillon }
86693db446aSBoris Brezillon
ns_erase_error(unsigned int erase_block_no)86788f9f3e8SMiquel Raynal static int ns_erase_error(unsigned int erase_block_no)
86893db446aSBoris Brezillon {
86993db446aSBoris Brezillon struct weak_block *wb;
87093db446aSBoris Brezillon
87193db446aSBoris Brezillon list_for_each_entry(wb, &weak_blocks, list)
87293db446aSBoris Brezillon if (wb->erase_block_no == erase_block_no) {
87393db446aSBoris Brezillon if (wb->erases_done >= wb->max_erases)
87493db446aSBoris Brezillon return 1;
87593db446aSBoris Brezillon wb->erases_done += 1;
87693db446aSBoris Brezillon return 0;
87793db446aSBoris Brezillon }
87893db446aSBoris Brezillon return 0;
87993db446aSBoris Brezillon }
88093db446aSBoris Brezillon
ns_parse_weakpages(void)88188f9f3e8SMiquel Raynal static int ns_parse_weakpages(void)
88293db446aSBoris Brezillon {
88393db446aSBoris Brezillon char *w;
88493db446aSBoris Brezillon int zero_ok;
88593db446aSBoris Brezillon unsigned int page_no;
88693db446aSBoris Brezillon unsigned int max_writes;
88793db446aSBoris Brezillon struct weak_page *wp;
88893db446aSBoris Brezillon
88993db446aSBoris Brezillon if (!weakpages)
89093db446aSBoris Brezillon return 0;
89193db446aSBoris Brezillon w = weakpages;
89293db446aSBoris Brezillon do {
89393db446aSBoris Brezillon zero_ok = (*w == '0' ? 1 : 0);
89493db446aSBoris Brezillon page_no = simple_strtoul(w, &w, 0);
89593db446aSBoris Brezillon if (!zero_ok && !page_no) {
89693db446aSBoris Brezillon NS_ERR("invalid weakpages.\n");
89793db446aSBoris Brezillon return -EINVAL;
89893db446aSBoris Brezillon }
89993db446aSBoris Brezillon max_writes = 3;
90093db446aSBoris Brezillon if (*w == ':') {
90193db446aSBoris Brezillon w += 1;
90293db446aSBoris Brezillon max_writes = simple_strtoul(w, &w, 0);
90393db446aSBoris Brezillon }
90493db446aSBoris Brezillon if (*w == ',')
90593db446aSBoris Brezillon w += 1;
90693db446aSBoris Brezillon wp = kzalloc(sizeof(*wp), GFP_KERNEL);
90793db446aSBoris Brezillon if (!wp) {
90893db446aSBoris Brezillon NS_ERR("unable to allocate memory.\n");
90993db446aSBoris Brezillon return -ENOMEM;
91093db446aSBoris Brezillon }
91193db446aSBoris Brezillon wp->page_no = page_no;
91293db446aSBoris Brezillon wp->max_writes = max_writes;
91393db446aSBoris Brezillon list_add(&wp->list, &weak_pages);
91493db446aSBoris Brezillon } while (*w);
91593db446aSBoris Brezillon return 0;
91693db446aSBoris Brezillon }
91793db446aSBoris Brezillon
ns_write_error(unsigned int page_no)91888f9f3e8SMiquel Raynal static int ns_write_error(unsigned int page_no)
91993db446aSBoris Brezillon {
92093db446aSBoris Brezillon struct weak_page *wp;
92193db446aSBoris Brezillon
92293db446aSBoris Brezillon list_for_each_entry(wp, &weak_pages, list)
92393db446aSBoris Brezillon if (wp->page_no == page_no) {
92493db446aSBoris Brezillon if (wp->writes_done >= wp->max_writes)
92593db446aSBoris Brezillon return 1;
92693db446aSBoris Brezillon wp->writes_done += 1;
92793db446aSBoris Brezillon return 0;
92893db446aSBoris Brezillon }
92993db446aSBoris Brezillon return 0;
93093db446aSBoris Brezillon }
93193db446aSBoris Brezillon
ns_parse_gravepages(void)93288f9f3e8SMiquel Raynal static int ns_parse_gravepages(void)
93393db446aSBoris Brezillon {
93493db446aSBoris Brezillon char *g;
93593db446aSBoris Brezillon int zero_ok;
93693db446aSBoris Brezillon unsigned int page_no;
93793db446aSBoris Brezillon unsigned int max_reads;
93893db446aSBoris Brezillon struct grave_page *gp;
93993db446aSBoris Brezillon
94093db446aSBoris Brezillon if (!gravepages)
94193db446aSBoris Brezillon return 0;
94293db446aSBoris Brezillon g = gravepages;
94393db446aSBoris Brezillon do {
94493db446aSBoris Brezillon zero_ok = (*g == '0' ? 1 : 0);
94593db446aSBoris Brezillon page_no = simple_strtoul(g, &g, 0);
94693db446aSBoris Brezillon if (!zero_ok && !page_no) {
94793db446aSBoris Brezillon NS_ERR("invalid gravepagess.\n");
94893db446aSBoris Brezillon return -EINVAL;
94993db446aSBoris Brezillon }
95093db446aSBoris Brezillon max_reads = 3;
95193db446aSBoris Brezillon if (*g == ':') {
95293db446aSBoris Brezillon g += 1;
95393db446aSBoris Brezillon max_reads = simple_strtoul(g, &g, 0);
95493db446aSBoris Brezillon }
95593db446aSBoris Brezillon if (*g == ',')
95693db446aSBoris Brezillon g += 1;
95793db446aSBoris Brezillon gp = kzalloc(sizeof(*gp), GFP_KERNEL);
95893db446aSBoris Brezillon if (!gp) {
95993db446aSBoris Brezillon NS_ERR("unable to allocate memory.\n");
96093db446aSBoris Brezillon return -ENOMEM;
96193db446aSBoris Brezillon }
96293db446aSBoris Brezillon gp->page_no = page_no;
96393db446aSBoris Brezillon gp->max_reads = max_reads;
96493db446aSBoris Brezillon list_add(&gp->list, &grave_pages);
96593db446aSBoris Brezillon } while (*g);
96693db446aSBoris Brezillon return 0;
96793db446aSBoris Brezillon }
96893db446aSBoris Brezillon
ns_read_error(unsigned int page_no)96988f9f3e8SMiquel Raynal static int ns_read_error(unsigned int page_no)
97093db446aSBoris Brezillon {
97193db446aSBoris Brezillon struct grave_page *gp;
97293db446aSBoris Brezillon
97393db446aSBoris Brezillon list_for_each_entry(gp, &grave_pages, list)
97493db446aSBoris Brezillon if (gp->page_no == page_no) {
97593db446aSBoris Brezillon if (gp->reads_done >= gp->max_reads)
97693db446aSBoris Brezillon return 1;
97793db446aSBoris Brezillon gp->reads_done += 1;
97893db446aSBoris Brezillon return 0;
97993db446aSBoris Brezillon }
98093db446aSBoris Brezillon return 0;
98193db446aSBoris Brezillon }
98293db446aSBoris Brezillon
ns_setup_wear_reporting(struct mtd_info * mtd)98388f9f3e8SMiquel Raynal static int ns_setup_wear_reporting(struct mtd_info *mtd)
98493db446aSBoris Brezillon {
98593db446aSBoris Brezillon wear_eb_count = div_u64(mtd->size, mtd->erasesize);
986e0a9ddd5SRinHizakura erase_block_wear = kcalloc(wear_eb_count, sizeof(unsigned long), GFP_KERNEL);
98793db446aSBoris Brezillon if (!erase_block_wear) {
98893db446aSBoris Brezillon NS_ERR("Too many erase blocks for wear reporting\n");
98993db446aSBoris Brezillon return -ENOMEM;
99093db446aSBoris Brezillon }
99193db446aSBoris Brezillon return 0;
99293db446aSBoris Brezillon }
99393db446aSBoris Brezillon
ns_update_wear(unsigned int erase_block_no)99488f9f3e8SMiquel Raynal static void ns_update_wear(unsigned int erase_block_no)
99593db446aSBoris Brezillon {
99693db446aSBoris Brezillon if (!erase_block_wear)
99793db446aSBoris Brezillon return;
99893db446aSBoris Brezillon total_wear += 1;
99993db446aSBoris Brezillon /*
100093db446aSBoris Brezillon * TODO: Notify this through a debugfs entry,
100193db446aSBoris Brezillon * instead of showing an error message.
100293db446aSBoris Brezillon */
100393db446aSBoris Brezillon if (total_wear == 0)
100493db446aSBoris Brezillon NS_ERR("Erase counter total overflow\n");
100593db446aSBoris Brezillon erase_block_wear[erase_block_no] += 1;
100693db446aSBoris Brezillon if (erase_block_wear[erase_block_no] == 0)
100793db446aSBoris Brezillon NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
100893db446aSBoris Brezillon }
100993db446aSBoris Brezillon
101093db446aSBoris Brezillon /*
101193db446aSBoris Brezillon * Returns the string representation of 'state' state.
101293db446aSBoris Brezillon */
ns_get_state_name(uint32_t state)101388f9f3e8SMiquel Raynal static char *ns_get_state_name(uint32_t state)
101493db446aSBoris Brezillon {
101593db446aSBoris Brezillon switch (NS_STATE(state)) {
101693db446aSBoris Brezillon case STATE_CMD_READ0:
101793db446aSBoris Brezillon return "STATE_CMD_READ0";
101893db446aSBoris Brezillon case STATE_CMD_READ1:
101993db446aSBoris Brezillon return "STATE_CMD_READ1";
102093db446aSBoris Brezillon case STATE_CMD_PAGEPROG:
102193db446aSBoris Brezillon return "STATE_CMD_PAGEPROG";
102293db446aSBoris Brezillon case STATE_CMD_READOOB:
102393db446aSBoris Brezillon return "STATE_CMD_READOOB";
102493db446aSBoris Brezillon case STATE_CMD_READSTART:
102593db446aSBoris Brezillon return "STATE_CMD_READSTART";
102693db446aSBoris Brezillon case STATE_CMD_ERASE1:
102793db446aSBoris Brezillon return "STATE_CMD_ERASE1";
102893db446aSBoris Brezillon case STATE_CMD_STATUS:
102993db446aSBoris Brezillon return "STATE_CMD_STATUS";
103093db446aSBoris Brezillon case STATE_CMD_SEQIN:
103193db446aSBoris Brezillon return "STATE_CMD_SEQIN";
103293db446aSBoris Brezillon case STATE_CMD_READID:
103393db446aSBoris Brezillon return "STATE_CMD_READID";
103493db446aSBoris Brezillon case STATE_CMD_ERASE2:
103593db446aSBoris Brezillon return "STATE_CMD_ERASE2";
103693db446aSBoris Brezillon case STATE_CMD_RESET:
103793db446aSBoris Brezillon return "STATE_CMD_RESET";
103893db446aSBoris Brezillon case STATE_CMD_RNDOUT:
103993db446aSBoris Brezillon return "STATE_CMD_RNDOUT";
104093db446aSBoris Brezillon case STATE_CMD_RNDOUTSTART:
104193db446aSBoris Brezillon return "STATE_CMD_RNDOUTSTART";
104293db446aSBoris Brezillon case STATE_ADDR_PAGE:
104393db446aSBoris Brezillon return "STATE_ADDR_PAGE";
104493db446aSBoris Brezillon case STATE_ADDR_SEC:
104593db446aSBoris Brezillon return "STATE_ADDR_SEC";
104693db446aSBoris Brezillon case STATE_ADDR_ZERO:
104793db446aSBoris Brezillon return "STATE_ADDR_ZERO";
104893db446aSBoris Brezillon case STATE_ADDR_COLUMN:
104993db446aSBoris Brezillon return "STATE_ADDR_COLUMN";
105093db446aSBoris Brezillon case STATE_DATAIN:
105193db446aSBoris Brezillon return "STATE_DATAIN";
105293db446aSBoris Brezillon case STATE_DATAOUT:
105393db446aSBoris Brezillon return "STATE_DATAOUT";
105493db446aSBoris Brezillon case STATE_DATAOUT_ID:
105593db446aSBoris Brezillon return "STATE_DATAOUT_ID";
105693db446aSBoris Brezillon case STATE_DATAOUT_STATUS:
105793db446aSBoris Brezillon return "STATE_DATAOUT_STATUS";
105893db446aSBoris Brezillon case STATE_READY:
105993db446aSBoris Brezillon return "STATE_READY";
106093db446aSBoris Brezillon case STATE_UNKNOWN:
106193db446aSBoris Brezillon return "STATE_UNKNOWN";
106293db446aSBoris Brezillon }
106393db446aSBoris Brezillon
106493db446aSBoris Brezillon NS_ERR("get_state_name: unknown state, BUG\n");
106593db446aSBoris Brezillon return NULL;
106693db446aSBoris Brezillon }
106793db446aSBoris Brezillon
106893db446aSBoris Brezillon /*
106993db446aSBoris Brezillon * Check if command is valid.
107093db446aSBoris Brezillon *
107193db446aSBoris Brezillon * RETURNS: 1 if wrong command, 0 if right.
107293db446aSBoris Brezillon */
ns_check_command(int cmd)107388f9f3e8SMiquel Raynal static int ns_check_command(int cmd)
107493db446aSBoris Brezillon {
107593db446aSBoris Brezillon switch (cmd) {
107693db446aSBoris Brezillon
107793db446aSBoris Brezillon case NAND_CMD_READ0:
107893db446aSBoris Brezillon case NAND_CMD_READ1:
107993db446aSBoris Brezillon case NAND_CMD_READSTART:
108093db446aSBoris Brezillon case NAND_CMD_PAGEPROG:
108193db446aSBoris Brezillon case NAND_CMD_READOOB:
108293db446aSBoris Brezillon case NAND_CMD_ERASE1:
108393db446aSBoris Brezillon case NAND_CMD_STATUS:
108493db446aSBoris Brezillon case NAND_CMD_SEQIN:
108593db446aSBoris Brezillon case NAND_CMD_READID:
108693db446aSBoris Brezillon case NAND_CMD_ERASE2:
108793db446aSBoris Brezillon case NAND_CMD_RESET:
108893db446aSBoris Brezillon case NAND_CMD_RNDOUT:
108993db446aSBoris Brezillon case NAND_CMD_RNDOUTSTART:
109093db446aSBoris Brezillon return 0;
109193db446aSBoris Brezillon
109293db446aSBoris Brezillon default:
109393db446aSBoris Brezillon return 1;
109493db446aSBoris Brezillon }
109593db446aSBoris Brezillon }
109693db446aSBoris Brezillon
109793db446aSBoris Brezillon /*
109893db446aSBoris Brezillon * Returns state after command is accepted by command number.
109993db446aSBoris Brezillon */
ns_get_state_by_command(unsigned command)110088f9f3e8SMiquel Raynal static uint32_t ns_get_state_by_command(unsigned command)
110193db446aSBoris Brezillon {
110293db446aSBoris Brezillon switch (command) {
110393db446aSBoris Brezillon case NAND_CMD_READ0:
110493db446aSBoris Brezillon return STATE_CMD_READ0;
110593db446aSBoris Brezillon case NAND_CMD_READ1:
110693db446aSBoris Brezillon return STATE_CMD_READ1;
110793db446aSBoris Brezillon case NAND_CMD_PAGEPROG:
110893db446aSBoris Brezillon return STATE_CMD_PAGEPROG;
110993db446aSBoris Brezillon case NAND_CMD_READSTART:
111093db446aSBoris Brezillon return STATE_CMD_READSTART;
111193db446aSBoris Brezillon case NAND_CMD_READOOB:
111293db446aSBoris Brezillon return STATE_CMD_READOOB;
111393db446aSBoris Brezillon case NAND_CMD_ERASE1:
111493db446aSBoris Brezillon return STATE_CMD_ERASE1;
111593db446aSBoris Brezillon case NAND_CMD_STATUS:
111693db446aSBoris Brezillon return STATE_CMD_STATUS;
111793db446aSBoris Brezillon case NAND_CMD_SEQIN:
111893db446aSBoris Brezillon return STATE_CMD_SEQIN;
111993db446aSBoris Brezillon case NAND_CMD_READID:
112093db446aSBoris Brezillon return STATE_CMD_READID;
112193db446aSBoris Brezillon case NAND_CMD_ERASE2:
112293db446aSBoris Brezillon return STATE_CMD_ERASE2;
112393db446aSBoris Brezillon case NAND_CMD_RESET:
112493db446aSBoris Brezillon return STATE_CMD_RESET;
112593db446aSBoris Brezillon case NAND_CMD_RNDOUT:
112693db446aSBoris Brezillon return STATE_CMD_RNDOUT;
112793db446aSBoris Brezillon case NAND_CMD_RNDOUTSTART:
112893db446aSBoris Brezillon return STATE_CMD_RNDOUTSTART;
112993db446aSBoris Brezillon }
113093db446aSBoris Brezillon
113193db446aSBoris Brezillon NS_ERR("get_state_by_command: unknown command, BUG\n");
113293db446aSBoris Brezillon return 0;
113393db446aSBoris Brezillon }
113493db446aSBoris Brezillon
113593db446aSBoris Brezillon /*
113693db446aSBoris Brezillon * Move an address byte to the correspondent internal register.
113793db446aSBoris Brezillon */
ns_accept_addr_byte(struct nandsim * ns,u_char bt)113888f9f3e8SMiquel Raynal static inline void ns_accept_addr_byte(struct nandsim *ns, u_char bt)
113993db446aSBoris Brezillon {
114093db446aSBoris Brezillon uint byte = (uint)bt;
114193db446aSBoris Brezillon
114293db446aSBoris Brezillon if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
114393db446aSBoris Brezillon ns->regs.column |= (byte << 8 * ns->regs.count);
114493db446aSBoris Brezillon else {
114593db446aSBoris Brezillon ns->regs.row |= (byte << 8 * (ns->regs.count -
114693db446aSBoris Brezillon ns->geom.pgaddrbytes +
114793db446aSBoris Brezillon ns->geom.secaddrbytes));
114893db446aSBoris Brezillon }
114993db446aSBoris Brezillon
115093db446aSBoris Brezillon return;
115193db446aSBoris Brezillon }
115293db446aSBoris Brezillon
115393db446aSBoris Brezillon /*
115493db446aSBoris Brezillon * Switch to STATE_READY state.
115593db446aSBoris Brezillon */
ns_switch_to_ready_state(struct nandsim * ns,u_char status)115688f9f3e8SMiquel Raynal static inline void ns_switch_to_ready_state(struct nandsim *ns, u_char status)
115793db446aSBoris Brezillon {
115888f9f3e8SMiquel Raynal NS_DBG("switch_to_ready_state: switch to %s state\n",
115988f9f3e8SMiquel Raynal ns_get_state_name(STATE_READY));
116093db446aSBoris Brezillon
116193db446aSBoris Brezillon ns->state = STATE_READY;
116293db446aSBoris Brezillon ns->nxstate = STATE_UNKNOWN;
116393db446aSBoris Brezillon ns->op = NULL;
116493db446aSBoris Brezillon ns->npstates = 0;
116593db446aSBoris Brezillon ns->stateidx = 0;
116693db446aSBoris Brezillon ns->regs.num = 0;
116793db446aSBoris Brezillon ns->regs.count = 0;
116893db446aSBoris Brezillon ns->regs.off = 0;
116993db446aSBoris Brezillon ns->regs.row = 0;
117093db446aSBoris Brezillon ns->regs.column = 0;
117193db446aSBoris Brezillon ns->regs.status = status;
117293db446aSBoris Brezillon }
117393db446aSBoris Brezillon
117493db446aSBoris Brezillon /*
117593db446aSBoris Brezillon * If the operation isn't known yet, try to find it in the global array
117693db446aSBoris Brezillon * of supported operations.
117793db446aSBoris Brezillon *
117893db446aSBoris Brezillon * Operation can be unknown because of the following.
117993db446aSBoris Brezillon * 1. New command was accepted and this is the first call to find the
118093db446aSBoris Brezillon * correspondent states chain. In this case ns->npstates = 0;
118193db446aSBoris Brezillon * 2. There are several operations which begin with the same command(s)
118293db446aSBoris Brezillon * (for example program from the second half and read from the
118393db446aSBoris Brezillon * second half operations both begin with the READ1 command). In this
118493db446aSBoris Brezillon * case the ns->pstates[] array contains previous states.
118593db446aSBoris Brezillon *
118693db446aSBoris Brezillon * Thus, the function tries to find operation containing the following
118793db446aSBoris Brezillon * states (if the 'flag' parameter is 0):
118893db446aSBoris Brezillon * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
118993db446aSBoris Brezillon *
119093db446aSBoris Brezillon * If (one and only one) matching operation is found, it is accepted (
119193db446aSBoris Brezillon * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
119293db446aSBoris Brezillon * zeroed).
119393db446aSBoris Brezillon *
119493db446aSBoris Brezillon * If there are several matches, the current state is pushed to the
119593db446aSBoris Brezillon * ns->pstates.
119693db446aSBoris Brezillon *
119793db446aSBoris Brezillon * The operation can be unknown only while commands are input to the chip.
119893db446aSBoris Brezillon * As soon as address command is accepted, the operation must be known.
119993db446aSBoris Brezillon * In such situation the function is called with 'flag' != 0, and the
120093db446aSBoris Brezillon * operation is searched using the following pattern:
120193db446aSBoris Brezillon * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
120293db446aSBoris Brezillon *
120393db446aSBoris Brezillon * It is supposed that this pattern must either match one operation or
120493db446aSBoris Brezillon * none. There can't be ambiguity in that case.
120593db446aSBoris Brezillon *
120693db446aSBoris Brezillon * If no matches found, the function does the following:
120793db446aSBoris Brezillon * 1. if there are saved states present, try to ignore them and search
120893db446aSBoris Brezillon * again only using the last command. If nothing was found, switch
120993db446aSBoris Brezillon * to the STATE_READY state.
121093db446aSBoris Brezillon * 2. if there are no saved states, switch to the STATE_READY state.
121193db446aSBoris Brezillon *
121293db446aSBoris Brezillon * RETURNS: -2 - no matched operations found.
121393db446aSBoris Brezillon * -1 - several matches.
121493db446aSBoris Brezillon * 0 - operation is found.
121593db446aSBoris Brezillon */
ns_find_operation(struct nandsim * ns,uint32_t flag)121688f9f3e8SMiquel Raynal static int ns_find_operation(struct nandsim *ns, uint32_t flag)
121793db446aSBoris Brezillon {
121893db446aSBoris Brezillon int opsfound = 0;
121993db446aSBoris Brezillon int i, j, idx = 0;
122093db446aSBoris Brezillon
122193db446aSBoris Brezillon for (i = 0; i < NS_OPER_NUM; i++) {
122293db446aSBoris Brezillon
122393db446aSBoris Brezillon int found = 1;
122493db446aSBoris Brezillon
122593db446aSBoris Brezillon if (!(ns->options & ops[i].reqopts))
122693db446aSBoris Brezillon /* Ignore operations we can't perform */
122793db446aSBoris Brezillon continue;
122893db446aSBoris Brezillon
122993db446aSBoris Brezillon if (flag) {
123093db446aSBoris Brezillon if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
123193db446aSBoris Brezillon continue;
123293db446aSBoris Brezillon } else {
123393db446aSBoris Brezillon if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
123493db446aSBoris Brezillon continue;
123593db446aSBoris Brezillon }
123693db446aSBoris Brezillon
123793db446aSBoris Brezillon for (j = 0; j < ns->npstates; j++)
123893db446aSBoris Brezillon if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
123993db446aSBoris Brezillon && (ns->options & ops[idx].reqopts)) {
124093db446aSBoris Brezillon found = 0;
124193db446aSBoris Brezillon break;
124293db446aSBoris Brezillon }
124393db446aSBoris Brezillon
124493db446aSBoris Brezillon if (found) {
124593db446aSBoris Brezillon idx = i;
124693db446aSBoris Brezillon opsfound += 1;
124793db446aSBoris Brezillon }
124893db446aSBoris Brezillon }
124993db446aSBoris Brezillon
125093db446aSBoris Brezillon if (opsfound == 1) {
125193db446aSBoris Brezillon /* Exact match */
125293db446aSBoris Brezillon ns->op = &ops[idx].states[0];
125393db446aSBoris Brezillon if (flag) {
125493db446aSBoris Brezillon /*
125593db446aSBoris Brezillon * In this case the find_operation function was
125693db446aSBoris Brezillon * called when address has just began input. But it isn't
125793db446aSBoris Brezillon * yet fully input and the current state must
125893db446aSBoris Brezillon * not be one of STATE_ADDR_*, but the STATE_ADDR_*
125993db446aSBoris Brezillon * state must be the next state (ns->nxstate).
126093db446aSBoris Brezillon */
126193db446aSBoris Brezillon ns->stateidx = ns->npstates - 1;
126293db446aSBoris Brezillon } else {
126393db446aSBoris Brezillon ns->stateidx = ns->npstates;
126493db446aSBoris Brezillon }
126593db446aSBoris Brezillon ns->npstates = 0;
126693db446aSBoris Brezillon ns->state = ns->op[ns->stateidx];
126793db446aSBoris Brezillon ns->nxstate = ns->op[ns->stateidx + 1];
126893db446aSBoris Brezillon NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
126988f9f3e8SMiquel Raynal idx, ns_get_state_name(ns->state),
127088f9f3e8SMiquel Raynal ns_get_state_name(ns->nxstate));
127193db446aSBoris Brezillon return 0;
127293db446aSBoris Brezillon }
127393db446aSBoris Brezillon
127493db446aSBoris Brezillon if (opsfound == 0) {
127593db446aSBoris Brezillon /* Nothing was found. Try to ignore previous commands (if any) and search again */
127693db446aSBoris Brezillon if (ns->npstates != 0) {
127793db446aSBoris Brezillon NS_DBG("find_operation: no operation found, try again with state %s\n",
127888f9f3e8SMiquel Raynal ns_get_state_name(ns->state));
127993db446aSBoris Brezillon ns->npstates = 0;
128088f9f3e8SMiquel Raynal return ns_find_operation(ns, 0);
128193db446aSBoris Brezillon
128293db446aSBoris Brezillon }
128393db446aSBoris Brezillon NS_DBG("find_operation: no operations found\n");
128488f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
128593db446aSBoris Brezillon return -2;
128693db446aSBoris Brezillon }
128793db446aSBoris Brezillon
128893db446aSBoris Brezillon if (flag) {
128993db446aSBoris Brezillon /* This shouldn't happen */
129093db446aSBoris Brezillon NS_DBG("find_operation: BUG, operation must be known if address is input\n");
129193db446aSBoris Brezillon return -2;
129293db446aSBoris Brezillon }
129393db446aSBoris Brezillon
129493db446aSBoris Brezillon NS_DBG("find_operation: there is still ambiguity\n");
129593db446aSBoris Brezillon
129693db446aSBoris Brezillon ns->pstates[ns->npstates++] = ns->state;
129793db446aSBoris Brezillon
129893db446aSBoris Brezillon return -1;
129993db446aSBoris Brezillon }
130093db446aSBoris Brezillon
ns_put_pages(struct nandsim * ns)130188f9f3e8SMiquel Raynal static void ns_put_pages(struct nandsim *ns)
130293db446aSBoris Brezillon {
130393db446aSBoris Brezillon int i;
130493db446aSBoris Brezillon
130593db446aSBoris Brezillon for (i = 0; i < ns->held_cnt; i++)
130693db446aSBoris Brezillon put_page(ns->held_pages[i]);
130793db446aSBoris Brezillon }
130893db446aSBoris Brezillon
130993db446aSBoris Brezillon /* Get page cache pages in advance to provide NOFS memory allocation */
ns_get_pages(struct nandsim * ns,struct file * file,size_t count,loff_t pos)131088f9f3e8SMiquel Raynal static int ns_get_pages(struct nandsim *ns, struct file *file, size_t count,
131188f9f3e8SMiquel Raynal loff_t pos)
131293db446aSBoris Brezillon {
131393db446aSBoris Brezillon pgoff_t index, start_index, end_index;
131493db446aSBoris Brezillon struct page *page;
131593db446aSBoris Brezillon struct address_space *mapping = file->f_mapping;
131693db446aSBoris Brezillon
131793db446aSBoris Brezillon start_index = pos >> PAGE_SHIFT;
131893db446aSBoris Brezillon end_index = (pos + count - 1) >> PAGE_SHIFT;
131993db446aSBoris Brezillon if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
132093db446aSBoris Brezillon return -EINVAL;
132193db446aSBoris Brezillon ns->held_cnt = 0;
132293db446aSBoris Brezillon for (index = start_index; index <= end_index; index++) {
132393db446aSBoris Brezillon page = find_get_page(mapping, index);
132493db446aSBoris Brezillon if (page == NULL) {
132593db446aSBoris Brezillon page = find_or_create_page(mapping, index, GFP_NOFS);
132693db446aSBoris Brezillon if (page == NULL) {
132793db446aSBoris Brezillon write_inode_now(mapping->host, 1);
132893db446aSBoris Brezillon page = find_or_create_page(mapping, index, GFP_NOFS);
132993db446aSBoris Brezillon }
133093db446aSBoris Brezillon if (page == NULL) {
133188f9f3e8SMiquel Raynal ns_put_pages(ns);
133293db446aSBoris Brezillon return -ENOMEM;
133393db446aSBoris Brezillon }
133493db446aSBoris Brezillon unlock_page(page);
133593db446aSBoris Brezillon }
133693db446aSBoris Brezillon ns->held_pages[ns->held_cnt++] = page;
133793db446aSBoris Brezillon }
133893db446aSBoris Brezillon return 0;
133993db446aSBoris Brezillon }
134093db446aSBoris Brezillon
ns_read_file(struct nandsim * ns,struct file * file,void * buf,size_t count,loff_t pos)134188f9f3e8SMiquel Raynal static ssize_t ns_read_file(struct nandsim *ns, struct file *file, void *buf,
134288f9f3e8SMiquel Raynal size_t count, loff_t pos)
134393db446aSBoris Brezillon {
134493db446aSBoris Brezillon ssize_t tx;
134593db446aSBoris Brezillon int err;
134693db446aSBoris Brezillon unsigned int noreclaim_flag;
134793db446aSBoris Brezillon
134888f9f3e8SMiquel Raynal err = ns_get_pages(ns, file, count, pos);
134993db446aSBoris Brezillon if (err)
135093db446aSBoris Brezillon return err;
135193db446aSBoris Brezillon noreclaim_flag = memalloc_noreclaim_save();
135293db446aSBoris Brezillon tx = kernel_read(file, buf, count, &pos);
135393db446aSBoris Brezillon memalloc_noreclaim_restore(noreclaim_flag);
135488f9f3e8SMiquel Raynal ns_put_pages(ns);
135593db446aSBoris Brezillon return tx;
135693db446aSBoris Brezillon }
135793db446aSBoris Brezillon
ns_write_file(struct nandsim * ns,struct file * file,void * buf,size_t count,loff_t pos)135888f9f3e8SMiquel Raynal static ssize_t ns_write_file(struct nandsim *ns, struct file *file, void *buf,
135988f9f3e8SMiquel Raynal size_t count, loff_t pos)
136093db446aSBoris Brezillon {
136193db446aSBoris Brezillon ssize_t tx;
136293db446aSBoris Brezillon int err;
136393db446aSBoris Brezillon unsigned int noreclaim_flag;
136493db446aSBoris Brezillon
136588f9f3e8SMiquel Raynal err = ns_get_pages(ns, file, count, pos);
136693db446aSBoris Brezillon if (err)
136793db446aSBoris Brezillon return err;
136893db446aSBoris Brezillon noreclaim_flag = memalloc_noreclaim_save();
136993db446aSBoris Brezillon tx = kernel_write(file, buf, count, &pos);
137093db446aSBoris Brezillon memalloc_noreclaim_restore(noreclaim_flag);
137188f9f3e8SMiquel Raynal ns_put_pages(ns);
137293db446aSBoris Brezillon return tx;
137393db446aSBoris Brezillon }
137493db446aSBoris Brezillon
137593db446aSBoris Brezillon /*
137693db446aSBoris Brezillon * Returns a pointer to the current page.
137793db446aSBoris Brezillon */
NS_GET_PAGE(struct nandsim * ns)137893db446aSBoris Brezillon static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
137993db446aSBoris Brezillon {
138093db446aSBoris Brezillon return &(ns->pages[ns->regs.row]);
138193db446aSBoris Brezillon }
138293db446aSBoris Brezillon
138393db446aSBoris Brezillon /*
1384*dab2214fSWangYuli * Returns a pointer to the current byte, within the current page.
138593db446aSBoris Brezillon */
NS_PAGE_BYTE_OFF(struct nandsim * ns)138693db446aSBoris Brezillon static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
138793db446aSBoris Brezillon {
1388109cf81fSRinHizakura return NS_GET_PAGE(ns)->byte + NS_PAGE_BYTE_SHIFT(ns);
138993db446aSBoris Brezillon }
139093db446aSBoris Brezillon
ns_do_read_error(struct nandsim * ns,int num)139188f9f3e8SMiquel Raynal static int ns_do_read_error(struct nandsim *ns, int num)
139293db446aSBoris Brezillon {
139393db446aSBoris Brezillon unsigned int page_no = ns->regs.row;
139493db446aSBoris Brezillon
139588f9f3e8SMiquel Raynal if (ns_read_error(page_no)) {
1396197173dbSJason A. Donenfeld get_random_bytes(ns->buf.byte, num);
139793db446aSBoris Brezillon NS_WARN("simulating read error in page %u\n", page_no);
139893db446aSBoris Brezillon return 1;
139993db446aSBoris Brezillon }
140093db446aSBoris Brezillon return 0;
140193db446aSBoris Brezillon }
140293db446aSBoris Brezillon
ns_do_bit_flips(struct nandsim * ns,int num)140388f9f3e8SMiquel Raynal static void ns_do_bit_flips(struct nandsim *ns, int num)
140493db446aSBoris Brezillon {
1405f743f16cSJason A. Donenfeld if (bitflips && get_random_u16() < (1 << 6)) {
140693db446aSBoris Brezillon int flips = 1;
140793db446aSBoris Brezillon if (bitflips > 1)
1408e8a533cbSJason A. Donenfeld flips = get_random_u32_inclusive(1, bitflips);
140993db446aSBoris Brezillon while (flips--) {
14108032bf12SJason A. Donenfeld int pos = get_random_u32_below(num * 8);
141193db446aSBoris Brezillon ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
141293db446aSBoris Brezillon NS_WARN("read_page: flipping bit %d in page %d "
141393db446aSBoris Brezillon "reading from %d ecc: corrected=%u failed=%u\n",
1414109cf81fSRinHizakura pos, ns->regs.row, NS_PAGE_BYTE_SHIFT(ns),
141593db446aSBoris Brezillon nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
141693db446aSBoris Brezillon }
141793db446aSBoris Brezillon }
141893db446aSBoris Brezillon }
141993db446aSBoris Brezillon
142093db446aSBoris Brezillon /*
142193db446aSBoris Brezillon * Fill the NAND buffer with data read from the specified page.
142293db446aSBoris Brezillon */
ns_read_page(struct nandsim * ns,int num)142388f9f3e8SMiquel Raynal static void ns_read_page(struct nandsim *ns, int num)
142493db446aSBoris Brezillon {
142593db446aSBoris Brezillon union ns_mem *mypage;
142693db446aSBoris Brezillon
142793db446aSBoris Brezillon if (ns->cfile) {
142893db446aSBoris Brezillon if (!test_bit(ns->regs.row, ns->pages_written)) {
142993db446aSBoris Brezillon NS_DBG("read_page: page %d not written\n", ns->regs.row);
143093db446aSBoris Brezillon memset(ns->buf.byte, 0xFF, num);
143193db446aSBoris Brezillon } else {
143293db446aSBoris Brezillon loff_t pos;
143393db446aSBoris Brezillon ssize_t tx;
143493db446aSBoris Brezillon
143593db446aSBoris Brezillon NS_DBG("read_page: page %d written, reading from %d\n",
1436109cf81fSRinHizakura ns->regs.row, NS_PAGE_BYTE_SHIFT(ns));
143788f9f3e8SMiquel Raynal if (ns_do_read_error(ns, num))
143893db446aSBoris Brezillon return;
143993db446aSBoris Brezillon pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
144088f9f3e8SMiquel Raynal tx = ns_read_file(ns, ns->cfile, ns->buf.byte, num,
144188f9f3e8SMiquel Raynal pos);
144293db446aSBoris Brezillon if (tx != num) {
144393db446aSBoris Brezillon NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
144493db446aSBoris Brezillon return;
144593db446aSBoris Brezillon }
144688f9f3e8SMiquel Raynal ns_do_bit_flips(ns, num);
144793db446aSBoris Brezillon }
144893db446aSBoris Brezillon return;
144993db446aSBoris Brezillon }
145093db446aSBoris Brezillon
145193db446aSBoris Brezillon mypage = NS_GET_PAGE(ns);
145293db446aSBoris Brezillon if (mypage->byte == NULL) {
145393db446aSBoris Brezillon NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
145493db446aSBoris Brezillon memset(ns->buf.byte, 0xFF, num);
145593db446aSBoris Brezillon } else {
145693db446aSBoris Brezillon NS_DBG("read_page: page %d allocated, reading from %d\n",
1457109cf81fSRinHizakura ns->regs.row, NS_PAGE_BYTE_SHIFT(ns));
145888f9f3e8SMiquel Raynal if (ns_do_read_error(ns, num))
145993db446aSBoris Brezillon return;
146093db446aSBoris Brezillon memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
146188f9f3e8SMiquel Raynal ns_do_bit_flips(ns, num);
146293db446aSBoris Brezillon }
146393db446aSBoris Brezillon }
146493db446aSBoris Brezillon
146593db446aSBoris Brezillon /*
146693db446aSBoris Brezillon * Erase all pages in the specified sector.
146793db446aSBoris Brezillon */
ns_erase_sector(struct nandsim * ns)146888f9f3e8SMiquel Raynal static void ns_erase_sector(struct nandsim *ns)
146993db446aSBoris Brezillon {
147093db446aSBoris Brezillon union ns_mem *mypage;
147193db446aSBoris Brezillon int i;
147293db446aSBoris Brezillon
147393db446aSBoris Brezillon if (ns->cfile) {
147493db446aSBoris Brezillon for (i = 0; i < ns->geom.pgsec; i++)
147593db446aSBoris Brezillon if (__test_and_clear_bit(ns->regs.row + i,
147693db446aSBoris Brezillon ns->pages_written)) {
147793db446aSBoris Brezillon NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
147893db446aSBoris Brezillon }
147993db446aSBoris Brezillon return;
148093db446aSBoris Brezillon }
148193db446aSBoris Brezillon
148293db446aSBoris Brezillon mypage = NS_GET_PAGE(ns);
148393db446aSBoris Brezillon for (i = 0; i < ns->geom.pgsec; i++) {
148493db446aSBoris Brezillon if (mypage->byte != NULL) {
148593db446aSBoris Brezillon NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
148693db446aSBoris Brezillon kmem_cache_free(ns->nand_pages_slab, mypage->byte);
148793db446aSBoris Brezillon mypage->byte = NULL;
148893db446aSBoris Brezillon }
148993db446aSBoris Brezillon mypage++;
149093db446aSBoris Brezillon }
149193db446aSBoris Brezillon }
149293db446aSBoris Brezillon
149393db446aSBoris Brezillon /*
149493db446aSBoris Brezillon * Program the specified page with the contents from the NAND buffer.
149593db446aSBoris Brezillon */
ns_prog_page(struct nandsim * ns,int num)149688f9f3e8SMiquel Raynal static int ns_prog_page(struct nandsim *ns, int num)
149793db446aSBoris Brezillon {
149893db446aSBoris Brezillon int i;
149993db446aSBoris Brezillon union ns_mem *mypage;
150093db446aSBoris Brezillon u_char *pg_off;
150193db446aSBoris Brezillon
150293db446aSBoris Brezillon if (ns->cfile) {
150393db446aSBoris Brezillon loff_t off;
150493db446aSBoris Brezillon ssize_t tx;
150593db446aSBoris Brezillon int all;
150693db446aSBoris Brezillon
150793db446aSBoris Brezillon NS_DBG("prog_page: writing page %d\n", ns->regs.row);
1508109cf81fSRinHizakura pg_off = ns->file_buf + NS_PAGE_BYTE_SHIFT(ns);
150993db446aSBoris Brezillon off = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
151093db446aSBoris Brezillon if (!test_bit(ns->regs.row, ns->pages_written)) {
151193db446aSBoris Brezillon all = 1;
151293db446aSBoris Brezillon memset(ns->file_buf, 0xff, ns->geom.pgszoob);
151393db446aSBoris Brezillon } else {
151493db446aSBoris Brezillon all = 0;
151588f9f3e8SMiquel Raynal tx = ns_read_file(ns, ns->cfile, pg_off, num, off);
151693db446aSBoris Brezillon if (tx != num) {
151793db446aSBoris Brezillon NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
151893db446aSBoris Brezillon return -1;
151993db446aSBoris Brezillon }
152093db446aSBoris Brezillon }
152193db446aSBoris Brezillon for (i = 0; i < num; i++)
152293db446aSBoris Brezillon pg_off[i] &= ns->buf.byte[i];
152393db446aSBoris Brezillon if (all) {
152493db446aSBoris Brezillon loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
152588f9f3e8SMiquel Raynal tx = ns_write_file(ns, ns->cfile, ns->file_buf,
152688f9f3e8SMiquel Raynal ns->geom.pgszoob, pos);
152793db446aSBoris Brezillon if (tx != ns->geom.pgszoob) {
152893db446aSBoris Brezillon NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
152993db446aSBoris Brezillon return -1;
153093db446aSBoris Brezillon }
153193db446aSBoris Brezillon __set_bit(ns->regs.row, ns->pages_written);
153293db446aSBoris Brezillon } else {
153388f9f3e8SMiquel Raynal tx = ns_write_file(ns, ns->cfile, pg_off, num, off);
153493db446aSBoris Brezillon if (tx != num) {
153593db446aSBoris Brezillon NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
153693db446aSBoris Brezillon return -1;
153793db446aSBoris Brezillon }
153893db446aSBoris Brezillon }
153993db446aSBoris Brezillon return 0;
154093db446aSBoris Brezillon }
154193db446aSBoris Brezillon
154293db446aSBoris Brezillon mypage = NS_GET_PAGE(ns);
154393db446aSBoris Brezillon if (mypage->byte == NULL) {
154493db446aSBoris Brezillon NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
154593db446aSBoris Brezillon /*
154693db446aSBoris Brezillon * We allocate memory with GFP_NOFS because a flash FS may
154793db446aSBoris Brezillon * utilize this. If it is holding an FS lock, then gets here,
154893db446aSBoris Brezillon * then kernel memory alloc runs writeback which goes to the FS
154993db446aSBoris Brezillon * again and deadlocks. This was seen in practice.
155093db446aSBoris Brezillon */
155193db446aSBoris Brezillon mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
155293db446aSBoris Brezillon if (mypage->byte == NULL) {
155393db446aSBoris Brezillon NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
155493db446aSBoris Brezillon return -1;
155593db446aSBoris Brezillon }
155693db446aSBoris Brezillon memset(mypage->byte, 0xFF, ns->geom.pgszoob);
155793db446aSBoris Brezillon }
155893db446aSBoris Brezillon
155993db446aSBoris Brezillon pg_off = NS_PAGE_BYTE_OFF(ns);
156093db446aSBoris Brezillon for (i = 0; i < num; i++)
156193db446aSBoris Brezillon pg_off[i] &= ns->buf.byte[i];
156293db446aSBoris Brezillon
156393db446aSBoris Brezillon return 0;
156493db446aSBoris Brezillon }
156593db446aSBoris Brezillon
156693db446aSBoris Brezillon /*
156793db446aSBoris Brezillon * If state has any action bit, perform this action.
156893db446aSBoris Brezillon *
156993db446aSBoris Brezillon * RETURNS: 0 if success, -1 if error.
157093db446aSBoris Brezillon */
ns_do_state_action(struct nandsim * ns,uint32_t action)157188f9f3e8SMiquel Raynal static int ns_do_state_action(struct nandsim *ns, uint32_t action)
157293db446aSBoris Brezillon {
157393db446aSBoris Brezillon int num;
157493db446aSBoris Brezillon int busdiv = ns->busw == 8 ? 1 : 2;
157593db446aSBoris Brezillon unsigned int erase_block_no, page_no;
157693db446aSBoris Brezillon
157793db446aSBoris Brezillon action &= ACTION_MASK;
157893db446aSBoris Brezillon
157993db446aSBoris Brezillon /* Check that page address input is correct */
158093db446aSBoris Brezillon if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
158193db446aSBoris Brezillon NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
158293db446aSBoris Brezillon return -1;
158393db446aSBoris Brezillon }
158493db446aSBoris Brezillon
158593db446aSBoris Brezillon switch (action) {
158693db446aSBoris Brezillon
158793db446aSBoris Brezillon case ACTION_CPY:
158893db446aSBoris Brezillon /*
158993db446aSBoris Brezillon * Copy page data to the internal buffer.
159093db446aSBoris Brezillon */
159193db446aSBoris Brezillon
159293db446aSBoris Brezillon /* Column shouldn't be very large */
159393db446aSBoris Brezillon if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
159493db446aSBoris Brezillon NS_ERR("do_state_action: column number is too large\n");
159593db446aSBoris Brezillon break;
159693db446aSBoris Brezillon }
1597109cf81fSRinHizakura num = ns->geom.pgszoob - NS_PAGE_BYTE_SHIFT(ns);
159888f9f3e8SMiquel Raynal ns_read_page(ns, num);
159993db446aSBoris Brezillon
160093db446aSBoris Brezillon NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
160193db446aSBoris Brezillon num, NS_RAW_OFFSET(ns) + ns->regs.off);
160293db446aSBoris Brezillon
160393db446aSBoris Brezillon if (ns->regs.off == 0)
160493db446aSBoris Brezillon NS_LOG("read page %d\n", ns->regs.row);
160593db446aSBoris Brezillon else if (ns->regs.off < ns->geom.pgsz)
160693db446aSBoris Brezillon NS_LOG("read page %d (second half)\n", ns->regs.row);
160793db446aSBoris Brezillon else
160893db446aSBoris Brezillon NS_LOG("read OOB of page %d\n", ns->regs.row);
160993db446aSBoris Brezillon
161093db446aSBoris Brezillon NS_UDELAY(access_delay);
161193db446aSBoris Brezillon NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
161293db446aSBoris Brezillon
161393db446aSBoris Brezillon break;
161493db446aSBoris Brezillon
161593db446aSBoris Brezillon case ACTION_SECERASE:
161693db446aSBoris Brezillon /*
161793db446aSBoris Brezillon * Erase sector.
161893db446aSBoris Brezillon */
161993db446aSBoris Brezillon
162093db446aSBoris Brezillon if (ns->lines.wp) {
162193db446aSBoris Brezillon NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
162293db446aSBoris Brezillon return -1;
162393db446aSBoris Brezillon }
162493db446aSBoris Brezillon
162593db446aSBoris Brezillon if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
162693db446aSBoris Brezillon || (ns->regs.row & ~(ns->geom.secsz - 1))) {
162793db446aSBoris Brezillon NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
162893db446aSBoris Brezillon return -1;
162993db446aSBoris Brezillon }
163093db446aSBoris Brezillon
163193db446aSBoris Brezillon ns->regs.row = (ns->regs.row <<
163293db446aSBoris Brezillon 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
163393db446aSBoris Brezillon ns->regs.column = 0;
163493db446aSBoris Brezillon
163593db446aSBoris Brezillon erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
163693db446aSBoris Brezillon
163793db446aSBoris Brezillon NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
163893db446aSBoris Brezillon ns->regs.row, NS_RAW_OFFSET(ns));
163993db446aSBoris Brezillon NS_LOG("erase sector %u\n", erase_block_no);
164093db446aSBoris Brezillon
164188f9f3e8SMiquel Raynal ns_erase_sector(ns);
164293db446aSBoris Brezillon
164393db446aSBoris Brezillon NS_MDELAY(erase_delay);
164493db446aSBoris Brezillon
164593db446aSBoris Brezillon if (erase_block_wear)
164688f9f3e8SMiquel Raynal ns_update_wear(erase_block_no);
164793db446aSBoris Brezillon
164888f9f3e8SMiquel Raynal if (ns_erase_error(erase_block_no)) {
164993db446aSBoris Brezillon NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
165093db446aSBoris Brezillon return -1;
165193db446aSBoris Brezillon }
165293db446aSBoris Brezillon
165393db446aSBoris Brezillon break;
165493db446aSBoris Brezillon
165593db446aSBoris Brezillon case ACTION_PRGPAGE:
165693db446aSBoris Brezillon /*
165793db446aSBoris Brezillon * Program page - move internal buffer data to the page.
165893db446aSBoris Brezillon */
165993db446aSBoris Brezillon
166093db446aSBoris Brezillon if (ns->lines.wp) {
166193db446aSBoris Brezillon NS_WARN("do_state_action: device is write-protected, programm\n");
166293db446aSBoris Brezillon return -1;
166393db446aSBoris Brezillon }
166493db446aSBoris Brezillon
1665109cf81fSRinHizakura num = ns->geom.pgszoob - NS_PAGE_BYTE_SHIFT(ns);
166693db446aSBoris Brezillon if (num != ns->regs.count) {
166793db446aSBoris Brezillon NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
166893db446aSBoris Brezillon ns->regs.count, num);
166993db446aSBoris Brezillon return -1;
167093db446aSBoris Brezillon }
167193db446aSBoris Brezillon
167288f9f3e8SMiquel Raynal if (ns_prog_page(ns, num) == -1)
167393db446aSBoris Brezillon return -1;
167493db446aSBoris Brezillon
167593db446aSBoris Brezillon page_no = ns->regs.row;
167693db446aSBoris Brezillon
167793db446aSBoris Brezillon NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
167893db446aSBoris Brezillon num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
167993db446aSBoris Brezillon NS_LOG("programm page %d\n", ns->regs.row);
168093db446aSBoris Brezillon
168193db446aSBoris Brezillon NS_UDELAY(programm_delay);
168293db446aSBoris Brezillon NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
168393db446aSBoris Brezillon
168488f9f3e8SMiquel Raynal if (ns_write_error(page_no)) {
168593db446aSBoris Brezillon NS_WARN("simulating write failure in page %u\n", page_no);
168693db446aSBoris Brezillon return -1;
168793db446aSBoris Brezillon }
168893db446aSBoris Brezillon
168993db446aSBoris Brezillon break;
169093db446aSBoris Brezillon
169193db446aSBoris Brezillon case ACTION_ZEROOFF:
169293db446aSBoris Brezillon NS_DBG("do_state_action: set internal offset to 0\n");
169393db446aSBoris Brezillon ns->regs.off = 0;
169493db446aSBoris Brezillon break;
169593db446aSBoris Brezillon
169693db446aSBoris Brezillon case ACTION_HALFOFF:
169793db446aSBoris Brezillon if (!(ns->options & OPT_PAGE512_8BIT)) {
169893db446aSBoris Brezillon NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
169993db446aSBoris Brezillon "byte page size 8x chips\n");
170093db446aSBoris Brezillon return -1;
170193db446aSBoris Brezillon }
170293db446aSBoris Brezillon NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
170393db446aSBoris Brezillon ns->regs.off = ns->geom.pgsz/2;
170493db446aSBoris Brezillon break;
170593db446aSBoris Brezillon
170693db446aSBoris Brezillon case ACTION_OOBOFF:
170793db446aSBoris Brezillon NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
170893db446aSBoris Brezillon ns->regs.off = ns->geom.pgsz;
170993db446aSBoris Brezillon break;
171093db446aSBoris Brezillon
171193db446aSBoris Brezillon default:
171293db446aSBoris Brezillon NS_DBG("do_state_action: BUG! unknown action\n");
171393db446aSBoris Brezillon }
171493db446aSBoris Brezillon
171593db446aSBoris Brezillon return 0;
171693db446aSBoris Brezillon }
171793db446aSBoris Brezillon
171893db446aSBoris Brezillon /*
171993db446aSBoris Brezillon * Switch simulator's state.
172093db446aSBoris Brezillon */
ns_switch_state(struct nandsim * ns)172188f9f3e8SMiquel Raynal static void ns_switch_state(struct nandsim *ns)
172293db446aSBoris Brezillon {
172393db446aSBoris Brezillon if (ns->op) {
172493db446aSBoris Brezillon /*
172593db446aSBoris Brezillon * The current operation have already been identified.
172693db446aSBoris Brezillon * Just follow the states chain.
172793db446aSBoris Brezillon */
172893db446aSBoris Brezillon
172993db446aSBoris Brezillon ns->stateidx += 1;
173093db446aSBoris Brezillon ns->state = ns->nxstate;
173193db446aSBoris Brezillon ns->nxstate = ns->op[ns->stateidx + 1];
173293db446aSBoris Brezillon
173393db446aSBoris Brezillon NS_DBG("switch_state: operation is known, switch to the next state, "
173493db446aSBoris Brezillon "state: %s, nxstate: %s\n",
173588f9f3e8SMiquel Raynal ns_get_state_name(ns->state),
173688f9f3e8SMiquel Raynal ns_get_state_name(ns->nxstate));
173793db446aSBoris Brezillon } else {
173893db446aSBoris Brezillon /*
173993db446aSBoris Brezillon * We don't yet know which operation we perform.
174093db446aSBoris Brezillon * Try to identify it.
174193db446aSBoris Brezillon */
174293db446aSBoris Brezillon
174393db446aSBoris Brezillon /*
174493db446aSBoris Brezillon * The only event causing the switch_state function to
174593db446aSBoris Brezillon * be called with yet unknown operation is new command.
174693db446aSBoris Brezillon */
174788f9f3e8SMiquel Raynal ns->state = ns_get_state_by_command(ns->regs.command);
174893db446aSBoris Brezillon
174993db446aSBoris Brezillon NS_DBG("switch_state: operation is unknown, try to find it\n");
175093db446aSBoris Brezillon
17511f6ce444SRichard Weinberger if (ns_find_operation(ns, 0))
175293db446aSBoris Brezillon return;
1753db52b445SRinHizakura }
175493db446aSBoris Brezillon
1755db52b445SRinHizakura /* See, whether we need to do some action */
175688f9f3e8SMiquel Raynal if ((ns->state & ACTION_MASK) &&
175788f9f3e8SMiquel Raynal ns_do_state_action(ns, ns->state) < 0) {
175888f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
175993db446aSBoris Brezillon return;
176093db446aSBoris Brezillon }
176193db446aSBoris Brezillon
176293db446aSBoris Brezillon /* For 16x devices column means the page offset in words */
176393db446aSBoris Brezillon if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
176493db446aSBoris Brezillon NS_DBG("switch_state: double the column number for 16x device\n");
176593db446aSBoris Brezillon ns->regs.column <<= 1;
176693db446aSBoris Brezillon }
176793db446aSBoris Brezillon
176893db446aSBoris Brezillon if (NS_STATE(ns->nxstate) == STATE_READY) {
176993db446aSBoris Brezillon /*
177093db446aSBoris Brezillon * The current state is the last. Return to STATE_READY
177193db446aSBoris Brezillon */
177293db446aSBoris Brezillon
177393db446aSBoris Brezillon u_char status = NS_STATUS_OK(ns);
177493db446aSBoris Brezillon
177593db446aSBoris Brezillon /* In case of data states, see if all bytes were input/output */
177693db446aSBoris Brezillon if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
177793db446aSBoris Brezillon && ns->regs.count != ns->regs.num) {
177893db446aSBoris Brezillon NS_WARN("switch_state: not all bytes were processed, %d left\n",
177993db446aSBoris Brezillon ns->regs.num - ns->regs.count);
178093db446aSBoris Brezillon status = NS_STATUS_FAILED(ns);
178193db446aSBoris Brezillon }
178293db446aSBoris Brezillon
178393db446aSBoris Brezillon NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
178493db446aSBoris Brezillon
178588f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, status);
178693db446aSBoris Brezillon
178793db446aSBoris Brezillon return;
178893db446aSBoris Brezillon } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
178993db446aSBoris Brezillon /*
179093db446aSBoris Brezillon * If the next state is data input/output, switch to it now
179193db446aSBoris Brezillon */
179293db446aSBoris Brezillon
179393db446aSBoris Brezillon ns->state = ns->nxstate;
179493db446aSBoris Brezillon ns->nxstate = ns->op[++ns->stateidx + 1];
179593db446aSBoris Brezillon ns->regs.num = ns->regs.count = 0;
179693db446aSBoris Brezillon
179793db446aSBoris Brezillon NS_DBG("switch_state: the next state is data I/O, switch, "
179893db446aSBoris Brezillon "state: %s, nxstate: %s\n",
179988f9f3e8SMiquel Raynal ns_get_state_name(ns->state),
180088f9f3e8SMiquel Raynal ns_get_state_name(ns->nxstate));
180193db446aSBoris Brezillon
180293db446aSBoris Brezillon /*
180393db446aSBoris Brezillon * Set the internal register to the count of bytes which
180493db446aSBoris Brezillon * are expected to be input or output
180593db446aSBoris Brezillon */
180693db446aSBoris Brezillon switch (NS_STATE(ns->state)) {
180793db446aSBoris Brezillon case STATE_DATAIN:
180893db446aSBoris Brezillon case STATE_DATAOUT:
1809109cf81fSRinHizakura ns->regs.num = ns->geom.pgszoob - NS_PAGE_BYTE_SHIFT(ns);
181093db446aSBoris Brezillon break;
181193db446aSBoris Brezillon
181293db446aSBoris Brezillon case STATE_DATAOUT_ID:
181393db446aSBoris Brezillon ns->regs.num = ns->geom.idbytes;
181493db446aSBoris Brezillon break;
181593db446aSBoris Brezillon
181693db446aSBoris Brezillon case STATE_DATAOUT_STATUS:
181793db446aSBoris Brezillon ns->regs.count = ns->regs.num = 0;
181893db446aSBoris Brezillon break;
181993db446aSBoris Brezillon
182093db446aSBoris Brezillon default:
182193db446aSBoris Brezillon NS_ERR("switch_state: BUG! unknown data state\n");
182293db446aSBoris Brezillon }
182393db446aSBoris Brezillon
182493db446aSBoris Brezillon } else if (ns->nxstate & STATE_ADDR_MASK) {
182593db446aSBoris Brezillon /*
182693db446aSBoris Brezillon * If the next state is address input, set the internal
182793db446aSBoris Brezillon * register to the number of expected address bytes
182893db446aSBoris Brezillon */
182993db446aSBoris Brezillon
183093db446aSBoris Brezillon ns->regs.count = 0;
183193db446aSBoris Brezillon
183293db446aSBoris Brezillon switch (NS_STATE(ns->nxstate)) {
183393db446aSBoris Brezillon case STATE_ADDR_PAGE:
183493db446aSBoris Brezillon ns->regs.num = ns->geom.pgaddrbytes;
183593db446aSBoris Brezillon
183693db446aSBoris Brezillon break;
183793db446aSBoris Brezillon case STATE_ADDR_SEC:
183893db446aSBoris Brezillon ns->regs.num = ns->geom.secaddrbytes;
183993db446aSBoris Brezillon break;
184093db446aSBoris Brezillon
184193db446aSBoris Brezillon case STATE_ADDR_ZERO:
184293db446aSBoris Brezillon ns->regs.num = 1;
184393db446aSBoris Brezillon break;
184493db446aSBoris Brezillon
184593db446aSBoris Brezillon case STATE_ADDR_COLUMN:
184693db446aSBoris Brezillon /* Column address is always 2 bytes */
184793db446aSBoris Brezillon ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
184893db446aSBoris Brezillon break;
184993db446aSBoris Brezillon
185093db446aSBoris Brezillon default:
185193db446aSBoris Brezillon NS_ERR("switch_state: BUG! unknown address state\n");
185293db446aSBoris Brezillon }
185393db446aSBoris Brezillon } else {
185493db446aSBoris Brezillon /*
185593db446aSBoris Brezillon * Just reset internal counters.
185693db446aSBoris Brezillon */
185793db446aSBoris Brezillon
185893db446aSBoris Brezillon ns->regs.num = 0;
185993db446aSBoris Brezillon ns->regs.count = 0;
186093db446aSBoris Brezillon }
186193db446aSBoris Brezillon }
186293db446aSBoris Brezillon
ns_nand_read_byte(struct nand_chip * chip)18637e534323SBoris Brezillon static u_char ns_nand_read_byte(struct nand_chip *chip)
186493db446aSBoris Brezillon {
186593db446aSBoris Brezillon struct nandsim *ns = nand_get_controller_data(chip);
186693db446aSBoris Brezillon u_char outb = 0x00;
186793db446aSBoris Brezillon
186893db446aSBoris Brezillon /* Sanity and correctness checks */
186993db446aSBoris Brezillon if (!ns->lines.ce) {
187093db446aSBoris Brezillon NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
187193db446aSBoris Brezillon return outb;
187293db446aSBoris Brezillon }
187393db446aSBoris Brezillon if (ns->lines.ale || ns->lines.cle) {
187493db446aSBoris Brezillon NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
187593db446aSBoris Brezillon return outb;
187693db446aSBoris Brezillon }
187793db446aSBoris Brezillon if (!(ns->state & STATE_DATAOUT_MASK)) {
187888f9f3e8SMiquel Raynal NS_WARN("read_byte: unexpected data output cycle, state is %s return %#x\n",
187988f9f3e8SMiquel Raynal ns_get_state_name(ns->state), (uint)outb);
188093db446aSBoris Brezillon return outb;
188193db446aSBoris Brezillon }
188293db446aSBoris Brezillon
188393db446aSBoris Brezillon /* Status register may be read as many times as it is wanted */
188493db446aSBoris Brezillon if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
188593db446aSBoris Brezillon NS_DBG("read_byte: return %#x status\n", ns->regs.status);
188693db446aSBoris Brezillon return ns->regs.status;
188793db446aSBoris Brezillon }
188893db446aSBoris Brezillon
188993db446aSBoris Brezillon /* Check if there is any data in the internal buffer which may be read */
189093db446aSBoris Brezillon if (ns->regs.count == ns->regs.num) {
189193db446aSBoris Brezillon NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
189293db446aSBoris Brezillon return outb;
189393db446aSBoris Brezillon }
189493db446aSBoris Brezillon
189593db446aSBoris Brezillon switch (NS_STATE(ns->state)) {
189693db446aSBoris Brezillon case STATE_DATAOUT:
189793db446aSBoris Brezillon if (ns->busw == 8) {
189893db446aSBoris Brezillon outb = ns->buf.byte[ns->regs.count];
189993db446aSBoris Brezillon ns->regs.count += 1;
190093db446aSBoris Brezillon } else {
190193db446aSBoris Brezillon outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
190293db446aSBoris Brezillon ns->regs.count += 2;
190393db446aSBoris Brezillon }
190493db446aSBoris Brezillon break;
190593db446aSBoris Brezillon case STATE_DATAOUT_ID:
190693db446aSBoris Brezillon NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
190793db446aSBoris Brezillon outb = ns->ids[ns->regs.count];
190893db446aSBoris Brezillon ns->regs.count += 1;
190993db446aSBoris Brezillon break;
191093db446aSBoris Brezillon default:
191193db446aSBoris Brezillon BUG();
191293db446aSBoris Brezillon }
191393db446aSBoris Brezillon
191493db446aSBoris Brezillon if (ns->regs.count == ns->regs.num) {
191593db446aSBoris Brezillon NS_DBG("read_byte: all bytes were read\n");
191693db446aSBoris Brezillon
191793db446aSBoris Brezillon if (NS_STATE(ns->nxstate) == STATE_READY)
191888f9f3e8SMiquel Raynal ns_switch_state(ns);
191993db446aSBoris Brezillon }
192093db446aSBoris Brezillon
192193db446aSBoris Brezillon return outb;
192293db446aSBoris Brezillon }
192393db446aSBoris Brezillon
ns_nand_write_byte(struct nand_chip * chip,u_char byte)1924c0739d85SBoris Brezillon static void ns_nand_write_byte(struct nand_chip *chip, u_char byte)
192593db446aSBoris Brezillon {
192693db446aSBoris Brezillon struct nandsim *ns = nand_get_controller_data(chip);
192793db446aSBoris Brezillon
192893db446aSBoris Brezillon /* Sanity and correctness checks */
192993db446aSBoris Brezillon if (!ns->lines.ce) {
193093db446aSBoris Brezillon NS_ERR("write_byte: chip is disabled, ignore write\n");
193193db446aSBoris Brezillon return;
193293db446aSBoris Brezillon }
193393db446aSBoris Brezillon if (ns->lines.ale && ns->lines.cle) {
193493db446aSBoris Brezillon NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
193593db446aSBoris Brezillon return;
193693db446aSBoris Brezillon }
193793db446aSBoris Brezillon
193893db446aSBoris Brezillon if (ns->lines.cle == 1) {
193993db446aSBoris Brezillon /*
194093db446aSBoris Brezillon * The byte written is a command.
194193db446aSBoris Brezillon */
194293db446aSBoris Brezillon
194393db446aSBoris Brezillon if (byte == NAND_CMD_RESET) {
194493db446aSBoris Brezillon NS_LOG("reset chip\n");
194588f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_OK(ns));
194693db446aSBoris Brezillon return;
194793db446aSBoris Brezillon }
194893db446aSBoris Brezillon
194993db446aSBoris Brezillon /* Check that the command byte is correct */
195088f9f3e8SMiquel Raynal if (ns_check_command(byte)) {
195193db446aSBoris Brezillon NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
195293db446aSBoris Brezillon return;
195393db446aSBoris Brezillon }
195493db446aSBoris Brezillon
195593db446aSBoris Brezillon if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
195693db446aSBoris Brezillon || NS_STATE(ns->state) == STATE_DATAOUT) {
195793db446aSBoris Brezillon int row = ns->regs.row;
195893db446aSBoris Brezillon
195988f9f3e8SMiquel Raynal ns_switch_state(ns);
196093db446aSBoris Brezillon if (byte == NAND_CMD_RNDOUT)
196193db446aSBoris Brezillon ns->regs.row = row;
196293db446aSBoris Brezillon }
196393db446aSBoris Brezillon
196493db446aSBoris Brezillon /* Check if chip is expecting command */
196593db446aSBoris Brezillon if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
196693db446aSBoris Brezillon /* Do not warn if only 2 id bytes are read */
196793db446aSBoris Brezillon if (!(ns->regs.command == NAND_CMD_READID &&
196893db446aSBoris Brezillon NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
196993db446aSBoris Brezillon /*
197093db446aSBoris Brezillon * We are in situation when something else (not command)
197193db446aSBoris Brezillon * was expected but command was input. In this case ignore
197293db446aSBoris Brezillon * previous command(s)/state(s) and accept the last one.
197393db446aSBoris Brezillon */
197488f9f3e8SMiquel Raynal NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, ignore previous states\n",
197588f9f3e8SMiquel Raynal (uint)byte,
197688f9f3e8SMiquel Raynal ns_get_state_name(ns->nxstate));
197793db446aSBoris Brezillon }
197888f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
197993db446aSBoris Brezillon }
198093db446aSBoris Brezillon
198193db446aSBoris Brezillon NS_DBG("command byte corresponding to %s state accepted\n",
198288f9f3e8SMiquel Raynal ns_get_state_name(ns_get_state_by_command(byte)));
198393db446aSBoris Brezillon ns->regs.command = byte;
198488f9f3e8SMiquel Raynal ns_switch_state(ns);
198593db446aSBoris Brezillon
198693db446aSBoris Brezillon } else if (ns->lines.ale == 1) {
198793db446aSBoris Brezillon /*
198893db446aSBoris Brezillon * The byte written is an address.
198993db446aSBoris Brezillon */
199093db446aSBoris Brezillon
199193db446aSBoris Brezillon if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
199293db446aSBoris Brezillon
199393db446aSBoris Brezillon NS_DBG("write_byte: operation isn't known yet, identify it\n");
199493db446aSBoris Brezillon
199588f9f3e8SMiquel Raynal if (ns_find_operation(ns, 1) < 0)
199693db446aSBoris Brezillon return;
199793db446aSBoris Brezillon
199888f9f3e8SMiquel Raynal if ((ns->state & ACTION_MASK) &&
199988f9f3e8SMiquel Raynal ns_do_state_action(ns, ns->state) < 0) {
200088f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns,
200188f9f3e8SMiquel Raynal NS_STATUS_FAILED(ns));
200293db446aSBoris Brezillon return;
200393db446aSBoris Brezillon }
200493db446aSBoris Brezillon
200593db446aSBoris Brezillon ns->regs.count = 0;
200693db446aSBoris Brezillon switch (NS_STATE(ns->nxstate)) {
200793db446aSBoris Brezillon case STATE_ADDR_PAGE:
200893db446aSBoris Brezillon ns->regs.num = ns->geom.pgaddrbytes;
200993db446aSBoris Brezillon break;
201093db446aSBoris Brezillon case STATE_ADDR_SEC:
201193db446aSBoris Brezillon ns->regs.num = ns->geom.secaddrbytes;
201293db446aSBoris Brezillon break;
201393db446aSBoris Brezillon case STATE_ADDR_ZERO:
201493db446aSBoris Brezillon ns->regs.num = 1;
201593db446aSBoris Brezillon break;
201693db446aSBoris Brezillon default:
201793db446aSBoris Brezillon BUG();
201893db446aSBoris Brezillon }
201993db446aSBoris Brezillon }
202093db446aSBoris Brezillon
202193db446aSBoris Brezillon /* Check that chip is expecting address */
202293db446aSBoris Brezillon if (!(ns->nxstate & STATE_ADDR_MASK)) {
202388f9f3e8SMiquel Raynal NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, switch to STATE_READY\n",
202488f9f3e8SMiquel Raynal (uint)byte, ns_get_state_name(ns->nxstate));
202588f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
202693db446aSBoris Brezillon return;
202793db446aSBoris Brezillon }
202893db446aSBoris Brezillon
202993db446aSBoris Brezillon /* Check if this is expected byte */
203093db446aSBoris Brezillon if (ns->regs.count == ns->regs.num) {
203193db446aSBoris Brezillon NS_ERR("write_byte: no more address bytes expected\n");
203288f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
203393db446aSBoris Brezillon return;
203493db446aSBoris Brezillon }
203593db446aSBoris Brezillon
203688f9f3e8SMiquel Raynal ns_accept_addr_byte(ns, byte);
203793db446aSBoris Brezillon
203893db446aSBoris Brezillon ns->regs.count += 1;
203993db446aSBoris Brezillon
204093db446aSBoris Brezillon NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
204193db446aSBoris Brezillon (uint)byte, ns->regs.count, ns->regs.num);
204293db446aSBoris Brezillon
204393db446aSBoris Brezillon if (ns->regs.count == ns->regs.num) {
204493db446aSBoris Brezillon NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
204588f9f3e8SMiquel Raynal ns_switch_state(ns);
204693db446aSBoris Brezillon }
204793db446aSBoris Brezillon
204893db446aSBoris Brezillon } else {
204993db446aSBoris Brezillon /*
205093db446aSBoris Brezillon * The byte written is an input data.
205193db446aSBoris Brezillon */
205293db446aSBoris Brezillon
205393db446aSBoris Brezillon /* Check that chip is expecting data input */
205493db446aSBoris Brezillon if (!(ns->state & STATE_DATAIN_MASK)) {
205588f9f3e8SMiquel Raynal NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, switch to %s\n",
205688f9f3e8SMiquel Raynal (uint)byte, ns_get_state_name(ns->state),
205788f9f3e8SMiquel Raynal ns_get_state_name(STATE_READY));
205888f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
205993db446aSBoris Brezillon return;
206093db446aSBoris Brezillon }
206193db446aSBoris Brezillon
206293db446aSBoris Brezillon /* Check if this is expected byte */
206393db446aSBoris Brezillon if (ns->regs.count == ns->regs.num) {
206493db446aSBoris Brezillon NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
206593db446aSBoris Brezillon ns->regs.num);
206693db446aSBoris Brezillon return;
206793db446aSBoris Brezillon }
206893db446aSBoris Brezillon
206993db446aSBoris Brezillon if (ns->busw == 8) {
207093db446aSBoris Brezillon ns->buf.byte[ns->regs.count] = byte;
207193db446aSBoris Brezillon ns->regs.count += 1;
207293db446aSBoris Brezillon } else {
207393db446aSBoris Brezillon ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
207493db446aSBoris Brezillon ns->regs.count += 2;
207593db446aSBoris Brezillon }
207693db446aSBoris Brezillon }
207793db446aSBoris Brezillon
207893db446aSBoris Brezillon return;
207993db446aSBoris Brezillon }
208093db446aSBoris Brezillon
ns_nand_write_buf(struct nand_chip * chip,const u_char * buf,int len)2081c0739d85SBoris Brezillon static void ns_nand_write_buf(struct nand_chip *chip, const u_char *buf,
2082c0739d85SBoris Brezillon int len)
208393db446aSBoris Brezillon {
208493db446aSBoris Brezillon struct nandsim *ns = nand_get_controller_data(chip);
208593db446aSBoris Brezillon
208693db446aSBoris Brezillon /* Check that chip is expecting data input */
208793db446aSBoris Brezillon if (!(ns->state & STATE_DATAIN_MASK)) {
208888f9f3e8SMiquel Raynal NS_ERR("write_buf: data input isn't expected, state is %s, switch to STATE_READY\n",
208988f9f3e8SMiquel Raynal ns_get_state_name(ns->state));
209088f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
209193db446aSBoris Brezillon return;
209293db446aSBoris Brezillon }
209393db446aSBoris Brezillon
209493db446aSBoris Brezillon /* Check if these are expected bytes */
209593db446aSBoris Brezillon if (ns->regs.count + len > ns->regs.num) {
209693db446aSBoris Brezillon NS_ERR("write_buf: too many input bytes\n");
209788f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
209893db446aSBoris Brezillon return;
209993db446aSBoris Brezillon }
210093db446aSBoris Brezillon
210193db446aSBoris Brezillon memcpy(ns->buf.byte + ns->regs.count, buf, len);
210293db446aSBoris Brezillon ns->regs.count += len;
210393db446aSBoris Brezillon
210493db446aSBoris Brezillon if (ns->regs.count == ns->regs.num) {
210593db446aSBoris Brezillon NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
210693db446aSBoris Brezillon }
210793db446aSBoris Brezillon }
210893db446aSBoris Brezillon
ns_nand_read_buf(struct nand_chip * chip,u_char * buf,int len)21097e534323SBoris Brezillon static void ns_nand_read_buf(struct nand_chip *chip, u_char *buf, int len)
211093db446aSBoris Brezillon {
211193db446aSBoris Brezillon struct nandsim *ns = nand_get_controller_data(chip);
211293db446aSBoris Brezillon
211393db446aSBoris Brezillon /* Sanity and correctness checks */
211493db446aSBoris Brezillon if (!ns->lines.ce) {
211593db446aSBoris Brezillon NS_ERR("read_buf: chip is disabled\n");
211693db446aSBoris Brezillon return;
211793db446aSBoris Brezillon }
211893db446aSBoris Brezillon if (ns->lines.ale || ns->lines.cle) {
211993db446aSBoris Brezillon NS_ERR("read_buf: ALE or CLE pin is high\n");
212093db446aSBoris Brezillon return;
212193db446aSBoris Brezillon }
212293db446aSBoris Brezillon if (!(ns->state & STATE_DATAOUT_MASK)) {
212393db446aSBoris Brezillon NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
212488f9f3e8SMiquel Raynal ns_get_state_name(ns->state));
212593db446aSBoris Brezillon return;
212693db446aSBoris Brezillon }
212793db446aSBoris Brezillon
212893db446aSBoris Brezillon if (NS_STATE(ns->state) != STATE_DATAOUT) {
212993db446aSBoris Brezillon int i;
213093db446aSBoris Brezillon
213193db446aSBoris Brezillon for (i = 0; i < len; i++)
21321c14fe21SRichard Weinberger buf[i] = ns_nand_read_byte(chip);
213393db446aSBoris Brezillon
213493db446aSBoris Brezillon return;
213593db446aSBoris Brezillon }
213693db446aSBoris Brezillon
213793db446aSBoris Brezillon /* Check if these are expected bytes */
213893db446aSBoris Brezillon if (ns->regs.count + len > ns->regs.num) {
213993db446aSBoris Brezillon NS_ERR("read_buf: too many bytes to read\n");
214088f9f3e8SMiquel Raynal ns_switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
214193db446aSBoris Brezillon return;
214293db446aSBoris Brezillon }
214393db446aSBoris Brezillon
214493db446aSBoris Brezillon memcpy(buf, ns->buf.byte + ns->regs.count, len);
214593db446aSBoris Brezillon ns->regs.count += len;
214693db446aSBoris Brezillon
214793db446aSBoris Brezillon if (ns->regs.count == ns->regs.num) {
214893db446aSBoris Brezillon if (NS_STATE(ns->nxstate) == STATE_READY)
214988f9f3e8SMiquel Raynal ns_switch_state(ns);
215093db446aSBoris Brezillon }
215193db446aSBoris Brezillon
215293db446aSBoris Brezillon return;
215393db446aSBoris Brezillon }
215493db446aSBoris Brezillon
ns_exec_op(struct nand_chip * chip,const struct nand_operation * op,bool check_only)21551c14fe21SRichard Weinberger static int ns_exec_op(struct nand_chip *chip, const struct nand_operation *op,
21561c14fe21SRichard Weinberger bool check_only)
21571c14fe21SRichard Weinberger {
21581c14fe21SRichard Weinberger int i;
21591c14fe21SRichard Weinberger unsigned int op_id;
21601c14fe21SRichard Weinberger const struct nand_op_instr *instr = NULL;
21611c14fe21SRichard Weinberger struct nandsim *ns = nand_get_controller_data(chip);
21621c14fe21SRichard Weinberger
21639b043c64SMiquel Raynal if (check_only) {
21649b043c64SMiquel Raynal /* The current implementation of nandsim needs to know the
21659b043c64SMiquel Raynal * ongoing operation when performing the address cycles. This
21669b043c64SMiquel Raynal * means it cannot make the difference between a regular read
21679b043c64SMiquel Raynal * and a continuous read. Hence, this hack to manually refuse
21689b043c64SMiquel Raynal * supporting sequential cached operations.
21699b043c64SMiquel Raynal */
21709b043c64SMiquel Raynal for (op_id = 0; op_id < op->ninstrs; op_id++) {
21719b043c64SMiquel Raynal instr = &op->instrs[op_id];
21729b043c64SMiquel Raynal if (instr->type == NAND_OP_CMD_INSTR &&
21739b043c64SMiquel Raynal (instr->ctx.cmd.opcode == NAND_CMD_READCACHEEND ||
21749b043c64SMiquel Raynal instr->ctx.cmd.opcode == NAND_CMD_READCACHESEQ))
21759b043c64SMiquel Raynal return -EOPNOTSUPP;
21769b043c64SMiquel Raynal }
21779b043c64SMiquel Raynal
2178ce446b4bSBoris Brezillon return 0;
21799b043c64SMiquel Raynal }
2180ce446b4bSBoris Brezillon
21811c14fe21SRichard Weinberger ns->lines.ce = 1;
21821c14fe21SRichard Weinberger
21831c14fe21SRichard Weinberger for (op_id = 0; op_id < op->ninstrs; op_id++) {
21841c14fe21SRichard Weinberger instr = &op->instrs[op_id];
21851c14fe21SRichard Weinberger ns->lines.cle = 0;
21861c14fe21SRichard Weinberger ns->lines.ale = 0;
21871c14fe21SRichard Weinberger
21881c14fe21SRichard Weinberger switch (instr->type) {
21891c14fe21SRichard Weinberger case NAND_OP_CMD_INSTR:
21901c14fe21SRichard Weinberger ns->lines.cle = 1;
21911c14fe21SRichard Weinberger ns_nand_write_byte(chip, instr->ctx.cmd.opcode);
21921c14fe21SRichard Weinberger break;
21931c14fe21SRichard Weinberger case NAND_OP_ADDR_INSTR:
21941c14fe21SRichard Weinberger ns->lines.ale = 1;
21951c14fe21SRichard Weinberger for (i = 0; i < instr->ctx.addr.naddrs; i++)
21961c14fe21SRichard Weinberger ns_nand_write_byte(chip, instr->ctx.addr.addrs[i]);
21971c14fe21SRichard Weinberger break;
21981c14fe21SRichard Weinberger case NAND_OP_DATA_IN_INSTR:
21991c14fe21SRichard Weinberger ns_nand_read_buf(chip, instr->ctx.data.buf.in, instr->ctx.data.len);
22001c14fe21SRichard Weinberger break;
22011c14fe21SRichard Weinberger case NAND_OP_DATA_OUT_INSTR:
22021c14fe21SRichard Weinberger ns_nand_write_buf(chip, instr->ctx.data.buf.out, instr->ctx.data.len);
22031c14fe21SRichard Weinberger break;
22041c14fe21SRichard Weinberger case NAND_OP_WAITRDY_INSTR:
22051c14fe21SRichard Weinberger /* we are always ready */
22061c14fe21SRichard Weinberger break;
22071c14fe21SRichard Weinberger }
22081c14fe21SRichard Weinberger }
22091c14fe21SRichard Weinberger
22101c14fe21SRichard Weinberger return 0;
22111c14fe21SRichard Weinberger }
22121c14fe21SRichard Weinberger
ns_attach_chip(struct nand_chip * chip)22135cbad9e3SMiquel Raynal static int ns_attach_chip(struct nand_chip *chip)
22145cbad9e3SMiquel Raynal {
22155cbad9e3SMiquel Raynal unsigned int eccsteps, eccbytes;
22165cbad9e3SMiquel Raynal
22173c97be69SMiquel Raynal chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_SOFT;
22183c97be69SMiquel Raynal chip->ecc.algo = bch ? NAND_ECC_ALGO_BCH : NAND_ECC_ALGO_HAMMING;
22193c97be69SMiquel Raynal
22205cbad9e3SMiquel Raynal if (!bch)
22215cbad9e3SMiquel Raynal return 0;
22225cbad9e3SMiquel Raynal
2223127aae60SMiquel Raynal if (!IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_BCH)) {
22245cbad9e3SMiquel Raynal NS_ERR("BCH ECC support is disabled\n");
22255cbad9e3SMiquel Raynal return -EINVAL;
22265cbad9e3SMiquel Raynal }
22275cbad9e3SMiquel Raynal
22285cbad9e3SMiquel Raynal /* Use 512-byte ecc blocks */
22295cbad9e3SMiquel Raynal eccsteps = nsmtd->writesize / 512;
22305cbad9e3SMiquel Raynal eccbytes = ((bch * 13) + 7) / 8;
22315cbad9e3SMiquel Raynal
22325cbad9e3SMiquel Raynal /* Do not bother supporting small page devices */
22335cbad9e3SMiquel Raynal if (nsmtd->oobsize < 64 || !eccsteps) {
22345cbad9e3SMiquel Raynal NS_ERR("BCH not available on small page devices\n");
22355cbad9e3SMiquel Raynal return -EINVAL;
22365cbad9e3SMiquel Raynal }
22375cbad9e3SMiquel Raynal
22385cbad9e3SMiquel Raynal if (((eccbytes * eccsteps) + 2) > nsmtd->oobsize) {
22395cbad9e3SMiquel Raynal NS_ERR("Invalid BCH value %u\n", bch);
22405cbad9e3SMiquel Raynal return -EINVAL;
22415cbad9e3SMiquel Raynal }
22425cbad9e3SMiquel Raynal
22435cbad9e3SMiquel Raynal chip->ecc.size = 512;
22445cbad9e3SMiquel Raynal chip->ecc.strength = bch;
22455cbad9e3SMiquel Raynal chip->ecc.bytes = eccbytes;
22465cbad9e3SMiquel Raynal
22475cbad9e3SMiquel Raynal NS_INFO("Using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
22485cbad9e3SMiquel Raynal
22495cbad9e3SMiquel Raynal return 0;
22505cbad9e3SMiquel Raynal }
22515cbad9e3SMiquel Raynal
22525cbad9e3SMiquel Raynal static const struct nand_controller_ops ns_controller_ops = {
22535cbad9e3SMiquel Raynal .attach_chip = ns_attach_chip,
22541c14fe21SRichard Weinberger .exec_op = ns_exec_op,
22555cbad9e3SMiquel Raynal };
22565cbad9e3SMiquel Raynal
225793db446aSBoris Brezillon /*
225893db446aSBoris Brezillon * Module initialization function
225993db446aSBoris Brezillon */
ns_init_module(void)226093db446aSBoris Brezillon static int __init ns_init_module(void)
226193db446aSBoris Brezillon {
226273f2b68cSMiquel Raynal struct list_head *pos, *n;
226393db446aSBoris Brezillon struct nand_chip *chip;
226474aee14cSRichard Weinberger struct nandsim *ns;
226572e840a1SMiquel Raynal int ret;
226693db446aSBoris Brezillon
226793db446aSBoris Brezillon if (bus_width != 8 && bus_width != 16) {
226893db446aSBoris Brezillon NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
226993db446aSBoris Brezillon return -EINVAL;
227093db446aSBoris Brezillon }
227193db446aSBoris Brezillon
227274aee14cSRichard Weinberger ns = kzalloc(sizeof(struct nandsim), GFP_KERNEL);
227374aee14cSRichard Weinberger if (!ns) {
227493db446aSBoris Brezillon NS_ERR("unable to allocate core structures.\n");
227593db446aSBoris Brezillon return -ENOMEM;
227693db446aSBoris Brezillon }
227774aee14cSRichard Weinberger chip = &ns->chip;
227893db446aSBoris Brezillon nsmtd = nand_to_mtd(chip);
227974aee14cSRichard Weinberger nand_set_controller_data(chip, (void *)ns);
228093db446aSBoris Brezillon
228193db446aSBoris Brezillon /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
228293db446aSBoris Brezillon /* and 'badblocks' parameters to work */
228393db446aSBoris Brezillon chip->options |= NAND_SKIP_BBTSCAN;
228493db446aSBoris Brezillon
228593db446aSBoris Brezillon switch (bbt) {
228693db446aSBoris Brezillon case 2:
228793db446aSBoris Brezillon chip->bbt_options |= NAND_BBT_NO_OOB;
2288025a06c1SMiquel Raynal fallthrough;
228993db446aSBoris Brezillon case 1:
229093db446aSBoris Brezillon chip->bbt_options |= NAND_BBT_USE_FLASH;
2291025a06c1SMiquel Raynal fallthrough;
229293db446aSBoris Brezillon case 0:
229393db446aSBoris Brezillon break;
229493db446aSBoris Brezillon default:
229593db446aSBoris Brezillon NS_ERR("bbt has to be 0..2\n");
2296052a7a53SMiquel Raynal ret = -EINVAL;
2297f82d82e2SMiquel Raynal goto free_ns_struct;
229893db446aSBoris Brezillon }
229993db446aSBoris Brezillon /*
230093db446aSBoris Brezillon * Perform minimum nandsim structure initialization to handle
230193db446aSBoris Brezillon * the initial ID read command correctly
230293db446aSBoris Brezillon */
230393db446aSBoris Brezillon if (id_bytes[6] != 0xFF || id_bytes[7] != 0xFF)
230474aee14cSRichard Weinberger ns->geom.idbytes = 8;
230593db446aSBoris Brezillon else if (id_bytes[4] != 0xFF || id_bytes[5] != 0xFF)
230674aee14cSRichard Weinberger ns->geom.idbytes = 6;
230793db446aSBoris Brezillon else if (id_bytes[2] != 0xFF || id_bytes[3] != 0xFF)
230874aee14cSRichard Weinberger ns->geom.idbytes = 4;
230993db446aSBoris Brezillon else
231074aee14cSRichard Weinberger ns->geom.idbytes = 2;
231174aee14cSRichard Weinberger ns->regs.status = NS_STATUS_OK(ns);
231274aee14cSRichard Weinberger ns->nxstate = STATE_UNKNOWN;
231374aee14cSRichard Weinberger ns->options |= OPT_PAGE512; /* temporary value */
231474aee14cSRichard Weinberger memcpy(ns->ids, id_bytes, sizeof(ns->ids));
231593db446aSBoris Brezillon if (bus_width == 16) {
231674aee14cSRichard Weinberger ns->busw = 16;
231793db446aSBoris Brezillon chip->options |= NAND_BUSWIDTH_16;
231893db446aSBoris Brezillon }
231993db446aSBoris Brezillon
232093db446aSBoris Brezillon nsmtd->owner = THIS_MODULE;
232193db446aSBoris Brezillon
2322052a7a53SMiquel Raynal ret = ns_parse_weakblocks();
2323052a7a53SMiquel Raynal if (ret)
2324f82d82e2SMiquel Raynal goto free_ns_struct;
232593db446aSBoris Brezillon
2326052a7a53SMiquel Raynal ret = ns_parse_weakpages();
2327052a7a53SMiquel Raynal if (ret)
232873f2b68cSMiquel Raynal goto free_wb_list;
232993db446aSBoris Brezillon
2330052a7a53SMiquel Raynal ret = ns_parse_gravepages();
2331052a7a53SMiquel Raynal if (ret)
233273f2b68cSMiquel Raynal goto free_wp_list;
233393db446aSBoris Brezillon
23341c14fe21SRichard Weinberger nand_controller_init(&ns->base);
23351c14fe21SRichard Weinberger ns->base.ops = &ns_controller_ops;
23361c14fe21SRichard Weinberger chip->controller = &ns->base;
23371c14fe21SRichard Weinberger
2338052a7a53SMiquel Raynal ret = nand_scan(chip, 1);
2339052a7a53SMiquel Raynal if (ret) {
23405cbad9e3SMiquel Raynal NS_ERR("Could not scan NAND Simulator device\n");
234173f2b68cSMiquel Raynal goto free_gp_list;
234293db446aSBoris Brezillon }
234393db446aSBoris Brezillon
234493db446aSBoris Brezillon if (overridesize) {
234593db446aSBoris Brezillon uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
2346629a442cSBoris Brezillon struct nand_memory_organization *memorg;
23476c836d51SBoris Brezillon u64 targetsize;
2348629a442cSBoris Brezillon
2349629a442cSBoris Brezillon memorg = nanddev_get_memorg(&chip->base);
2350629a442cSBoris Brezillon
235193db446aSBoris Brezillon if (new_size >> overridesize != nsmtd->erasesize) {
235293db446aSBoris Brezillon NS_ERR("overridesize is too big\n");
2353052a7a53SMiquel Raynal ret = -EINVAL;
2354dc2733deSMiquel Raynal goto cleanup_nand;
235593db446aSBoris Brezillon }
23566c836d51SBoris Brezillon
235793db446aSBoris Brezillon /* N.B. This relies on nand_scan not doing anything with the size before we change it */
235893db446aSBoris Brezillon nsmtd->size = new_size;
2359629a442cSBoris Brezillon memorg->eraseblocks_per_lun = 1 << overridesize;
23606c836d51SBoris Brezillon targetsize = nanddev_target_size(&chip->base);
236193db446aSBoris Brezillon chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
23626c836d51SBoris Brezillon chip->pagemask = (targetsize >> chip->page_shift) - 1;
236393db446aSBoris Brezillon }
236493db446aSBoris Brezillon
2365052a7a53SMiquel Raynal ret = ns_setup_wear_reporting(nsmtd);
2366052a7a53SMiquel Raynal if (ret)
2367dc2733deSMiquel Raynal goto cleanup_nand;
236893db446aSBoris Brezillon
2369052a7a53SMiquel Raynal ret = ns_init(nsmtd);
2370052a7a53SMiquel Raynal if (ret)
23715dcb5164SMiquel Raynal goto free_ebw;
237293db446aSBoris Brezillon
2373052a7a53SMiquel Raynal ret = nand_create_bbt(chip);
2374052a7a53SMiquel Raynal if (ret)
237582503f84SMiquel Raynal goto free_ns_object;
237693db446aSBoris Brezillon
2377052a7a53SMiquel Raynal ret = ns_parse_badblocks(ns, nsmtd);
2378052a7a53SMiquel Raynal if (ret)
237982503f84SMiquel Raynal goto free_ns_object;
238093db446aSBoris Brezillon
238193db446aSBoris Brezillon /* Register NAND partitions */
2382052a7a53SMiquel Raynal ret = mtd_device_register(nsmtd, &ns->partitions[0], ns->nbparts);
2383052a7a53SMiquel Raynal if (ret)
238482503f84SMiquel Raynal goto free_ns_object;
238593db446aSBoris Brezillon
2386052a7a53SMiquel Raynal ret = ns_debugfs_create(ns);
2387052a7a53SMiquel Raynal if (ret)
2388d6e4fd52SMiquel Raynal goto unregister_mtd;
238993db446aSBoris Brezillon
239093db446aSBoris Brezillon return 0;
239193db446aSBoris Brezillon
2392d6e4fd52SMiquel Raynal unregister_mtd:
2393d6e4fd52SMiquel Raynal WARN_ON(mtd_device_unregister(nsmtd));
239482503f84SMiquel Raynal free_ns_object:
239588f9f3e8SMiquel Raynal ns_free(ns);
23965dcb5164SMiquel Raynal free_ebw:
23975dcb5164SMiquel Raynal kfree(erase_block_wear);
2398dc2733deSMiquel Raynal cleanup_nand:
2399d6e4fd52SMiquel Raynal nand_cleanup(chip);
240073f2b68cSMiquel Raynal free_gp_list:
240173f2b68cSMiquel Raynal list_for_each_safe(pos, n, &grave_pages) {
240273f2b68cSMiquel Raynal list_del(pos);
240373f2b68cSMiquel Raynal kfree(list_entry(pos, struct grave_page, list));
240473f2b68cSMiquel Raynal }
240573f2b68cSMiquel Raynal free_wp_list:
240673f2b68cSMiquel Raynal list_for_each_safe(pos, n, &weak_pages) {
240773f2b68cSMiquel Raynal list_del(pos);
240873f2b68cSMiquel Raynal kfree(list_entry(pos, struct weak_page, list));
240973f2b68cSMiquel Raynal }
241073f2b68cSMiquel Raynal free_wb_list:
241173f2b68cSMiquel Raynal list_for_each_safe(pos, n, &weak_blocks) {
241273f2b68cSMiquel Raynal list_del(pos);
241373f2b68cSMiquel Raynal kfree(list_entry(pos, struct weak_block, list));
241473f2b68cSMiquel Raynal }
2415f82d82e2SMiquel Raynal free_ns_struct:
241674aee14cSRichard Weinberger kfree(ns);
241793db446aSBoris Brezillon
2418052a7a53SMiquel Raynal return ret;
241993db446aSBoris Brezillon }
242093db446aSBoris Brezillon
242193db446aSBoris Brezillon module_init(ns_init_module);
242293db446aSBoris Brezillon
242393db446aSBoris Brezillon /*
242493db446aSBoris Brezillon * Module clean-up function
242593db446aSBoris Brezillon */
ns_cleanup_module(void)242693db446aSBoris Brezillon static void __exit ns_cleanup_module(void)
242793db446aSBoris Brezillon {
242893db446aSBoris Brezillon struct nand_chip *chip = mtd_to_nand(nsmtd);
242993db446aSBoris Brezillon struct nandsim *ns = nand_get_controller_data(chip);
24305724fa7fSMiquel Raynal struct list_head *pos, *n;
243193db446aSBoris Brezillon
2432cde495f8SMiquel Raynal ns_debugfs_remove(ns);
24335724fa7fSMiquel Raynal WARN_ON(mtd_device_unregister(nsmtd));
24345724fa7fSMiquel Raynal ns_free(ns);
24355724fa7fSMiquel Raynal kfree(erase_block_wear);
24365724fa7fSMiquel Raynal nand_cleanup(chip);
24375724fa7fSMiquel Raynal
24385724fa7fSMiquel Raynal list_for_each_safe(pos, n, &grave_pages) {
24395724fa7fSMiquel Raynal list_del(pos);
24405724fa7fSMiquel Raynal kfree(list_entry(pos, struct grave_page, list));
24415724fa7fSMiquel Raynal }
24425724fa7fSMiquel Raynal
24435724fa7fSMiquel Raynal list_for_each_safe(pos, n, &weak_pages) {
24445724fa7fSMiquel Raynal list_del(pos);
24455724fa7fSMiquel Raynal kfree(list_entry(pos, struct weak_page, list));
24465724fa7fSMiquel Raynal }
24475724fa7fSMiquel Raynal
24485724fa7fSMiquel Raynal list_for_each_safe(pos, n, &weak_blocks) {
24495724fa7fSMiquel Raynal list_del(pos);
24505724fa7fSMiquel Raynal kfree(list_entry(pos, struct weak_block, list));
24515724fa7fSMiquel Raynal }
24525724fa7fSMiquel Raynal
24535724fa7fSMiquel Raynal kfree(ns);
245493db446aSBoris Brezillon }
245593db446aSBoris Brezillon
245693db446aSBoris Brezillon module_exit(ns_cleanup_module);
245793db446aSBoris Brezillon
245893db446aSBoris Brezillon MODULE_LICENSE ("GPL");
245993db446aSBoris Brezillon MODULE_AUTHOR ("Artem B. Bityuckiy");
246093db446aSBoris Brezillon MODULE_DESCRIPTION ("The NAND flash simulator");
2461