nandsim.c 59 KB

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