nandsim.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626
  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. * A union to represent flash memory contents and flash buffer.
  204. */
  205. union ns_mem {
  206. u_char *byte; /* for byte access */
  207. uint16_t *word; /* for 16-bit word access */
  208. };
  209. /*
  210. * The structure which describes all the internal simulator data.
  211. */
  212. struct nandsim {
  213. struct mtd_partition part;
  214. uint busw; /* flash chip bus width (8 or 16) */
  215. u_char ids[4]; /* chip's ID bytes */
  216. uint32_t options; /* chip's characteristic bits */
  217. uint32_t state; /* current chip state */
  218. uint32_t nxstate; /* next expected state */
  219. uint32_t *op; /* current operation, NULL operations isn't known yet */
  220. uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
  221. uint16_t npstates; /* number of previous states saved */
  222. uint16_t stateidx; /* current state index */
  223. /* The simulated NAND flash pages array */
  224. union ns_mem *pages;
  225. /* Internal buffer of page + OOB size bytes */
  226. union ns_mem buf;
  227. /* NAND flash "geometry" */
  228. struct nandsin_geometry {
  229. uint32_t totsz; /* total flash size, bytes */
  230. uint32_t secsz; /* flash sector (erase block) size, bytes */
  231. uint pgsz; /* NAND flash page size, bytes */
  232. uint oobsz; /* page OOB area size, bytes */
  233. uint32_t totszoob; /* total flash size including OOB, bytes */
  234. uint pgszoob; /* page size including OOB , bytes*/
  235. uint secszoob; /* sector size including OOB, bytes */
  236. uint pgnum; /* total number of pages */
  237. uint pgsec; /* number of pages per sector */
  238. uint secshift; /* bits number in sector size */
  239. uint pgshift; /* bits number in page size */
  240. uint oobshift; /* bits number in OOB size */
  241. uint pgaddrbytes; /* bytes per page address */
  242. uint secaddrbytes; /* bytes per sector address */
  243. uint idbytes; /* the number ID bytes that this chip outputs */
  244. } geom;
  245. /* NAND flash internal registers */
  246. struct nandsim_regs {
  247. unsigned command; /* the command register */
  248. u_char status; /* the status register */
  249. uint row; /* the page number */
  250. uint column; /* the offset within page */
  251. uint count; /* internal counter */
  252. uint num; /* number of bytes which must be processed */
  253. uint off; /* fixed page offset */
  254. } regs;
  255. /* NAND flash lines state */
  256. struct ns_lines_status {
  257. int ce; /* chip Enable */
  258. int cle; /* command Latch Enable */
  259. int ale; /* address Latch Enable */
  260. int wp; /* write Protect */
  261. } lines;
  262. };
  263. /*
  264. * Operations array. To perform any operation the simulator must pass
  265. * through the correspondent states chain.
  266. */
  267. static struct nandsim_operations {
  268. uint32_t reqopts; /* options which are required to perform the operation */
  269. uint32_t states[NS_OPER_STATES]; /* operation's states */
  270. } ops[NS_OPER_NUM] = {
  271. /* Read page + OOB from the beginning */
  272. {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
  273. STATE_DATAOUT, STATE_READY}},
  274. /* Read page + OOB from the second half */
  275. {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
  276. STATE_DATAOUT, STATE_READY}},
  277. /* Read OOB */
  278. {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
  279. STATE_DATAOUT, STATE_READY}},
  280. /* Programm page starting from the beginning */
  281. {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
  282. STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  283. /* Programm page starting from the beginning */
  284. {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
  285. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  286. /* Programm page starting from the second half */
  287. {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
  288. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  289. /* Programm OOB */
  290. {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
  291. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  292. /* Erase sector */
  293. {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}},
  294. /* Read status */
  295. {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}},
  296. /* Read multi-plane status */
  297. {OPT_SMARTMEDIA, {STATE_CMD_STATUS_M, STATE_DATAOUT_STATUS_M, STATE_READY}},
  298. /* Read ID */
  299. {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
  300. /* Large page devices read page */
  301. {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
  302. STATE_DATAOUT, STATE_READY}}
  303. };
  304. /* MTD structure for NAND controller */
  305. static struct mtd_info *nsmtd;
  306. static u_char ns_verify_buf[NS_LARGEST_PAGE_SIZE];
  307. /*
  308. * Allocate array of page pointers and initialize the array to NULL
  309. * pointers.
  310. *
  311. * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
  312. */
  313. static int
  314. alloc_device(struct nandsim *ns)
  315. {
  316. int i;
  317. ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
  318. if (!ns->pages) {
  319. NS_ERR("alloc_map: unable to allocate page array\n");
  320. return -ENOMEM;
  321. }
  322. for (i = 0; i < ns->geom.pgnum; i++) {
  323. ns->pages[i].byte = NULL;
  324. }
  325. return 0;
  326. }
  327. /*
  328. * Free any allocated pages, and free the array of page pointers.
  329. */
  330. static void
  331. free_device(struct nandsim *ns)
  332. {
  333. int i;
  334. if (ns->pages) {
  335. for (i = 0; i < ns->geom.pgnum; i++) {
  336. if (ns->pages[i].byte)
  337. kfree(ns->pages[i].byte);
  338. }
  339. vfree(ns->pages);
  340. }
  341. }
  342. /*
  343. * Initialize the nandsim structure.
  344. *
  345. * RETURNS: 0 if success, -ERRNO if failure.
  346. */
  347. static int
  348. init_nandsim(struct mtd_info *mtd)
  349. {
  350. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  351. struct nandsim *ns = (struct nandsim *)(chip->priv);
  352. int i;
  353. if (NS_IS_INITIALIZED(ns)) {
  354. NS_ERR("init_nandsim: nandsim is already initialized\n");
  355. return -EIO;
  356. }
  357. /* Force mtd to not do delays */
  358. chip->chip_delay = 0;
  359. /* Initialize the NAND flash parameters */
  360. ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
  361. ns->geom.totsz = mtd->size;
  362. ns->geom.pgsz = mtd->writesize;
  363. ns->geom.oobsz = mtd->oobsize;
  364. ns->geom.secsz = mtd->erasesize;
  365. ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
  366. ns->geom.pgnum = ns->geom.totsz / ns->geom.pgsz;
  367. ns->geom.totszoob = ns->geom.totsz + ns->geom.pgnum * ns->geom.oobsz;
  368. ns->geom.secshift = ffs(ns->geom.secsz) - 1;
  369. ns->geom.pgshift = chip->page_shift;
  370. ns->geom.oobshift = ffs(ns->geom.oobsz) - 1;
  371. ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
  372. ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
  373. ns->options = 0;
  374. if (ns->geom.pgsz == 256) {
  375. ns->options |= OPT_PAGE256;
  376. }
  377. else if (ns->geom.pgsz == 512) {
  378. ns->options |= (OPT_PAGE512 | OPT_AUTOINCR);
  379. if (ns->busw == 8)
  380. ns->options |= OPT_PAGE512_8BIT;
  381. } else if (ns->geom.pgsz == 2048) {
  382. ns->options |= OPT_PAGE2048;
  383. } else {
  384. NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
  385. return -EIO;
  386. }
  387. if (ns->options & OPT_SMALLPAGE) {
  388. if (ns->geom.totsz < (64 << 20)) {
  389. ns->geom.pgaddrbytes = 3;
  390. ns->geom.secaddrbytes = 2;
  391. } else {
  392. ns->geom.pgaddrbytes = 4;
  393. ns->geom.secaddrbytes = 3;
  394. }
  395. } else {
  396. if (ns->geom.totsz <= (128 << 20)) {
  397. ns->geom.pgaddrbytes = 5;
  398. ns->geom.secaddrbytes = 2;
  399. } else {
  400. ns->geom.pgaddrbytes = 5;
  401. ns->geom.secaddrbytes = 3;
  402. }
  403. }
  404. /* Detect how many ID bytes the NAND chip outputs */
  405. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  406. if (second_id_byte != nand_flash_ids[i].id)
  407. continue;
  408. if (!(nand_flash_ids[i].options & NAND_NO_AUTOINCR))
  409. ns->options |= OPT_AUTOINCR;
  410. }
  411. if (ns->busw == 16)
  412. NS_WARN("16-bit flashes support wasn't tested\n");
  413. printk("flash size: %u MiB\n", ns->geom.totsz >> 20);
  414. printk("page size: %u bytes\n", ns->geom.pgsz);
  415. printk("OOB area size: %u bytes\n", ns->geom.oobsz);
  416. printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
  417. printk("pages number: %u\n", ns->geom.pgnum);
  418. printk("pages per sector: %u\n", ns->geom.pgsec);
  419. printk("bus width: %u\n", ns->busw);
  420. printk("bits in sector size: %u\n", ns->geom.secshift);
  421. printk("bits in page size: %u\n", ns->geom.pgshift);
  422. printk("bits in OOB size: %u\n", ns->geom.oobshift);
  423. printk("flash size with OOB: %u KiB\n", ns->geom.totszoob >> 10);
  424. printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
  425. printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
  426. printk("options: %#x\n", ns->options);
  427. if (alloc_device(ns) != 0)
  428. goto error;
  429. /* Allocate / initialize the internal buffer */
  430. ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  431. if (!ns->buf.byte) {
  432. NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
  433. ns->geom.pgszoob);
  434. goto error;
  435. }
  436. memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
  437. /* Fill the partition_info structure */
  438. ns->part.name = "NAND simulator partition";
  439. ns->part.offset = 0;
  440. ns->part.size = ns->geom.totsz;
  441. return 0;
  442. error:
  443. free_device(ns);
  444. return -ENOMEM;
  445. }
  446. /*
  447. * Free the nandsim structure.
  448. */
  449. static void
  450. free_nandsim(struct nandsim *ns)
  451. {
  452. kfree(ns->buf.byte);
  453. free_device(ns);
  454. return;
  455. }
  456. /*
  457. * Returns the string representation of 'state' state.
  458. */
  459. static char *
  460. get_state_name(uint32_t state)
  461. {
  462. switch (NS_STATE(state)) {
  463. case STATE_CMD_READ0:
  464. return "STATE_CMD_READ0";
  465. case STATE_CMD_READ1:
  466. return "STATE_CMD_READ1";
  467. case STATE_CMD_PAGEPROG:
  468. return "STATE_CMD_PAGEPROG";
  469. case STATE_CMD_READOOB:
  470. return "STATE_CMD_READOOB";
  471. case STATE_CMD_READSTART:
  472. return "STATE_CMD_READSTART";
  473. case STATE_CMD_ERASE1:
  474. return "STATE_CMD_ERASE1";
  475. case STATE_CMD_STATUS:
  476. return "STATE_CMD_STATUS";
  477. case STATE_CMD_STATUS_M:
  478. return "STATE_CMD_STATUS_M";
  479. case STATE_CMD_SEQIN:
  480. return "STATE_CMD_SEQIN";
  481. case STATE_CMD_READID:
  482. return "STATE_CMD_READID";
  483. case STATE_CMD_ERASE2:
  484. return "STATE_CMD_ERASE2";
  485. case STATE_CMD_RESET:
  486. return "STATE_CMD_RESET";
  487. case STATE_ADDR_PAGE:
  488. return "STATE_ADDR_PAGE";
  489. case STATE_ADDR_SEC:
  490. return "STATE_ADDR_SEC";
  491. case STATE_ADDR_ZERO:
  492. return "STATE_ADDR_ZERO";
  493. case STATE_DATAIN:
  494. return "STATE_DATAIN";
  495. case STATE_DATAOUT:
  496. return "STATE_DATAOUT";
  497. case STATE_DATAOUT_ID:
  498. return "STATE_DATAOUT_ID";
  499. case STATE_DATAOUT_STATUS:
  500. return "STATE_DATAOUT_STATUS";
  501. case STATE_DATAOUT_STATUS_M:
  502. return "STATE_DATAOUT_STATUS_M";
  503. case STATE_READY:
  504. return "STATE_READY";
  505. case STATE_UNKNOWN:
  506. return "STATE_UNKNOWN";
  507. }
  508. NS_ERR("get_state_name: unknown state, BUG\n");
  509. return NULL;
  510. }
  511. /*
  512. * Check if command is valid.
  513. *
  514. * RETURNS: 1 if wrong command, 0 if right.
  515. */
  516. static int
  517. check_command(int cmd)
  518. {
  519. switch (cmd) {
  520. case NAND_CMD_READ0:
  521. case NAND_CMD_READSTART:
  522. case NAND_CMD_PAGEPROG:
  523. case NAND_CMD_READOOB:
  524. case NAND_CMD_ERASE1:
  525. case NAND_CMD_STATUS:
  526. case NAND_CMD_SEQIN:
  527. case NAND_CMD_READID:
  528. case NAND_CMD_ERASE2:
  529. case NAND_CMD_RESET:
  530. case NAND_CMD_READ1:
  531. return 0;
  532. case NAND_CMD_STATUS_MULTI:
  533. default:
  534. return 1;
  535. }
  536. }
  537. /*
  538. * Returns state after command is accepted by command number.
  539. */
  540. static uint32_t
  541. get_state_by_command(unsigned command)
  542. {
  543. switch (command) {
  544. case NAND_CMD_READ0:
  545. return STATE_CMD_READ0;
  546. case NAND_CMD_READ1:
  547. return STATE_CMD_READ1;
  548. case NAND_CMD_PAGEPROG:
  549. return STATE_CMD_PAGEPROG;
  550. case NAND_CMD_READSTART:
  551. return STATE_CMD_READSTART;
  552. case NAND_CMD_READOOB:
  553. return STATE_CMD_READOOB;
  554. case NAND_CMD_ERASE1:
  555. return STATE_CMD_ERASE1;
  556. case NAND_CMD_STATUS:
  557. return STATE_CMD_STATUS;
  558. case NAND_CMD_STATUS_MULTI:
  559. return STATE_CMD_STATUS_M;
  560. case NAND_CMD_SEQIN:
  561. return STATE_CMD_SEQIN;
  562. case NAND_CMD_READID:
  563. return STATE_CMD_READID;
  564. case NAND_CMD_ERASE2:
  565. return STATE_CMD_ERASE2;
  566. case NAND_CMD_RESET:
  567. return STATE_CMD_RESET;
  568. }
  569. NS_ERR("get_state_by_command: unknown command, BUG\n");
  570. return 0;
  571. }
  572. /*
  573. * Move an address byte to the correspondent internal register.
  574. */
  575. static inline void
  576. accept_addr_byte(struct nandsim *ns, u_char bt)
  577. {
  578. uint byte = (uint)bt;
  579. if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
  580. ns->regs.column |= (byte << 8 * ns->regs.count);
  581. else {
  582. ns->regs.row |= (byte << 8 * (ns->regs.count -
  583. ns->geom.pgaddrbytes +
  584. ns->geom.secaddrbytes));
  585. }
  586. return;
  587. }
  588. /*
  589. * Switch to STATE_READY state.
  590. */
  591. static inline void
  592. switch_to_ready_state(struct nandsim *ns, u_char status)
  593. {
  594. NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
  595. ns->state = STATE_READY;
  596. ns->nxstate = STATE_UNKNOWN;
  597. ns->op = NULL;
  598. ns->npstates = 0;
  599. ns->stateidx = 0;
  600. ns->regs.num = 0;
  601. ns->regs.count = 0;
  602. ns->regs.off = 0;
  603. ns->regs.row = 0;
  604. ns->regs.column = 0;
  605. ns->regs.status = status;
  606. }
  607. /*
  608. * If the operation isn't known yet, try to find it in the global array
  609. * of supported operations.
  610. *
  611. * Operation can be unknown because of the following.
  612. * 1. New command was accepted and this is the firs call to find the
  613. * correspondent states chain. In this case ns->npstates = 0;
  614. * 2. There is several operations which begin with the same command(s)
  615. * (for example program from the second half and read from the
  616. * second half operations both begin with the READ1 command). In this
  617. * case the ns->pstates[] array contains previous states.
  618. *
  619. * Thus, the function tries to find operation containing the following
  620. * states (if the 'flag' parameter is 0):
  621. * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
  622. *
  623. * If (one and only one) matching operation is found, it is accepted (
  624. * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
  625. * zeroed).
  626. *
  627. * If there are several maches, the current state is pushed to the
  628. * ns->pstates.
  629. *
  630. * The operation can be unknown only while commands are input to the chip.
  631. * As soon as address command is accepted, the operation must be known.
  632. * In such situation the function is called with 'flag' != 0, and the
  633. * operation is searched using the following pattern:
  634. * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
  635. *
  636. * It is supposed that this pattern must either match one operation on
  637. * none. There can't be ambiguity in that case.
  638. *
  639. * If no matches found, the functions does the following:
  640. * 1. if there are saved states present, try to ignore them and search
  641. * again only using the last command. If nothing was found, switch
  642. * to the STATE_READY state.
  643. * 2. if there are no saved states, switch to the STATE_READY state.
  644. *
  645. * RETURNS: -2 - no matched operations found.
  646. * -1 - several matches.
  647. * 0 - operation is found.
  648. */
  649. static int
  650. find_operation(struct nandsim *ns, uint32_t flag)
  651. {
  652. int opsfound = 0;
  653. int i, j, idx = 0;
  654. for (i = 0; i < NS_OPER_NUM; i++) {
  655. int found = 1;
  656. if (!(ns->options & ops[i].reqopts))
  657. /* Ignore operations we can't perform */
  658. continue;
  659. if (flag) {
  660. if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
  661. continue;
  662. } else {
  663. if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
  664. continue;
  665. }
  666. for (j = 0; j < ns->npstates; j++)
  667. if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
  668. && (ns->options & ops[idx].reqopts)) {
  669. found = 0;
  670. break;
  671. }
  672. if (found) {
  673. idx = i;
  674. opsfound += 1;
  675. }
  676. }
  677. if (opsfound == 1) {
  678. /* Exact match */
  679. ns->op = &ops[idx].states[0];
  680. if (flag) {
  681. /*
  682. * In this case the find_operation function was
  683. * called when address has just began input. But it isn't
  684. * yet fully input and the current state must
  685. * not be one of STATE_ADDR_*, but the STATE_ADDR_*
  686. * state must be the next state (ns->nxstate).
  687. */
  688. ns->stateidx = ns->npstates - 1;
  689. } else {
  690. ns->stateidx = ns->npstates;
  691. }
  692. ns->npstates = 0;
  693. ns->state = ns->op[ns->stateidx];
  694. ns->nxstate = ns->op[ns->stateidx + 1];
  695. NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
  696. idx, get_state_name(ns->state), get_state_name(ns->nxstate));
  697. return 0;
  698. }
  699. if (opsfound == 0) {
  700. /* Nothing was found. Try to ignore previous commands (if any) and search again */
  701. if (ns->npstates != 0) {
  702. NS_DBG("find_operation: no operation found, try again with state %s\n",
  703. get_state_name(ns->state));
  704. ns->npstates = 0;
  705. return find_operation(ns, 0);
  706. }
  707. NS_DBG("find_operation: no operations found\n");
  708. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  709. return -2;
  710. }
  711. if (flag) {
  712. /* This shouldn't happen */
  713. NS_DBG("find_operation: BUG, operation must be known if address is input\n");
  714. return -2;
  715. }
  716. NS_DBG("find_operation: there is still ambiguity\n");
  717. ns->pstates[ns->npstates++] = ns->state;
  718. return -1;
  719. }
  720. /*
  721. * Returns a pointer to the current page.
  722. */
  723. static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
  724. {
  725. return &(ns->pages[ns->regs.row]);
  726. }
  727. /*
  728. * Retuns a pointer to the current byte, within the current page.
  729. */
  730. static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
  731. {
  732. return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
  733. }
  734. /*
  735. * Fill the NAND buffer with data read from the specified page.
  736. */
  737. static void read_page(struct nandsim *ns, int num)
  738. {
  739. union ns_mem *mypage;
  740. mypage = NS_GET_PAGE(ns);
  741. if (mypage->byte == NULL) {
  742. NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
  743. memset(ns->buf.byte, 0xFF, num);
  744. } else {
  745. NS_DBG("read_page: page %d allocated, reading from %d\n",
  746. ns->regs.row, ns->regs.column + ns->regs.off);
  747. memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
  748. }
  749. }
  750. /*
  751. * Erase all pages in the specified sector.
  752. */
  753. static void erase_sector(struct nandsim *ns)
  754. {
  755. union ns_mem *mypage;
  756. int i;
  757. mypage = NS_GET_PAGE(ns);
  758. for (i = 0; i < ns->geom.pgsec; i++) {
  759. if (mypage->byte != NULL) {
  760. NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
  761. kfree(mypage->byte);
  762. mypage->byte = NULL;
  763. }
  764. mypage++;
  765. }
  766. }
  767. /*
  768. * Program the specified page with the contents from the NAND buffer.
  769. */
  770. static int prog_page(struct nandsim *ns, int num)
  771. {
  772. union ns_mem *mypage;
  773. u_char *pg_off;
  774. mypage = NS_GET_PAGE(ns);
  775. if (mypage->byte == NULL) {
  776. NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
  777. mypage->byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  778. if (mypage->byte == NULL) {
  779. NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
  780. return -1;
  781. }
  782. memset(mypage->byte, 0xFF, ns->geom.pgszoob);
  783. }
  784. pg_off = NS_PAGE_BYTE_OFF(ns);
  785. memcpy(pg_off, ns->buf.byte, num);
  786. return 0;
  787. }
  788. /*
  789. * If state has any action bit, perform this action.
  790. *
  791. * RETURNS: 0 if success, -1 if error.
  792. */
  793. static int
  794. do_state_action(struct nandsim *ns, uint32_t action)
  795. {
  796. int num;
  797. int busdiv = ns->busw == 8 ? 1 : 2;
  798. action &= ACTION_MASK;
  799. /* Check that page address input is correct */
  800. if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
  801. NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
  802. return -1;
  803. }
  804. switch (action) {
  805. case ACTION_CPY:
  806. /*
  807. * Copy page data to the internal buffer.
  808. */
  809. /* Column shouldn't be very large */
  810. if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
  811. NS_ERR("do_state_action: column number is too large\n");
  812. break;
  813. }
  814. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  815. read_page(ns, num);
  816. NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
  817. num, NS_RAW_OFFSET(ns) + ns->regs.off);
  818. if (ns->regs.off == 0)
  819. NS_LOG("read page %d\n", ns->regs.row);
  820. else if (ns->regs.off < ns->geom.pgsz)
  821. NS_LOG("read page %d (second half)\n", ns->regs.row);
  822. else
  823. NS_LOG("read OOB of page %d\n", ns->regs.row);
  824. NS_UDELAY(access_delay);
  825. NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
  826. break;
  827. case ACTION_SECERASE:
  828. /*
  829. * Erase sector.
  830. */
  831. if (ns->lines.wp) {
  832. NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
  833. return -1;
  834. }
  835. if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
  836. || (ns->regs.row & ~(ns->geom.secsz - 1))) {
  837. NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
  838. return -1;
  839. }
  840. ns->regs.row = (ns->regs.row <<
  841. 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
  842. ns->regs.column = 0;
  843. NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
  844. ns->regs.row, NS_RAW_OFFSET(ns));
  845. NS_LOG("erase sector %d\n", ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift));
  846. erase_sector(ns);
  847. NS_MDELAY(erase_delay);
  848. break;
  849. case ACTION_PRGPAGE:
  850. /*
  851. * Programm page - move internal buffer data to the page.
  852. */
  853. if (ns->lines.wp) {
  854. NS_WARN("do_state_action: device is write-protected, programm\n");
  855. return -1;
  856. }
  857. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  858. if (num != ns->regs.count) {
  859. NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
  860. ns->regs.count, num);
  861. return -1;
  862. }
  863. if (prog_page(ns, num) == -1)
  864. return -1;
  865. NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
  866. num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
  867. NS_LOG("programm page %d\n", ns->regs.row);
  868. NS_UDELAY(programm_delay);
  869. NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
  870. break;
  871. case ACTION_ZEROOFF:
  872. NS_DBG("do_state_action: set internal offset to 0\n");
  873. ns->regs.off = 0;
  874. break;
  875. case ACTION_HALFOFF:
  876. if (!(ns->options & OPT_PAGE512_8BIT)) {
  877. NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
  878. "byte page size 8x chips\n");
  879. return -1;
  880. }
  881. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
  882. ns->regs.off = ns->geom.pgsz/2;
  883. break;
  884. case ACTION_OOBOFF:
  885. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
  886. ns->regs.off = ns->geom.pgsz;
  887. break;
  888. default:
  889. NS_DBG("do_state_action: BUG! unknown action\n");
  890. }
  891. return 0;
  892. }
  893. /*
  894. * Switch simulator's state.
  895. */
  896. static void
  897. switch_state(struct nandsim *ns)
  898. {
  899. if (ns->op) {
  900. /*
  901. * The current operation have already been identified.
  902. * Just follow the states chain.
  903. */
  904. ns->stateidx += 1;
  905. ns->state = ns->nxstate;
  906. ns->nxstate = ns->op[ns->stateidx + 1];
  907. NS_DBG("switch_state: operation is known, switch to the next state, "
  908. "state: %s, nxstate: %s\n",
  909. get_state_name(ns->state), get_state_name(ns->nxstate));
  910. /* See, whether we need to do some action */
  911. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  912. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  913. return;
  914. }
  915. } else {
  916. /*
  917. * We don't yet know which operation we perform.
  918. * Try to identify it.
  919. */
  920. /*
  921. * The only event causing the switch_state function to
  922. * be called with yet unknown operation is new command.
  923. */
  924. ns->state = get_state_by_command(ns->regs.command);
  925. NS_DBG("switch_state: operation is unknown, try to find it\n");
  926. if (find_operation(ns, 0) != 0)
  927. return;
  928. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  929. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  930. return;
  931. }
  932. }
  933. /* For 16x devices column means the page offset in words */
  934. if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
  935. NS_DBG("switch_state: double the column number for 16x device\n");
  936. ns->regs.column <<= 1;
  937. }
  938. if (NS_STATE(ns->nxstate) == STATE_READY) {
  939. /*
  940. * The current state is the last. Return to STATE_READY
  941. */
  942. u_char status = NS_STATUS_OK(ns);
  943. /* In case of data states, see if all bytes were input/output */
  944. if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
  945. && ns->regs.count != ns->regs.num) {
  946. NS_WARN("switch_state: not all bytes were processed, %d left\n",
  947. ns->regs.num - ns->regs.count);
  948. status = NS_STATUS_FAILED(ns);
  949. }
  950. NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
  951. switch_to_ready_state(ns, status);
  952. return;
  953. } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
  954. /*
  955. * If the next state is data input/output, switch to it now
  956. */
  957. ns->state = ns->nxstate;
  958. ns->nxstate = ns->op[++ns->stateidx + 1];
  959. ns->regs.num = ns->regs.count = 0;
  960. NS_DBG("switch_state: the next state is data I/O, switch, "
  961. "state: %s, nxstate: %s\n",
  962. get_state_name(ns->state), get_state_name(ns->nxstate));
  963. /*
  964. * Set the internal register to the count of bytes which
  965. * are expected to be input or output
  966. */
  967. switch (NS_STATE(ns->state)) {
  968. case STATE_DATAIN:
  969. case STATE_DATAOUT:
  970. ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  971. break;
  972. case STATE_DATAOUT_ID:
  973. ns->regs.num = ns->geom.idbytes;
  974. break;
  975. case STATE_DATAOUT_STATUS:
  976. case STATE_DATAOUT_STATUS_M:
  977. ns->regs.count = ns->regs.num = 0;
  978. break;
  979. default:
  980. NS_ERR("switch_state: BUG! unknown data state\n");
  981. }
  982. } else if (ns->nxstate & STATE_ADDR_MASK) {
  983. /*
  984. * If the next state is address input, set the internal
  985. * register to the number of expected address bytes
  986. */
  987. ns->regs.count = 0;
  988. switch (NS_STATE(ns->nxstate)) {
  989. case STATE_ADDR_PAGE:
  990. ns->regs.num = ns->geom.pgaddrbytes;
  991. break;
  992. case STATE_ADDR_SEC:
  993. ns->regs.num = ns->geom.secaddrbytes;
  994. break;
  995. case STATE_ADDR_ZERO:
  996. ns->regs.num = 1;
  997. break;
  998. default:
  999. NS_ERR("switch_state: BUG! unknown address state\n");
  1000. }
  1001. } else {
  1002. /*
  1003. * Just reset internal counters.
  1004. */
  1005. ns->regs.num = 0;
  1006. ns->regs.count = 0;
  1007. }
  1008. }
  1009. static u_char
  1010. ns_nand_read_byte(struct mtd_info *mtd)
  1011. {
  1012. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1013. u_char outb = 0x00;
  1014. /* Sanity and correctness checks */
  1015. if (!ns->lines.ce) {
  1016. NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
  1017. return outb;
  1018. }
  1019. if (ns->lines.ale || ns->lines.cle) {
  1020. NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
  1021. return outb;
  1022. }
  1023. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1024. NS_WARN("read_byte: unexpected data output cycle, state is %s "
  1025. "return %#x\n", get_state_name(ns->state), (uint)outb);
  1026. return outb;
  1027. }
  1028. /* Status register may be read as many times as it is wanted */
  1029. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
  1030. NS_DBG("read_byte: return %#x status\n", ns->regs.status);
  1031. return ns->regs.status;
  1032. }
  1033. /* Check if there is any data in the internal buffer which may be read */
  1034. if (ns->regs.count == ns->regs.num) {
  1035. NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
  1036. return outb;
  1037. }
  1038. switch (NS_STATE(ns->state)) {
  1039. case STATE_DATAOUT:
  1040. if (ns->busw == 8) {
  1041. outb = ns->buf.byte[ns->regs.count];
  1042. ns->regs.count += 1;
  1043. } else {
  1044. outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
  1045. ns->regs.count += 2;
  1046. }
  1047. break;
  1048. case STATE_DATAOUT_ID:
  1049. NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
  1050. outb = ns->ids[ns->regs.count];
  1051. ns->regs.count += 1;
  1052. break;
  1053. default:
  1054. BUG();
  1055. }
  1056. if (ns->regs.count == ns->regs.num) {
  1057. NS_DBG("read_byte: all bytes were read\n");
  1058. /*
  1059. * The OPT_AUTOINCR allows to read next conseqitive pages without
  1060. * new read operation cycle.
  1061. */
  1062. if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) {
  1063. ns->regs.count = 0;
  1064. if (ns->regs.row + 1 < ns->geom.pgnum)
  1065. ns->regs.row += 1;
  1066. NS_DBG("read_byte: switch to the next page (%#x)\n", ns->regs.row);
  1067. do_state_action(ns, ACTION_CPY);
  1068. }
  1069. else if (NS_STATE(ns->nxstate) == STATE_READY)
  1070. switch_state(ns);
  1071. }
  1072. return outb;
  1073. }
  1074. static void
  1075. ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
  1076. {
  1077. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1078. /* Sanity and correctness checks */
  1079. if (!ns->lines.ce) {
  1080. NS_ERR("write_byte: chip is disabled, ignore write\n");
  1081. return;
  1082. }
  1083. if (ns->lines.ale && ns->lines.cle) {
  1084. NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
  1085. return;
  1086. }
  1087. if (ns->lines.cle == 1) {
  1088. /*
  1089. * The byte written is a command.
  1090. */
  1091. if (byte == NAND_CMD_RESET) {
  1092. NS_LOG("reset chip\n");
  1093. switch_to_ready_state(ns, NS_STATUS_OK(ns));
  1094. return;
  1095. }
  1096. /*
  1097. * Chip might still be in STATE_DATAOUT
  1098. * (if OPT_AUTOINCR feature is supported), STATE_DATAOUT_STATUS or
  1099. * STATE_DATAOUT_STATUS_M state. If so, switch state.
  1100. */
  1101. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
  1102. || NS_STATE(ns->state) == STATE_DATAOUT_STATUS_M
  1103. || ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT))
  1104. switch_state(ns);
  1105. /* Check if chip is expecting command */
  1106. if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
  1107. /*
  1108. * We are in situation when something else (not command)
  1109. * was expected but command was input. In this case ignore
  1110. * previous command(s)/state(s) and accept the last one.
  1111. */
  1112. NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
  1113. "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
  1114. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1115. }
  1116. /* Check that the command byte is correct */
  1117. if (check_command(byte)) {
  1118. NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
  1119. return;
  1120. }
  1121. NS_DBG("command byte corresponding to %s state accepted\n",
  1122. get_state_name(get_state_by_command(byte)));
  1123. ns->regs.command = byte;
  1124. switch_state(ns);
  1125. } else if (ns->lines.ale == 1) {
  1126. /*
  1127. * The byte written is an address.
  1128. */
  1129. if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
  1130. NS_DBG("write_byte: operation isn't known yet, identify it\n");
  1131. if (find_operation(ns, 1) < 0)
  1132. return;
  1133. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1134. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1135. return;
  1136. }
  1137. ns->regs.count = 0;
  1138. switch (NS_STATE(ns->nxstate)) {
  1139. case STATE_ADDR_PAGE:
  1140. ns->regs.num = ns->geom.pgaddrbytes;
  1141. break;
  1142. case STATE_ADDR_SEC:
  1143. ns->regs.num = ns->geom.secaddrbytes;
  1144. break;
  1145. case STATE_ADDR_ZERO:
  1146. ns->regs.num = 1;
  1147. break;
  1148. default:
  1149. BUG();
  1150. }
  1151. }
  1152. /* Check that chip is expecting address */
  1153. if (!(ns->nxstate & STATE_ADDR_MASK)) {
  1154. NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
  1155. "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
  1156. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1157. return;
  1158. }
  1159. /* Check if this is expected byte */
  1160. if (ns->regs.count == ns->regs.num) {
  1161. NS_ERR("write_byte: no more address bytes expected\n");
  1162. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1163. return;
  1164. }
  1165. accept_addr_byte(ns, byte);
  1166. ns->regs.count += 1;
  1167. NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
  1168. (uint)byte, ns->regs.count, ns->regs.num);
  1169. if (ns->regs.count == ns->regs.num) {
  1170. NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
  1171. switch_state(ns);
  1172. }
  1173. } else {
  1174. /*
  1175. * The byte written is an input data.
  1176. */
  1177. /* Check that chip is expecting data input */
  1178. if (!(ns->state & STATE_DATAIN_MASK)) {
  1179. NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
  1180. "switch to %s\n", (uint)byte,
  1181. get_state_name(ns->state), get_state_name(STATE_READY));
  1182. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1183. return;
  1184. }
  1185. /* Check if this is expected byte */
  1186. if (ns->regs.count == ns->regs.num) {
  1187. NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
  1188. ns->regs.num);
  1189. return;
  1190. }
  1191. if (ns->busw == 8) {
  1192. ns->buf.byte[ns->regs.count] = byte;
  1193. ns->regs.count += 1;
  1194. } else {
  1195. ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
  1196. ns->regs.count += 2;
  1197. }
  1198. }
  1199. return;
  1200. }
  1201. static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
  1202. {
  1203. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1204. ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
  1205. ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
  1206. ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
  1207. if (cmd != NAND_CMD_NONE)
  1208. ns_nand_write_byte(mtd, cmd);
  1209. }
  1210. static int
  1211. ns_device_ready(struct mtd_info *mtd)
  1212. {
  1213. NS_DBG("device_ready\n");
  1214. return 1;
  1215. }
  1216. static uint16_t
  1217. ns_nand_read_word(struct mtd_info *mtd)
  1218. {
  1219. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  1220. NS_DBG("read_word\n");
  1221. return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
  1222. }
  1223. static void
  1224. ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1225. {
  1226. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1227. /* Check that chip is expecting data input */
  1228. if (!(ns->state & STATE_DATAIN_MASK)) {
  1229. NS_ERR("write_buf: data input isn't expected, state is %s, "
  1230. "switch to STATE_READY\n", get_state_name(ns->state));
  1231. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1232. return;
  1233. }
  1234. /* Check if these are expected bytes */
  1235. if (ns->regs.count + len > ns->regs.num) {
  1236. NS_ERR("write_buf: too many input bytes\n");
  1237. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1238. return;
  1239. }
  1240. memcpy(ns->buf.byte + ns->regs.count, buf, len);
  1241. ns->regs.count += len;
  1242. if (ns->regs.count == ns->regs.num) {
  1243. NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
  1244. }
  1245. }
  1246. static void
  1247. ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  1248. {
  1249. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1250. /* Sanity and correctness checks */
  1251. if (!ns->lines.ce) {
  1252. NS_ERR("read_buf: chip is disabled\n");
  1253. return;
  1254. }
  1255. if (ns->lines.ale || ns->lines.cle) {
  1256. NS_ERR("read_buf: ALE or CLE pin is high\n");
  1257. return;
  1258. }
  1259. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1260. NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
  1261. get_state_name(ns->state));
  1262. return;
  1263. }
  1264. if (NS_STATE(ns->state) != STATE_DATAOUT) {
  1265. int i;
  1266. for (i = 0; i < len; i++)
  1267. buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
  1268. return;
  1269. }
  1270. /* Check if these are expected bytes */
  1271. if (ns->regs.count + len > ns->regs.num) {
  1272. NS_ERR("read_buf: too many bytes to read\n");
  1273. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1274. return;
  1275. }
  1276. memcpy(buf, ns->buf.byte + ns->regs.count, len);
  1277. ns->regs.count += len;
  1278. if (ns->regs.count == ns->regs.num) {
  1279. if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) {
  1280. ns->regs.count = 0;
  1281. if (ns->regs.row + 1 < ns->geom.pgnum)
  1282. ns->regs.row += 1;
  1283. NS_DBG("read_buf: switch to the next page (%#x)\n", ns->regs.row);
  1284. do_state_action(ns, ACTION_CPY);
  1285. }
  1286. else if (NS_STATE(ns->nxstate) == STATE_READY)
  1287. switch_state(ns);
  1288. }
  1289. return;
  1290. }
  1291. static int
  1292. ns_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1293. {
  1294. ns_nand_read_buf(mtd, (u_char *)&ns_verify_buf[0], len);
  1295. if (!memcmp(buf, &ns_verify_buf[0], len)) {
  1296. NS_DBG("verify_buf: the buffer is OK\n");
  1297. return 0;
  1298. } else {
  1299. NS_DBG("verify_buf: the buffer is wrong\n");
  1300. return -EFAULT;
  1301. }
  1302. }
  1303. /*
  1304. * Module initialization function
  1305. */
  1306. static int __init ns_init_module(void)
  1307. {
  1308. struct nand_chip *chip;
  1309. struct nandsim *nand;
  1310. int retval = -ENOMEM;
  1311. if (bus_width != 8 && bus_width != 16) {
  1312. NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
  1313. return -EINVAL;
  1314. }
  1315. /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
  1316. nsmtd = kmalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
  1317. + sizeof(struct nandsim), GFP_KERNEL);
  1318. if (!nsmtd) {
  1319. NS_ERR("unable to allocate core structures.\n");
  1320. return -ENOMEM;
  1321. }
  1322. memset(nsmtd, 0, sizeof(struct mtd_info) + sizeof(struct nand_chip) +
  1323. sizeof(struct nandsim));
  1324. chip = (struct nand_chip *)(nsmtd + 1);
  1325. nsmtd->priv = (void *)chip;
  1326. nand = (struct nandsim *)(chip + 1);
  1327. chip->priv = (void *)nand;
  1328. /*
  1329. * Register simulator's callbacks.
  1330. */
  1331. chip->cmd_ctrl = ns_hwcontrol;
  1332. chip->read_byte = ns_nand_read_byte;
  1333. chip->dev_ready = ns_device_ready;
  1334. chip->write_buf = ns_nand_write_buf;
  1335. chip->read_buf = ns_nand_read_buf;
  1336. chip->verify_buf = ns_nand_verify_buf;
  1337. chip->read_word = ns_nand_read_word;
  1338. chip->ecc.mode = NAND_ECC_SOFT;
  1339. chip->options |= NAND_SKIP_BBTSCAN;
  1340. /*
  1341. * Perform minimum nandsim structure initialization to handle
  1342. * the initial ID read command correctly
  1343. */
  1344. if (third_id_byte != 0xFF || fourth_id_byte != 0xFF)
  1345. nand->geom.idbytes = 4;
  1346. else
  1347. nand->geom.idbytes = 2;
  1348. nand->regs.status = NS_STATUS_OK(nand);
  1349. nand->nxstate = STATE_UNKNOWN;
  1350. nand->options |= OPT_PAGE256; /* temporary value */
  1351. nand->ids[0] = first_id_byte;
  1352. nand->ids[1] = second_id_byte;
  1353. nand->ids[2] = third_id_byte;
  1354. nand->ids[3] = fourth_id_byte;
  1355. if (bus_width == 16) {
  1356. nand->busw = 16;
  1357. chip->options |= NAND_BUSWIDTH_16;
  1358. }
  1359. nsmtd->owner = THIS_MODULE;
  1360. if ((retval = nand_scan(nsmtd, 1)) != 0) {
  1361. NS_ERR("can't register NAND Simulator\n");
  1362. if (retval > 0)
  1363. retval = -ENXIO;
  1364. goto error;
  1365. }
  1366. if ((retval = init_nandsim(nsmtd)) != 0) {
  1367. NS_ERR("scan_bbt: can't initialize the nandsim structure\n");
  1368. goto error;
  1369. }
  1370. if ((retval = nand_default_bbt(nsmtd)) != 0) {
  1371. free_nandsim(nand);
  1372. goto error;
  1373. }
  1374. /* Register NAND as one big partition */
  1375. add_mtd_partitions(nsmtd, &nand->part, 1);
  1376. return 0;
  1377. error:
  1378. kfree(nsmtd);
  1379. return retval;
  1380. }
  1381. module_init(ns_init_module);
  1382. /*
  1383. * Module clean-up function
  1384. */
  1385. static void __exit ns_cleanup_module(void)
  1386. {
  1387. struct nandsim *ns = (struct nandsim *)(((struct nand_chip *)nsmtd->priv)->priv);
  1388. free_nandsim(ns); /* Free nandsim private resources */
  1389. nand_release(nsmtd); /* Unregisterd drived */
  1390. kfree(nsmtd); /* Free other structures */
  1391. }
  1392. module_exit(ns_cleanup_module);
  1393. MODULE_LICENSE ("GPL");
  1394. MODULE_AUTHOR ("Artem B. Bityuckiy");
  1395. MODULE_DESCRIPTION ("The NAND flash simulator");