nandsim.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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. /* Default simulator parameters values */
  40. #if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
  41. !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
  42. !defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
  43. !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
  44. #define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
  45. #define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
  46. #define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF /* No byte */
  47. #define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */
  48. #endif
  49. #ifndef CONFIG_NANDSIM_ACCESS_DELAY
  50. #define CONFIG_NANDSIM_ACCESS_DELAY 25
  51. #endif
  52. #ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
  53. #define CONFIG_NANDSIM_PROGRAMM_DELAY 200
  54. #endif
  55. #ifndef CONFIG_NANDSIM_ERASE_DELAY
  56. #define CONFIG_NANDSIM_ERASE_DELAY 2
  57. #endif
  58. #ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
  59. #define CONFIG_NANDSIM_OUTPUT_CYCLE 40
  60. #endif
  61. #ifndef CONFIG_NANDSIM_INPUT_CYCLE
  62. #define CONFIG_NANDSIM_INPUT_CYCLE 50
  63. #endif
  64. #ifndef CONFIG_NANDSIM_BUS_WIDTH
  65. #define CONFIG_NANDSIM_BUS_WIDTH 8
  66. #endif
  67. #ifndef CONFIG_NANDSIM_DO_DELAYS
  68. #define CONFIG_NANDSIM_DO_DELAYS 0
  69. #endif
  70. #ifndef CONFIG_NANDSIM_LOG
  71. #define CONFIG_NANDSIM_LOG 0
  72. #endif
  73. #ifndef CONFIG_NANDSIM_DBG
  74. #define CONFIG_NANDSIM_DBG 0
  75. #endif
  76. static uint first_id_byte = CONFIG_NANDSIM_FIRST_ID_BYTE;
  77. static uint second_id_byte = CONFIG_NANDSIM_SECOND_ID_BYTE;
  78. static uint third_id_byte = CONFIG_NANDSIM_THIRD_ID_BYTE;
  79. static uint fourth_id_byte = CONFIG_NANDSIM_FOURTH_ID_BYTE;
  80. static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
  81. static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
  82. static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
  83. static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
  84. static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
  85. static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
  86. static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
  87. static uint log = CONFIG_NANDSIM_LOG;
  88. static uint dbg = CONFIG_NANDSIM_DBG;
  89. module_param(first_id_byte, uint, 0400);
  90. module_param(second_id_byte, uint, 0400);
  91. module_param(third_id_byte, uint, 0400);
  92. module_param(fourth_id_byte, uint, 0400);
  93. module_param(access_delay, uint, 0400);
  94. module_param(programm_delay, uint, 0400);
  95. module_param(erase_delay, uint, 0400);
  96. module_param(output_cycle, uint, 0400);
  97. module_param(input_cycle, uint, 0400);
  98. module_param(bus_width, uint, 0400);
  99. module_param(do_delays, uint, 0400);
  100. module_param(log, uint, 0400);
  101. module_param(dbg, uint, 0400);
  102. MODULE_PARM_DESC(first_id_byte, "The fist byte returned by NAND Flash 'read ID' command (manufaturer ID)");
  103. MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID)");
  104. MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command");
  105. MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command");
  106. MODULE_PARM_DESC(access_delay, "Initial page access delay (microiseconds)");
  107. MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
  108. MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
  109. MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanodeconds)");
  110. MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanodeconds)");
  111. MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
  112. MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
  113. MODULE_PARM_DESC(log, "Perform logging if not zero");
  114. MODULE_PARM_DESC(dbg, "Output debug information if not zero");
  115. /* The largest possible page size */
  116. #define NS_LARGEST_PAGE_SIZE 2048
  117. /* The prefix for simulator output */
  118. #define NS_OUTPUT_PREFIX "[nandsim]"
  119. /* Simulator's output macros (logging, debugging, warning, error) */
  120. #define NS_LOG(args...) \
  121. do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0)
  122. #define NS_DBG(args...) \
  123. do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
  124. #define NS_WARN(args...) \
  125. do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warnig: " args); } while(0)
  126. #define NS_ERR(args...) \
  127. do { printk(KERN_ERR NS_OUTPUT_PREFIX " errorr: " args); } while(0)
  128. /* Busy-wait delay macros (microseconds, milliseconds) */
  129. #define NS_UDELAY(us) \
  130. do { if (do_delays) udelay(us); } while(0)
  131. #define NS_MDELAY(us) \
  132. do { if (do_delays) mdelay(us); } while(0)
  133. /* Is the nandsim structure initialized ? */
  134. #define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
  135. /* Good operation completion status */
  136. #define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
  137. /* Operation failed completion status */
  138. #define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
  139. /* Calculate the page offset in flash RAM image by (row, column) address */
  140. #define NS_RAW_OFFSET(ns) \
  141. (((ns)->regs.row << (ns)->geom.pgshift) + ((ns)->regs.row * (ns)->geom.oobsz) + (ns)->regs.column)
  142. /* Calculate the OOB offset in flash RAM image by (row, column) address */
  143. #define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
  144. /* After a command is input, the simulator goes to one of the following states */
  145. #define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */
  146. #define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */
  147. #define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
  148. #define STATE_CMD_PAGEPROG 0x00000004 /* start page programm */
  149. #define STATE_CMD_READOOB 0x00000005 /* read OOB area */
  150. #define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
  151. #define STATE_CMD_STATUS 0x00000007 /* read status */
  152. #define STATE_CMD_STATUS_M 0x00000008 /* read multi-plane status (isn't implemented) */
  153. #define STATE_CMD_SEQIN 0x00000009 /* sequential data imput */
  154. #define STATE_CMD_READID 0x0000000A /* read ID */
  155. #define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
  156. #define STATE_CMD_RESET 0x0000000C /* reset */
  157. #define STATE_CMD_MASK 0x0000000F /* command states mask */
  158. /* After an addres is input, the simulator goes to one of these states */
  159. #define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
  160. #define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
  161. #define STATE_ADDR_ZERO 0x00000030 /* one byte zero address was accepted */
  162. #define STATE_ADDR_MASK 0x00000030 /* address states mask */
  163. /* Durind data input/output the simulator is in these states */
  164. #define STATE_DATAIN 0x00000100 /* waiting for data input */
  165. #define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
  166. #define STATE_DATAOUT 0x00001000 /* waiting for page data output */
  167. #define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */
  168. #define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */
  169. #define STATE_DATAOUT_STATUS_M 0x00004000 /* waiting for multi-plane status output */
  170. #define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
  171. /* Previous operation is done, ready to accept new requests */
  172. #define STATE_READY 0x00000000
  173. /* This state is used to mark that the next state isn't known yet */
  174. #define STATE_UNKNOWN 0x10000000
  175. /* Simulator's actions bit masks */
  176. #define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */
  177. #define ACTION_PRGPAGE 0x00200000 /* programm the internal buffer to flash */
  178. #define ACTION_SECERASE 0x00300000 /* erase sector */
  179. #define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */
  180. #define ACTION_HALFOFF 0x00500000 /* add to address half of page */
  181. #define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */
  182. #define ACTION_MASK 0x00700000 /* action mask */
  183. #define NS_OPER_NUM 12 /* Number of operations supported by the simulator */
  184. #define NS_OPER_STATES 6 /* Maximum number of states in operation */
  185. #define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
  186. #define OPT_PAGE256 0x00000001 /* 256-byte page chips */
  187. #define OPT_PAGE512 0x00000002 /* 512-byte page chips */
  188. #define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
  189. #define OPT_SMARTMEDIA 0x00000010 /* SmartMedia technology chips */
  190. #define OPT_AUTOINCR 0x00000020 /* page number auto inctimentation is possible */
  191. #define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
  192. #define OPT_LARGEPAGE (OPT_PAGE2048) /* 2048-byte page chips */
  193. #define OPT_SMALLPAGE (OPT_PAGE256 | OPT_PAGE512) /* 256 and 512-byte page chips */
  194. /* Remove action bits ftom state */
  195. #define NS_STATE(x) ((x) & ~ACTION_MASK)
  196. /*
  197. * Maximum previous states which need to be saved. Currently saving is
  198. * only needed for page programm operation with preceeded read command
  199. * (which is only valid for 512-byte pages).
  200. */
  201. #define NS_MAX_PREVSTATES 1
  202. /*
  203. * The structure which describes all the internal simulator data.
  204. */
  205. struct nandsim {
  206. struct mtd_partition part;
  207. uint busw; /* flash chip bus width (8 or 16) */
  208. u_char ids[4]; /* chip's ID bytes */
  209. uint32_t options; /* chip's characteristic bits */
  210. uint32_t state; /* current chip state */
  211. uint32_t nxstate; /* next expected state */
  212. uint32_t *op; /* current operation, NULL operations isn't known yet */
  213. uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
  214. uint16_t npstates; /* number of previous states saved */
  215. uint16_t stateidx; /* current state index */
  216. /* The simulated NAND flash image */
  217. union flash_media {
  218. u_char *byte;
  219. uint16_t *word;
  220. } mem;
  221. /* Internal buffer of page + OOB size bytes */
  222. union internal_buffer {
  223. u_char *byte; /* for byte access */
  224. uint16_t *word; /* for 16-bit word access */
  225. } buf;
  226. /* NAND flash "geometry" */
  227. struct nandsin_geometry {
  228. uint32_t totsz; /* total flash size, bytes */
  229. uint32_t secsz; /* flash sector (erase block) size, bytes */
  230. uint pgsz; /* NAND flash page size, bytes */
  231. uint oobsz; /* page OOB area size, bytes */
  232. uint32_t totszoob; /* total flash size including OOB, bytes */
  233. uint pgszoob; /* page size including OOB , bytes*/
  234. uint secszoob; /* sector size including OOB, bytes */
  235. uint pgnum; /* total number of pages */
  236. uint pgsec; /* number of pages per sector */
  237. uint secshift; /* bits number in sector size */
  238. uint pgshift; /* bits number in page size */
  239. uint oobshift; /* bits number in OOB size */
  240. uint pgaddrbytes; /* bytes per page address */
  241. uint secaddrbytes; /* bytes per sector address */
  242. uint idbytes; /* the number ID bytes that this chip outputs */
  243. } geom;
  244. /* NAND flash internal registers */
  245. struct nandsim_regs {
  246. unsigned command; /* the command register */
  247. u_char status; /* the status register */
  248. uint row; /* the page number */
  249. uint column; /* the offset within page */
  250. uint count; /* internal counter */
  251. uint num; /* number of bytes which must be processed */
  252. uint off; /* fixed page offset */
  253. } regs;
  254. /* NAND flash lines state */
  255. struct ns_lines_status {
  256. int ce; /* chip Enable */
  257. int cle; /* command Latch Enable */
  258. int ale; /* address Latch Enable */
  259. int wp; /* write Protect */
  260. } lines;
  261. };
  262. /*
  263. * Operations array. To perform any operation the simulator must pass
  264. * through the correspondent states chain.
  265. */
  266. static struct nandsim_operations {
  267. uint32_t reqopts; /* options which are required to perform the operation */
  268. uint32_t states[NS_OPER_STATES]; /* operation's states */
  269. } ops[NS_OPER_NUM] = {
  270. /* Read page + OOB from the beginning */
  271. {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
  272. STATE_DATAOUT, STATE_READY}},
  273. /* Read page + OOB from the second half */
  274. {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
  275. STATE_DATAOUT, STATE_READY}},
  276. /* Read OOB */
  277. {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
  278. STATE_DATAOUT, STATE_READY}},
  279. /* Programm page starting from the beginning */
  280. {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
  281. STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  282. /* Programm page starting from the beginning */
  283. {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
  284. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  285. /* Programm page starting from the second half */
  286. {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
  287. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  288. /* Programm OOB */
  289. {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
  290. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  291. /* Erase sector */
  292. {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}},
  293. /* Read status */
  294. {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}},
  295. /* Read multi-plane status */
  296. {OPT_SMARTMEDIA, {STATE_CMD_STATUS_M, STATE_DATAOUT_STATUS_M, STATE_READY}},
  297. /* Read ID */
  298. {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
  299. /* Large page devices read page */
  300. {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
  301. STATE_DATAOUT, STATE_READY}}
  302. };
  303. /* MTD structure for NAND controller */
  304. static struct mtd_info *nsmtd;
  305. static u_char ns_verify_buf[NS_LARGEST_PAGE_SIZE];
  306. /*
  307. * Initialize the nandsim structure.
  308. *
  309. * RETURNS: 0 if success, -ERRNO if failure.
  310. */
  311. static int
  312. init_nandsim(struct mtd_info *mtd)
  313. {
  314. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  315. struct nandsim *ns = (struct nandsim *)(chip->priv);
  316. int i;
  317. if (NS_IS_INITIALIZED(ns)) {
  318. NS_ERR("init_nandsim: nandsim is already initialized\n");
  319. return -EIO;
  320. }
  321. /* Force mtd to not do delays */
  322. chip->chip_delay = 0;
  323. /* Initialize the NAND flash parameters */
  324. ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
  325. ns->geom.totsz = mtd->size;
  326. ns->geom.pgsz = mtd->writesize;
  327. ns->geom.oobsz = mtd->oobsize;
  328. ns->geom.secsz = mtd->erasesize;
  329. ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
  330. ns->geom.pgnum = ns->geom.totsz / ns->geom.pgsz;
  331. ns->geom.totszoob = ns->geom.totsz + ns->geom.pgnum * ns->geom.oobsz;
  332. ns->geom.secshift = ffs(ns->geom.secsz) - 1;
  333. ns->geom.pgshift = chip->page_shift;
  334. ns->geom.oobshift = ffs(ns->geom.oobsz) - 1;
  335. ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
  336. ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
  337. ns->options = 0;
  338. if (ns->geom.pgsz == 256) {
  339. ns->options |= OPT_PAGE256;
  340. }
  341. else if (ns->geom.pgsz == 512) {
  342. ns->options |= (OPT_PAGE512 | OPT_AUTOINCR);
  343. if (ns->busw == 8)
  344. ns->options |= OPT_PAGE512_8BIT;
  345. } else if (ns->geom.pgsz == 2048) {
  346. ns->options |= OPT_PAGE2048;
  347. } else {
  348. NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
  349. return -EIO;
  350. }
  351. if (ns->options & OPT_SMALLPAGE) {
  352. if (ns->geom.totsz < (64 << 20)) {
  353. ns->geom.pgaddrbytes = 3;
  354. ns->geom.secaddrbytes = 2;
  355. } else {
  356. ns->geom.pgaddrbytes = 4;
  357. ns->geom.secaddrbytes = 3;
  358. }
  359. } else {
  360. if (ns->geom.totsz <= (128 << 20)) {
  361. ns->geom.pgaddrbytes = 5;
  362. ns->geom.secaddrbytes = 2;
  363. } else {
  364. ns->geom.pgaddrbytes = 5;
  365. ns->geom.secaddrbytes = 3;
  366. }
  367. }
  368. /* Detect how many ID bytes the NAND chip outputs */
  369. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  370. if (second_id_byte != nand_flash_ids[i].id)
  371. continue;
  372. if (!(nand_flash_ids[i].options & NAND_NO_AUTOINCR))
  373. ns->options |= OPT_AUTOINCR;
  374. }
  375. if (ns->busw == 16)
  376. NS_WARN("16-bit flashes support wasn't tested\n");
  377. printk("flash size: %u MiB\n", ns->geom.totsz >> 20);
  378. printk("page size: %u bytes\n", ns->geom.pgsz);
  379. printk("OOB area size: %u bytes\n", ns->geom.oobsz);
  380. printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
  381. printk("pages number: %u\n", ns->geom.pgnum);
  382. printk("pages per sector: %u\n", ns->geom.pgsec);
  383. printk("bus width: %u\n", ns->busw);
  384. printk("bits in sector size: %u\n", ns->geom.secshift);
  385. printk("bits in page size: %u\n", ns->geom.pgshift);
  386. printk("bits in OOB size: %u\n", ns->geom.oobshift);
  387. printk("flash size with OOB: %u KiB\n", ns->geom.totszoob >> 10);
  388. printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
  389. printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
  390. printk("options: %#x\n", ns->options);
  391. /* Map / allocate and initialize the flash image */
  392. ns->mem.byte = vmalloc(ns->geom.totszoob);
  393. if (!ns->mem.byte) {
  394. NS_ERR("init_nandsim: unable to allocate %u bytes for flash image\n",
  395. ns->geom.totszoob);
  396. return -ENOMEM;
  397. }
  398. memset(ns->mem.byte, 0xFF, ns->geom.totszoob);
  399. /* Allocate / initialize the internal buffer */
  400. ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  401. if (!ns->buf.byte) {
  402. NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
  403. ns->geom.pgszoob);
  404. goto error;
  405. }
  406. memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
  407. /* Fill the partition_info structure */
  408. ns->part.name = "NAND simulator partition";
  409. ns->part.offset = 0;
  410. ns->part.size = ns->geom.totsz;
  411. return 0;
  412. error:
  413. vfree(ns->mem.byte);
  414. return -ENOMEM;
  415. }
  416. /*
  417. * Free the nandsim structure.
  418. */
  419. static void
  420. free_nandsim(struct nandsim *ns)
  421. {
  422. kfree(ns->buf.byte);
  423. vfree(ns->mem.byte);
  424. return;
  425. }
  426. /*
  427. * Returns the string representation of 'state' state.
  428. */
  429. static char *
  430. get_state_name(uint32_t state)
  431. {
  432. switch (NS_STATE(state)) {
  433. case STATE_CMD_READ0:
  434. return "STATE_CMD_READ0";
  435. case STATE_CMD_READ1:
  436. return "STATE_CMD_READ1";
  437. case STATE_CMD_PAGEPROG:
  438. return "STATE_CMD_PAGEPROG";
  439. case STATE_CMD_READOOB:
  440. return "STATE_CMD_READOOB";
  441. case STATE_CMD_READSTART:
  442. return "STATE_CMD_READSTART";
  443. case STATE_CMD_ERASE1:
  444. return "STATE_CMD_ERASE1";
  445. case STATE_CMD_STATUS:
  446. return "STATE_CMD_STATUS";
  447. case STATE_CMD_STATUS_M:
  448. return "STATE_CMD_STATUS_M";
  449. case STATE_CMD_SEQIN:
  450. return "STATE_CMD_SEQIN";
  451. case STATE_CMD_READID:
  452. return "STATE_CMD_READID";
  453. case STATE_CMD_ERASE2:
  454. return "STATE_CMD_ERASE2";
  455. case STATE_CMD_RESET:
  456. return "STATE_CMD_RESET";
  457. case STATE_ADDR_PAGE:
  458. return "STATE_ADDR_PAGE";
  459. case STATE_ADDR_SEC:
  460. return "STATE_ADDR_SEC";
  461. case STATE_ADDR_ZERO:
  462. return "STATE_ADDR_ZERO";
  463. case STATE_DATAIN:
  464. return "STATE_DATAIN";
  465. case STATE_DATAOUT:
  466. return "STATE_DATAOUT";
  467. case STATE_DATAOUT_ID:
  468. return "STATE_DATAOUT_ID";
  469. case STATE_DATAOUT_STATUS:
  470. return "STATE_DATAOUT_STATUS";
  471. case STATE_DATAOUT_STATUS_M:
  472. return "STATE_DATAOUT_STATUS_M";
  473. case STATE_READY:
  474. return "STATE_READY";
  475. case STATE_UNKNOWN:
  476. return "STATE_UNKNOWN";
  477. }
  478. NS_ERR("get_state_name: unknown state, BUG\n");
  479. return NULL;
  480. }
  481. /*
  482. * Check if command is valid.
  483. *
  484. * RETURNS: 1 if wrong command, 0 if right.
  485. */
  486. static int
  487. check_command(int cmd)
  488. {
  489. switch (cmd) {
  490. case NAND_CMD_READ0:
  491. case NAND_CMD_READSTART:
  492. case NAND_CMD_PAGEPROG:
  493. case NAND_CMD_READOOB:
  494. case NAND_CMD_ERASE1:
  495. case NAND_CMD_STATUS:
  496. case NAND_CMD_SEQIN:
  497. case NAND_CMD_READID:
  498. case NAND_CMD_ERASE2:
  499. case NAND_CMD_RESET:
  500. case NAND_CMD_READ1:
  501. return 0;
  502. case NAND_CMD_STATUS_MULTI:
  503. default:
  504. return 1;
  505. }
  506. }
  507. /*
  508. * Returns state after command is accepted by command number.
  509. */
  510. static uint32_t
  511. get_state_by_command(unsigned command)
  512. {
  513. switch (command) {
  514. case NAND_CMD_READ0:
  515. return STATE_CMD_READ0;
  516. case NAND_CMD_READ1:
  517. return STATE_CMD_READ1;
  518. case NAND_CMD_PAGEPROG:
  519. return STATE_CMD_PAGEPROG;
  520. case NAND_CMD_READSTART:
  521. return STATE_CMD_READSTART;
  522. case NAND_CMD_READOOB:
  523. return STATE_CMD_READOOB;
  524. case NAND_CMD_ERASE1:
  525. return STATE_CMD_ERASE1;
  526. case NAND_CMD_STATUS:
  527. return STATE_CMD_STATUS;
  528. case NAND_CMD_STATUS_MULTI:
  529. return STATE_CMD_STATUS_M;
  530. case NAND_CMD_SEQIN:
  531. return STATE_CMD_SEQIN;
  532. case NAND_CMD_READID:
  533. return STATE_CMD_READID;
  534. case NAND_CMD_ERASE2:
  535. return STATE_CMD_ERASE2;
  536. case NAND_CMD_RESET:
  537. return STATE_CMD_RESET;
  538. }
  539. NS_ERR("get_state_by_command: unknown command, BUG\n");
  540. return 0;
  541. }
  542. /*
  543. * Move an address byte to the correspondent internal register.
  544. */
  545. static inline void
  546. accept_addr_byte(struct nandsim *ns, u_char bt)
  547. {
  548. uint byte = (uint)bt;
  549. if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
  550. ns->regs.column |= (byte << 8 * ns->regs.count);
  551. else {
  552. ns->regs.row |= (byte << 8 * (ns->regs.count -
  553. ns->geom.pgaddrbytes +
  554. ns->geom.secaddrbytes));
  555. }
  556. return;
  557. }
  558. /*
  559. * Switch to STATE_READY state.
  560. */
  561. static inline void
  562. switch_to_ready_state(struct nandsim *ns, u_char status)
  563. {
  564. NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
  565. ns->state = STATE_READY;
  566. ns->nxstate = STATE_UNKNOWN;
  567. ns->op = NULL;
  568. ns->npstates = 0;
  569. ns->stateidx = 0;
  570. ns->regs.num = 0;
  571. ns->regs.count = 0;
  572. ns->regs.off = 0;
  573. ns->regs.row = 0;
  574. ns->regs.column = 0;
  575. ns->regs.status = status;
  576. }
  577. /*
  578. * If the operation isn't known yet, try to find it in the global array
  579. * of supported operations.
  580. *
  581. * Operation can be unknown because of the following.
  582. * 1. New command was accepted and this is the firs call to find the
  583. * correspondent states chain. In this case ns->npstates = 0;
  584. * 2. There is several operations which begin with the same command(s)
  585. * (for example program from the second half and read from the
  586. * second half operations both begin with the READ1 command). In this
  587. * case the ns->pstates[] array contains previous states.
  588. *
  589. * Thus, the function tries to find operation containing the following
  590. * states (if the 'flag' parameter is 0):
  591. * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
  592. *
  593. * If (one and only one) matching operation is found, it is accepted (
  594. * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
  595. * zeroed).
  596. *
  597. * If there are several maches, the current state is pushed to the
  598. * ns->pstates.
  599. *
  600. * The operation can be unknown only while commands are input to the chip.
  601. * As soon as address command is accepted, the operation must be known.
  602. * In such situation the function is called with 'flag' != 0, and the
  603. * operation is searched using the following pattern:
  604. * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
  605. *
  606. * It is supposed that this pattern must either match one operation on
  607. * none. There can't be ambiguity in that case.
  608. *
  609. * If no matches found, the functions does the following:
  610. * 1. if there are saved states present, try to ignore them and search
  611. * again only using the last command. If nothing was found, switch
  612. * to the STATE_READY state.
  613. * 2. if there are no saved states, switch to the STATE_READY state.
  614. *
  615. * RETURNS: -2 - no matched operations found.
  616. * -1 - several matches.
  617. * 0 - operation is found.
  618. */
  619. static int
  620. find_operation(struct nandsim *ns, uint32_t flag)
  621. {
  622. int opsfound = 0;
  623. int i, j, idx = 0;
  624. for (i = 0; i < NS_OPER_NUM; i++) {
  625. int found = 1;
  626. if (!(ns->options & ops[i].reqopts))
  627. /* Ignore operations we can't perform */
  628. continue;
  629. if (flag) {
  630. if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
  631. continue;
  632. } else {
  633. if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
  634. continue;
  635. }
  636. for (j = 0; j < ns->npstates; j++)
  637. if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
  638. && (ns->options & ops[idx].reqopts)) {
  639. found = 0;
  640. break;
  641. }
  642. if (found) {
  643. idx = i;
  644. opsfound += 1;
  645. }
  646. }
  647. if (opsfound == 1) {
  648. /* Exact match */
  649. ns->op = &ops[idx].states[0];
  650. if (flag) {
  651. /*
  652. * In this case the find_operation function was
  653. * called when address has just began input. But it isn't
  654. * yet fully input and the current state must
  655. * not be one of STATE_ADDR_*, but the STATE_ADDR_*
  656. * state must be the next state (ns->nxstate).
  657. */
  658. ns->stateidx = ns->npstates - 1;
  659. } else {
  660. ns->stateidx = ns->npstates;
  661. }
  662. ns->npstates = 0;
  663. ns->state = ns->op[ns->stateidx];
  664. ns->nxstate = ns->op[ns->stateidx + 1];
  665. NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
  666. idx, get_state_name(ns->state), get_state_name(ns->nxstate));
  667. return 0;
  668. }
  669. if (opsfound == 0) {
  670. /* Nothing was found. Try to ignore previous commands (if any) and search again */
  671. if (ns->npstates != 0) {
  672. NS_DBG("find_operation: no operation found, try again with state %s\n",
  673. get_state_name(ns->state));
  674. ns->npstates = 0;
  675. return find_operation(ns, 0);
  676. }
  677. NS_DBG("find_operation: no operations found\n");
  678. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  679. return -2;
  680. }
  681. if (flag) {
  682. /* This shouldn't happen */
  683. NS_DBG("find_operation: BUG, operation must be known if address is input\n");
  684. return -2;
  685. }
  686. NS_DBG("find_operation: there is still ambiguity\n");
  687. ns->pstates[ns->npstates++] = ns->state;
  688. return -1;
  689. }
  690. /*
  691. * If state has any action bit, perform this action.
  692. *
  693. * RETURNS: 0 if success, -1 if error.
  694. */
  695. static int
  696. do_state_action(struct nandsim *ns, uint32_t action)
  697. {
  698. int i, num;
  699. int busdiv = ns->busw == 8 ? 1 : 2;
  700. action &= ACTION_MASK;
  701. /* Check that page address input is correct */
  702. if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
  703. NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
  704. return -1;
  705. }
  706. switch (action) {
  707. case ACTION_CPY:
  708. /*
  709. * Copy page data to the internal buffer.
  710. */
  711. /* Column shouldn't be very large */
  712. if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
  713. NS_ERR("do_state_action: column number is too large\n");
  714. break;
  715. }
  716. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  717. memcpy(ns->buf.byte, ns->mem.byte + NS_RAW_OFFSET(ns) + ns->regs.off, num);
  718. NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
  719. num, NS_RAW_OFFSET(ns) + ns->regs.off);
  720. if (ns->regs.off == 0)
  721. NS_LOG("read page %d\n", ns->regs.row);
  722. else if (ns->regs.off < ns->geom.pgsz)
  723. NS_LOG("read page %d (second half)\n", ns->regs.row);
  724. else
  725. NS_LOG("read OOB of page %d\n", ns->regs.row);
  726. NS_UDELAY(access_delay);
  727. NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
  728. break;
  729. case ACTION_SECERASE:
  730. /*
  731. * Erase sector.
  732. */
  733. if (ns->lines.wp) {
  734. NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
  735. return -1;
  736. }
  737. if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
  738. || (ns->regs.row & ~(ns->geom.secsz - 1))) {
  739. NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
  740. return -1;
  741. }
  742. ns->regs.row = (ns->regs.row <<
  743. 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
  744. ns->regs.column = 0;
  745. NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
  746. ns->regs.row, NS_RAW_OFFSET(ns));
  747. NS_LOG("erase sector %d\n", ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift));
  748. memset(ns->mem.byte + NS_RAW_OFFSET(ns), 0xFF, ns->geom.secszoob);
  749. NS_MDELAY(erase_delay);
  750. break;
  751. case ACTION_PRGPAGE:
  752. /*
  753. * Programm page - move internal buffer data to the page.
  754. */
  755. if (ns->lines.wp) {
  756. NS_WARN("do_state_action: device is write-protected, programm\n");
  757. return -1;
  758. }
  759. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  760. if (num != ns->regs.count) {
  761. NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
  762. ns->regs.count, num);
  763. return -1;
  764. }
  765. for (i = 0; i < num; i++)
  766. ns->mem.byte[NS_RAW_OFFSET(ns) + ns->regs.off + i] &= ns->buf.byte[i];
  767. NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
  768. num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
  769. NS_LOG("programm page %d\n", ns->regs.row);
  770. NS_UDELAY(programm_delay);
  771. NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
  772. break;
  773. case ACTION_ZEROOFF:
  774. NS_DBG("do_state_action: set internal offset to 0\n");
  775. ns->regs.off = 0;
  776. break;
  777. case ACTION_HALFOFF:
  778. if (!(ns->options & OPT_PAGE512_8BIT)) {
  779. NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
  780. "byte page size 8x chips\n");
  781. return -1;
  782. }
  783. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
  784. ns->regs.off = ns->geom.pgsz/2;
  785. break;
  786. case ACTION_OOBOFF:
  787. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
  788. ns->regs.off = ns->geom.pgsz;
  789. break;
  790. default:
  791. NS_DBG("do_state_action: BUG! unknown action\n");
  792. }
  793. return 0;
  794. }
  795. /*
  796. * Switch simulator's state.
  797. */
  798. static void
  799. switch_state(struct nandsim *ns)
  800. {
  801. if (ns->op) {
  802. /*
  803. * The current operation have already been identified.
  804. * Just follow the states chain.
  805. */
  806. ns->stateidx += 1;
  807. ns->state = ns->nxstate;
  808. ns->nxstate = ns->op[ns->stateidx + 1];
  809. NS_DBG("switch_state: operation is known, switch to the next state, "
  810. "state: %s, nxstate: %s\n",
  811. get_state_name(ns->state), get_state_name(ns->nxstate));
  812. /* See, whether we need to do some action */
  813. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  814. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  815. return;
  816. }
  817. } else {
  818. /*
  819. * We don't yet know which operation we perform.
  820. * Try to identify it.
  821. */
  822. /*
  823. * The only event causing the switch_state function to
  824. * be called with yet unknown operation is new command.
  825. */
  826. ns->state = get_state_by_command(ns->regs.command);
  827. NS_DBG("switch_state: operation is unknown, try to find it\n");
  828. if (find_operation(ns, 0) != 0)
  829. return;
  830. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  831. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  832. return;
  833. }
  834. }
  835. /* For 16x devices column means the page offset in words */
  836. if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
  837. NS_DBG("switch_state: double the column number for 16x device\n");
  838. ns->regs.column <<= 1;
  839. }
  840. if (NS_STATE(ns->nxstate) == STATE_READY) {
  841. /*
  842. * The current state is the last. Return to STATE_READY
  843. */
  844. u_char status = NS_STATUS_OK(ns);
  845. /* In case of data states, see if all bytes were input/output */
  846. if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
  847. && ns->regs.count != ns->regs.num) {
  848. NS_WARN("switch_state: not all bytes were processed, %d left\n",
  849. ns->regs.num - ns->regs.count);
  850. status = NS_STATUS_FAILED(ns);
  851. }
  852. NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
  853. switch_to_ready_state(ns, status);
  854. return;
  855. } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
  856. /*
  857. * If the next state is data input/output, switch to it now
  858. */
  859. ns->state = ns->nxstate;
  860. ns->nxstate = ns->op[++ns->stateidx + 1];
  861. ns->regs.num = ns->regs.count = 0;
  862. NS_DBG("switch_state: the next state is data I/O, switch, "
  863. "state: %s, nxstate: %s\n",
  864. get_state_name(ns->state), get_state_name(ns->nxstate));
  865. /*
  866. * Set the internal register to the count of bytes which
  867. * are expected to be input or output
  868. */
  869. switch (NS_STATE(ns->state)) {
  870. case STATE_DATAIN:
  871. case STATE_DATAOUT:
  872. ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  873. break;
  874. case STATE_DATAOUT_ID:
  875. ns->regs.num = ns->geom.idbytes;
  876. break;
  877. case STATE_DATAOUT_STATUS:
  878. case STATE_DATAOUT_STATUS_M:
  879. ns->regs.count = ns->regs.num = 0;
  880. break;
  881. default:
  882. NS_ERR("switch_state: BUG! unknown data state\n");
  883. }
  884. } else if (ns->nxstate & STATE_ADDR_MASK) {
  885. /*
  886. * If the next state is address input, set the internal
  887. * register to the number of expected address bytes
  888. */
  889. ns->regs.count = 0;
  890. switch (NS_STATE(ns->nxstate)) {
  891. case STATE_ADDR_PAGE:
  892. ns->regs.num = ns->geom.pgaddrbytes;
  893. break;
  894. case STATE_ADDR_SEC:
  895. ns->regs.num = ns->geom.secaddrbytes;
  896. break;
  897. case STATE_ADDR_ZERO:
  898. ns->regs.num = 1;
  899. break;
  900. default:
  901. NS_ERR("switch_state: BUG! unknown address state\n");
  902. }
  903. } else {
  904. /*
  905. * Just reset internal counters.
  906. */
  907. ns->regs.num = 0;
  908. ns->regs.count = 0;
  909. }
  910. }
  911. static u_char
  912. ns_nand_read_byte(struct mtd_info *mtd)
  913. {
  914. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  915. u_char outb = 0x00;
  916. /* Sanity and correctness checks */
  917. if (!ns->lines.ce) {
  918. NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
  919. return outb;
  920. }
  921. if (ns->lines.ale || ns->lines.cle) {
  922. NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
  923. return outb;
  924. }
  925. if (!(ns->state & STATE_DATAOUT_MASK)) {
  926. NS_WARN("read_byte: unexpected data output cycle, state is %s "
  927. "return %#x\n", get_state_name(ns->state), (uint)outb);
  928. return outb;
  929. }
  930. /* Status register may be read as many times as it is wanted */
  931. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
  932. NS_DBG("read_byte: return %#x status\n", ns->regs.status);
  933. return ns->regs.status;
  934. }
  935. /* Check if there is any data in the internal buffer which may be read */
  936. if (ns->regs.count == ns->regs.num) {
  937. NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
  938. return outb;
  939. }
  940. switch (NS_STATE(ns->state)) {
  941. case STATE_DATAOUT:
  942. if (ns->busw == 8) {
  943. outb = ns->buf.byte[ns->regs.count];
  944. ns->regs.count += 1;
  945. } else {
  946. outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
  947. ns->regs.count += 2;
  948. }
  949. break;
  950. case STATE_DATAOUT_ID:
  951. NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
  952. outb = ns->ids[ns->regs.count];
  953. ns->regs.count += 1;
  954. break;
  955. default:
  956. BUG();
  957. }
  958. if (ns->regs.count == ns->regs.num) {
  959. NS_DBG("read_byte: all bytes were read\n");
  960. /*
  961. * The OPT_AUTOINCR allows to read next conseqitive pages without
  962. * new read operation cycle.
  963. */
  964. if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) {
  965. ns->regs.count = 0;
  966. if (ns->regs.row + 1 < ns->geom.pgnum)
  967. ns->regs.row += 1;
  968. NS_DBG("read_byte: switch to the next page (%#x)\n", ns->regs.row);
  969. do_state_action(ns, ACTION_CPY);
  970. }
  971. else if (NS_STATE(ns->nxstate) == STATE_READY)
  972. switch_state(ns);
  973. }
  974. return outb;
  975. }
  976. static void
  977. ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
  978. {
  979. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  980. /* Sanity and correctness checks */
  981. if (!ns->lines.ce) {
  982. NS_ERR("write_byte: chip is disabled, ignore write\n");
  983. return;
  984. }
  985. if (ns->lines.ale && ns->lines.cle) {
  986. NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
  987. return;
  988. }
  989. if (ns->lines.cle == 1) {
  990. /*
  991. * The byte written is a command.
  992. */
  993. if (byte == NAND_CMD_RESET) {
  994. NS_LOG("reset chip\n");
  995. switch_to_ready_state(ns, NS_STATUS_OK(ns));
  996. return;
  997. }
  998. /*
  999. * Chip might still be in STATE_DATAOUT
  1000. * (if OPT_AUTOINCR feature is supported), STATE_DATAOUT_STATUS or
  1001. * STATE_DATAOUT_STATUS_M state. If so, switch state.
  1002. */
  1003. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
  1004. || NS_STATE(ns->state) == STATE_DATAOUT_STATUS_M
  1005. || ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT))
  1006. switch_state(ns);
  1007. /* Check if chip is expecting command */
  1008. if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
  1009. /*
  1010. * We are in situation when something else (not command)
  1011. * was expected but command was input. In this case ignore
  1012. * previous command(s)/state(s) and accept the last one.
  1013. */
  1014. NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
  1015. "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
  1016. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1017. }
  1018. /* Check that the command byte is correct */
  1019. if (check_command(byte)) {
  1020. NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
  1021. return;
  1022. }
  1023. NS_DBG("command byte corresponding to %s state accepted\n",
  1024. get_state_name(get_state_by_command(byte)));
  1025. ns->regs.command = byte;
  1026. switch_state(ns);
  1027. } else if (ns->lines.ale == 1) {
  1028. /*
  1029. * The byte written is an address.
  1030. */
  1031. if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
  1032. NS_DBG("write_byte: operation isn't known yet, identify it\n");
  1033. if (find_operation(ns, 1) < 0)
  1034. return;
  1035. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1036. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1037. return;
  1038. }
  1039. ns->regs.count = 0;
  1040. switch (NS_STATE(ns->nxstate)) {
  1041. case STATE_ADDR_PAGE:
  1042. ns->regs.num = ns->geom.pgaddrbytes;
  1043. break;
  1044. case STATE_ADDR_SEC:
  1045. ns->regs.num = ns->geom.secaddrbytes;
  1046. break;
  1047. case STATE_ADDR_ZERO:
  1048. ns->regs.num = 1;
  1049. break;
  1050. default:
  1051. BUG();
  1052. }
  1053. }
  1054. /* Check that chip is expecting address */
  1055. if (!(ns->nxstate & STATE_ADDR_MASK)) {
  1056. NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
  1057. "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
  1058. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1059. return;
  1060. }
  1061. /* Check if this is expected byte */
  1062. if (ns->regs.count == ns->regs.num) {
  1063. NS_ERR("write_byte: no more address bytes expected\n");
  1064. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1065. return;
  1066. }
  1067. accept_addr_byte(ns, byte);
  1068. ns->regs.count += 1;
  1069. NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
  1070. (uint)byte, ns->regs.count, ns->regs.num);
  1071. if (ns->regs.count == ns->regs.num) {
  1072. NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
  1073. switch_state(ns);
  1074. }
  1075. } else {
  1076. /*
  1077. * The byte written is an input data.
  1078. */
  1079. /* Check that chip is expecting data input */
  1080. if (!(ns->state & STATE_DATAIN_MASK)) {
  1081. NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
  1082. "switch to %s\n", (uint)byte,
  1083. get_state_name(ns->state), get_state_name(STATE_READY));
  1084. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1085. return;
  1086. }
  1087. /* Check if this is expected byte */
  1088. if (ns->regs.count == ns->regs.num) {
  1089. NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
  1090. ns->regs.num);
  1091. return;
  1092. }
  1093. if (ns->busw == 8) {
  1094. ns->buf.byte[ns->regs.count] = byte;
  1095. ns->regs.count += 1;
  1096. } else {
  1097. ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
  1098. ns->regs.count += 2;
  1099. }
  1100. }
  1101. return;
  1102. }
  1103. static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
  1104. {
  1105. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1106. ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
  1107. ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
  1108. ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
  1109. if (cmd != NAND_CMD_NONE)
  1110. ns_nand_write_byte(mtd, cmd);
  1111. }
  1112. static int
  1113. ns_device_ready(struct mtd_info *mtd)
  1114. {
  1115. NS_DBG("device_ready\n");
  1116. return 1;
  1117. }
  1118. static uint16_t
  1119. ns_nand_read_word(struct mtd_info *mtd)
  1120. {
  1121. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  1122. NS_DBG("read_word\n");
  1123. return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
  1124. }
  1125. static void
  1126. ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1127. {
  1128. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1129. /* Check that chip is expecting data input */
  1130. if (!(ns->state & STATE_DATAIN_MASK)) {
  1131. NS_ERR("write_buf: data input isn't expected, state is %s, "
  1132. "switch to STATE_READY\n", get_state_name(ns->state));
  1133. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1134. return;
  1135. }
  1136. /* Check if these are expected bytes */
  1137. if (ns->regs.count + len > ns->regs.num) {
  1138. NS_ERR("write_buf: too many input bytes\n");
  1139. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1140. return;
  1141. }
  1142. memcpy(ns->buf.byte + ns->regs.count, buf, len);
  1143. ns->regs.count += len;
  1144. if (ns->regs.count == ns->regs.num) {
  1145. NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
  1146. }
  1147. }
  1148. static void
  1149. ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  1150. {
  1151. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1152. /* Sanity and correctness checks */
  1153. if (!ns->lines.ce) {
  1154. NS_ERR("read_buf: chip is disabled\n");
  1155. return;
  1156. }
  1157. if (ns->lines.ale || ns->lines.cle) {
  1158. NS_ERR("read_buf: ALE or CLE pin is high\n");
  1159. return;
  1160. }
  1161. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1162. NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
  1163. get_state_name(ns->state));
  1164. return;
  1165. }
  1166. if (NS_STATE(ns->state) != STATE_DATAOUT) {
  1167. int i;
  1168. for (i = 0; i < len; i++)
  1169. buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
  1170. return;
  1171. }
  1172. /* Check if these are expected bytes */
  1173. if (ns->regs.count + len > ns->regs.num) {
  1174. NS_ERR("read_buf: too many bytes to read\n");
  1175. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1176. return;
  1177. }
  1178. memcpy(buf, ns->buf.byte + ns->regs.count, len);
  1179. ns->regs.count += len;
  1180. if (ns->regs.count == ns->regs.num) {
  1181. if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) {
  1182. ns->regs.count = 0;
  1183. if (ns->regs.row + 1 < ns->geom.pgnum)
  1184. ns->regs.row += 1;
  1185. NS_DBG("read_buf: switch to the next page (%#x)\n", ns->regs.row);
  1186. do_state_action(ns, ACTION_CPY);
  1187. }
  1188. else if (NS_STATE(ns->nxstate) == STATE_READY)
  1189. switch_state(ns);
  1190. }
  1191. return;
  1192. }
  1193. static int
  1194. ns_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1195. {
  1196. ns_nand_read_buf(mtd, (u_char *)&ns_verify_buf[0], len);
  1197. if (!memcmp(buf, &ns_verify_buf[0], len)) {
  1198. NS_DBG("verify_buf: the buffer is OK\n");
  1199. return 0;
  1200. } else {
  1201. NS_DBG("verify_buf: the buffer is wrong\n");
  1202. return -EFAULT;
  1203. }
  1204. }
  1205. /*
  1206. * Module initialization function
  1207. */
  1208. static int __init ns_init_module(void)
  1209. {
  1210. struct nand_chip *chip;
  1211. struct nandsim *nand;
  1212. int retval = -ENOMEM;
  1213. if (bus_width != 8 && bus_width != 16) {
  1214. NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
  1215. return -EINVAL;
  1216. }
  1217. /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
  1218. nsmtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
  1219. + sizeof(struct nandsim), GFP_KERNEL);
  1220. if (!nsmtd) {
  1221. NS_ERR("unable to allocate core structures.\n");
  1222. return -ENOMEM;
  1223. }
  1224. memset(nsmtd, 0, sizeof(struct mtd_info) + sizeof(struct nand_chip) +
  1225. sizeof(struct nandsim));
  1226. chip = (struct nand_chip *)(nsmtd + 1);
  1227. nsmtd->priv = (void *)chip;
  1228. nand = (struct nandsim *)(chip + 1);
  1229. chip->priv = (void *)nand;
  1230. /*
  1231. * Register simulator's callbacks.
  1232. */
  1233. chip->cmd_ctrl = ns_hwcontrol;
  1234. chip->read_byte = ns_nand_read_byte;
  1235. chip->dev_ready = ns_device_ready;
  1236. chip->write_buf = ns_nand_write_buf;
  1237. chip->read_buf = ns_nand_read_buf;
  1238. chip->verify_buf = ns_nand_verify_buf;
  1239. chip->read_word = ns_nand_read_word;
  1240. chip->ecc.mode = NAND_ECC_SOFT;
  1241. chip->options |= NAND_SKIP_BBTSCAN;
  1242. /*
  1243. * Perform minimum nandsim structure initialization to handle
  1244. * the initial ID read command correctly
  1245. */
  1246. if (third_id_byte != 0xFF || fourth_id_byte != 0xFF)
  1247. nand->geom.idbytes = 4;
  1248. else
  1249. nand->geom.idbytes = 2;
  1250. nand->regs.status = NS_STATUS_OK(nand);
  1251. nand->nxstate = STATE_UNKNOWN;
  1252. nand->options |= OPT_PAGE256; /* temporary value */
  1253. nand->ids[0] = first_id_byte;
  1254. nand->ids[1] = second_id_byte;
  1255. nand->ids[2] = third_id_byte;
  1256. nand->ids[3] = fourth_id_byte;
  1257. if (bus_width == 16) {
  1258. nand->busw = 16;
  1259. chip->options |= NAND_BUSWIDTH_16;
  1260. }
  1261. nsmtd->owner = THIS_MODULE;
  1262. if ((retval = nand_scan(nsmtd, 1)) != 0) {
  1263. NS_ERR("can't register NAND Simulator\n");
  1264. if (retval > 0)
  1265. retval = -ENXIO;
  1266. goto error;
  1267. }
  1268. if ((retval = init_nandsim(nsmtd)) != 0) {
  1269. NS_ERR("scan_bbt: can't initialize the nandsim structure\n");
  1270. goto error;
  1271. }
  1272. if ((retval = nand_default_bbt(nsmtd)) != 0) {
  1273. free_nandsim(nand);
  1274. goto error;
  1275. }
  1276. /* Register NAND as one big partition */
  1277. add_mtd_partitions(nsmtd, &nand->part, 1);
  1278. return 0;
  1279. error:
  1280. kfree(nsmtd);
  1281. return retval;
  1282. }
  1283. module_init(ns_init_module);
  1284. /*
  1285. * Module clean-up function
  1286. */
  1287. static void __exit ns_cleanup_module(void)
  1288. {
  1289. struct nandsim *ns = (struct nandsim *)(((struct nand_chip *)nsmtd->priv)->priv);
  1290. free_nandsim(ns); /* Free nandsim private resources */
  1291. nand_release(nsmtd); /* Unregisterd drived */
  1292. kfree(nsmtd); /* Free other structures */
  1293. }
  1294. module_exit(ns_cleanup_module);
  1295. MODULE_LICENSE ("GPL");
  1296. MODULE_AUTHOR ("Artem B. Bityuckiy");
  1297. MODULE_DESCRIPTION ("The NAND flash simulator");