nandsim.c 67 KB

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