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