nandsim.c 67 KB

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