nandsim.c 66 KB

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