nandsim.c 66 KB

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