nandsim.c 60 KB

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