nandsim.c 67 KB

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