nandsim.c 55 KB

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