nandsim.c 67 KB

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