nandsim.c 67 KB

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