nandsim.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436
  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. ret = -EINVAL;
  653. goto error;
  654. }
  655. remains = ns->geom.totsz;
  656. next_offset = 0;
  657. for (i = 0; i < parts_num; ++i) {
  658. uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
  659. if (!part_sz || part_sz > remains) {
  660. NS_ERR("bad partition size.\n");
  661. ret = -EINVAL;
  662. goto error;
  663. }
  664. ns->partitions[i].name = get_partition_name(i);
  665. if (!ns->partitions[i].name) {
  666. NS_ERR("unable to allocate memory.\n");
  667. ret = -ENOMEM;
  668. goto error;
  669. }
  670. ns->partitions[i].offset = next_offset;
  671. ns->partitions[i].size = part_sz;
  672. next_offset += ns->partitions[i].size;
  673. remains -= ns->partitions[i].size;
  674. }
  675. ns->nbparts = parts_num;
  676. if (remains) {
  677. if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
  678. NS_ERR("too many partitions.\n");
  679. ret = -EINVAL;
  680. goto error;
  681. }
  682. ns->partitions[i].name = get_partition_name(i);
  683. if (!ns->partitions[i].name) {
  684. NS_ERR("unable to allocate memory.\n");
  685. ret = -ENOMEM;
  686. goto error;
  687. }
  688. ns->partitions[i].offset = next_offset;
  689. ns->partitions[i].size = remains;
  690. ns->nbparts += 1;
  691. }
  692. if (ns->busw == 16)
  693. NS_WARN("16-bit flashes support wasn't tested\n");
  694. printk("flash size: %llu MiB\n",
  695. (unsigned long long)ns->geom.totsz >> 20);
  696. printk("page size: %u bytes\n", ns->geom.pgsz);
  697. printk("OOB area size: %u bytes\n", ns->geom.oobsz);
  698. printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
  699. printk("pages number: %u\n", ns->geom.pgnum);
  700. printk("pages per sector: %u\n", ns->geom.pgsec);
  701. printk("bus width: %u\n", ns->busw);
  702. printk("bits in sector size: %u\n", ns->geom.secshift);
  703. printk("bits in page size: %u\n", ns->geom.pgshift);
  704. printk("bits in OOB size: %u\n", ffs(ns->geom.oobsz) - 1);
  705. printk("flash size with OOB: %llu KiB\n",
  706. (unsigned long long)ns->geom.totszoob >> 10);
  707. printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
  708. printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
  709. printk("options: %#x\n", ns->options);
  710. if ((ret = alloc_device(ns)) != 0)
  711. goto error;
  712. /* Allocate / initialize the internal buffer */
  713. ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  714. if (!ns->buf.byte) {
  715. NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
  716. ns->geom.pgszoob);
  717. ret = -ENOMEM;
  718. goto error;
  719. }
  720. memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
  721. return 0;
  722. error:
  723. free_device(ns);
  724. return ret;
  725. }
  726. /*
  727. * Free the nandsim structure.
  728. */
  729. static void free_nandsim(struct nandsim *ns)
  730. {
  731. kfree(ns->buf.byte);
  732. free_device(ns);
  733. return;
  734. }
  735. static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
  736. {
  737. char *w;
  738. int zero_ok;
  739. unsigned int erase_block_no;
  740. loff_t offset;
  741. if (!badblocks)
  742. return 0;
  743. w = badblocks;
  744. do {
  745. zero_ok = (*w == '0' ? 1 : 0);
  746. erase_block_no = simple_strtoul(w, &w, 0);
  747. if (!zero_ok && !erase_block_no) {
  748. NS_ERR("invalid badblocks.\n");
  749. return -EINVAL;
  750. }
  751. offset = (loff_t)erase_block_no * ns->geom.secsz;
  752. if (mtd_block_markbad(mtd, offset)) {
  753. NS_ERR("invalid badblocks.\n");
  754. return -EINVAL;
  755. }
  756. if (*w == ',')
  757. w += 1;
  758. } while (*w);
  759. return 0;
  760. }
  761. static int parse_weakblocks(void)
  762. {
  763. char *w;
  764. int zero_ok;
  765. unsigned int erase_block_no;
  766. unsigned int max_erases;
  767. struct weak_block *wb;
  768. if (!weakblocks)
  769. return 0;
  770. w = weakblocks;
  771. do {
  772. zero_ok = (*w == '0' ? 1 : 0);
  773. erase_block_no = simple_strtoul(w, &w, 0);
  774. if (!zero_ok && !erase_block_no) {
  775. NS_ERR("invalid weakblocks.\n");
  776. return -EINVAL;
  777. }
  778. max_erases = 3;
  779. if (*w == ':') {
  780. w += 1;
  781. max_erases = simple_strtoul(w, &w, 0);
  782. }
  783. if (*w == ',')
  784. w += 1;
  785. wb = kzalloc(sizeof(*wb), GFP_KERNEL);
  786. if (!wb) {
  787. NS_ERR("unable to allocate memory.\n");
  788. return -ENOMEM;
  789. }
  790. wb->erase_block_no = erase_block_no;
  791. wb->max_erases = max_erases;
  792. list_add(&wb->list, &weak_blocks);
  793. } while (*w);
  794. return 0;
  795. }
  796. static int erase_error(unsigned int erase_block_no)
  797. {
  798. struct weak_block *wb;
  799. list_for_each_entry(wb, &weak_blocks, list)
  800. if (wb->erase_block_no == erase_block_no) {
  801. if (wb->erases_done >= wb->max_erases)
  802. return 1;
  803. wb->erases_done += 1;
  804. return 0;
  805. }
  806. return 0;
  807. }
  808. static int parse_weakpages(void)
  809. {
  810. char *w;
  811. int zero_ok;
  812. unsigned int page_no;
  813. unsigned int max_writes;
  814. struct weak_page *wp;
  815. if (!weakpages)
  816. return 0;
  817. w = weakpages;
  818. do {
  819. zero_ok = (*w == '0' ? 1 : 0);
  820. page_no = simple_strtoul(w, &w, 0);
  821. if (!zero_ok && !page_no) {
  822. NS_ERR("invalid weakpagess.\n");
  823. return -EINVAL;
  824. }
  825. max_writes = 3;
  826. if (*w == ':') {
  827. w += 1;
  828. max_writes = simple_strtoul(w, &w, 0);
  829. }
  830. if (*w == ',')
  831. w += 1;
  832. wp = kzalloc(sizeof(*wp), GFP_KERNEL);
  833. if (!wp) {
  834. NS_ERR("unable to allocate memory.\n");
  835. return -ENOMEM;
  836. }
  837. wp->page_no = page_no;
  838. wp->max_writes = max_writes;
  839. list_add(&wp->list, &weak_pages);
  840. } while (*w);
  841. return 0;
  842. }
  843. static int write_error(unsigned int page_no)
  844. {
  845. struct weak_page *wp;
  846. list_for_each_entry(wp, &weak_pages, list)
  847. if (wp->page_no == page_no) {
  848. if (wp->writes_done >= wp->max_writes)
  849. return 1;
  850. wp->writes_done += 1;
  851. return 0;
  852. }
  853. return 0;
  854. }
  855. static int parse_gravepages(void)
  856. {
  857. char *g;
  858. int zero_ok;
  859. unsigned int page_no;
  860. unsigned int max_reads;
  861. struct grave_page *gp;
  862. if (!gravepages)
  863. return 0;
  864. g = gravepages;
  865. do {
  866. zero_ok = (*g == '0' ? 1 : 0);
  867. page_no = simple_strtoul(g, &g, 0);
  868. if (!zero_ok && !page_no) {
  869. NS_ERR("invalid gravepagess.\n");
  870. return -EINVAL;
  871. }
  872. max_reads = 3;
  873. if (*g == ':') {
  874. g += 1;
  875. max_reads = simple_strtoul(g, &g, 0);
  876. }
  877. if (*g == ',')
  878. g += 1;
  879. gp = kzalloc(sizeof(*gp), GFP_KERNEL);
  880. if (!gp) {
  881. NS_ERR("unable to allocate memory.\n");
  882. return -ENOMEM;
  883. }
  884. gp->page_no = page_no;
  885. gp->max_reads = max_reads;
  886. list_add(&gp->list, &grave_pages);
  887. } while (*g);
  888. return 0;
  889. }
  890. static int read_error(unsigned int page_no)
  891. {
  892. struct grave_page *gp;
  893. list_for_each_entry(gp, &grave_pages, list)
  894. if (gp->page_no == page_no) {
  895. if (gp->reads_done >= gp->max_reads)
  896. return 1;
  897. gp->reads_done += 1;
  898. return 0;
  899. }
  900. return 0;
  901. }
  902. static void free_lists(void)
  903. {
  904. struct list_head *pos, *n;
  905. list_for_each_safe(pos, n, &weak_blocks) {
  906. list_del(pos);
  907. kfree(list_entry(pos, struct weak_block, list));
  908. }
  909. list_for_each_safe(pos, n, &weak_pages) {
  910. list_del(pos);
  911. kfree(list_entry(pos, struct weak_page, list));
  912. }
  913. list_for_each_safe(pos, n, &grave_pages) {
  914. list_del(pos);
  915. kfree(list_entry(pos, struct grave_page, list));
  916. }
  917. kfree(erase_block_wear);
  918. }
  919. static int setup_wear_reporting(struct mtd_info *mtd)
  920. {
  921. size_t mem;
  922. wear_eb_count = div_u64(mtd->size, mtd->erasesize);
  923. mem = wear_eb_count * sizeof(unsigned long);
  924. if (mem / sizeof(unsigned long) != wear_eb_count) {
  925. NS_ERR("Too many erase blocks for wear reporting\n");
  926. return -ENOMEM;
  927. }
  928. erase_block_wear = kzalloc(mem, GFP_KERNEL);
  929. if (!erase_block_wear) {
  930. NS_ERR("Too many erase blocks for wear reporting\n");
  931. return -ENOMEM;
  932. }
  933. return 0;
  934. }
  935. static void update_wear(unsigned int erase_block_no)
  936. {
  937. if (!erase_block_wear)
  938. return;
  939. total_wear += 1;
  940. /*
  941. * TODO: Notify this through a debugfs entry,
  942. * instead of showing an error message.
  943. */
  944. if (total_wear == 0)
  945. NS_ERR("Erase counter total overflow\n");
  946. erase_block_wear[erase_block_no] += 1;
  947. if (erase_block_wear[erase_block_no] == 0)
  948. NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
  949. }
  950. /*
  951. * Returns the string representation of 'state' state.
  952. */
  953. static char *get_state_name(uint32_t state)
  954. {
  955. switch (NS_STATE(state)) {
  956. case STATE_CMD_READ0:
  957. return "STATE_CMD_READ0";
  958. case STATE_CMD_READ1:
  959. return "STATE_CMD_READ1";
  960. case STATE_CMD_PAGEPROG:
  961. return "STATE_CMD_PAGEPROG";
  962. case STATE_CMD_READOOB:
  963. return "STATE_CMD_READOOB";
  964. case STATE_CMD_READSTART:
  965. return "STATE_CMD_READSTART";
  966. case STATE_CMD_ERASE1:
  967. return "STATE_CMD_ERASE1";
  968. case STATE_CMD_STATUS:
  969. return "STATE_CMD_STATUS";
  970. case STATE_CMD_SEQIN:
  971. return "STATE_CMD_SEQIN";
  972. case STATE_CMD_READID:
  973. return "STATE_CMD_READID";
  974. case STATE_CMD_ERASE2:
  975. return "STATE_CMD_ERASE2";
  976. case STATE_CMD_RESET:
  977. return "STATE_CMD_RESET";
  978. case STATE_CMD_RNDOUT:
  979. return "STATE_CMD_RNDOUT";
  980. case STATE_CMD_RNDOUTSTART:
  981. return "STATE_CMD_RNDOUTSTART";
  982. case STATE_ADDR_PAGE:
  983. return "STATE_ADDR_PAGE";
  984. case STATE_ADDR_SEC:
  985. return "STATE_ADDR_SEC";
  986. case STATE_ADDR_ZERO:
  987. return "STATE_ADDR_ZERO";
  988. case STATE_ADDR_COLUMN:
  989. return "STATE_ADDR_COLUMN";
  990. case STATE_DATAIN:
  991. return "STATE_DATAIN";
  992. case STATE_DATAOUT:
  993. return "STATE_DATAOUT";
  994. case STATE_DATAOUT_ID:
  995. return "STATE_DATAOUT_ID";
  996. case STATE_DATAOUT_STATUS:
  997. return "STATE_DATAOUT_STATUS";
  998. case STATE_READY:
  999. return "STATE_READY";
  1000. case STATE_UNKNOWN:
  1001. return "STATE_UNKNOWN";
  1002. }
  1003. NS_ERR("get_state_name: unknown state, BUG\n");
  1004. return NULL;
  1005. }
  1006. /*
  1007. * Check if command is valid.
  1008. *
  1009. * RETURNS: 1 if wrong command, 0 if right.
  1010. */
  1011. static int check_command(int cmd)
  1012. {
  1013. switch (cmd) {
  1014. case NAND_CMD_READ0:
  1015. case NAND_CMD_READ1:
  1016. case NAND_CMD_READSTART:
  1017. case NAND_CMD_PAGEPROG:
  1018. case NAND_CMD_READOOB:
  1019. case NAND_CMD_ERASE1:
  1020. case NAND_CMD_STATUS:
  1021. case NAND_CMD_SEQIN:
  1022. case NAND_CMD_READID:
  1023. case NAND_CMD_ERASE2:
  1024. case NAND_CMD_RESET:
  1025. case NAND_CMD_RNDOUT:
  1026. case NAND_CMD_RNDOUTSTART:
  1027. return 0;
  1028. default:
  1029. return 1;
  1030. }
  1031. }
  1032. /*
  1033. * Returns state after command is accepted by command number.
  1034. */
  1035. static uint32_t get_state_by_command(unsigned command)
  1036. {
  1037. switch (command) {
  1038. case NAND_CMD_READ0:
  1039. return STATE_CMD_READ0;
  1040. case NAND_CMD_READ1:
  1041. return STATE_CMD_READ1;
  1042. case NAND_CMD_PAGEPROG:
  1043. return STATE_CMD_PAGEPROG;
  1044. case NAND_CMD_READSTART:
  1045. return STATE_CMD_READSTART;
  1046. case NAND_CMD_READOOB:
  1047. return STATE_CMD_READOOB;
  1048. case NAND_CMD_ERASE1:
  1049. return STATE_CMD_ERASE1;
  1050. case NAND_CMD_STATUS:
  1051. return STATE_CMD_STATUS;
  1052. case NAND_CMD_SEQIN:
  1053. return STATE_CMD_SEQIN;
  1054. case NAND_CMD_READID:
  1055. return STATE_CMD_READID;
  1056. case NAND_CMD_ERASE2:
  1057. return STATE_CMD_ERASE2;
  1058. case NAND_CMD_RESET:
  1059. return STATE_CMD_RESET;
  1060. case NAND_CMD_RNDOUT:
  1061. return STATE_CMD_RNDOUT;
  1062. case NAND_CMD_RNDOUTSTART:
  1063. return STATE_CMD_RNDOUTSTART;
  1064. }
  1065. NS_ERR("get_state_by_command: unknown command, BUG\n");
  1066. return 0;
  1067. }
  1068. /*
  1069. * Move an address byte to the correspondent internal register.
  1070. */
  1071. static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
  1072. {
  1073. uint byte = (uint)bt;
  1074. if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
  1075. ns->regs.column |= (byte << 8 * ns->regs.count);
  1076. else {
  1077. ns->regs.row |= (byte << 8 * (ns->regs.count -
  1078. ns->geom.pgaddrbytes +
  1079. ns->geom.secaddrbytes));
  1080. }
  1081. return;
  1082. }
  1083. /*
  1084. * Switch to STATE_READY state.
  1085. */
  1086. static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
  1087. {
  1088. NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
  1089. ns->state = STATE_READY;
  1090. ns->nxstate = STATE_UNKNOWN;
  1091. ns->op = NULL;
  1092. ns->npstates = 0;
  1093. ns->stateidx = 0;
  1094. ns->regs.num = 0;
  1095. ns->regs.count = 0;
  1096. ns->regs.off = 0;
  1097. ns->regs.row = 0;
  1098. ns->regs.column = 0;
  1099. ns->regs.status = status;
  1100. }
  1101. /*
  1102. * If the operation isn't known yet, try to find it in the global array
  1103. * of supported operations.
  1104. *
  1105. * Operation can be unknown because of the following.
  1106. * 1. New command was accepted and this is the first call to find the
  1107. * correspondent states chain. In this case ns->npstates = 0;
  1108. * 2. There are several operations which begin with the same command(s)
  1109. * (for example program from the second half and read from the
  1110. * second half operations both begin with the READ1 command). In this
  1111. * case the ns->pstates[] array contains previous states.
  1112. *
  1113. * Thus, the function tries to find operation containing the following
  1114. * states (if the 'flag' parameter is 0):
  1115. * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
  1116. *
  1117. * If (one and only one) matching operation is found, it is accepted (
  1118. * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
  1119. * zeroed).
  1120. *
  1121. * If there are several matches, the current state is pushed to the
  1122. * ns->pstates.
  1123. *
  1124. * The operation can be unknown only while commands are input to the chip.
  1125. * As soon as address command is accepted, the operation must be known.
  1126. * In such situation the function is called with 'flag' != 0, and the
  1127. * operation is searched using the following pattern:
  1128. * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
  1129. *
  1130. * It is supposed that this pattern must either match one operation or
  1131. * none. There can't be ambiguity in that case.
  1132. *
  1133. * If no matches found, the function does the following:
  1134. * 1. if there are saved states present, try to ignore them and search
  1135. * again only using the last command. If nothing was found, switch
  1136. * to the STATE_READY state.
  1137. * 2. if there are no saved states, switch to the STATE_READY state.
  1138. *
  1139. * RETURNS: -2 - no matched operations found.
  1140. * -1 - several matches.
  1141. * 0 - operation is found.
  1142. */
  1143. static int find_operation(struct nandsim *ns, uint32_t flag)
  1144. {
  1145. int opsfound = 0;
  1146. int i, j, idx = 0;
  1147. for (i = 0; i < NS_OPER_NUM; i++) {
  1148. int found = 1;
  1149. if (!(ns->options & ops[i].reqopts))
  1150. /* Ignore operations we can't perform */
  1151. continue;
  1152. if (flag) {
  1153. if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
  1154. continue;
  1155. } else {
  1156. if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
  1157. continue;
  1158. }
  1159. for (j = 0; j < ns->npstates; j++)
  1160. if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
  1161. && (ns->options & ops[idx].reqopts)) {
  1162. found = 0;
  1163. break;
  1164. }
  1165. if (found) {
  1166. idx = i;
  1167. opsfound += 1;
  1168. }
  1169. }
  1170. if (opsfound == 1) {
  1171. /* Exact match */
  1172. ns->op = &ops[idx].states[0];
  1173. if (flag) {
  1174. /*
  1175. * In this case the find_operation function was
  1176. * called when address has just began input. But it isn't
  1177. * yet fully input and the current state must
  1178. * not be one of STATE_ADDR_*, but the STATE_ADDR_*
  1179. * state must be the next state (ns->nxstate).
  1180. */
  1181. ns->stateidx = ns->npstates - 1;
  1182. } else {
  1183. ns->stateidx = ns->npstates;
  1184. }
  1185. ns->npstates = 0;
  1186. ns->state = ns->op[ns->stateidx];
  1187. ns->nxstate = ns->op[ns->stateidx + 1];
  1188. NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
  1189. idx, get_state_name(ns->state), get_state_name(ns->nxstate));
  1190. return 0;
  1191. }
  1192. if (opsfound == 0) {
  1193. /* Nothing was found. Try to ignore previous commands (if any) and search again */
  1194. if (ns->npstates != 0) {
  1195. NS_DBG("find_operation: no operation found, try again with state %s\n",
  1196. get_state_name(ns->state));
  1197. ns->npstates = 0;
  1198. return find_operation(ns, 0);
  1199. }
  1200. NS_DBG("find_operation: no operations found\n");
  1201. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1202. return -2;
  1203. }
  1204. if (flag) {
  1205. /* This shouldn't happen */
  1206. NS_DBG("find_operation: BUG, operation must be known if address is input\n");
  1207. return -2;
  1208. }
  1209. NS_DBG("find_operation: there is still ambiguity\n");
  1210. ns->pstates[ns->npstates++] = ns->state;
  1211. return -1;
  1212. }
  1213. static void put_pages(struct nandsim *ns)
  1214. {
  1215. int i;
  1216. for (i = 0; i < ns->held_cnt; i++)
  1217. page_cache_release(ns->held_pages[i]);
  1218. }
  1219. /* Get page cache pages in advance to provide NOFS memory allocation */
  1220. static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
  1221. {
  1222. pgoff_t index, start_index, end_index;
  1223. struct page *page;
  1224. struct address_space *mapping = file->f_mapping;
  1225. start_index = pos >> PAGE_CACHE_SHIFT;
  1226. end_index = (pos + count - 1) >> PAGE_CACHE_SHIFT;
  1227. if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
  1228. return -EINVAL;
  1229. ns->held_cnt = 0;
  1230. for (index = start_index; index <= end_index; index++) {
  1231. page = find_get_page(mapping, index);
  1232. if (page == NULL) {
  1233. page = find_or_create_page(mapping, index, GFP_NOFS);
  1234. if (page == NULL) {
  1235. write_inode_now(mapping->host, 1);
  1236. page = find_or_create_page(mapping, index, GFP_NOFS);
  1237. }
  1238. if (page == NULL) {
  1239. put_pages(ns);
  1240. return -ENOMEM;
  1241. }
  1242. unlock_page(page);
  1243. }
  1244. ns->held_pages[ns->held_cnt++] = page;
  1245. }
  1246. return 0;
  1247. }
  1248. static int set_memalloc(void)
  1249. {
  1250. if (current->flags & PF_MEMALLOC)
  1251. return 0;
  1252. current->flags |= PF_MEMALLOC;
  1253. return 1;
  1254. }
  1255. static void clear_memalloc(int memalloc)
  1256. {
  1257. if (memalloc)
  1258. current->flags &= ~PF_MEMALLOC;
  1259. }
  1260. static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
  1261. {
  1262. ssize_t tx;
  1263. int err, memalloc;
  1264. err = get_pages(ns, file, count, pos);
  1265. if (err)
  1266. return err;
  1267. memalloc = set_memalloc();
  1268. tx = kernel_read(file, pos, buf, count);
  1269. clear_memalloc(memalloc);
  1270. put_pages(ns);
  1271. return tx;
  1272. }
  1273. static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t pos)
  1274. {
  1275. ssize_t tx;
  1276. int err, memalloc;
  1277. err = get_pages(ns, file, count, pos);
  1278. if (err)
  1279. return err;
  1280. memalloc = set_memalloc();
  1281. tx = kernel_write(file, buf, count, pos);
  1282. clear_memalloc(memalloc);
  1283. put_pages(ns);
  1284. return tx;
  1285. }
  1286. /*
  1287. * Returns a pointer to the current page.
  1288. */
  1289. static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
  1290. {
  1291. return &(ns->pages[ns->regs.row]);
  1292. }
  1293. /*
  1294. * Retuns a pointer to the current byte, within the current page.
  1295. */
  1296. static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
  1297. {
  1298. return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
  1299. }
  1300. static int do_read_error(struct nandsim *ns, int num)
  1301. {
  1302. unsigned int page_no = ns->regs.row;
  1303. if (read_error(page_no)) {
  1304. prandom_bytes(ns->buf.byte, num);
  1305. NS_WARN("simulating read error in page %u\n", page_no);
  1306. return 1;
  1307. }
  1308. return 0;
  1309. }
  1310. static void do_bit_flips(struct nandsim *ns, int num)
  1311. {
  1312. if (bitflips && prandom_u32() < (1 << 22)) {
  1313. int flips = 1;
  1314. if (bitflips > 1)
  1315. flips = (prandom_u32() % (int) bitflips) + 1;
  1316. while (flips--) {
  1317. int pos = prandom_u32() % (num * 8);
  1318. ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
  1319. NS_WARN("read_page: flipping bit %d in page %d "
  1320. "reading from %d ecc: corrected=%u failed=%u\n",
  1321. pos, ns->regs.row, ns->regs.column + ns->regs.off,
  1322. nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
  1323. }
  1324. }
  1325. }
  1326. /*
  1327. * Fill the NAND buffer with data read from the specified page.
  1328. */
  1329. static void read_page(struct nandsim *ns, int num)
  1330. {
  1331. union ns_mem *mypage;
  1332. if (ns->cfile) {
  1333. if (!test_bit(ns->regs.row, ns->pages_written)) {
  1334. NS_DBG("read_page: page %d not written\n", ns->regs.row);
  1335. memset(ns->buf.byte, 0xFF, num);
  1336. } else {
  1337. loff_t pos;
  1338. ssize_t tx;
  1339. NS_DBG("read_page: page %d written, reading from %d\n",
  1340. ns->regs.row, ns->regs.column + ns->regs.off);
  1341. if (do_read_error(ns, num))
  1342. return;
  1343. pos = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
  1344. tx = read_file(ns, ns->cfile, ns->buf.byte, num, pos);
  1345. if (tx != num) {
  1346. NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1347. return;
  1348. }
  1349. do_bit_flips(ns, num);
  1350. }
  1351. return;
  1352. }
  1353. mypage = NS_GET_PAGE(ns);
  1354. if (mypage->byte == NULL) {
  1355. NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
  1356. memset(ns->buf.byte, 0xFF, num);
  1357. } else {
  1358. NS_DBG("read_page: page %d allocated, reading from %d\n",
  1359. ns->regs.row, ns->regs.column + ns->regs.off);
  1360. if (do_read_error(ns, num))
  1361. return;
  1362. memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
  1363. do_bit_flips(ns, num);
  1364. }
  1365. }
  1366. /*
  1367. * Erase all pages in the specified sector.
  1368. */
  1369. static void erase_sector(struct nandsim *ns)
  1370. {
  1371. union ns_mem *mypage;
  1372. int i;
  1373. if (ns->cfile) {
  1374. for (i = 0; i < ns->geom.pgsec; i++)
  1375. if (__test_and_clear_bit(ns->regs.row + i,
  1376. ns->pages_written)) {
  1377. NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
  1378. }
  1379. return;
  1380. }
  1381. mypage = NS_GET_PAGE(ns);
  1382. for (i = 0; i < ns->geom.pgsec; i++) {
  1383. if (mypage->byte != NULL) {
  1384. NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
  1385. kmem_cache_free(ns->nand_pages_slab, mypage->byte);
  1386. mypage->byte = NULL;
  1387. }
  1388. mypage++;
  1389. }
  1390. }
  1391. /*
  1392. * Program the specified page with the contents from the NAND buffer.
  1393. */
  1394. static int prog_page(struct nandsim *ns, int num)
  1395. {
  1396. int i;
  1397. union ns_mem *mypage;
  1398. u_char *pg_off;
  1399. if (ns->cfile) {
  1400. loff_t off;
  1401. ssize_t tx;
  1402. int all;
  1403. NS_DBG("prog_page: writing page %d\n", ns->regs.row);
  1404. pg_off = ns->file_buf + ns->regs.column + ns->regs.off;
  1405. off = (loff_t)NS_RAW_OFFSET(ns) + ns->regs.off;
  1406. if (!test_bit(ns->regs.row, ns->pages_written)) {
  1407. all = 1;
  1408. memset(ns->file_buf, 0xff, ns->geom.pgszoob);
  1409. } else {
  1410. all = 0;
  1411. tx = read_file(ns, ns->cfile, pg_off, num, off);
  1412. if (tx != num) {
  1413. NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1414. return -1;
  1415. }
  1416. }
  1417. for (i = 0; i < num; i++)
  1418. pg_off[i] &= ns->buf.byte[i];
  1419. if (all) {
  1420. loff_t pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
  1421. tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, pos);
  1422. if (tx != ns->geom.pgszoob) {
  1423. NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1424. return -1;
  1425. }
  1426. __set_bit(ns->regs.row, ns->pages_written);
  1427. } else {
  1428. tx = write_file(ns, ns->cfile, pg_off, num, off);
  1429. if (tx != num) {
  1430. NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1431. return -1;
  1432. }
  1433. }
  1434. return 0;
  1435. }
  1436. mypage = NS_GET_PAGE(ns);
  1437. if (mypage->byte == NULL) {
  1438. NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
  1439. /*
  1440. * We allocate memory with GFP_NOFS because a flash FS may
  1441. * utilize this. If it is holding an FS lock, then gets here,
  1442. * then kernel memory alloc runs writeback which goes to the FS
  1443. * again and deadlocks. This was seen in practice.
  1444. */
  1445. mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
  1446. if (mypage->byte == NULL) {
  1447. NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
  1448. return -1;
  1449. }
  1450. memset(mypage->byte, 0xFF, ns->geom.pgszoob);
  1451. }
  1452. pg_off = NS_PAGE_BYTE_OFF(ns);
  1453. for (i = 0; i < num; i++)
  1454. pg_off[i] &= ns->buf.byte[i];
  1455. return 0;
  1456. }
  1457. /*
  1458. * If state has any action bit, perform this action.
  1459. *
  1460. * RETURNS: 0 if success, -1 if error.
  1461. */
  1462. static int do_state_action(struct nandsim *ns, uint32_t action)
  1463. {
  1464. int num;
  1465. int busdiv = ns->busw == 8 ? 1 : 2;
  1466. unsigned int erase_block_no, page_no;
  1467. action &= ACTION_MASK;
  1468. /* Check that page address input is correct */
  1469. if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
  1470. NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
  1471. return -1;
  1472. }
  1473. switch (action) {
  1474. case ACTION_CPY:
  1475. /*
  1476. * Copy page data to the internal buffer.
  1477. */
  1478. /* Column shouldn't be very large */
  1479. if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
  1480. NS_ERR("do_state_action: column number is too large\n");
  1481. break;
  1482. }
  1483. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  1484. read_page(ns, num);
  1485. NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
  1486. num, NS_RAW_OFFSET(ns) + ns->regs.off);
  1487. if (ns->regs.off == 0)
  1488. NS_LOG("read page %d\n", ns->regs.row);
  1489. else if (ns->regs.off < ns->geom.pgsz)
  1490. NS_LOG("read page %d (second half)\n", ns->regs.row);
  1491. else
  1492. NS_LOG("read OOB of page %d\n", ns->regs.row);
  1493. NS_UDELAY(access_delay);
  1494. NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
  1495. break;
  1496. case ACTION_SECERASE:
  1497. /*
  1498. * Erase sector.
  1499. */
  1500. if (ns->lines.wp) {
  1501. NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
  1502. return -1;
  1503. }
  1504. if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
  1505. || (ns->regs.row & ~(ns->geom.secsz - 1))) {
  1506. NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
  1507. return -1;
  1508. }
  1509. ns->regs.row = (ns->regs.row <<
  1510. 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
  1511. ns->regs.column = 0;
  1512. erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
  1513. NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
  1514. ns->regs.row, NS_RAW_OFFSET(ns));
  1515. NS_LOG("erase sector %u\n", erase_block_no);
  1516. erase_sector(ns);
  1517. NS_MDELAY(erase_delay);
  1518. if (erase_block_wear)
  1519. update_wear(erase_block_no);
  1520. if (erase_error(erase_block_no)) {
  1521. NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
  1522. return -1;
  1523. }
  1524. break;
  1525. case ACTION_PRGPAGE:
  1526. /*
  1527. * Program page - move internal buffer data to the page.
  1528. */
  1529. if (ns->lines.wp) {
  1530. NS_WARN("do_state_action: device is write-protected, programm\n");
  1531. return -1;
  1532. }
  1533. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  1534. if (num != ns->regs.count) {
  1535. NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
  1536. ns->regs.count, num);
  1537. return -1;
  1538. }
  1539. if (prog_page(ns, num) == -1)
  1540. return -1;
  1541. page_no = ns->regs.row;
  1542. NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
  1543. num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
  1544. NS_LOG("programm page %d\n", ns->regs.row);
  1545. NS_UDELAY(programm_delay);
  1546. NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
  1547. if (write_error(page_no)) {
  1548. NS_WARN("simulating write failure in page %u\n", page_no);
  1549. return -1;
  1550. }
  1551. break;
  1552. case ACTION_ZEROOFF:
  1553. NS_DBG("do_state_action: set internal offset to 0\n");
  1554. ns->regs.off = 0;
  1555. break;
  1556. case ACTION_HALFOFF:
  1557. if (!(ns->options & OPT_PAGE512_8BIT)) {
  1558. NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
  1559. "byte page size 8x chips\n");
  1560. return -1;
  1561. }
  1562. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
  1563. ns->regs.off = ns->geom.pgsz/2;
  1564. break;
  1565. case ACTION_OOBOFF:
  1566. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
  1567. ns->regs.off = ns->geom.pgsz;
  1568. break;
  1569. default:
  1570. NS_DBG("do_state_action: BUG! unknown action\n");
  1571. }
  1572. return 0;
  1573. }
  1574. /*
  1575. * Switch simulator's state.
  1576. */
  1577. static void switch_state(struct nandsim *ns)
  1578. {
  1579. if (ns->op) {
  1580. /*
  1581. * The current operation have already been identified.
  1582. * Just follow the states chain.
  1583. */
  1584. ns->stateidx += 1;
  1585. ns->state = ns->nxstate;
  1586. ns->nxstate = ns->op[ns->stateidx + 1];
  1587. NS_DBG("switch_state: operation is known, switch to the next state, "
  1588. "state: %s, nxstate: %s\n",
  1589. get_state_name(ns->state), get_state_name(ns->nxstate));
  1590. /* See, whether we need to do some action */
  1591. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1592. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1593. return;
  1594. }
  1595. } else {
  1596. /*
  1597. * We don't yet know which operation we perform.
  1598. * Try to identify it.
  1599. */
  1600. /*
  1601. * The only event causing the switch_state function to
  1602. * be called with yet unknown operation is new command.
  1603. */
  1604. ns->state = get_state_by_command(ns->regs.command);
  1605. NS_DBG("switch_state: operation is unknown, try to find it\n");
  1606. if (find_operation(ns, 0) != 0)
  1607. return;
  1608. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1609. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1610. return;
  1611. }
  1612. }
  1613. /* For 16x devices column means the page offset in words */
  1614. if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
  1615. NS_DBG("switch_state: double the column number for 16x device\n");
  1616. ns->regs.column <<= 1;
  1617. }
  1618. if (NS_STATE(ns->nxstate) == STATE_READY) {
  1619. /*
  1620. * The current state is the last. Return to STATE_READY
  1621. */
  1622. u_char status = NS_STATUS_OK(ns);
  1623. /* In case of data states, see if all bytes were input/output */
  1624. if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
  1625. && ns->regs.count != ns->regs.num) {
  1626. NS_WARN("switch_state: not all bytes were processed, %d left\n",
  1627. ns->regs.num - ns->regs.count);
  1628. status = NS_STATUS_FAILED(ns);
  1629. }
  1630. NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
  1631. switch_to_ready_state(ns, status);
  1632. return;
  1633. } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
  1634. /*
  1635. * If the next state is data input/output, switch to it now
  1636. */
  1637. ns->state = ns->nxstate;
  1638. ns->nxstate = ns->op[++ns->stateidx + 1];
  1639. ns->regs.num = ns->regs.count = 0;
  1640. NS_DBG("switch_state: the next state is data I/O, switch, "
  1641. "state: %s, nxstate: %s\n",
  1642. get_state_name(ns->state), get_state_name(ns->nxstate));
  1643. /*
  1644. * Set the internal register to the count of bytes which
  1645. * are expected to be input or output
  1646. */
  1647. switch (NS_STATE(ns->state)) {
  1648. case STATE_DATAIN:
  1649. case STATE_DATAOUT:
  1650. ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  1651. break;
  1652. case STATE_DATAOUT_ID:
  1653. ns->regs.num = ns->geom.idbytes;
  1654. break;
  1655. case STATE_DATAOUT_STATUS:
  1656. ns->regs.count = ns->regs.num = 0;
  1657. break;
  1658. default:
  1659. NS_ERR("switch_state: BUG! unknown data state\n");
  1660. }
  1661. } else if (ns->nxstate & STATE_ADDR_MASK) {
  1662. /*
  1663. * If the next state is address input, set the internal
  1664. * register to the number of expected address bytes
  1665. */
  1666. ns->regs.count = 0;
  1667. switch (NS_STATE(ns->nxstate)) {
  1668. case STATE_ADDR_PAGE:
  1669. ns->regs.num = ns->geom.pgaddrbytes;
  1670. break;
  1671. case STATE_ADDR_SEC:
  1672. ns->regs.num = ns->geom.secaddrbytes;
  1673. break;
  1674. case STATE_ADDR_ZERO:
  1675. ns->regs.num = 1;
  1676. break;
  1677. case STATE_ADDR_COLUMN:
  1678. /* Column address is always 2 bytes */
  1679. ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
  1680. break;
  1681. default:
  1682. NS_ERR("switch_state: BUG! unknown address state\n");
  1683. }
  1684. } else {
  1685. /*
  1686. * Just reset internal counters.
  1687. */
  1688. ns->regs.num = 0;
  1689. ns->regs.count = 0;
  1690. }
  1691. }
  1692. static u_char ns_nand_read_byte(struct mtd_info *mtd)
  1693. {
  1694. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1695. u_char outb = 0x00;
  1696. /* Sanity and correctness checks */
  1697. if (!ns->lines.ce) {
  1698. NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
  1699. return outb;
  1700. }
  1701. if (ns->lines.ale || ns->lines.cle) {
  1702. NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
  1703. return outb;
  1704. }
  1705. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1706. NS_WARN("read_byte: unexpected data output cycle, state is %s "
  1707. "return %#x\n", get_state_name(ns->state), (uint)outb);
  1708. return outb;
  1709. }
  1710. /* Status register may be read as many times as it is wanted */
  1711. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
  1712. NS_DBG("read_byte: return %#x status\n", ns->regs.status);
  1713. return ns->regs.status;
  1714. }
  1715. /* Check if there is any data in the internal buffer which may be read */
  1716. if (ns->regs.count == ns->regs.num) {
  1717. NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
  1718. return outb;
  1719. }
  1720. switch (NS_STATE(ns->state)) {
  1721. case STATE_DATAOUT:
  1722. if (ns->busw == 8) {
  1723. outb = ns->buf.byte[ns->regs.count];
  1724. ns->regs.count += 1;
  1725. } else {
  1726. outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
  1727. ns->regs.count += 2;
  1728. }
  1729. break;
  1730. case STATE_DATAOUT_ID:
  1731. NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
  1732. outb = ns->ids[ns->regs.count];
  1733. ns->regs.count += 1;
  1734. break;
  1735. default:
  1736. BUG();
  1737. }
  1738. if (ns->regs.count == ns->regs.num) {
  1739. NS_DBG("read_byte: all bytes were read\n");
  1740. if (NS_STATE(ns->nxstate) == STATE_READY)
  1741. switch_state(ns);
  1742. }
  1743. return outb;
  1744. }
  1745. static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
  1746. {
  1747. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1748. /* Sanity and correctness checks */
  1749. if (!ns->lines.ce) {
  1750. NS_ERR("write_byte: chip is disabled, ignore write\n");
  1751. return;
  1752. }
  1753. if (ns->lines.ale && ns->lines.cle) {
  1754. NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
  1755. return;
  1756. }
  1757. if (ns->lines.cle == 1) {
  1758. /*
  1759. * The byte written is a command.
  1760. */
  1761. if (byte == NAND_CMD_RESET) {
  1762. NS_LOG("reset chip\n");
  1763. switch_to_ready_state(ns, NS_STATUS_OK(ns));
  1764. return;
  1765. }
  1766. /* Check that the command byte is correct */
  1767. if (check_command(byte)) {
  1768. NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
  1769. return;
  1770. }
  1771. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
  1772. || NS_STATE(ns->state) == STATE_DATAOUT) {
  1773. int row = ns->regs.row;
  1774. switch_state(ns);
  1775. if (byte == NAND_CMD_RNDOUT)
  1776. ns->regs.row = row;
  1777. }
  1778. /* Check if chip is expecting command */
  1779. if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
  1780. /* Do not warn if only 2 id bytes are read */
  1781. if (!(ns->regs.command == NAND_CMD_READID &&
  1782. NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
  1783. /*
  1784. * We are in situation when something else (not command)
  1785. * was expected but command was input. In this case ignore
  1786. * previous command(s)/state(s) and accept the last one.
  1787. */
  1788. NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
  1789. "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
  1790. }
  1791. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1792. }
  1793. NS_DBG("command byte corresponding to %s state accepted\n",
  1794. get_state_name(get_state_by_command(byte)));
  1795. ns->regs.command = byte;
  1796. switch_state(ns);
  1797. } else if (ns->lines.ale == 1) {
  1798. /*
  1799. * The byte written is an address.
  1800. */
  1801. if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
  1802. NS_DBG("write_byte: operation isn't known yet, identify it\n");
  1803. if (find_operation(ns, 1) < 0)
  1804. return;
  1805. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1806. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1807. return;
  1808. }
  1809. ns->regs.count = 0;
  1810. switch (NS_STATE(ns->nxstate)) {
  1811. case STATE_ADDR_PAGE:
  1812. ns->regs.num = ns->geom.pgaddrbytes;
  1813. break;
  1814. case STATE_ADDR_SEC:
  1815. ns->regs.num = ns->geom.secaddrbytes;
  1816. break;
  1817. case STATE_ADDR_ZERO:
  1818. ns->regs.num = 1;
  1819. break;
  1820. default:
  1821. BUG();
  1822. }
  1823. }
  1824. /* Check that chip is expecting address */
  1825. if (!(ns->nxstate & STATE_ADDR_MASK)) {
  1826. NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
  1827. "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
  1828. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1829. return;
  1830. }
  1831. /* Check if this is expected byte */
  1832. if (ns->regs.count == ns->regs.num) {
  1833. NS_ERR("write_byte: no more address bytes expected\n");
  1834. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1835. return;
  1836. }
  1837. accept_addr_byte(ns, byte);
  1838. ns->regs.count += 1;
  1839. NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
  1840. (uint)byte, ns->regs.count, ns->regs.num);
  1841. if (ns->regs.count == ns->regs.num) {
  1842. NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
  1843. switch_state(ns);
  1844. }
  1845. } else {
  1846. /*
  1847. * The byte written is an input data.
  1848. */
  1849. /* Check that chip is expecting data input */
  1850. if (!(ns->state & STATE_DATAIN_MASK)) {
  1851. NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
  1852. "switch to %s\n", (uint)byte,
  1853. get_state_name(ns->state), get_state_name(STATE_READY));
  1854. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1855. return;
  1856. }
  1857. /* Check if this is expected byte */
  1858. if (ns->regs.count == ns->regs.num) {
  1859. NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
  1860. ns->regs.num);
  1861. return;
  1862. }
  1863. if (ns->busw == 8) {
  1864. ns->buf.byte[ns->regs.count] = byte;
  1865. ns->regs.count += 1;
  1866. } else {
  1867. ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
  1868. ns->regs.count += 2;
  1869. }
  1870. }
  1871. return;
  1872. }
  1873. static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
  1874. {
  1875. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1876. ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
  1877. ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
  1878. ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
  1879. if (cmd != NAND_CMD_NONE)
  1880. ns_nand_write_byte(mtd, cmd);
  1881. }
  1882. static int ns_device_ready(struct mtd_info *mtd)
  1883. {
  1884. NS_DBG("device_ready\n");
  1885. return 1;
  1886. }
  1887. static uint16_t ns_nand_read_word(struct mtd_info *mtd)
  1888. {
  1889. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  1890. NS_DBG("read_word\n");
  1891. return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
  1892. }
  1893. static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1894. {
  1895. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1896. /* Check that chip is expecting data input */
  1897. if (!(ns->state & STATE_DATAIN_MASK)) {
  1898. NS_ERR("write_buf: data input isn't expected, state is %s, "
  1899. "switch to STATE_READY\n", get_state_name(ns->state));
  1900. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1901. return;
  1902. }
  1903. /* Check if these are expected bytes */
  1904. if (ns->regs.count + len > ns->regs.num) {
  1905. NS_ERR("write_buf: too many input bytes\n");
  1906. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1907. return;
  1908. }
  1909. memcpy(ns->buf.byte + ns->regs.count, buf, len);
  1910. ns->regs.count += len;
  1911. if (ns->regs.count == ns->regs.num) {
  1912. NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
  1913. }
  1914. }
  1915. static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  1916. {
  1917. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1918. /* Sanity and correctness checks */
  1919. if (!ns->lines.ce) {
  1920. NS_ERR("read_buf: chip is disabled\n");
  1921. return;
  1922. }
  1923. if (ns->lines.ale || ns->lines.cle) {
  1924. NS_ERR("read_buf: ALE or CLE pin is high\n");
  1925. return;
  1926. }
  1927. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1928. NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
  1929. get_state_name(ns->state));
  1930. return;
  1931. }
  1932. if (NS_STATE(ns->state) != STATE_DATAOUT) {
  1933. int i;
  1934. for (i = 0; i < len; i++)
  1935. buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
  1936. return;
  1937. }
  1938. /* Check if these are expected bytes */
  1939. if (ns->regs.count + len > ns->regs.num) {
  1940. NS_ERR("read_buf: too many bytes to read\n");
  1941. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1942. return;
  1943. }
  1944. memcpy(buf, ns->buf.byte + ns->regs.count, len);
  1945. ns->regs.count += len;
  1946. if (ns->regs.count == ns->regs.num) {
  1947. if (NS_STATE(ns->nxstate) == STATE_READY)
  1948. switch_state(ns);
  1949. }
  1950. return;
  1951. }
  1952. /*
  1953. * Module initialization function
  1954. */
  1955. static int __init ns_init_module(void)
  1956. {
  1957. struct nand_chip *chip;
  1958. struct nandsim *nand;
  1959. int retval = -ENOMEM, i;
  1960. if (bus_width != 8 && bus_width != 16) {
  1961. NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
  1962. return -EINVAL;
  1963. }
  1964. /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
  1965. nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
  1966. + sizeof(struct nandsim), GFP_KERNEL);
  1967. if (!nsmtd) {
  1968. NS_ERR("unable to allocate core structures.\n");
  1969. return -ENOMEM;
  1970. }
  1971. chip = (struct nand_chip *)(nsmtd + 1);
  1972. nsmtd->priv = (void *)chip;
  1973. nand = (struct nandsim *)(chip + 1);
  1974. chip->priv = (void *)nand;
  1975. /*
  1976. * Register simulator's callbacks.
  1977. */
  1978. chip->cmd_ctrl = ns_hwcontrol;
  1979. chip->read_byte = ns_nand_read_byte;
  1980. chip->dev_ready = ns_device_ready;
  1981. chip->write_buf = ns_nand_write_buf;
  1982. chip->read_buf = ns_nand_read_buf;
  1983. chip->read_word = ns_nand_read_word;
  1984. chip->ecc.mode = NAND_ECC_SOFT;
  1985. /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
  1986. /* and 'badblocks' parameters to work */
  1987. chip->options |= NAND_SKIP_BBTSCAN;
  1988. switch (bbt) {
  1989. case 2:
  1990. chip->bbt_options |= NAND_BBT_NO_OOB;
  1991. case 1:
  1992. chip->bbt_options |= NAND_BBT_USE_FLASH;
  1993. case 0:
  1994. break;
  1995. default:
  1996. NS_ERR("bbt has to be 0..2\n");
  1997. retval = -EINVAL;
  1998. goto error;
  1999. }
  2000. /*
  2001. * Perform minimum nandsim structure initialization to handle
  2002. * the initial ID read command correctly
  2003. */
  2004. if (id_bytes[6] != 0xFF || id_bytes[7] != 0xFF)
  2005. nand->geom.idbytes = 8;
  2006. else if (id_bytes[4] != 0xFF || id_bytes[5] != 0xFF)
  2007. nand->geom.idbytes = 6;
  2008. else if (id_bytes[2] != 0xFF || id_bytes[3] != 0xFF)
  2009. nand->geom.idbytes = 4;
  2010. else
  2011. nand->geom.idbytes = 2;
  2012. nand->regs.status = NS_STATUS_OK(nand);
  2013. nand->nxstate = STATE_UNKNOWN;
  2014. nand->options |= OPT_PAGE512; /* temporary value */
  2015. memcpy(nand->ids, id_bytes, sizeof(nand->ids));
  2016. if (bus_width == 16) {
  2017. nand->busw = 16;
  2018. chip->options |= NAND_BUSWIDTH_16;
  2019. }
  2020. nsmtd->owner = THIS_MODULE;
  2021. if ((retval = parse_weakblocks()) != 0)
  2022. goto error;
  2023. if ((retval = parse_weakpages()) != 0)
  2024. goto error;
  2025. if ((retval = parse_gravepages()) != 0)
  2026. goto error;
  2027. retval = nand_scan_ident(nsmtd, 1, NULL);
  2028. if (retval) {
  2029. NS_ERR("cannot scan NAND Simulator device\n");
  2030. if (retval > 0)
  2031. retval = -ENXIO;
  2032. goto error;
  2033. }
  2034. if (bch) {
  2035. unsigned int eccsteps, eccbytes;
  2036. if (!mtd_nand_has_bch()) {
  2037. NS_ERR("BCH ECC support is disabled\n");
  2038. retval = -EINVAL;
  2039. goto error;
  2040. }
  2041. /* use 512-byte ecc blocks */
  2042. eccsteps = nsmtd->writesize/512;
  2043. eccbytes = (bch*13+7)/8;
  2044. /* do not bother supporting small page devices */
  2045. if ((nsmtd->oobsize < 64) || !eccsteps) {
  2046. NS_ERR("bch not available on small page devices\n");
  2047. retval = -EINVAL;
  2048. goto error;
  2049. }
  2050. if ((eccbytes*eccsteps+2) > nsmtd->oobsize) {
  2051. NS_ERR("invalid bch value %u\n", bch);
  2052. retval = -EINVAL;
  2053. goto error;
  2054. }
  2055. chip->ecc.mode = NAND_ECC_SOFT_BCH;
  2056. chip->ecc.size = 512;
  2057. chip->ecc.strength = bch;
  2058. chip->ecc.bytes = eccbytes;
  2059. NS_INFO("using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
  2060. }
  2061. retval = nand_scan_tail(nsmtd);
  2062. if (retval) {
  2063. NS_ERR("can't register NAND Simulator\n");
  2064. if (retval > 0)
  2065. retval = -ENXIO;
  2066. goto error;
  2067. }
  2068. if (overridesize) {
  2069. uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
  2070. if (new_size >> overridesize != nsmtd->erasesize) {
  2071. NS_ERR("overridesize is too big\n");
  2072. retval = -EINVAL;
  2073. goto err_exit;
  2074. }
  2075. /* N.B. This relies on nand_scan not doing anything with the size before we change it */
  2076. nsmtd->size = new_size;
  2077. chip->chipsize = new_size;
  2078. chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
  2079. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2080. }
  2081. if ((retval = setup_wear_reporting(nsmtd)) != 0)
  2082. goto err_exit;
  2083. if ((retval = nandsim_debugfs_create(nand)) != 0)
  2084. goto err_exit;
  2085. if ((retval = init_nandsim(nsmtd)) != 0)
  2086. goto err_exit;
  2087. if ((retval = chip->scan_bbt(nsmtd)) != 0)
  2088. goto err_exit;
  2089. if ((retval = parse_badblocks(nand, nsmtd)) != 0)
  2090. goto err_exit;
  2091. /* Register NAND partitions */
  2092. retval = mtd_device_register(nsmtd, &nand->partitions[0],
  2093. nand->nbparts);
  2094. if (retval != 0)
  2095. goto err_exit;
  2096. return 0;
  2097. err_exit:
  2098. free_nandsim(nand);
  2099. nand_release(nsmtd);
  2100. for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
  2101. kfree(nand->partitions[i].name);
  2102. error:
  2103. kfree(nsmtd);
  2104. free_lists();
  2105. return retval;
  2106. }
  2107. module_init(ns_init_module);
  2108. /*
  2109. * Module clean-up function
  2110. */
  2111. static void __exit ns_cleanup_module(void)
  2112. {
  2113. struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
  2114. int i;
  2115. nandsim_debugfs_remove(ns);
  2116. free_nandsim(ns); /* Free nandsim private resources */
  2117. nand_release(nsmtd); /* Unregister driver */
  2118. for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
  2119. kfree(ns->partitions[i].name);
  2120. kfree(nsmtd); /* Free other structures */
  2121. free_lists();
  2122. }
  2123. module_exit(ns_cleanup_module);
  2124. MODULE_LICENSE ("GPL");
  2125. MODULE_AUTHOR ("Artem B. Bityuckiy");
  2126. MODULE_DESCRIPTION ("The NAND flash simulator");