| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | * NAND flash simulator. | 
|  | 3 | * | 
|  | 4 | * Author: Artem B. Bityuckiy <dedekind@oktetlabs.ru>, <dedekind@infradead.org> | 
|  | 5 | * | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 6 | * Copyright (C) 2004 Nokia Corporation | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * | 
|  | 8 | * Note: NS means "NAND Simulator". | 
|  | 9 | * Note: Input means input TO flash chip, output means output FROM chip. | 
|  | 10 | * | 
|  | 11 | * This program is free software; you can redistribute it and/or modify it | 
|  | 12 | * under the terms of the GNU General Public License as published by the | 
|  | 13 | * Free Software Foundation; either version 2, or (at your option) any later | 
|  | 14 | * version. | 
|  | 15 | * | 
|  | 16 | * This program is distributed in the hope that it will be useful, but | 
|  | 17 | * WITHOUT ANY WARRANTY; without even the implied warranty of | 
|  | 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General | 
|  | 19 | * Public License for more details. | 
|  | 20 | * | 
|  | 21 | * You should have received a copy of the GNU General Public License | 
|  | 22 | * along with this program; if not, write to the Free Software | 
|  | 23 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | */ | 
|  | 25 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | #include <linux/init.h> | 
|  | 27 | #include <linux/types.h> | 
|  | 28 | #include <linux/module.h> | 
|  | 29 | #include <linux/moduleparam.h> | 
|  | 30 | #include <linux/vmalloc.h> | 
|  | 31 | #include <linux/slab.h> | 
|  | 32 | #include <linux/errno.h> | 
|  | 33 | #include <linux/string.h> | 
|  | 34 | #include <linux/mtd/mtd.h> | 
|  | 35 | #include <linux/mtd/nand.h> | 
|  | 36 | #include <linux/mtd/partitions.h> | 
|  | 37 | #include <linux/delay.h> | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 38 | #include <linux/list.h> | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 39 | #include <linux/random.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 |  | 
|  | 41 | /* Default simulator parameters values */ | 
|  | 42 | #if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE)  || \ | 
|  | 43 | !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \ | 
|  | 44 | !defined(CONFIG_NANDSIM_THIRD_ID_BYTE)  || \ | 
|  | 45 | !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE) | 
|  | 46 | #define CONFIG_NANDSIM_FIRST_ID_BYTE  0x98 | 
|  | 47 | #define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39 | 
|  | 48 | #define CONFIG_NANDSIM_THIRD_ID_BYTE  0xFF /* No byte */ | 
|  | 49 | #define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */ | 
|  | 50 | #endif | 
|  | 51 |  | 
|  | 52 | #ifndef CONFIG_NANDSIM_ACCESS_DELAY | 
|  | 53 | #define CONFIG_NANDSIM_ACCESS_DELAY 25 | 
|  | 54 | #endif | 
|  | 55 | #ifndef CONFIG_NANDSIM_PROGRAMM_DELAY | 
|  | 56 | #define CONFIG_NANDSIM_PROGRAMM_DELAY 200 | 
|  | 57 | #endif | 
|  | 58 | #ifndef CONFIG_NANDSIM_ERASE_DELAY | 
|  | 59 | #define CONFIG_NANDSIM_ERASE_DELAY 2 | 
|  | 60 | #endif | 
|  | 61 | #ifndef CONFIG_NANDSIM_OUTPUT_CYCLE | 
|  | 62 | #define CONFIG_NANDSIM_OUTPUT_CYCLE 40 | 
|  | 63 | #endif | 
|  | 64 | #ifndef CONFIG_NANDSIM_INPUT_CYCLE | 
|  | 65 | #define CONFIG_NANDSIM_INPUT_CYCLE  50 | 
|  | 66 | #endif | 
|  | 67 | #ifndef CONFIG_NANDSIM_BUS_WIDTH | 
|  | 68 | #define CONFIG_NANDSIM_BUS_WIDTH  8 | 
|  | 69 | #endif | 
|  | 70 | #ifndef CONFIG_NANDSIM_DO_DELAYS | 
|  | 71 | #define CONFIG_NANDSIM_DO_DELAYS  0 | 
|  | 72 | #endif | 
|  | 73 | #ifndef CONFIG_NANDSIM_LOG | 
|  | 74 | #define CONFIG_NANDSIM_LOG        0 | 
|  | 75 | #endif | 
|  | 76 | #ifndef CONFIG_NANDSIM_DBG | 
|  | 77 | #define CONFIG_NANDSIM_DBG        0 | 
|  | 78 | #endif | 
|  | 79 |  | 
|  | 80 | static uint first_id_byte  = CONFIG_NANDSIM_FIRST_ID_BYTE; | 
|  | 81 | static uint second_id_byte = CONFIG_NANDSIM_SECOND_ID_BYTE; | 
|  | 82 | static uint third_id_byte  = CONFIG_NANDSIM_THIRD_ID_BYTE; | 
|  | 83 | static uint fourth_id_byte = CONFIG_NANDSIM_FOURTH_ID_BYTE; | 
|  | 84 | static uint access_delay   = CONFIG_NANDSIM_ACCESS_DELAY; | 
|  | 85 | static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY; | 
|  | 86 | static uint erase_delay    = CONFIG_NANDSIM_ERASE_DELAY; | 
|  | 87 | static uint output_cycle   = CONFIG_NANDSIM_OUTPUT_CYCLE; | 
|  | 88 | static uint input_cycle    = CONFIG_NANDSIM_INPUT_CYCLE; | 
|  | 89 | static uint bus_width      = CONFIG_NANDSIM_BUS_WIDTH; | 
|  | 90 | static uint do_delays      = CONFIG_NANDSIM_DO_DELAYS; | 
|  | 91 | static uint log            = CONFIG_NANDSIM_LOG; | 
|  | 92 | static uint dbg            = CONFIG_NANDSIM_DBG; | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 93 | static unsigned long parts[MAX_MTD_DEVICES]; | 
|  | 94 | static unsigned int parts_num; | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 95 | static char *badblocks = NULL; | 
|  | 96 | static char *weakblocks = NULL; | 
|  | 97 | static char *weakpages = NULL; | 
|  | 98 | static unsigned int bitflips = 0; | 
|  | 99 | static char *gravepages = NULL; | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 100 | static unsigned int rptwear = 0; | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 101 | static unsigned int overridesize = 0; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 |  | 
|  | 103 | module_param(first_id_byte,  uint, 0400); | 
|  | 104 | module_param(second_id_byte, uint, 0400); | 
|  | 105 | module_param(third_id_byte,  uint, 0400); | 
|  | 106 | module_param(fourth_id_byte, uint, 0400); | 
|  | 107 | module_param(access_delay,   uint, 0400); | 
|  | 108 | module_param(programm_delay, uint, 0400); | 
|  | 109 | module_param(erase_delay,    uint, 0400); | 
|  | 110 | module_param(output_cycle,   uint, 0400); | 
|  | 111 | module_param(input_cycle,    uint, 0400); | 
|  | 112 | module_param(bus_width,      uint, 0400); | 
|  | 113 | module_param(do_delays,      uint, 0400); | 
|  | 114 | module_param(log,            uint, 0400); | 
|  | 115 | module_param(dbg,            uint, 0400); | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 116 | module_param_array(parts, ulong, &parts_num, 0400); | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 117 | module_param(badblocks,      charp, 0400); | 
|  | 118 | module_param(weakblocks,     charp, 0400); | 
|  | 119 | module_param(weakpages,      charp, 0400); | 
|  | 120 | module_param(bitflips,       uint, 0400); | 
|  | 121 | module_param(gravepages,     charp, 0400); | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 122 | module_param(rptwear,        uint, 0400); | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 123 | module_param(overridesize,   uint, 0400); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 |  | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 125 | MODULE_PARM_DESC(first_id_byte,  "The first byte returned by NAND Flash 'read ID' command (manufacturer ID)"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID)"); | 
|  | 127 | MODULE_PARM_DESC(third_id_byte,  "The third byte returned by NAND Flash 'read ID' command"); | 
|  | 128 | MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command"); | 
|  | 129 | MODULE_PARM_DESC(access_delay,   "Initial page access delay (microiseconds)"); | 
|  | 130 | MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds"); | 
|  | 131 | MODULE_PARM_DESC(erase_delay,    "Sector erase delay (milliseconds)"); | 
|  | 132 | MODULE_PARM_DESC(output_cycle,   "Word output (from flash) time (nanodeconds)"); | 
|  | 133 | MODULE_PARM_DESC(input_cycle,    "Word input (to flash) time (nanodeconds)"); | 
|  | 134 | MODULE_PARM_DESC(bus_width,      "Chip's bus width (8- or 16-bit)"); | 
|  | 135 | MODULE_PARM_DESC(do_delays,      "Simulate NAND delays using busy-waits if not zero"); | 
|  | 136 | MODULE_PARM_DESC(log,            "Perform logging if not zero"); | 
|  | 137 | MODULE_PARM_DESC(dbg,            "Output debug information if not zero"); | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 138 | MODULE_PARM_DESC(parts,          "Partition sizes (in erase blocks) separated by commas"); | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 139 | /* Page and erase block positions for the following parameters are independent of any partitions */ | 
|  | 140 | MODULE_PARM_DESC(badblocks,      "Erase blocks that are initially marked bad, separated by commas"); | 
|  | 141 | MODULE_PARM_DESC(weakblocks,     "Weak erase blocks [: remaining erase cycles (defaults to 3)]" | 
|  | 142 | " separated by commas e.g. 113:2 means eb 113" | 
|  | 143 | " can be erased only twice before failing"); | 
|  | 144 | MODULE_PARM_DESC(weakpages,      "Weak pages [: maximum writes (defaults to 3)]" | 
|  | 145 | " separated by commas e.g. 1401:2 means page 1401" | 
|  | 146 | " can be written only twice before failing"); | 
|  | 147 | MODULE_PARM_DESC(bitflips,       "Maximum number of random bit flips per page (zero by default)"); | 
|  | 148 | MODULE_PARM_DESC(gravepages,     "Pages that lose data [: maximum reads (defaults to 3)]" | 
|  | 149 | " separated by commas e.g. 1401:2 means page 1401" | 
|  | 150 | " can be read only twice before failing"); | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 151 | MODULE_PARM_DESC(rptwear,        "Number of erases inbetween reporting wear, if not zero"); | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 152 | MODULE_PARM_DESC(overridesize,   "Specifies the NAND Flash size overriding the ID bytes. " | 
|  | 153 | "The size is specified in erase blocks and as the exponent of a power of two" | 
|  | 154 | " e.g. 5 means a size of 32 erase blocks"); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 |  | 
|  | 156 | /* The largest possible page size */ | 
|  | 157 | #define NS_LARGEST_PAGE_SIZE	2048 | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 158 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | /* The prefix for simulator output */ | 
|  | 160 | #define NS_OUTPUT_PREFIX "[nandsim]" | 
|  | 161 |  | 
|  | 162 | /* Simulator's output macros (logging, debugging, warning, error) */ | 
|  | 163 | #define NS_LOG(args...) \ | 
|  | 164 | do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0) | 
|  | 165 | #define NS_DBG(args...) \ | 
|  | 166 | do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0) | 
|  | 167 | #define NS_WARN(args...) \ | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 168 | do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warning: " args); } while(0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | #define NS_ERR(args...) \ | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 170 | do { printk(KERN_ERR NS_OUTPUT_PREFIX " error: " args); } while(0) | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 171 | #define NS_INFO(args...) \ | 
|  | 172 | do { printk(KERN_INFO NS_OUTPUT_PREFIX " " args); } while(0) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 173 |  | 
|  | 174 | /* Busy-wait delay macros (microseconds, milliseconds) */ | 
|  | 175 | #define NS_UDELAY(us) \ | 
|  | 176 | do { if (do_delays) udelay(us); } while(0) | 
|  | 177 | #define NS_MDELAY(us) \ | 
|  | 178 | do { if (do_delays) mdelay(us); } while(0) | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 179 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 180 | /* Is the nandsim structure initialized ? */ | 
|  | 181 | #define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0) | 
|  | 182 |  | 
|  | 183 | /* Good operation completion status */ | 
|  | 184 | #define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0))) | 
|  | 185 |  | 
|  | 186 | /* Operation failed completion status */ | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 187 | #define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns)) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 |  | 
|  | 189 | /* Calculate the page offset in flash RAM image by (row, column) address */ | 
|  | 190 | #define NS_RAW_OFFSET(ns) \ | 
|  | 191 | (((ns)->regs.row << (ns)->geom.pgshift) + ((ns)->regs.row * (ns)->geom.oobsz) + (ns)->regs.column) | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 192 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | /* Calculate the OOB offset in flash RAM image by (row, column) address */ | 
|  | 194 | #define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz) | 
|  | 195 |  | 
|  | 196 | /* After a command is input, the simulator goes to one of the following states */ | 
|  | 197 | #define STATE_CMD_READ0        0x00000001 /* read data from the beginning of page */ | 
|  | 198 | #define STATE_CMD_READ1        0x00000002 /* read data from the second half of page */ | 
| Artem Bityutskiy | 4a0c50c | 2006-12-06 21:52:32 +0200 | [diff] [blame] | 199 | #define STATE_CMD_READSTART    0x00000003 /* read data second command (large page devices) */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 200 | #define STATE_CMD_PAGEPROG     0x00000004 /* start page programm */ | 
|  | 201 | #define STATE_CMD_READOOB      0x00000005 /* read OOB area */ | 
|  | 202 | #define STATE_CMD_ERASE1       0x00000006 /* sector erase first command */ | 
|  | 203 | #define STATE_CMD_STATUS       0x00000007 /* read status */ | 
|  | 204 | #define STATE_CMD_STATUS_M     0x00000008 /* read multi-plane status (isn't implemented) */ | 
|  | 205 | #define STATE_CMD_SEQIN        0x00000009 /* sequential data imput */ | 
|  | 206 | #define STATE_CMD_READID       0x0000000A /* read ID */ | 
|  | 207 | #define STATE_CMD_ERASE2       0x0000000B /* sector erase second command */ | 
|  | 208 | #define STATE_CMD_RESET        0x0000000C /* reset */ | 
|  | 209 | #define STATE_CMD_MASK         0x0000000F /* command states mask */ | 
|  | 210 |  | 
| Joe Perches | 8e87d78 | 2008-02-03 17:22:34 +0200 | [diff] [blame] | 211 | /* After an address is input, the simulator goes to one of these states */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | #define STATE_ADDR_PAGE        0x00000010 /* full (row, column) address is accepted */ | 
|  | 213 | #define STATE_ADDR_SEC         0x00000020 /* sector address was accepted */ | 
|  | 214 | #define STATE_ADDR_ZERO        0x00000030 /* one byte zero address was accepted */ | 
|  | 215 | #define STATE_ADDR_MASK        0x00000030 /* address states mask */ | 
|  | 216 |  | 
|  | 217 | /* Durind data input/output the simulator is in these states */ | 
|  | 218 | #define STATE_DATAIN           0x00000100 /* waiting for data input */ | 
|  | 219 | #define STATE_DATAIN_MASK      0x00000100 /* data input states mask */ | 
|  | 220 |  | 
|  | 221 | #define STATE_DATAOUT          0x00001000 /* waiting for page data output */ | 
|  | 222 | #define STATE_DATAOUT_ID       0x00002000 /* waiting for ID bytes output */ | 
|  | 223 | #define STATE_DATAOUT_STATUS   0x00003000 /* waiting for status output */ | 
|  | 224 | #define STATE_DATAOUT_STATUS_M 0x00004000 /* waiting for multi-plane status output */ | 
|  | 225 | #define STATE_DATAOUT_MASK     0x00007000 /* data output states mask */ | 
|  | 226 |  | 
|  | 227 | /* Previous operation is done, ready to accept new requests */ | 
|  | 228 | #define STATE_READY            0x00000000 | 
|  | 229 |  | 
|  | 230 | /* This state is used to mark that the next state isn't known yet */ | 
|  | 231 | #define STATE_UNKNOWN          0x10000000 | 
|  | 232 |  | 
|  | 233 | /* Simulator's actions bit masks */ | 
|  | 234 | #define ACTION_CPY       0x00100000 /* copy page/OOB to the internal buffer */ | 
|  | 235 | #define ACTION_PRGPAGE   0x00200000 /* programm the internal buffer to flash */ | 
|  | 236 | #define ACTION_SECERASE  0x00300000 /* erase sector */ | 
|  | 237 | #define ACTION_ZEROOFF   0x00400000 /* don't add any offset to address */ | 
|  | 238 | #define ACTION_HALFOFF   0x00500000 /* add to address half of page */ | 
|  | 239 | #define ACTION_OOBOFF    0x00600000 /* add to address OOB offset */ | 
|  | 240 | #define ACTION_MASK      0x00700000 /* action mask */ | 
|  | 241 |  | 
|  | 242 | #define NS_OPER_NUM      12 /* Number of operations supported by the simulator */ | 
|  | 243 | #define NS_OPER_STATES   6  /* Maximum number of states in operation */ | 
|  | 244 |  | 
|  | 245 | #define OPT_ANY          0xFFFFFFFF /* any chip supports this operation */ | 
|  | 246 | #define OPT_PAGE256      0x00000001 /* 256-byte  page chips */ | 
|  | 247 | #define OPT_PAGE512      0x00000002 /* 512-byte  page chips */ | 
|  | 248 | #define OPT_PAGE2048     0x00000008 /* 2048-byte page chips */ | 
|  | 249 | #define OPT_SMARTMEDIA   0x00000010 /* SmartMedia technology chips */ | 
|  | 250 | #define OPT_AUTOINCR     0x00000020 /* page number auto inctimentation is possible */ | 
|  | 251 | #define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */ | 
|  | 252 | #define OPT_LARGEPAGE    (OPT_PAGE2048) /* 2048-byte page chips */ | 
|  | 253 | #define OPT_SMALLPAGE    (OPT_PAGE256  | OPT_PAGE512)  /* 256 and 512-byte page chips */ | 
|  | 254 |  | 
|  | 255 | /* Remove action bits ftom state */ | 
|  | 256 | #define NS_STATE(x) ((x) & ~ACTION_MASK) | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 257 |  | 
|  | 258 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | * Maximum previous states which need to be saved. Currently saving is | 
|  | 260 | * only needed for page programm operation with preceeded read command | 
|  | 261 | * (which is only valid for 512-byte pages). | 
|  | 262 | */ | 
|  | 263 | #define NS_MAX_PREVSTATES 1 | 
|  | 264 |  | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 265 | /* | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 266 | * A union to represent flash memory contents and flash buffer. | 
|  | 267 | */ | 
|  | 268 | union ns_mem { | 
|  | 269 | u_char *byte;    /* for byte access */ | 
|  | 270 | uint16_t *word;  /* for 16-bit word access */ | 
|  | 271 | }; | 
|  | 272 |  | 
|  | 273 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | * The structure which describes all the internal simulator data. | 
|  | 275 | */ | 
|  | 276 | struct nandsim { | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 277 | struct mtd_partition partitions[MAX_MTD_DEVICES]; | 
|  | 278 | unsigned int nbparts; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 279 |  | 
|  | 280 | uint busw;              /* flash chip bus width (8 or 16) */ | 
|  | 281 | u_char ids[4];          /* chip's ID bytes */ | 
|  | 282 | uint32_t options;       /* chip's characteristic bits */ | 
|  | 283 | uint32_t state;         /* current chip state */ | 
|  | 284 | uint32_t nxstate;       /* next expected state */ | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 285 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 286 | uint32_t *op;           /* current operation, NULL operations isn't known yet  */ | 
|  | 287 | uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */ | 
|  | 288 | uint16_t npstates;      /* number of previous states saved */ | 
|  | 289 | uint16_t stateidx;      /* current state index */ | 
|  | 290 |  | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 291 | /* The simulated NAND flash pages array */ | 
|  | 292 | union ns_mem *pages; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 |  | 
|  | 294 | /* Internal buffer of page + OOB size bytes */ | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 295 | union ns_mem buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 |  | 
|  | 297 | /* NAND flash "geometry" */ | 
|  | 298 | struct nandsin_geometry { | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 299 | uint64_t totsz;     /* total flash size, bytes */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | uint32_t secsz;     /* flash sector (erase block) size, bytes */ | 
|  | 301 | uint pgsz;          /* NAND flash page size, bytes */ | 
|  | 302 | uint oobsz;         /* page OOB area size, bytes */ | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 303 | uint64_t totszoob;  /* total flash size including OOB, bytes */ | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | uint pgszoob;       /* page size including OOB , bytes*/ | 
|  | 305 | uint secszoob;      /* sector size including OOB, bytes */ | 
|  | 306 | uint pgnum;         /* total number of pages */ | 
|  | 307 | uint pgsec;         /* number of pages per sector */ | 
|  | 308 | uint secshift;      /* bits number in sector size */ | 
|  | 309 | uint pgshift;       /* bits number in page size */ | 
|  | 310 | uint oobshift;      /* bits number in OOB size */ | 
|  | 311 | uint pgaddrbytes;   /* bytes per page address */ | 
|  | 312 | uint secaddrbytes;  /* bytes per sector address */ | 
|  | 313 | uint idbytes;       /* the number ID bytes that this chip outputs */ | 
|  | 314 | } geom; | 
|  | 315 |  | 
|  | 316 | /* NAND flash internal registers */ | 
|  | 317 | struct nandsim_regs { | 
|  | 318 | unsigned command; /* the command register */ | 
|  | 319 | u_char   status;  /* the status register */ | 
|  | 320 | uint     row;     /* the page number */ | 
|  | 321 | uint     column;  /* the offset within page */ | 
|  | 322 | uint     count;   /* internal counter */ | 
|  | 323 | uint     num;     /* number of bytes which must be processed */ | 
|  | 324 | uint     off;     /* fixed page offset */ | 
|  | 325 | } regs; | 
|  | 326 |  | 
|  | 327 | /* NAND flash lines state */ | 
|  | 328 | struct ns_lines_status { | 
|  | 329 | int ce;  /* chip Enable */ | 
|  | 330 | int cle; /* command Latch Enable */ | 
|  | 331 | int ale; /* address Latch Enable */ | 
|  | 332 | int wp;  /* write Protect */ | 
|  | 333 | } lines; | 
|  | 334 | }; | 
|  | 335 |  | 
|  | 336 | /* | 
|  | 337 | * Operations array. To perform any operation the simulator must pass | 
|  | 338 | * through the correspondent states chain. | 
|  | 339 | */ | 
|  | 340 | static struct nandsim_operations { | 
|  | 341 | uint32_t reqopts;  /* options which are required to perform the operation */ | 
|  | 342 | uint32_t states[NS_OPER_STATES]; /* operation's states */ | 
|  | 343 | } ops[NS_OPER_NUM] = { | 
|  | 344 | /* Read page + OOB from the beginning */ | 
|  | 345 | {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY, | 
|  | 346 | STATE_DATAOUT, STATE_READY}}, | 
|  | 347 | /* Read page + OOB from the second half */ | 
|  | 348 | {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY, | 
|  | 349 | STATE_DATAOUT, STATE_READY}}, | 
|  | 350 | /* Read OOB */ | 
|  | 351 | {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY, | 
|  | 352 | STATE_DATAOUT, STATE_READY}}, | 
|  | 353 | /* Programm page starting from the beginning */ | 
|  | 354 | {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN, | 
|  | 355 | STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, | 
|  | 356 | /* Programm page starting from the beginning */ | 
|  | 357 | {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE, | 
|  | 358 | STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, | 
|  | 359 | /* Programm page starting from the second half */ | 
|  | 360 | {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE, | 
|  | 361 | STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, | 
|  | 362 | /* Programm OOB */ | 
|  | 363 | {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE, | 
|  | 364 | STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}}, | 
|  | 365 | /* Erase sector */ | 
|  | 366 | {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}}, | 
|  | 367 | /* Read status */ | 
|  | 368 | {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}}, | 
|  | 369 | /* Read multi-plane status */ | 
|  | 370 | {OPT_SMARTMEDIA, {STATE_CMD_STATUS_M, STATE_DATAOUT_STATUS_M, STATE_READY}}, | 
|  | 371 | /* Read ID */ | 
|  | 372 | {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}}, | 
|  | 373 | /* Large page devices read page */ | 
|  | 374 | {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY, | 
|  | 375 | STATE_DATAOUT, STATE_READY}} | 
|  | 376 | }; | 
|  | 377 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 378 | struct weak_block { | 
|  | 379 | struct list_head list; | 
|  | 380 | unsigned int erase_block_no; | 
|  | 381 | unsigned int max_erases; | 
|  | 382 | unsigned int erases_done; | 
|  | 383 | }; | 
|  | 384 |  | 
|  | 385 | static LIST_HEAD(weak_blocks); | 
|  | 386 |  | 
|  | 387 | struct weak_page { | 
|  | 388 | struct list_head list; | 
|  | 389 | unsigned int page_no; | 
|  | 390 | unsigned int max_writes; | 
|  | 391 | unsigned int writes_done; | 
|  | 392 | }; | 
|  | 393 |  | 
|  | 394 | static LIST_HEAD(weak_pages); | 
|  | 395 |  | 
|  | 396 | struct grave_page { | 
|  | 397 | struct list_head list; | 
|  | 398 | unsigned int page_no; | 
|  | 399 | unsigned int max_reads; | 
|  | 400 | unsigned int reads_done; | 
|  | 401 | }; | 
|  | 402 |  | 
|  | 403 | static LIST_HEAD(grave_pages); | 
|  | 404 |  | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 405 | static unsigned long *erase_block_wear = NULL; | 
|  | 406 | static unsigned int wear_eb_count = 0; | 
|  | 407 | static unsigned long total_wear = 0; | 
|  | 408 | static unsigned int rptwear_cnt = 0; | 
|  | 409 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 410 | /* MTD structure for NAND controller */ | 
|  | 411 | static struct mtd_info *nsmtd; | 
|  | 412 |  | 
|  | 413 | static u_char ns_verify_buf[NS_LARGEST_PAGE_SIZE]; | 
|  | 414 |  | 
|  | 415 | /* | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 416 | * Allocate array of page pointers and initialize the array to NULL | 
|  | 417 | * pointers. | 
|  | 418 | * | 
|  | 419 | * RETURNS: 0 if success, -ENOMEM if memory alloc fails. | 
|  | 420 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 421 | static int alloc_device(struct nandsim *ns) | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 422 | { | 
|  | 423 | int i; | 
|  | 424 |  | 
|  | 425 | ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem)); | 
|  | 426 | if (!ns->pages) { | 
|  | 427 | NS_ERR("alloc_map: unable to allocate page array\n"); | 
|  | 428 | return -ENOMEM; | 
|  | 429 | } | 
|  | 430 | for (i = 0; i < ns->geom.pgnum; i++) { | 
|  | 431 | ns->pages[i].byte = NULL; | 
|  | 432 | } | 
|  | 433 |  | 
|  | 434 | return 0; | 
|  | 435 | } | 
|  | 436 |  | 
|  | 437 | /* | 
|  | 438 | * Free any allocated pages, and free the array of page pointers. | 
|  | 439 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 440 | static void free_device(struct nandsim *ns) | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 441 | { | 
|  | 442 | int i; | 
|  | 443 |  | 
|  | 444 | if (ns->pages) { | 
|  | 445 | for (i = 0; i < ns->geom.pgnum; i++) { | 
|  | 446 | if (ns->pages[i].byte) | 
|  | 447 | kfree(ns->pages[i].byte); | 
|  | 448 | } | 
|  | 449 | vfree(ns->pages); | 
|  | 450 | } | 
|  | 451 | } | 
|  | 452 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 453 | static char *get_partition_name(int i) | 
|  | 454 | { | 
|  | 455 | char buf[64]; | 
|  | 456 | sprintf(buf, "NAND simulator partition %d", i); | 
|  | 457 | return kstrdup(buf, GFP_KERNEL); | 
|  | 458 | } | 
|  | 459 |  | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 460 | static u_int64_t divide(u_int64_t n, u_int32_t d) | 
|  | 461 | { | 
|  | 462 | do_div(n, d); | 
|  | 463 | return n; | 
|  | 464 | } | 
|  | 465 |  | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 466 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | * Initialize the nandsim structure. | 
|  | 468 | * | 
|  | 469 | * RETURNS: 0 if success, -ERRNO if failure. | 
|  | 470 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 471 | static int init_nandsim(struct mtd_info *mtd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | { | 
|  | 473 | struct nand_chip *chip = (struct nand_chip *)mtd->priv; | 
|  | 474 | struct nandsim   *ns   = (struct nandsim *)(chip->priv); | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 475 | int i, ret = 0; | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 476 | u_int64_t remains; | 
|  | 477 | u_int64_t next_offset; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 |  | 
|  | 479 | if (NS_IS_INITIALIZED(ns)) { | 
|  | 480 | NS_ERR("init_nandsim: nandsim is already initialized\n"); | 
|  | 481 | return -EIO; | 
|  | 482 | } | 
|  | 483 |  | 
|  | 484 | /* Force mtd to not do delays */ | 
|  | 485 | chip->chip_delay = 0; | 
|  | 486 |  | 
|  | 487 | /* Initialize the NAND flash parameters */ | 
|  | 488 | ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8; | 
|  | 489 | ns->geom.totsz    = mtd->size; | 
| Joern Engel | 2831877 | 2006-05-22 23:18:05 +0200 | [diff] [blame] | 490 | ns->geom.pgsz     = mtd->writesize; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | ns->geom.oobsz    = mtd->oobsize; | 
|  | 492 | ns->geom.secsz    = mtd->erasesize; | 
|  | 493 | ns->geom.pgszoob  = ns->geom.pgsz + ns->geom.oobsz; | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 494 | ns->geom.pgnum    = divide(ns->geom.totsz, ns->geom.pgsz); | 
|  | 495 | ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | ns->geom.secshift = ffs(ns->geom.secsz) - 1; | 
|  | 497 | ns->geom.pgshift  = chip->page_shift; | 
|  | 498 | ns->geom.oobshift = ffs(ns->geom.oobsz) - 1; | 
|  | 499 | ns->geom.pgsec    = ns->geom.secsz / ns->geom.pgsz; | 
|  | 500 | ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec; | 
|  | 501 | ns->options = 0; | 
|  | 502 |  | 
|  | 503 | if (ns->geom.pgsz == 256) { | 
|  | 504 | ns->options |= OPT_PAGE256; | 
|  | 505 | } | 
|  | 506 | else if (ns->geom.pgsz == 512) { | 
|  | 507 | ns->options |= (OPT_PAGE512 | OPT_AUTOINCR); | 
|  | 508 | if (ns->busw == 8) | 
|  | 509 | ns->options |= OPT_PAGE512_8BIT; | 
|  | 510 | } else if (ns->geom.pgsz == 2048) { | 
|  | 511 | ns->options |= OPT_PAGE2048; | 
|  | 512 | } else { | 
|  | 513 | NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz); | 
|  | 514 | return -EIO; | 
|  | 515 | } | 
|  | 516 |  | 
|  | 517 | if (ns->options & OPT_SMALLPAGE) { | 
| Adrian Hunter | af3decc | 2008-05-30 15:56:18 +0300 | [diff] [blame] | 518 | if (ns->geom.totsz <= (32 << 20)) { | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | ns->geom.pgaddrbytes  = 3; | 
|  | 520 | ns->geom.secaddrbytes = 2; | 
|  | 521 | } else { | 
|  | 522 | ns->geom.pgaddrbytes  = 4; | 
|  | 523 | ns->geom.secaddrbytes = 3; | 
|  | 524 | } | 
|  | 525 | } else { | 
|  | 526 | if (ns->geom.totsz <= (128 << 20)) { | 
| Artem Bityutskiy | 4a0c50c | 2006-12-06 21:52:32 +0200 | [diff] [blame] | 527 | ns->geom.pgaddrbytes  = 4; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | ns->geom.secaddrbytes = 2; | 
|  | 529 | } else { | 
|  | 530 | ns->geom.pgaddrbytes  = 5; | 
|  | 531 | ns->geom.secaddrbytes = 3; | 
|  | 532 | } | 
|  | 533 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 534 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 535 | /* Fill the partition_info structure */ | 
|  | 536 | if (parts_num > ARRAY_SIZE(ns->partitions)) { | 
|  | 537 | NS_ERR("too many partitions.\n"); | 
|  | 538 | ret = -EINVAL; | 
|  | 539 | goto error; | 
|  | 540 | } | 
|  | 541 | remains = ns->geom.totsz; | 
|  | 542 | next_offset = 0; | 
|  | 543 | for (i = 0; i < parts_num; ++i) { | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 544 | u_int64_t part_sz = (u_int64_t)parts[i] * ns->geom.secsz; | 
|  | 545 |  | 
|  | 546 | if (!part_sz || part_sz > remains) { | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 547 | NS_ERR("bad partition size.\n"); | 
|  | 548 | ret = -EINVAL; | 
|  | 549 | goto error; | 
|  | 550 | } | 
|  | 551 | ns->partitions[i].name   = get_partition_name(i); | 
|  | 552 | ns->partitions[i].offset = next_offset; | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 553 | ns->partitions[i].size   = part_sz; | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 554 | next_offset += ns->partitions[i].size; | 
|  | 555 | remains -= ns->partitions[i].size; | 
|  | 556 | } | 
|  | 557 | ns->nbparts = parts_num; | 
|  | 558 | if (remains) { | 
|  | 559 | if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) { | 
|  | 560 | NS_ERR("too many partitions.\n"); | 
|  | 561 | ret = -EINVAL; | 
|  | 562 | goto error; | 
|  | 563 | } | 
|  | 564 | ns->partitions[i].name   = get_partition_name(i); | 
|  | 565 | ns->partitions[i].offset = next_offset; | 
|  | 566 | ns->partitions[i].size   = remains; | 
|  | 567 | ns->nbparts += 1; | 
|  | 568 | } | 
|  | 569 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | /* Detect how many ID bytes the NAND chip outputs */ | 
|  | 571 | for (i = 0; nand_flash_ids[i].name != NULL; i++) { | 
|  | 572 | if (second_id_byte != nand_flash_ids[i].id) | 
|  | 573 | continue; | 
|  | 574 | if (!(nand_flash_ids[i].options & NAND_NO_AUTOINCR)) | 
|  | 575 | ns->options |= OPT_AUTOINCR; | 
|  | 576 | } | 
|  | 577 |  | 
|  | 578 | if (ns->busw == 16) | 
|  | 579 | NS_WARN("16-bit flashes support wasn't tested\n"); | 
|  | 580 |  | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 581 | printk("flash size: %llu MiB\n",        ns->geom.totsz >> 20); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 582 | printk("page size: %u bytes\n",         ns->geom.pgsz); | 
|  | 583 | printk("OOB area size: %u bytes\n",     ns->geom.oobsz); | 
|  | 584 | printk("sector size: %u KiB\n",         ns->geom.secsz >> 10); | 
|  | 585 | printk("pages number: %u\n",            ns->geom.pgnum); | 
|  | 586 | printk("pages per sector: %u\n",        ns->geom.pgsec); | 
|  | 587 | printk("bus width: %u\n",               ns->busw); | 
|  | 588 | printk("bits in sector size: %u\n",     ns->geom.secshift); | 
|  | 589 | printk("bits in page size: %u\n",       ns->geom.pgshift); | 
|  | 590 | printk("bits in OOB size: %u\n",        ns->geom.oobshift); | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 591 | printk("flash size with OOB: %llu KiB\n", ns->geom.totszoob >> 10); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 592 | printk("page address bytes: %u\n",      ns->geom.pgaddrbytes); | 
|  | 593 | printk("sector address bytes: %u\n",    ns->geom.secaddrbytes); | 
|  | 594 | printk("options: %#x\n",                ns->options); | 
|  | 595 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 596 | if ((ret = alloc_device(ns)) != 0) | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 597 | goto error; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 |  | 
|  | 599 | /* Allocate / initialize the internal buffer */ | 
|  | 600 | ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL); | 
|  | 601 | if (!ns->buf.byte) { | 
|  | 602 | NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n", | 
|  | 603 | ns->geom.pgszoob); | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 604 | ret = -ENOMEM; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | goto error; | 
|  | 606 | } | 
|  | 607 | memset(ns->buf.byte, 0xFF, ns->geom.pgszoob); | 
|  | 608 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | return 0; | 
|  | 610 |  | 
|  | 611 | error: | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 612 | free_device(ns); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 614 | return ret; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | } | 
|  | 616 |  | 
|  | 617 | /* | 
|  | 618 | * Free the nandsim structure. | 
|  | 619 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 620 | static void free_nandsim(struct nandsim *ns) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 621 | { | 
|  | 622 | kfree(ns->buf.byte); | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 623 | free_device(ns); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 |  | 
|  | 625 | return; | 
|  | 626 | } | 
|  | 627 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 628 | static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd) | 
|  | 629 | { | 
|  | 630 | char *w; | 
|  | 631 | int zero_ok; | 
|  | 632 | unsigned int erase_block_no; | 
|  | 633 | loff_t offset; | 
|  | 634 |  | 
|  | 635 | if (!badblocks) | 
|  | 636 | return 0; | 
|  | 637 | w = badblocks; | 
|  | 638 | do { | 
|  | 639 | zero_ok = (*w == '0' ? 1 : 0); | 
|  | 640 | erase_block_no = simple_strtoul(w, &w, 0); | 
|  | 641 | if (!zero_ok && !erase_block_no) { | 
|  | 642 | NS_ERR("invalid badblocks.\n"); | 
|  | 643 | return -EINVAL; | 
|  | 644 | } | 
|  | 645 | offset = erase_block_no * ns->geom.secsz; | 
|  | 646 | if (mtd->block_markbad(mtd, offset)) { | 
|  | 647 | NS_ERR("invalid badblocks.\n"); | 
|  | 648 | return -EINVAL; | 
|  | 649 | } | 
|  | 650 | if (*w == ',') | 
|  | 651 | w += 1; | 
|  | 652 | } while (*w); | 
|  | 653 | return 0; | 
|  | 654 | } | 
|  | 655 |  | 
|  | 656 | static int parse_weakblocks(void) | 
|  | 657 | { | 
|  | 658 | char *w; | 
|  | 659 | int zero_ok; | 
|  | 660 | unsigned int erase_block_no; | 
|  | 661 | unsigned int max_erases; | 
|  | 662 | struct weak_block *wb; | 
|  | 663 |  | 
|  | 664 | if (!weakblocks) | 
|  | 665 | return 0; | 
|  | 666 | w = weakblocks; | 
|  | 667 | do { | 
|  | 668 | zero_ok = (*w == '0' ? 1 : 0); | 
|  | 669 | erase_block_no = simple_strtoul(w, &w, 0); | 
|  | 670 | if (!zero_ok && !erase_block_no) { | 
|  | 671 | NS_ERR("invalid weakblocks.\n"); | 
|  | 672 | return -EINVAL; | 
|  | 673 | } | 
|  | 674 | max_erases = 3; | 
|  | 675 | if (*w == ':') { | 
|  | 676 | w += 1; | 
|  | 677 | max_erases = simple_strtoul(w, &w, 0); | 
|  | 678 | } | 
|  | 679 | if (*w == ',') | 
|  | 680 | w += 1; | 
|  | 681 | wb = kzalloc(sizeof(*wb), GFP_KERNEL); | 
|  | 682 | if (!wb) { | 
|  | 683 | NS_ERR("unable to allocate memory.\n"); | 
|  | 684 | return -ENOMEM; | 
|  | 685 | } | 
|  | 686 | wb->erase_block_no = erase_block_no; | 
|  | 687 | wb->max_erases = max_erases; | 
|  | 688 | list_add(&wb->list, &weak_blocks); | 
|  | 689 | } while (*w); | 
|  | 690 | return 0; | 
|  | 691 | } | 
|  | 692 |  | 
|  | 693 | static int erase_error(unsigned int erase_block_no) | 
|  | 694 | { | 
|  | 695 | struct weak_block *wb; | 
|  | 696 |  | 
|  | 697 | list_for_each_entry(wb, &weak_blocks, list) | 
|  | 698 | if (wb->erase_block_no == erase_block_no) { | 
|  | 699 | if (wb->erases_done >= wb->max_erases) | 
|  | 700 | return 1; | 
|  | 701 | wb->erases_done += 1; | 
|  | 702 | return 0; | 
|  | 703 | } | 
|  | 704 | return 0; | 
|  | 705 | } | 
|  | 706 |  | 
|  | 707 | static int parse_weakpages(void) | 
|  | 708 | { | 
|  | 709 | char *w; | 
|  | 710 | int zero_ok; | 
|  | 711 | unsigned int page_no; | 
|  | 712 | unsigned int max_writes; | 
|  | 713 | struct weak_page *wp; | 
|  | 714 |  | 
|  | 715 | if (!weakpages) | 
|  | 716 | return 0; | 
|  | 717 | w = weakpages; | 
|  | 718 | do { | 
|  | 719 | zero_ok = (*w == '0' ? 1 : 0); | 
|  | 720 | page_no = simple_strtoul(w, &w, 0); | 
|  | 721 | if (!zero_ok && !page_no) { | 
|  | 722 | NS_ERR("invalid weakpagess.\n"); | 
|  | 723 | return -EINVAL; | 
|  | 724 | } | 
|  | 725 | max_writes = 3; | 
|  | 726 | if (*w == ':') { | 
|  | 727 | w += 1; | 
|  | 728 | max_writes = simple_strtoul(w, &w, 0); | 
|  | 729 | } | 
|  | 730 | if (*w == ',') | 
|  | 731 | w += 1; | 
|  | 732 | wp = kzalloc(sizeof(*wp), GFP_KERNEL); | 
|  | 733 | if (!wp) { | 
|  | 734 | NS_ERR("unable to allocate memory.\n"); | 
|  | 735 | return -ENOMEM; | 
|  | 736 | } | 
|  | 737 | wp->page_no = page_no; | 
|  | 738 | wp->max_writes = max_writes; | 
|  | 739 | list_add(&wp->list, &weak_pages); | 
|  | 740 | } while (*w); | 
|  | 741 | return 0; | 
|  | 742 | } | 
|  | 743 |  | 
|  | 744 | static int write_error(unsigned int page_no) | 
|  | 745 | { | 
|  | 746 | struct weak_page *wp; | 
|  | 747 |  | 
|  | 748 | list_for_each_entry(wp, &weak_pages, list) | 
|  | 749 | if (wp->page_no == page_no) { | 
|  | 750 | if (wp->writes_done >= wp->max_writes) | 
|  | 751 | return 1; | 
|  | 752 | wp->writes_done += 1; | 
|  | 753 | return 0; | 
|  | 754 | } | 
|  | 755 | return 0; | 
|  | 756 | } | 
|  | 757 |  | 
|  | 758 | static int parse_gravepages(void) | 
|  | 759 | { | 
|  | 760 | char *g; | 
|  | 761 | int zero_ok; | 
|  | 762 | unsigned int page_no; | 
|  | 763 | unsigned int max_reads; | 
|  | 764 | struct grave_page *gp; | 
|  | 765 |  | 
|  | 766 | if (!gravepages) | 
|  | 767 | return 0; | 
|  | 768 | g = gravepages; | 
|  | 769 | do { | 
|  | 770 | zero_ok = (*g == '0' ? 1 : 0); | 
|  | 771 | page_no = simple_strtoul(g, &g, 0); | 
|  | 772 | if (!zero_ok && !page_no) { | 
|  | 773 | NS_ERR("invalid gravepagess.\n"); | 
|  | 774 | return -EINVAL; | 
|  | 775 | } | 
|  | 776 | max_reads = 3; | 
|  | 777 | if (*g == ':') { | 
|  | 778 | g += 1; | 
|  | 779 | max_reads = simple_strtoul(g, &g, 0); | 
|  | 780 | } | 
|  | 781 | if (*g == ',') | 
|  | 782 | g += 1; | 
|  | 783 | gp = kzalloc(sizeof(*gp), GFP_KERNEL); | 
|  | 784 | if (!gp) { | 
|  | 785 | NS_ERR("unable to allocate memory.\n"); | 
|  | 786 | return -ENOMEM; | 
|  | 787 | } | 
|  | 788 | gp->page_no = page_no; | 
|  | 789 | gp->max_reads = max_reads; | 
|  | 790 | list_add(&gp->list, &grave_pages); | 
|  | 791 | } while (*g); | 
|  | 792 | return 0; | 
|  | 793 | } | 
|  | 794 |  | 
|  | 795 | static int read_error(unsigned int page_no) | 
|  | 796 | { | 
|  | 797 | struct grave_page *gp; | 
|  | 798 |  | 
|  | 799 | list_for_each_entry(gp, &grave_pages, list) | 
|  | 800 | if (gp->page_no == page_no) { | 
|  | 801 | if (gp->reads_done >= gp->max_reads) | 
|  | 802 | return 1; | 
|  | 803 | gp->reads_done += 1; | 
|  | 804 | return 0; | 
|  | 805 | } | 
|  | 806 | return 0; | 
|  | 807 | } | 
|  | 808 |  | 
|  | 809 | static void free_lists(void) | 
|  | 810 | { | 
|  | 811 | struct list_head *pos, *n; | 
|  | 812 | list_for_each_safe(pos, n, &weak_blocks) { | 
|  | 813 | list_del(pos); | 
|  | 814 | kfree(list_entry(pos, struct weak_block, list)); | 
|  | 815 | } | 
|  | 816 | list_for_each_safe(pos, n, &weak_pages) { | 
|  | 817 | list_del(pos); | 
|  | 818 | kfree(list_entry(pos, struct weak_page, list)); | 
|  | 819 | } | 
|  | 820 | list_for_each_safe(pos, n, &grave_pages) { | 
|  | 821 | list_del(pos); | 
|  | 822 | kfree(list_entry(pos, struct grave_page, list)); | 
|  | 823 | } | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 824 | kfree(erase_block_wear); | 
|  | 825 | } | 
|  | 826 |  | 
|  | 827 | static int setup_wear_reporting(struct mtd_info *mtd) | 
|  | 828 | { | 
|  | 829 | size_t mem; | 
|  | 830 |  | 
|  | 831 | if (!rptwear) | 
|  | 832 | return 0; | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 833 | wear_eb_count = divide(mtd->size, mtd->erasesize); | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 834 | mem = wear_eb_count * sizeof(unsigned long); | 
|  | 835 | if (mem / sizeof(unsigned long) != wear_eb_count) { | 
|  | 836 | NS_ERR("Too many erase blocks for wear reporting\n"); | 
|  | 837 | return -ENOMEM; | 
|  | 838 | } | 
|  | 839 | erase_block_wear = kzalloc(mem, GFP_KERNEL); | 
|  | 840 | if (!erase_block_wear) { | 
|  | 841 | NS_ERR("Too many erase blocks for wear reporting\n"); | 
|  | 842 | return -ENOMEM; | 
|  | 843 | } | 
|  | 844 | return 0; | 
|  | 845 | } | 
|  | 846 |  | 
|  | 847 | static void update_wear(unsigned int erase_block_no) | 
|  | 848 | { | 
|  | 849 | unsigned long wmin = -1, wmax = 0, avg; | 
|  | 850 | unsigned long deciles[10], decile_max[10], tot = 0; | 
|  | 851 | unsigned int i; | 
|  | 852 |  | 
|  | 853 | if (!erase_block_wear) | 
|  | 854 | return; | 
|  | 855 | total_wear += 1; | 
|  | 856 | if (total_wear == 0) | 
|  | 857 | NS_ERR("Erase counter total overflow\n"); | 
|  | 858 | erase_block_wear[erase_block_no] += 1; | 
|  | 859 | if (erase_block_wear[erase_block_no] == 0) | 
|  | 860 | NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no); | 
|  | 861 | rptwear_cnt += 1; | 
|  | 862 | if (rptwear_cnt < rptwear) | 
|  | 863 | return; | 
|  | 864 | rptwear_cnt = 0; | 
|  | 865 | /* Calc wear stats */ | 
|  | 866 | for (i = 0; i < wear_eb_count; ++i) { | 
|  | 867 | unsigned long wear = erase_block_wear[i]; | 
|  | 868 | if (wear < wmin) | 
|  | 869 | wmin = wear; | 
|  | 870 | if (wear > wmax) | 
|  | 871 | wmax = wear; | 
|  | 872 | tot += wear; | 
|  | 873 | } | 
|  | 874 | for (i = 0; i < 9; ++i) { | 
|  | 875 | deciles[i] = 0; | 
|  | 876 | decile_max[i] = (wmax * (i + 1) + 5) / 10; | 
|  | 877 | } | 
|  | 878 | deciles[9] = 0; | 
|  | 879 | decile_max[9] = wmax; | 
|  | 880 | for (i = 0; i < wear_eb_count; ++i) { | 
|  | 881 | int d; | 
|  | 882 | unsigned long wear = erase_block_wear[i]; | 
|  | 883 | for (d = 0; d < 10; ++d) | 
|  | 884 | if (wear <= decile_max[d]) { | 
|  | 885 | deciles[d] += 1; | 
|  | 886 | break; | 
|  | 887 | } | 
|  | 888 | } | 
|  | 889 | avg = tot / wear_eb_count; | 
|  | 890 | /* Output wear report */ | 
|  | 891 | NS_INFO("*** Wear Report ***\n"); | 
|  | 892 | NS_INFO("Total numbers of erases:  %lu\n", tot); | 
|  | 893 | NS_INFO("Number of erase blocks:   %u\n", wear_eb_count); | 
|  | 894 | NS_INFO("Average number of erases: %lu\n", avg); | 
|  | 895 | NS_INFO("Maximum number of erases: %lu\n", wmax); | 
|  | 896 | NS_INFO("Minimum number of erases: %lu\n", wmin); | 
|  | 897 | for (i = 0; i < 10; ++i) { | 
|  | 898 | unsigned long from = (i ? decile_max[i - 1] + 1 : 0); | 
|  | 899 | if (from > decile_max[i]) | 
|  | 900 | continue; | 
|  | 901 | NS_INFO("Number of ebs with erase counts from %lu to %lu : %lu\n", | 
|  | 902 | from, | 
|  | 903 | decile_max[i], | 
|  | 904 | deciles[i]); | 
|  | 905 | } | 
|  | 906 | NS_INFO("*** End of Wear Report ***\n"); | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 907 | } | 
|  | 908 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 909 | /* | 
|  | 910 | * Returns the string representation of 'state' state. | 
|  | 911 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 912 | static char *get_state_name(uint32_t state) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 913 | { | 
|  | 914 | switch (NS_STATE(state)) { | 
|  | 915 | case STATE_CMD_READ0: | 
|  | 916 | return "STATE_CMD_READ0"; | 
|  | 917 | case STATE_CMD_READ1: | 
|  | 918 | return "STATE_CMD_READ1"; | 
|  | 919 | case STATE_CMD_PAGEPROG: | 
|  | 920 | return "STATE_CMD_PAGEPROG"; | 
|  | 921 | case STATE_CMD_READOOB: | 
|  | 922 | return "STATE_CMD_READOOB"; | 
|  | 923 | case STATE_CMD_READSTART: | 
|  | 924 | return "STATE_CMD_READSTART"; | 
|  | 925 | case STATE_CMD_ERASE1: | 
|  | 926 | return "STATE_CMD_ERASE1"; | 
|  | 927 | case STATE_CMD_STATUS: | 
|  | 928 | return "STATE_CMD_STATUS"; | 
|  | 929 | case STATE_CMD_STATUS_M: | 
|  | 930 | return "STATE_CMD_STATUS_M"; | 
|  | 931 | case STATE_CMD_SEQIN: | 
|  | 932 | return "STATE_CMD_SEQIN"; | 
|  | 933 | case STATE_CMD_READID: | 
|  | 934 | return "STATE_CMD_READID"; | 
|  | 935 | case STATE_CMD_ERASE2: | 
|  | 936 | return "STATE_CMD_ERASE2"; | 
|  | 937 | case STATE_CMD_RESET: | 
|  | 938 | return "STATE_CMD_RESET"; | 
|  | 939 | case STATE_ADDR_PAGE: | 
|  | 940 | return "STATE_ADDR_PAGE"; | 
|  | 941 | case STATE_ADDR_SEC: | 
|  | 942 | return "STATE_ADDR_SEC"; | 
|  | 943 | case STATE_ADDR_ZERO: | 
|  | 944 | return "STATE_ADDR_ZERO"; | 
|  | 945 | case STATE_DATAIN: | 
|  | 946 | return "STATE_DATAIN"; | 
|  | 947 | case STATE_DATAOUT: | 
|  | 948 | return "STATE_DATAOUT"; | 
|  | 949 | case STATE_DATAOUT_ID: | 
|  | 950 | return "STATE_DATAOUT_ID"; | 
|  | 951 | case STATE_DATAOUT_STATUS: | 
|  | 952 | return "STATE_DATAOUT_STATUS"; | 
|  | 953 | case STATE_DATAOUT_STATUS_M: | 
|  | 954 | return "STATE_DATAOUT_STATUS_M"; | 
|  | 955 | case STATE_READY: | 
|  | 956 | return "STATE_READY"; | 
|  | 957 | case STATE_UNKNOWN: | 
|  | 958 | return "STATE_UNKNOWN"; | 
|  | 959 | } | 
|  | 960 |  | 
|  | 961 | NS_ERR("get_state_name: unknown state, BUG\n"); | 
|  | 962 | return NULL; | 
|  | 963 | } | 
|  | 964 |  | 
|  | 965 | /* | 
|  | 966 | * Check if command is valid. | 
|  | 967 | * | 
|  | 968 | * RETURNS: 1 if wrong command, 0 if right. | 
|  | 969 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 970 | static int check_command(int cmd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 971 | { | 
|  | 972 | switch (cmd) { | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 973 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 974 | case NAND_CMD_READ0: | 
|  | 975 | case NAND_CMD_READSTART: | 
|  | 976 | case NAND_CMD_PAGEPROG: | 
|  | 977 | case NAND_CMD_READOOB: | 
|  | 978 | case NAND_CMD_ERASE1: | 
|  | 979 | case NAND_CMD_STATUS: | 
|  | 980 | case NAND_CMD_SEQIN: | 
|  | 981 | case NAND_CMD_READID: | 
|  | 982 | case NAND_CMD_ERASE2: | 
|  | 983 | case NAND_CMD_RESET: | 
|  | 984 | case NAND_CMD_READ1: | 
|  | 985 | return 0; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 986 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 987 | case NAND_CMD_STATUS_MULTI: | 
|  | 988 | default: | 
|  | 989 | return 1; | 
|  | 990 | } | 
|  | 991 | } | 
|  | 992 |  | 
|  | 993 | /* | 
|  | 994 | * Returns state after command is accepted by command number. | 
|  | 995 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 996 | static uint32_t get_state_by_command(unsigned command) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | { | 
|  | 998 | switch (command) { | 
|  | 999 | case NAND_CMD_READ0: | 
|  | 1000 | return STATE_CMD_READ0; | 
|  | 1001 | case NAND_CMD_READ1: | 
|  | 1002 | return STATE_CMD_READ1; | 
|  | 1003 | case NAND_CMD_PAGEPROG: | 
|  | 1004 | return STATE_CMD_PAGEPROG; | 
|  | 1005 | case NAND_CMD_READSTART: | 
|  | 1006 | return STATE_CMD_READSTART; | 
|  | 1007 | case NAND_CMD_READOOB: | 
|  | 1008 | return STATE_CMD_READOOB; | 
|  | 1009 | case NAND_CMD_ERASE1: | 
|  | 1010 | return STATE_CMD_ERASE1; | 
|  | 1011 | case NAND_CMD_STATUS: | 
|  | 1012 | return STATE_CMD_STATUS; | 
|  | 1013 | case NAND_CMD_STATUS_MULTI: | 
|  | 1014 | return STATE_CMD_STATUS_M; | 
|  | 1015 | case NAND_CMD_SEQIN: | 
|  | 1016 | return STATE_CMD_SEQIN; | 
|  | 1017 | case NAND_CMD_READID: | 
|  | 1018 | return STATE_CMD_READID; | 
|  | 1019 | case NAND_CMD_ERASE2: | 
|  | 1020 | return STATE_CMD_ERASE2; | 
|  | 1021 | case NAND_CMD_RESET: | 
|  | 1022 | return STATE_CMD_RESET; | 
|  | 1023 | } | 
|  | 1024 |  | 
|  | 1025 | NS_ERR("get_state_by_command: unknown command, BUG\n"); | 
|  | 1026 | return 0; | 
|  | 1027 | } | 
|  | 1028 |  | 
|  | 1029 | /* | 
|  | 1030 | * Move an address byte to the correspondent internal register. | 
|  | 1031 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1032 | static inline void accept_addr_byte(struct nandsim *ns, u_char bt) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1033 | { | 
|  | 1034 | uint byte = (uint)bt; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1035 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | 
|  | 1037 | ns->regs.column |= (byte << 8 * ns->regs.count); | 
|  | 1038 | else { | 
|  | 1039 | ns->regs.row |= (byte << 8 * (ns->regs.count - | 
|  | 1040 | ns->geom.pgaddrbytes + | 
|  | 1041 | ns->geom.secaddrbytes)); | 
|  | 1042 | } | 
|  | 1043 |  | 
|  | 1044 | return; | 
|  | 1045 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1046 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | /* | 
|  | 1048 | * Switch to STATE_READY state. | 
|  | 1049 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1050 | static inline void switch_to_ready_state(struct nandsim *ns, u_char status) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1051 | { | 
|  | 1052 | NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY)); | 
|  | 1053 |  | 
|  | 1054 | ns->state       = STATE_READY; | 
|  | 1055 | ns->nxstate     = STATE_UNKNOWN; | 
|  | 1056 | ns->op          = NULL; | 
|  | 1057 | ns->npstates    = 0; | 
|  | 1058 | ns->stateidx    = 0; | 
|  | 1059 | ns->regs.num    = 0; | 
|  | 1060 | ns->regs.count  = 0; | 
|  | 1061 | ns->regs.off    = 0; | 
|  | 1062 | ns->regs.row    = 0; | 
|  | 1063 | ns->regs.column = 0; | 
|  | 1064 | ns->regs.status = status; | 
|  | 1065 | } | 
|  | 1066 |  | 
|  | 1067 | /* | 
|  | 1068 | * If the operation isn't known yet, try to find it in the global array | 
|  | 1069 | * of supported operations. | 
|  | 1070 | * | 
|  | 1071 | * Operation can be unknown because of the following. | 
|  | 1072 | *   1. New command was accepted and this is the firs call to find the | 
|  | 1073 | *      correspondent states chain. In this case ns->npstates = 0; | 
|  | 1074 | *   2. There is several operations which begin with the same command(s) | 
|  | 1075 | *      (for example program from the second half and read from the | 
|  | 1076 | *      second half operations both begin with the READ1 command). In this | 
|  | 1077 | *      case the ns->pstates[] array contains previous states. | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1078 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1079 | * Thus, the function tries to find operation containing the following | 
|  | 1080 | * states (if the 'flag' parameter is 0): | 
|  | 1081 | *    ns->pstates[0], ... ns->pstates[ns->npstates], ns->state | 
|  | 1082 | * | 
|  | 1083 | * If (one and only one) matching operation is found, it is accepted ( | 
|  | 1084 | * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is | 
|  | 1085 | * zeroed). | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1086 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | * If there are several maches, the current state is pushed to the | 
|  | 1088 | * ns->pstates. | 
|  | 1089 | * | 
|  | 1090 | * The operation can be unknown only while commands are input to the chip. | 
|  | 1091 | * As soon as address command is accepted, the operation must be known. | 
|  | 1092 | * In such situation the function is called with 'flag' != 0, and the | 
|  | 1093 | * operation is searched using the following pattern: | 
|  | 1094 | *     ns->pstates[0], ... ns->pstates[ns->npstates], <address input> | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1095 | * | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1096 | * It is supposed that this pattern must either match one operation on | 
|  | 1097 | * none. There can't be ambiguity in that case. | 
|  | 1098 | * | 
|  | 1099 | * If no matches found, the functions does the following: | 
|  | 1100 | *   1. if there are saved states present, try to ignore them and search | 
|  | 1101 | *      again only using the last command. If nothing was found, switch | 
|  | 1102 | *      to the STATE_READY state. | 
|  | 1103 | *   2. if there are no saved states, switch to the STATE_READY state. | 
|  | 1104 | * | 
|  | 1105 | * RETURNS: -2 - no matched operations found. | 
|  | 1106 | *          -1 - several matches. | 
|  | 1107 | *           0 - operation is found. | 
|  | 1108 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1109 | static int find_operation(struct nandsim *ns, uint32_t flag) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1110 | { | 
|  | 1111 | int opsfound = 0; | 
|  | 1112 | int i, j, idx = 0; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1113 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1114 | for (i = 0; i < NS_OPER_NUM; i++) { | 
|  | 1115 |  | 
|  | 1116 | int found = 1; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1117 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | if (!(ns->options & ops[i].reqopts)) | 
|  | 1119 | /* Ignore operations we can't perform */ | 
|  | 1120 | continue; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1121 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1122 | if (flag) { | 
|  | 1123 | if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK)) | 
|  | 1124 | continue; | 
|  | 1125 | } else { | 
|  | 1126 | if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates])) | 
|  | 1127 | continue; | 
|  | 1128 | } | 
|  | 1129 |  | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1130 | for (j = 0; j < ns->npstates; j++) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1131 | if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j]) | 
|  | 1132 | && (ns->options & ops[idx].reqopts)) { | 
|  | 1133 | found = 0; | 
|  | 1134 | break; | 
|  | 1135 | } | 
|  | 1136 |  | 
|  | 1137 | if (found) { | 
|  | 1138 | idx = i; | 
|  | 1139 | opsfound += 1; | 
|  | 1140 | } | 
|  | 1141 | } | 
|  | 1142 |  | 
|  | 1143 | if (opsfound == 1) { | 
|  | 1144 | /* Exact match */ | 
|  | 1145 | ns->op = &ops[idx].states[0]; | 
|  | 1146 | if (flag) { | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1147 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1148 | * In this case the find_operation function was | 
|  | 1149 | * called when address has just began input. But it isn't | 
|  | 1150 | * yet fully input and the current state must | 
|  | 1151 | * not be one of STATE_ADDR_*, but the STATE_ADDR_* | 
|  | 1152 | * state must be the next state (ns->nxstate). | 
|  | 1153 | */ | 
|  | 1154 | ns->stateidx = ns->npstates - 1; | 
|  | 1155 | } else { | 
|  | 1156 | ns->stateidx = ns->npstates; | 
|  | 1157 | } | 
|  | 1158 | ns->npstates = 0; | 
|  | 1159 | ns->state = ns->op[ns->stateidx]; | 
|  | 1160 | ns->nxstate = ns->op[ns->stateidx + 1]; | 
|  | 1161 | NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n", | 
|  | 1162 | idx, get_state_name(ns->state), get_state_name(ns->nxstate)); | 
|  | 1163 | return 0; | 
|  | 1164 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1165 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1166 | if (opsfound == 0) { | 
|  | 1167 | /* Nothing was found. Try to ignore previous commands (if any) and search again */ | 
|  | 1168 | if (ns->npstates != 0) { | 
|  | 1169 | NS_DBG("find_operation: no operation found, try again with state %s\n", | 
|  | 1170 | get_state_name(ns->state)); | 
|  | 1171 | ns->npstates = 0; | 
|  | 1172 | return find_operation(ns, 0); | 
|  | 1173 |  | 
|  | 1174 | } | 
|  | 1175 | NS_DBG("find_operation: no operations found\n"); | 
|  | 1176 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1177 | return -2; | 
|  | 1178 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1179 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1180 | if (flag) { | 
|  | 1181 | /* This shouldn't happen */ | 
|  | 1182 | NS_DBG("find_operation: BUG, operation must be known if address is input\n"); | 
|  | 1183 | return -2; | 
|  | 1184 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1185 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1186 | NS_DBG("find_operation: there is still ambiguity\n"); | 
|  | 1187 |  | 
|  | 1188 | ns->pstates[ns->npstates++] = ns->state; | 
|  | 1189 |  | 
|  | 1190 | return -1; | 
|  | 1191 | } | 
|  | 1192 |  | 
|  | 1193 | /* | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1194 | * Returns a pointer to the current page. | 
|  | 1195 | */ | 
|  | 1196 | static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns) | 
|  | 1197 | { | 
|  | 1198 | return &(ns->pages[ns->regs.row]); | 
|  | 1199 | } | 
|  | 1200 |  | 
|  | 1201 | /* | 
|  | 1202 | * Retuns a pointer to the current byte, within the current page. | 
|  | 1203 | */ | 
|  | 1204 | static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns) | 
|  | 1205 | { | 
|  | 1206 | return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off; | 
|  | 1207 | } | 
|  | 1208 |  | 
|  | 1209 | /* | 
|  | 1210 | * Fill the NAND buffer with data read from the specified page. | 
|  | 1211 | */ | 
|  | 1212 | static void read_page(struct nandsim *ns, int num) | 
|  | 1213 | { | 
|  | 1214 | union ns_mem *mypage; | 
|  | 1215 |  | 
|  | 1216 | mypage = NS_GET_PAGE(ns); | 
|  | 1217 | if (mypage->byte == NULL) { | 
|  | 1218 | NS_DBG("read_page: page %d not allocated\n", ns->regs.row); | 
|  | 1219 | memset(ns->buf.byte, 0xFF, num); | 
|  | 1220 | } else { | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1221 | unsigned int page_no = ns->regs.row; | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1222 | NS_DBG("read_page: page %d allocated, reading from %d\n", | 
|  | 1223 | ns->regs.row, ns->regs.column + ns->regs.off); | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1224 | if (read_error(page_no)) { | 
|  | 1225 | int i; | 
|  | 1226 | memset(ns->buf.byte, 0xFF, num); | 
|  | 1227 | for (i = 0; i < num; ++i) | 
|  | 1228 | ns->buf.byte[i] = random32(); | 
|  | 1229 | NS_WARN("simulating read error in page %u\n", page_no); | 
|  | 1230 | return; | 
|  | 1231 | } | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1232 | memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num); | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1233 | if (bitflips && random32() < (1 << 22)) { | 
|  | 1234 | int flips = 1; | 
|  | 1235 | if (bitflips > 1) | 
|  | 1236 | flips = (random32() % (int) bitflips) + 1; | 
|  | 1237 | while (flips--) { | 
|  | 1238 | int pos = random32() % (num * 8); | 
|  | 1239 | ns->buf.byte[pos / 8] ^= (1 << (pos % 8)); | 
|  | 1240 | NS_WARN("read_page: flipping bit %d in page %d " | 
|  | 1241 | "reading from %d ecc: corrected=%u failed=%u\n", | 
|  | 1242 | pos, ns->regs.row, ns->regs.column + ns->regs.off, | 
|  | 1243 | nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed); | 
|  | 1244 | } | 
|  | 1245 | } | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1246 | } | 
|  | 1247 | } | 
|  | 1248 |  | 
|  | 1249 | /* | 
|  | 1250 | * Erase all pages in the specified sector. | 
|  | 1251 | */ | 
|  | 1252 | static void erase_sector(struct nandsim *ns) | 
|  | 1253 | { | 
|  | 1254 | union ns_mem *mypage; | 
|  | 1255 | int i; | 
|  | 1256 |  | 
|  | 1257 | mypage = NS_GET_PAGE(ns); | 
|  | 1258 | for (i = 0; i < ns->geom.pgsec; i++) { | 
|  | 1259 | if (mypage->byte != NULL) { | 
|  | 1260 | NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i); | 
|  | 1261 | kfree(mypage->byte); | 
|  | 1262 | mypage->byte = NULL; | 
|  | 1263 | } | 
|  | 1264 | mypage++; | 
|  | 1265 | } | 
|  | 1266 | } | 
|  | 1267 |  | 
|  | 1268 | /* | 
|  | 1269 | * Program the specified page with the contents from the NAND buffer. | 
|  | 1270 | */ | 
|  | 1271 | static int prog_page(struct nandsim *ns, int num) | 
|  | 1272 | { | 
| Artem Bityutskiy | 82810b7b6 | 2006-10-20 11:23:56 +0300 | [diff] [blame] | 1273 | int i; | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1274 | union ns_mem *mypage; | 
|  | 1275 | u_char *pg_off; | 
|  | 1276 |  | 
|  | 1277 | mypage = NS_GET_PAGE(ns); | 
|  | 1278 | if (mypage->byte == NULL) { | 
|  | 1279 | NS_DBG("prog_page: allocating page %d\n", ns->regs.row); | 
| Artem Bityutskiy | 98b830d | 2007-08-28 20:33:32 +0300 | [diff] [blame] | 1280 | /* | 
|  | 1281 | * We allocate memory with GFP_NOFS because a flash FS may | 
|  | 1282 | * utilize this. If it is holding an FS lock, then gets here, | 
|  | 1283 | * then kmalloc runs writeback which goes to the FS again | 
|  | 1284 | * and deadlocks. This was seen in practice. | 
|  | 1285 | */ | 
|  | 1286 | mypage->byte = kmalloc(ns->geom.pgszoob, GFP_NOFS); | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1287 | if (mypage->byte == NULL) { | 
|  | 1288 | NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row); | 
|  | 1289 | return -1; | 
|  | 1290 | } | 
|  | 1291 | memset(mypage->byte, 0xFF, ns->geom.pgszoob); | 
|  | 1292 | } | 
|  | 1293 |  | 
|  | 1294 | pg_off = NS_PAGE_BYTE_OFF(ns); | 
| Artem Bityutskiy | 82810b7b6 | 2006-10-20 11:23:56 +0300 | [diff] [blame] | 1295 | for (i = 0; i < num; i++) | 
|  | 1296 | pg_off[i] &= ns->buf.byte[i]; | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1297 |  | 
|  | 1298 | return 0; | 
|  | 1299 | } | 
|  | 1300 |  | 
|  | 1301 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | * If state has any action bit, perform this action. | 
|  | 1303 | * | 
|  | 1304 | * RETURNS: 0 if success, -1 if error. | 
|  | 1305 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1306 | static int do_state_action(struct nandsim *ns, uint32_t action) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | { | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1308 | int num; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | int busdiv = ns->busw == 8 ? 1 : 2; | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1310 | unsigned int erase_block_no, page_no; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1311 |  | 
|  | 1312 | action &= ACTION_MASK; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1313 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1314 | /* Check that page address input is correct */ | 
|  | 1315 | if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) { | 
|  | 1316 | NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row); | 
|  | 1317 | return -1; | 
|  | 1318 | } | 
|  | 1319 |  | 
|  | 1320 | switch (action) { | 
|  | 1321 |  | 
|  | 1322 | case ACTION_CPY: | 
|  | 1323 | /* | 
|  | 1324 | * Copy page data to the internal buffer. | 
|  | 1325 | */ | 
|  | 1326 |  | 
|  | 1327 | /* Column shouldn't be very large */ | 
|  | 1328 | if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) { | 
|  | 1329 | NS_ERR("do_state_action: column number is too large\n"); | 
|  | 1330 | break; | 
|  | 1331 | } | 
|  | 1332 | num = ns->geom.pgszoob - ns->regs.off - ns->regs.column; | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1333 | read_page(ns, num); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1334 |  | 
|  | 1335 | NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n", | 
|  | 1336 | num, NS_RAW_OFFSET(ns) + ns->regs.off); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1337 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1338 | if (ns->regs.off == 0) | 
|  | 1339 | NS_LOG("read page %d\n", ns->regs.row); | 
|  | 1340 | else if (ns->regs.off < ns->geom.pgsz) | 
|  | 1341 | NS_LOG("read page %d (second half)\n", ns->regs.row); | 
|  | 1342 | else | 
|  | 1343 | NS_LOG("read OOB of page %d\n", ns->regs.row); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1344 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1345 | NS_UDELAY(access_delay); | 
|  | 1346 | NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv); | 
|  | 1347 |  | 
|  | 1348 | break; | 
|  | 1349 |  | 
|  | 1350 | case ACTION_SECERASE: | 
|  | 1351 | /* | 
|  | 1352 | * Erase sector. | 
|  | 1353 | */ | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1354 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1355 | if (ns->lines.wp) { | 
|  | 1356 | NS_ERR("do_state_action: device is write-protected, ignore sector erase\n"); | 
|  | 1357 | return -1; | 
|  | 1358 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1359 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1360 | if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec | 
|  | 1361 | || (ns->regs.row & ~(ns->geom.secsz - 1))) { | 
|  | 1362 | NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row); | 
|  | 1363 | return -1; | 
|  | 1364 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1365 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | ns->regs.row = (ns->regs.row << | 
|  | 1367 | 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column; | 
|  | 1368 | ns->regs.column = 0; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1369 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1370 | erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift); | 
|  | 1371 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1372 | NS_DBG("do_state_action: erase sector at address %#x, off = %d\n", | 
|  | 1373 | ns->regs.row, NS_RAW_OFFSET(ns)); | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1374 | NS_LOG("erase sector %u\n", erase_block_no); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 |  | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1376 | erase_sector(ns); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1377 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1378 | NS_MDELAY(erase_delay); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1379 |  | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 1380 | if (erase_block_wear) | 
|  | 1381 | update_wear(erase_block_no); | 
|  | 1382 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1383 | if (erase_error(erase_block_no)) { | 
|  | 1384 | NS_WARN("simulating erase failure in erase block %u\n", erase_block_no); | 
|  | 1385 | return -1; | 
|  | 1386 | } | 
|  | 1387 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1388 | break; | 
|  | 1389 |  | 
|  | 1390 | case ACTION_PRGPAGE: | 
|  | 1391 | /* | 
|  | 1392 | * Programm page - move internal buffer data to the page. | 
|  | 1393 | */ | 
|  | 1394 |  | 
|  | 1395 | if (ns->lines.wp) { | 
|  | 1396 | NS_WARN("do_state_action: device is write-protected, programm\n"); | 
|  | 1397 | return -1; | 
|  | 1398 | } | 
|  | 1399 |  | 
|  | 1400 | num = ns->geom.pgszoob - ns->regs.off - ns->regs.column; | 
|  | 1401 | if (num != ns->regs.count) { | 
|  | 1402 | NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n", | 
|  | 1403 | ns->regs.count, num); | 
|  | 1404 | return -1; | 
|  | 1405 | } | 
|  | 1406 |  | 
| Vijay Kumar | d086d43 | 2006-10-08 22:02:31 +0530 | [diff] [blame] | 1407 | if (prog_page(ns, num) == -1) | 
|  | 1408 | return -1; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1410 | page_no = ns->regs.row; | 
|  | 1411 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1412 | NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n", | 
|  | 1413 | num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off); | 
|  | 1414 | NS_LOG("programm page %d\n", ns->regs.row); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1415 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1416 | NS_UDELAY(programm_delay); | 
|  | 1417 | NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1418 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 1419 | if (write_error(page_no)) { | 
|  | 1420 | NS_WARN("simulating write failure in page %u\n", page_no); | 
|  | 1421 | return -1; | 
|  | 1422 | } | 
|  | 1423 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | break; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1425 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1426 | case ACTION_ZEROOFF: | 
|  | 1427 | NS_DBG("do_state_action: set internal offset to 0\n"); | 
|  | 1428 | ns->regs.off = 0; | 
|  | 1429 | break; | 
|  | 1430 |  | 
|  | 1431 | case ACTION_HALFOFF: | 
|  | 1432 | if (!(ns->options & OPT_PAGE512_8BIT)) { | 
|  | 1433 | NS_ERR("do_state_action: BUG! can't skip half of page for non-512" | 
|  | 1434 | "byte page size 8x chips\n"); | 
|  | 1435 | return -1; | 
|  | 1436 | } | 
|  | 1437 | NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2); | 
|  | 1438 | ns->regs.off = ns->geom.pgsz/2; | 
|  | 1439 | break; | 
|  | 1440 |  | 
|  | 1441 | case ACTION_OOBOFF: | 
|  | 1442 | NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz); | 
|  | 1443 | ns->regs.off = ns->geom.pgsz; | 
|  | 1444 | break; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1445 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1446 | default: | 
|  | 1447 | NS_DBG("do_state_action: BUG! unknown action\n"); | 
|  | 1448 | } | 
|  | 1449 |  | 
|  | 1450 | return 0; | 
|  | 1451 | } | 
|  | 1452 |  | 
|  | 1453 | /* | 
|  | 1454 | * Switch simulator's state. | 
|  | 1455 | */ | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1456 | static void switch_state(struct nandsim *ns) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | { | 
|  | 1458 | if (ns->op) { | 
|  | 1459 | /* | 
|  | 1460 | * The current operation have already been identified. | 
|  | 1461 | * Just follow the states chain. | 
|  | 1462 | */ | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1463 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1464 | ns->stateidx += 1; | 
|  | 1465 | ns->state = ns->nxstate; | 
|  | 1466 | ns->nxstate = ns->op[ns->stateidx + 1]; | 
|  | 1467 |  | 
|  | 1468 | NS_DBG("switch_state: operation is known, switch to the next state, " | 
|  | 1469 | "state: %s, nxstate: %s\n", | 
|  | 1470 | get_state_name(ns->state), get_state_name(ns->nxstate)); | 
|  | 1471 |  | 
|  | 1472 | /* See, whether we need to do some action */ | 
|  | 1473 | if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) { | 
|  | 1474 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1475 | return; | 
|  | 1476 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1477 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | } else { | 
|  | 1479 | /* | 
|  | 1480 | * We don't yet know which operation we perform. | 
|  | 1481 | * Try to identify it. | 
|  | 1482 | */ | 
|  | 1483 |  | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1484 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | *  The only event causing the switch_state function to | 
|  | 1486 | *  be called with yet unknown operation is new command. | 
|  | 1487 | */ | 
|  | 1488 | ns->state = get_state_by_command(ns->regs.command); | 
|  | 1489 |  | 
|  | 1490 | NS_DBG("switch_state: operation is unknown, try to find it\n"); | 
|  | 1491 |  | 
|  | 1492 | if (find_operation(ns, 0) != 0) | 
|  | 1493 | return; | 
|  | 1494 |  | 
|  | 1495 | if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) { | 
|  | 1496 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1497 | return; | 
|  | 1498 | } | 
|  | 1499 | } | 
|  | 1500 |  | 
|  | 1501 | /* For 16x devices column means the page offset in words */ | 
|  | 1502 | if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) { | 
|  | 1503 | NS_DBG("switch_state: double the column number for 16x device\n"); | 
|  | 1504 | ns->regs.column <<= 1; | 
|  | 1505 | } | 
|  | 1506 |  | 
|  | 1507 | if (NS_STATE(ns->nxstate) == STATE_READY) { | 
|  | 1508 | /* | 
|  | 1509 | * The current state is the last. Return to STATE_READY | 
|  | 1510 | */ | 
|  | 1511 |  | 
|  | 1512 | u_char status = NS_STATUS_OK(ns); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1513 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1514 | /* In case of data states, see if all bytes were input/output */ | 
|  | 1515 | if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) | 
|  | 1516 | && ns->regs.count != ns->regs.num) { | 
|  | 1517 | NS_WARN("switch_state: not all bytes were processed, %d left\n", | 
|  | 1518 | ns->regs.num - ns->regs.count); | 
|  | 1519 | status = NS_STATUS_FAILED(ns); | 
|  | 1520 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1521 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | NS_DBG("switch_state: operation complete, switch to STATE_READY state\n"); | 
|  | 1523 |  | 
|  | 1524 | switch_to_ready_state(ns, status); | 
|  | 1525 |  | 
|  | 1526 | return; | 
|  | 1527 | } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) { | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1528 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | * If the next state is data input/output, switch to it now | 
|  | 1530 | */ | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1531 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1532 | ns->state      = ns->nxstate; | 
|  | 1533 | ns->nxstate    = ns->op[++ns->stateidx + 1]; | 
|  | 1534 | ns->regs.num   = ns->regs.count = 0; | 
|  | 1535 |  | 
|  | 1536 | NS_DBG("switch_state: the next state is data I/O, switch, " | 
|  | 1537 | "state: %s, nxstate: %s\n", | 
|  | 1538 | get_state_name(ns->state), get_state_name(ns->nxstate)); | 
|  | 1539 |  | 
|  | 1540 | /* | 
|  | 1541 | * Set the internal register to the count of bytes which | 
|  | 1542 | * are expected to be input or output | 
|  | 1543 | */ | 
|  | 1544 | switch (NS_STATE(ns->state)) { | 
|  | 1545 | case STATE_DATAIN: | 
|  | 1546 | case STATE_DATAOUT: | 
|  | 1547 | ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column; | 
|  | 1548 | break; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1549 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1550 | case STATE_DATAOUT_ID: | 
|  | 1551 | ns->regs.num = ns->geom.idbytes; | 
|  | 1552 | break; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1553 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1554 | case STATE_DATAOUT_STATUS: | 
|  | 1555 | case STATE_DATAOUT_STATUS_M: | 
|  | 1556 | ns->regs.count = ns->regs.num = 0; | 
|  | 1557 | break; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1558 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1559 | default: | 
|  | 1560 | NS_ERR("switch_state: BUG! unknown data state\n"); | 
|  | 1561 | } | 
|  | 1562 |  | 
|  | 1563 | } else if (ns->nxstate & STATE_ADDR_MASK) { | 
|  | 1564 | /* | 
|  | 1565 | * If the next state is address input, set the internal | 
|  | 1566 | * register to the number of expected address bytes | 
|  | 1567 | */ | 
|  | 1568 |  | 
|  | 1569 | ns->regs.count = 0; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1570 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1571 | switch (NS_STATE(ns->nxstate)) { | 
|  | 1572 | case STATE_ADDR_PAGE: | 
|  | 1573 | ns->regs.num = ns->geom.pgaddrbytes; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1574 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1575 | break; | 
|  | 1576 | case STATE_ADDR_SEC: | 
|  | 1577 | ns->regs.num = ns->geom.secaddrbytes; | 
|  | 1578 | break; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1579 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | case STATE_ADDR_ZERO: | 
|  | 1581 | ns->regs.num = 1; | 
|  | 1582 | break; | 
|  | 1583 |  | 
|  | 1584 | default: | 
|  | 1585 | NS_ERR("switch_state: BUG! unknown address state\n"); | 
|  | 1586 | } | 
|  | 1587 | } else { | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1588 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1589 | * Just reset internal counters. | 
|  | 1590 | */ | 
|  | 1591 |  | 
|  | 1592 | ns->regs.num = 0; | 
|  | 1593 | ns->regs.count = 0; | 
|  | 1594 | } | 
|  | 1595 | } | 
|  | 1596 |  | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1597 | static u_char ns_nand_read_byte(struct mtd_info *mtd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1598 | { | 
|  | 1599 | struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; | 
|  | 1600 | u_char outb = 0x00; | 
|  | 1601 |  | 
|  | 1602 | /* Sanity and correctness checks */ | 
|  | 1603 | if (!ns->lines.ce) { | 
|  | 1604 | NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb); | 
|  | 1605 | return outb; | 
|  | 1606 | } | 
|  | 1607 | if (ns->lines.ale || ns->lines.cle) { | 
|  | 1608 | NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb); | 
|  | 1609 | return outb; | 
|  | 1610 | } | 
|  | 1611 | if (!(ns->state & STATE_DATAOUT_MASK)) { | 
|  | 1612 | NS_WARN("read_byte: unexpected data output cycle, state is %s " | 
|  | 1613 | "return %#x\n", get_state_name(ns->state), (uint)outb); | 
|  | 1614 | return outb; | 
|  | 1615 | } | 
|  | 1616 |  | 
|  | 1617 | /* Status register may be read as many times as it is wanted */ | 
|  | 1618 | if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) { | 
|  | 1619 | NS_DBG("read_byte: return %#x status\n", ns->regs.status); | 
|  | 1620 | return ns->regs.status; | 
|  | 1621 | } | 
|  | 1622 |  | 
|  | 1623 | /* Check if there is any data in the internal buffer which may be read */ | 
|  | 1624 | if (ns->regs.count == ns->regs.num) { | 
|  | 1625 | NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb); | 
|  | 1626 | return outb; | 
|  | 1627 | } | 
|  | 1628 |  | 
|  | 1629 | switch (NS_STATE(ns->state)) { | 
|  | 1630 | case STATE_DATAOUT: | 
|  | 1631 | if (ns->busw == 8) { | 
|  | 1632 | outb = ns->buf.byte[ns->regs.count]; | 
|  | 1633 | ns->regs.count += 1; | 
|  | 1634 | } else { | 
|  | 1635 | outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]); | 
|  | 1636 | ns->regs.count += 2; | 
|  | 1637 | } | 
|  | 1638 | break; | 
|  | 1639 | case STATE_DATAOUT_ID: | 
|  | 1640 | NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num); | 
|  | 1641 | outb = ns->ids[ns->regs.count]; | 
|  | 1642 | ns->regs.count += 1; | 
|  | 1643 | break; | 
|  | 1644 | default: | 
|  | 1645 | BUG(); | 
|  | 1646 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1647 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1648 | if (ns->regs.count == ns->regs.num) { | 
|  | 1649 | NS_DBG("read_byte: all bytes were read\n"); | 
|  | 1650 |  | 
|  | 1651 | /* | 
|  | 1652 | * The OPT_AUTOINCR allows to read next conseqitive pages without | 
|  | 1653 | * new read operation cycle. | 
|  | 1654 | */ | 
|  | 1655 | if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) { | 
|  | 1656 | ns->regs.count = 0; | 
|  | 1657 | if (ns->regs.row + 1 < ns->geom.pgnum) | 
|  | 1658 | ns->regs.row += 1; | 
|  | 1659 | NS_DBG("read_byte: switch to the next page (%#x)\n", ns->regs.row); | 
|  | 1660 | do_state_action(ns, ACTION_CPY); | 
|  | 1661 | } | 
|  | 1662 | else if (NS_STATE(ns->nxstate) == STATE_READY) | 
|  | 1663 | switch_state(ns); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1664 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1665 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1666 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | return outb; | 
|  | 1668 | } | 
|  | 1669 |  | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1670 | static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1671 | { | 
|  | 1672 | struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1673 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | /* Sanity and correctness checks */ | 
|  | 1675 | if (!ns->lines.ce) { | 
|  | 1676 | NS_ERR("write_byte: chip is disabled, ignore write\n"); | 
|  | 1677 | return; | 
|  | 1678 | } | 
|  | 1679 | if (ns->lines.ale && ns->lines.cle) { | 
|  | 1680 | NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n"); | 
|  | 1681 | return; | 
|  | 1682 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1683 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1684 | if (ns->lines.cle == 1) { | 
|  | 1685 | /* | 
|  | 1686 | * The byte written is a command. | 
|  | 1687 | */ | 
|  | 1688 |  | 
|  | 1689 | if (byte == NAND_CMD_RESET) { | 
|  | 1690 | NS_LOG("reset chip\n"); | 
|  | 1691 | switch_to_ready_state(ns, NS_STATUS_OK(ns)); | 
|  | 1692 | return; | 
|  | 1693 | } | 
|  | 1694 |  | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1695 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | * Chip might still be in STATE_DATAOUT | 
|  | 1697 | * (if OPT_AUTOINCR feature is supported), STATE_DATAOUT_STATUS or | 
|  | 1698 | * STATE_DATAOUT_STATUS_M state. If so, switch state. | 
|  | 1699 | */ | 
|  | 1700 | if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS | 
|  | 1701 | || NS_STATE(ns->state) == STATE_DATAOUT_STATUS_M | 
|  | 1702 | || ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT)) | 
|  | 1703 | switch_state(ns); | 
|  | 1704 |  | 
|  | 1705 | /* Check if chip is expecting command */ | 
|  | 1706 | if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) { | 
|  | 1707 | /* | 
|  | 1708 | * We are in situation when something else (not command) | 
|  | 1709 | * was expected but command was input. In this case ignore | 
|  | 1710 | * previous command(s)/state(s) and accept the last one. | 
|  | 1711 | */ | 
|  | 1712 | NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, " | 
|  | 1713 | "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate)); | 
|  | 1714 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1715 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1716 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1717 | /* Check that the command byte is correct */ | 
|  | 1718 | if (check_command(byte)) { | 
|  | 1719 | NS_ERR("write_byte: unknown command %#x\n", (uint)byte); | 
|  | 1720 | return; | 
|  | 1721 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1722 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | NS_DBG("command byte corresponding to %s state accepted\n", | 
|  | 1724 | get_state_name(get_state_by_command(byte))); | 
|  | 1725 | ns->regs.command = byte; | 
|  | 1726 | switch_state(ns); | 
|  | 1727 |  | 
|  | 1728 | } else if (ns->lines.ale == 1) { | 
|  | 1729 | /* | 
|  | 1730 | * The byte written is an address. | 
|  | 1731 | */ | 
|  | 1732 |  | 
|  | 1733 | if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) { | 
|  | 1734 |  | 
|  | 1735 | NS_DBG("write_byte: operation isn't known yet, identify it\n"); | 
|  | 1736 |  | 
|  | 1737 | if (find_operation(ns, 1) < 0) | 
|  | 1738 | return; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1739 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1740 | if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) { | 
|  | 1741 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1742 | return; | 
|  | 1743 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1744 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | ns->regs.count = 0; | 
|  | 1746 | switch (NS_STATE(ns->nxstate)) { | 
|  | 1747 | case STATE_ADDR_PAGE: | 
|  | 1748 | ns->regs.num = ns->geom.pgaddrbytes; | 
|  | 1749 | break; | 
|  | 1750 | case STATE_ADDR_SEC: | 
|  | 1751 | ns->regs.num = ns->geom.secaddrbytes; | 
|  | 1752 | break; | 
|  | 1753 | case STATE_ADDR_ZERO: | 
|  | 1754 | ns->regs.num = 1; | 
|  | 1755 | break; | 
|  | 1756 | default: | 
|  | 1757 | BUG(); | 
|  | 1758 | } | 
|  | 1759 | } | 
|  | 1760 |  | 
|  | 1761 | /* Check that chip is expecting address */ | 
|  | 1762 | if (!(ns->nxstate & STATE_ADDR_MASK)) { | 
|  | 1763 | NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, " | 
|  | 1764 | "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate)); | 
|  | 1765 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1766 | return; | 
|  | 1767 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1768 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | /* Check if this is expected byte */ | 
|  | 1770 | if (ns->regs.count == ns->regs.num) { | 
|  | 1771 | NS_ERR("write_byte: no more address bytes expected\n"); | 
|  | 1772 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1773 | return; | 
|  | 1774 | } | 
|  | 1775 |  | 
|  | 1776 | accept_addr_byte(ns, byte); | 
|  | 1777 |  | 
|  | 1778 | ns->regs.count += 1; | 
|  | 1779 |  | 
|  | 1780 | NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n", | 
|  | 1781 | (uint)byte, ns->regs.count, ns->regs.num); | 
|  | 1782 |  | 
|  | 1783 | if (ns->regs.count == ns->regs.num) { | 
|  | 1784 | NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column); | 
|  | 1785 | switch_state(ns); | 
|  | 1786 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1787 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1788 | } else { | 
|  | 1789 | /* | 
|  | 1790 | * The byte written is an input data. | 
|  | 1791 | */ | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1792 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | /* Check that chip is expecting data input */ | 
|  | 1794 | if (!(ns->state & STATE_DATAIN_MASK)) { | 
|  | 1795 | NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, " | 
|  | 1796 | "switch to %s\n", (uint)byte, | 
|  | 1797 | get_state_name(ns->state), get_state_name(STATE_READY)); | 
|  | 1798 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1799 | return; | 
|  | 1800 | } | 
|  | 1801 |  | 
|  | 1802 | /* Check if this is expected byte */ | 
|  | 1803 | if (ns->regs.count == ns->regs.num) { | 
|  | 1804 | NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n", | 
|  | 1805 | ns->regs.num); | 
|  | 1806 | return; | 
|  | 1807 | } | 
|  | 1808 |  | 
|  | 1809 | if (ns->busw == 8) { | 
|  | 1810 | ns->buf.byte[ns->regs.count] = byte; | 
|  | 1811 | ns->regs.count += 1; | 
|  | 1812 | } else { | 
|  | 1813 | ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte); | 
|  | 1814 | ns->regs.count += 2; | 
|  | 1815 | } | 
|  | 1816 | } | 
|  | 1817 |  | 
|  | 1818 | return; | 
|  | 1819 | } | 
|  | 1820 |  | 
| Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 1821 | static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask) | 
|  | 1822 | { | 
|  | 1823 | struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv; | 
|  | 1824 |  | 
|  | 1825 | ns->lines.cle = bitmask & NAND_CLE ? 1 : 0; | 
|  | 1826 | ns->lines.ale = bitmask & NAND_ALE ? 1 : 0; | 
|  | 1827 | ns->lines.ce = bitmask & NAND_NCE ? 1 : 0; | 
|  | 1828 |  | 
|  | 1829 | if (cmd != NAND_CMD_NONE) | 
|  | 1830 | ns_nand_write_byte(mtd, cmd); | 
|  | 1831 | } | 
|  | 1832 |  | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1833 | static int ns_device_ready(struct mtd_info *mtd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1834 | { | 
|  | 1835 | NS_DBG("device_ready\n"); | 
|  | 1836 | return 1; | 
|  | 1837 | } | 
|  | 1838 |  | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1839 | static uint16_t ns_nand_read_word(struct mtd_info *mtd) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | { | 
|  | 1841 | struct nand_chip *chip = (struct nand_chip *)mtd->priv; | 
|  | 1842 |  | 
|  | 1843 | NS_DBG("read_word\n"); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1844 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8); | 
|  | 1846 | } | 
|  | 1847 |  | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1848 | static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | { | 
|  | 1850 | struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; | 
|  | 1851 |  | 
|  | 1852 | /* Check that chip is expecting data input */ | 
|  | 1853 | if (!(ns->state & STATE_DATAIN_MASK)) { | 
|  | 1854 | NS_ERR("write_buf: data input isn't expected, state is %s, " | 
|  | 1855 | "switch to STATE_READY\n", get_state_name(ns->state)); | 
|  | 1856 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1857 | return; | 
|  | 1858 | } | 
|  | 1859 |  | 
|  | 1860 | /* Check if these are expected bytes */ | 
|  | 1861 | if (ns->regs.count + len > ns->regs.num) { | 
|  | 1862 | NS_ERR("write_buf: too many input bytes\n"); | 
|  | 1863 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1864 | return; | 
|  | 1865 | } | 
|  | 1866 |  | 
|  | 1867 | memcpy(ns->buf.byte + ns->regs.count, buf, len); | 
|  | 1868 | ns->regs.count += len; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1869 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | if (ns->regs.count == ns->regs.num) { | 
|  | 1871 | NS_DBG("write_buf: %d bytes were written\n", ns->regs.count); | 
|  | 1872 | } | 
|  | 1873 | } | 
|  | 1874 |  | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1875 | static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1876 | { | 
|  | 1877 | struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv; | 
|  | 1878 |  | 
|  | 1879 | /* Sanity and correctness checks */ | 
|  | 1880 | if (!ns->lines.ce) { | 
|  | 1881 | NS_ERR("read_buf: chip is disabled\n"); | 
|  | 1882 | return; | 
|  | 1883 | } | 
|  | 1884 | if (ns->lines.ale || ns->lines.cle) { | 
|  | 1885 | NS_ERR("read_buf: ALE or CLE pin is high\n"); | 
|  | 1886 | return; | 
|  | 1887 | } | 
|  | 1888 | if (!(ns->state & STATE_DATAOUT_MASK)) { | 
|  | 1889 | NS_WARN("read_buf: unexpected data output cycle, current state is %s\n", | 
|  | 1890 | get_state_name(ns->state)); | 
|  | 1891 | return; | 
|  | 1892 | } | 
|  | 1893 |  | 
|  | 1894 | if (NS_STATE(ns->state) != STATE_DATAOUT) { | 
|  | 1895 | int i; | 
|  | 1896 |  | 
|  | 1897 | for (i = 0; i < len; i++) | 
|  | 1898 | buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd); | 
|  | 1899 |  | 
|  | 1900 | return; | 
|  | 1901 | } | 
|  | 1902 |  | 
|  | 1903 | /* Check if these are expected bytes */ | 
|  | 1904 | if (ns->regs.count + len > ns->regs.num) { | 
|  | 1905 | NS_ERR("read_buf: too many bytes to read\n"); | 
|  | 1906 | switch_to_ready_state(ns, NS_STATUS_FAILED(ns)); | 
|  | 1907 | return; | 
|  | 1908 | } | 
|  | 1909 |  | 
|  | 1910 | memcpy(buf, ns->buf.byte + ns->regs.count, len); | 
|  | 1911 | ns->regs.count += len; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1912 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1913 | if (ns->regs.count == ns->regs.num) { | 
|  | 1914 | if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) { | 
|  | 1915 | ns->regs.count = 0; | 
|  | 1916 | if (ns->regs.row + 1 < ns->geom.pgnum) | 
|  | 1917 | ns->regs.row += 1; | 
|  | 1918 | NS_DBG("read_buf: switch to the next page (%#x)\n", ns->regs.row); | 
|  | 1919 | do_state_action(ns, ACTION_CPY); | 
|  | 1920 | } | 
|  | 1921 | else if (NS_STATE(ns->nxstate) == STATE_READY) | 
|  | 1922 | switch_state(ns); | 
|  | 1923 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1924 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1925 | return; | 
|  | 1926 | } | 
|  | 1927 |  | 
| Vijay Kumar | a560214 | 2006-10-14 21:33:34 +0530 | [diff] [blame] | 1928 | static int ns_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | { | 
|  | 1930 | ns_nand_read_buf(mtd, (u_char *)&ns_verify_buf[0], len); | 
|  | 1931 |  | 
|  | 1932 | if (!memcmp(buf, &ns_verify_buf[0], len)) { | 
|  | 1933 | NS_DBG("verify_buf: the buffer is OK\n"); | 
|  | 1934 | return 0; | 
|  | 1935 | } else { | 
|  | 1936 | NS_DBG("verify_buf: the buffer is wrong\n"); | 
|  | 1937 | return -EFAULT; | 
|  | 1938 | } | 
|  | 1939 | } | 
|  | 1940 |  | 
|  | 1941 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1942 | * Module initialization function | 
|  | 1943 | */ | 
| Adrian Bunk | 2b9175c | 2005-11-29 14:49:38 +0000 | [diff] [blame] | 1944 | static int __init ns_init_module(void) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1945 | { | 
|  | 1946 | struct nand_chip *chip; | 
|  | 1947 | struct nandsim *nand; | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 1948 | int retval = -ENOMEM, i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 |  | 
|  | 1950 | if (bus_width != 8 && bus_width != 16) { | 
|  | 1951 | NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width); | 
|  | 1952 | return -EINVAL; | 
|  | 1953 | } | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1954 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | /* Allocate and initialize mtd_info, nand_chip and nandsim structures */ | 
| Burman Yan | 95b93a0 | 2006-11-15 21:10:29 +0200 | [diff] [blame] | 1956 | nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1957 | + sizeof(struct nandsim), GFP_KERNEL); | 
|  | 1958 | if (!nsmtd) { | 
|  | 1959 | NS_ERR("unable to allocate core structures.\n"); | 
|  | 1960 | return -ENOMEM; | 
|  | 1961 | } | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1962 | chip        = (struct nand_chip *)(nsmtd + 1); | 
|  | 1963 | nsmtd->priv = (void *)chip; | 
|  | 1964 | nand        = (struct nandsim *)(chip + 1); | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1965 | chip->priv  = (void *)nand; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1966 |  | 
|  | 1967 | /* | 
|  | 1968 | * Register simulator's callbacks. | 
|  | 1969 | */ | 
| Thomas Gleixner | 7abd3ef | 2006-05-23 23:25:53 +0200 | [diff] [blame] | 1970 | chip->cmd_ctrl	 = ns_hwcontrol; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | chip->read_byte  = ns_nand_read_byte; | 
|  | 1972 | chip->dev_ready  = ns_device_ready; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | chip->write_buf  = ns_nand_write_buf; | 
|  | 1974 | chip->read_buf   = ns_nand_read_buf; | 
|  | 1975 | chip->verify_buf = ns_nand_verify_buf; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1976 | chip->read_word  = ns_nand_read_word; | 
| Thomas Gleixner | 6dfc6d2 | 2006-05-23 12:00:46 +0200 | [diff] [blame] | 1977 | chip->ecc.mode   = NAND_ECC_SOFT; | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 1978 | /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */ | 
|  | 1979 | /* and 'badblocks' parameters to work */ | 
| Artem B. Bityuckiy | 5150228 | 2005-03-19 15:33:59 +0000 | [diff] [blame] | 1980 | chip->options   |= NAND_SKIP_BBTSCAN; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 |  | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1982 | /* | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1983 | * Perform minimum nandsim structure initialization to handle | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 1984 | * the initial ID read command correctly | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | */ | 
|  | 1986 | if (third_id_byte != 0xFF || fourth_id_byte != 0xFF) | 
|  | 1987 | nand->geom.idbytes = 4; | 
|  | 1988 | else | 
|  | 1989 | nand->geom.idbytes = 2; | 
|  | 1990 | nand->regs.status = NS_STATUS_OK(nand); | 
|  | 1991 | nand->nxstate = STATE_UNKNOWN; | 
|  | 1992 | nand->options |= OPT_PAGE256; /* temporary value */ | 
|  | 1993 | nand->ids[0] = first_id_byte; | 
|  | 1994 | nand->ids[1] = second_id_byte; | 
|  | 1995 | nand->ids[2] = third_id_byte; | 
|  | 1996 | nand->ids[3] = fourth_id_byte; | 
|  | 1997 | if (bus_width == 16) { | 
|  | 1998 | nand->busw = 16; | 
|  | 1999 | chip->options |= NAND_BUSWIDTH_16; | 
|  | 2000 | } | 
|  | 2001 |  | 
| David Woodhouse | 552d920 | 2006-05-14 01:20:46 +0100 | [diff] [blame] | 2002 | nsmtd->owner = THIS_MODULE; | 
|  | 2003 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 2004 | if ((retval = parse_weakblocks()) != 0) | 
|  | 2005 | goto error; | 
|  | 2006 |  | 
|  | 2007 | if ((retval = parse_weakpages()) != 0) | 
|  | 2008 | goto error; | 
|  | 2009 |  | 
|  | 2010 | if ((retval = parse_gravepages()) != 0) | 
|  | 2011 | goto error; | 
|  | 2012 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2013 | if ((retval = nand_scan(nsmtd, 1)) != 0) { | 
|  | 2014 | NS_ERR("can't register NAND Simulator\n"); | 
|  | 2015 | if (retval > 0) | 
|  | 2016 | retval = -ENXIO; | 
|  | 2017 | goto error; | 
|  | 2018 | } | 
|  | 2019 |  | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 2020 | if (overridesize) { | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 2021 | u_int64_t new_size = (u_int64_t)nsmtd->erasesize << overridesize; | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 2022 | if (new_size >> overridesize != nsmtd->erasesize) { | 
|  | 2023 | NS_ERR("overridesize is too big\n"); | 
|  | 2024 | goto err_exit; | 
|  | 2025 | } | 
|  | 2026 | /* N.B. This relies on nand_scan not doing anything with the size before we change it */ | 
|  | 2027 | nsmtd->size = new_size; | 
|  | 2028 | chip->chipsize = new_size; | 
| Adrian Hunter | 6eda7a5 | 2008-05-30 15:56:26 +0300 | [diff] [blame] | 2029 | chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1; | 
| Adrian Hunter | 07293b2 | 2008-05-30 15:56:23 +0300 | [diff] [blame] | 2030 | chip->pagemask = (chip->chipsize >> chip->page_shift) - 1; | 
| Adrian Hunter | a5ac8ae | 2007-03-19 12:49:11 +0200 | [diff] [blame] | 2031 | } | 
|  | 2032 |  | 
| Adrian Hunter | 57aa6b5 | 2007-03-19 12:40:41 +0200 | [diff] [blame] | 2033 | if ((retval = setup_wear_reporting(nsmtd)) != 0) | 
|  | 2034 | goto err_exit; | 
|  | 2035 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 2036 | if ((retval = init_nandsim(nsmtd)) != 0) | 
|  | 2037 | goto err_exit; | 
| Thomas Gleixner | 61b03bd | 2005-11-07 11:15:49 +0000 | [diff] [blame] | 2038 |  | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 2039 | if ((retval = parse_badblocks(nand, nsmtd)) != 0) | 
|  | 2040 | goto err_exit; | 
|  | 2041 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 2042 | if ((retval = nand_default_bbt(nsmtd)) != 0) | 
|  | 2043 | goto err_exit; | 
| Artem B. Bityuckiy | 5150228 | 2005-03-19 15:33:59 +0000 | [diff] [blame] | 2044 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 2045 | /* Register NAND partitions */ | 
|  | 2046 | if ((retval = add_mtd_partitions(nsmtd, &nand->partitions[0], nand->nbparts)) != 0) | 
|  | 2047 | goto err_exit; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 |  | 
|  | 2049 | return 0; | 
|  | 2050 |  | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 2051 | err_exit: | 
|  | 2052 | free_nandsim(nand); | 
|  | 2053 | nand_release(nsmtd); | 
|  | 2054 | for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i) | 
|  | 2055 | kfree(nand->partitions[i].name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | error: | 
|  | 2057 | kfree(nsmtd); | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 2058 | free_lists(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 |  | 
|  | 2060 | return retval; | 
|  | 2061 | } | 
|  | 2062 |  | 
|  | 2063 | module_init(ns_init_module); | 
|  | 2064 |  | 
|  | 2065 | /* | 
|  | 2066 | * Module clean-up function | 
|  | 2067 | */ | 
|  | 2068 | static void __exit ns_cleanup_module(void) | 
|  | 2069 | { | 
|  | 2070 | struct nandsim *ns = (struct nandsim *)(((struct nand_chip *)nsmtd->priv)->priv); | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 2071 | int i; | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 |  | 
|  | 2073 | free_nandsim(ns);    /* Free nandsim private resources */ | 
| Adrian Hunter | 2b77a0e | 2007-03-19 12:46:43 +0200 | [diff] [blame] | 2074 | nand_release(nsmtd); /* Unregister driver */ | 
|  | 2075 | for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i) | 
|  | 2076 | kfree(ns->partitions[i].name); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2077 | kfree(nsmtd);        /* Free other structures */ | 
| Adrian Hunter | 514087e7 | 2007-03-19 12:47:45 +0200 | [diff] [blame] | 2078 | free_lists(); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2079 | } | 
|  | 2080 |  | 
|  | 2081 | module_exit(ns_cleanup_module); | 
|  | 2082 |  | 
|  | 2083 | MODULE_LICENSE ("GPL"); | 
|  | 2084 | MODULE_AUTHOR ("Artem B. Bityuckiy"); | 
|  | 2085 | MODULE_DESCRIPTION ("The NAND flash simulator"); |