nandsim.c 59 KB

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