nandsim.c 60 KB

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