nandsim.c 45 KB

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