nandsim.c 60 KB

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