nandsim.c 66 KB

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