nandsim.c 66 KB

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