nandsim.c 46 KB

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