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