nandsim.c 66 KB

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