nandsim.c 66 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384
  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. /*
  403. * Allocate array of page pointers, create slab allocation for an array
  404. * and initialize the array by NULL pointers.
  405. *
  406. * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
  407. */
  408. static int alloc_device(struct nandsim *ns)
  409. {
  410. struct file *cfile;
  411. int i, err;
  412. if (cache_file) {
  413. cfile = filp_open(cache_file, O_CREAT | O_RDWR | O_LARGEFILE, 0600);
  414. if (IS_ERR(cfile))
  415. return PTR_ERR(cfile);
  416. if (!cfile->f_op || (!cfile->f_op->read && !cfile->f_op->aio_read)) {
  417. NS_ERR("alloc_device: cache file not readable\n");
  418. err = -EINVAL;
  419. goto err_close;
  420. }
  421. if (!cfile->f_op->write && !cfile->f_op->aio_write) {
  422. NS_ERR("alloc_device: cache file not writeable\n");
  423. err = -EINVAL;
  424. goto err_close;
  425. }
  426. ns->pages_written = vzalloc(ns->geom.pgnum);
  427. if (!ns->pages_written) {
  428. NS_ERR("alloc_device: unable to allocate pages written array\n");
  429. err = -ENOMEM;
  430. goto err_close;
  431. }
  432. ns->file_buf = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  433. if (!ns->file_buf) {
  434. NS_ERR("alloc_device: unable to allocate file buf\n");
  435. err = -ENOMEM;
  436. goto err_free;
  437. }
  438. ns->cfile = cfile;
  439. return 0;
  440. }
  441. ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
  442. if (!ns->pages) {
  443. NS_ERR("alloc_device: unable to allocate page array\n");
  444. return -ENOMEM;
  445. }
  446. for (i = 0; i < ns->geom.pgnum; i++) {
  447. ns->pages[i].byte = NULL;
  448. }
  449. ns->nand_pages_slab = kmem_cache_create("nandsim",
  450. ns->geom.pgszoob, 0, 0, NULL);
  451. if (!ns->nand_pages_slab) {
  452. NS_ERR("cache_create: unable to create kmem_cache\n");
  453. return -ENOMEM;
  454. }
  455. return 0;
  456. err_free:
  457. vfree(ns->pages_written);
  458. err_close:
  459. filp_close(cfile, NULL);
  460. return err;
  461. }
  462. /*
  463. * Free any allocated pages, and free the array of page pointers.
  464. */
  465. static void free_device(struct nandsim *ns)
  466. {
  467. int i;
  468. if (ns->cfile) {
  469. kfree(ns->file_buf);
  470. vfree(ns->pages_written);
  471. filp_close(ns->cfile, NULL);
  472. return;
  473. }
  474. if (ns->pages) {
  475. for (i = 0; i < ns->geom.pgnum; i++) {
  476. if (ns->pages[i].byte)
  477. kmem_cache_free(ns->nand_pages_slab,
  478. ns->pages[i].byte);
  479. }
  480. kmem_cache_destroy(ns->nand_pages_slab);
  481. vfree(ns->pages);
  482. }
  483. }
  484. static char *get_partition_name(int i)
  485. {
  486. char buf[64];
  487. sprintf(buf, "NAND simulator partition %d", i);
  488. return kstrdup(buf, GFP_KERNEL);
  489. }
  490. /*
  491. * Initialize the nandsim structure.
  492. *
  493. * RETURNS: 0 if success, -ERRNO if failure.
  494. */
  495. static int init_nandsim(struct mtd_info *mtd)
  496. {
  497. struct nand_chip *chip = mtd->priv;
  498. struct nandsim *ns = chip->priv;
  499. int i, ret = 0;
  500. uint64_t remains;
  501. uint64_t next_offset;
  502. if (NS_IS_INITIALIZED(ns)) {
  503. NS_ERR("init_nandsim: nandsim is already initialized\n");
  504. return -EIO;
  505. }
  506. /* Force mtd to not do delays */
  507. chip->chip_delay = 0;
  508. /* Initialize the NAND flash parameters */
  509. ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
  510. ns->geom.totsz = mtd->size;
  511. ns->geom.pgsz = mtd->writesize;
  512. ns->geom.oobsz = mtd->oobsize;
  513. ns->geom.secsz = mtd->erasesize;
  514. ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
  515. ns->geom.pgnum = div_u64(ns->geom.totsz, ns->geom.pgsz);
  516. ns->geom.totszoob = ns->geom.totsz + (uint64_t)ns->geom.pgnum * ns->geom.oobsz;
  517. ns->geom.secshift = ffs(ns->geom.secsz) - 1;
  518. ns->geom.pgshift = chip->page_shift;
  519. ns->geom.oobshift = ffs(ns->geom.oobsz) - 1;
  520. ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
  521. ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
  522. ns->options = 0;
  523. if (ns->geom.pgsz == 256) {
  524. ns->options |= OPT_PAGE256;
  525. }
  526. else if (ns->geom.pgsz == 512) {
  527. ns->options |= OPT_PAGE512;
  528. if (ns->busw == 8)
  529. ns->options |= OPT_PAGE512_8BIT;
  530. } else if (ns->geom.pgsz == 2048) {
  531. ns->options |= OPT_PAGE2048;
  532. } else if (ns->geom.pgsz == 4096) {
  533. ns->options |= OPT_PAGE4096;
  534. } else {
  535. NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
  536. return -EIO;
  537. }
  538. if (ns->options & OPT_SMALLPAGE) {
  539. if (ns->geom.totsz <= (32 << 20)) {
  540. ns->geom.pgaddrbytes = 3;
  541. ns->geom.secaddrbytes = 2;
  542. } else {
  543. ns->geom.pgaddrbytes = 4;
  544. ns->geom.secaddrbytes = 3;
  545. }
  546. } else {
  547. if (ns->geom.totsz <= (128 << 20)) {
  548. ns->geom.pgaddrbytes = 4;
  549. ns->geom.secaddrbytes = 2;
  550. } else {
  551. ns->geom.pgaddrbytes = 5;
  552. ns->geom.secaddrbytes = 3;
  553. }
  554. }
  555. /* Fill the partition_info structure */
  556. if (parts_num > ARRAY_SIZE(ns->partitions)) {
  557. NS_ERR("too many partitions.\n");
  558. ret = -EINVAL;
  559. goto error;
  560. }
  561. remains = ns->geom.totsz;
  562. next_offset = 0;
  563. for (i = 0; i < parts_num; ++i) {
  564. uint64_t part_sz = (uint64_t)parts[i] * ns->geom.secsz;
  565. if (!part_sz || part_sz > remains) {
  566. NS_ERR("bad partition size.\n");
  567. ret = -EINVAL;
  568. goto error;
  569. }
  570. ns->partitions[i].name = get_partition_name(i);
  571. ns->partitions[i].offset = next_offset;
  572. ns->partitions[i].size = part_sz;
  573. next_offset += ns->partitions[i].size;
  574. remains -= ns->partitions[i].size;
  575. }
  576. ns->nbparts = parts_num;
  577. if (remains) {
  578. if (parts_num + 1 > ARRAY_SIZE(ns->partitions)) {
  579. NS_ERR("too many partitions.\n");
  580. ret = -EINVAL;
  581. goto error;
  582. }
  583. ns->partitions[i].name = get_partition_name(i);
  584. ns->partitions[i].offset = next_offset;
  585. ns->partitions[i].size = remains;
  586. ns->nbparts += 1;
  587. }
  588. /* Detect how many ID bytes the NAND chip outputs */
  589. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  590. if (second_id_byte != nand_flash_ids[i].id)
  591. continue;
  592. }
  593. if (ns->busw == 16)
  594. NS_WARN("16-bit flashes support wasn't tested\n");
  595. printk("flash size: %llu MiB\n",
  596. (unsigned long long)ns->geom.totsz >> 20);
  597. printk("page size: %u bytes\n", ns->geom.pgsz);
  598. printk("OOB area size: %u bytes\n", ns->geom.oobsz);
  599. printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
  600. printk("pages number: %u\n", ns->geom.pgnum);
  601. printk("pages per sector: %u\n", ns->geom.pgsec);
  602. printk("bus width: %u\n", ns->busw);
  603. printk("bits in sector size: %u\n", ns->geom.secshift);
  604. printk("bits in page size: %u\n", ns->geom.pgshift);
  605. printk("bits in OOB size: %u\n", ns->geom.oobshift);
  606. printk("flash size with OOB: %llu KiB\n",
  607. (unsigned long long)ns->geom.totszoob >> 10);
  608. printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
  609. printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
  610. printk("options: %#x\n", ns->options);
  611. if ((ret = alloc_device(ns)) != 0)
  612. goto error;
  613. /* Allocate / initialize the internal buffer */
  614. ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  615. if (!ns->buf.byte) {
  616. NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
  617. ns->geom.pgszoob);
  618. ret = -ENOMEM;
  619. goto error;
  620. }
  621. memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
  622. return 0;
  623. error:
  624. free_device(ns);
  625. return ret;
  626. }
  627. /*
  628. * Free the nandsim structure.
  629. */
  630. static void free_nandsim(struct nandsim *ns)
  631. {
  632. kfree(ns->buf.byte);
  633. free_device(ns);
  634. return;
  635. }
  636. static int parse_badblocks(struct nandsim *ns, struct mtd_info *mtd)
  637. {
  638. char *w;
  639. int zero_ok;
  640. unsigned int erase_block_no;
  641. loff_t offset;
  642. if (!badblocks)
  643. return 0;
  644. w = badblocks;
  645. do {
  646. zero_ok = (*w == '0' ? 1 : 0);
  647. erase_block_no = simple_strtoul(w, &w, 0);
  648. if (!zero_ok && !erase_block_no) {
  649. NS_ERR("invalid badblocks.\n");
  650. return -EINVAL;
  651. }
  652. offset = erase_block_no * ns->geom.secsz;
  653. if (mtd_block_markbad(mtd, offset)) {
  654. NS_ERR("invalid badblocks.\n");
  655. return -EINVAL;
  656. }
  657. if (*w == ',')
  658. w += 1;
  659. } while (*w);
  660. return 0;
  661. }
  662. static int parse_weakblocks(void)
  663. {
  664. char *w;
  665. int zero_ok;
  666. unsigned int erase_block_no;
  667. unsigned int max_erases;
  668. struct weak_block *wb;
  669. if (!weakblocks)
  670. return 0;
  671. w = weakblocks;
  672. do {
  673. zero_ok = (*w == '0' ? 1 : 0);
  674. erase_block_no = simple_strtoul(w, &w, 0);
  675. if (!zero_ok && !erase_block_no) {
  676. NS_ERR("invalid weakblocks.\n");
  677. return -EINVAL;
  678. }
  679. max_erases = 3;
  680. if (*w == ':') {
  681. w += 1;
  682. max_erases = simple_strtoul(w, &w, 0);
  683. }
  684. if (*w == ',')
  685. w += 1;
  686. wb = kzalloc(sizeof(*wb), GFP_KERNEL);
  687. if (!wb) {
  688. NS_ERR("unable to allocate memory.\n");
  689. return -ENOMEM;
  690. }
  691. wb->erase_block_no = erase_block_no;
  692. wb->max_erases = max_erases;
  693. list_add(&wb->list, &weak_blocks);
  694. } while (*w);
  695. return 0;
  696. }
  697. static int erase_error(unsigned int erase_block_no)
  698. {
  699. struct weak_block *wb;
  700. list_for_each_entry(wb, &weak_blocks, list)
  701. if (wb->erase_block_no == erase_block_no) {
  702. if (wb->erases_done >= wb->max_erases)
  703. return 1;
  704. wb->erases_done += 1;
  705. return 0;
  706. }
  707. return 0;
  708. }
  709. static int parse_weakpages(void)
  710. {
  711. char *w;
  712. int zero_ok;
  713. unsigned int page_no;
  714. unsigned int max_writes;
  715. struct weak_page *wp;
  716. if (!weakpages)
  717. return 0;
  718. w = weakpages;
  719. do {
  720. zero_ok = (*w == '0' ? 1 : 0);
  721. page_no = simple_strtoul(w, &w, 0);
  722. if (!zero_ok && !page_no) {
  723. NS_ERR("invalid weakpagess.\n");
  724. return -EINVAL;
  725. }
  726. max_writes = 3;
  727. if (*w == ':') {
  728. w += 1;
  729. max_writes = simple_strtoul(w, &w, 0);
  730. }
  731. if (*w == ',')
  732. w += 1;
  733. wp = kzalloc(sizeof(*wp), GFP_KERNEL);
  734. if (!wp) {
  735. NS_ERR("unable to allocate memory.\n");
  736. return -ENOMEM;
  737. }
  738. wp->page_no = page_no;
  739. wp->max_writes = max_writes;
  740. list_add(&wp->list, &weak_pages);
  741. } while (*w);
  742. return 0;
  743. }
  744. static int write_error(unsigned int page_no)
  745. {
  746. struct weak_page *wp;
  747. list_for_each_entry(wp, &weak_pages, list)
  748. if (wp->page_no == page_no) {
  749. if (wp->writes_done >= wp->max_writes)
  750. return 1;
  751. wp->writes_done += 1;
  752. return 0;
  753. }
  754. return 0;
  755. }
  756. static int parse_gravepages(void)
  757. {
  758. char *g;
  759. int zero_ok;
  760. unsigned int page_no;
  761. unsigned int max_reads;
  762. struct grave_page *gp;
  763. if (!gravepages)
  764. return 0;
  765. g = gravepages;
  766. do {
  767. zero_ok = (*g == '0' ? 1 : 0);
  768. page_no = simple_strtoul(g, &g, 0);
  769. if (!zero_ok && !page_no) {
  770. NS_ERR("invalid gravepagess.\n");
  771. return -EINVAL;
  772. }
  773. max_reads = 3;
  774. if (*g == ':') {
  775. g += 1;
  776. max_reads = simple_strtoul(g, &g, 0);
  777. }
  778. if (*g == ',')
  779. g += 1;
  780. gp = kzalloc(sizeof(*gp), GFP_KERNEL);
  781. if (!gp) {
  782. NS_ERR("unable to allocate memory.\n");
  783. return -ENOMEM;
  784. }
  785. gp->page_no = page_no;
  786. gp->max_reads = max_reads;
  787. list_add(&gp->list, &grave_pages);
  788. } while (*g);
  789. return 0;
  790. }
  791. static int read_error(unsigned int page_no)
  792. {
  793. struct grave_page *gp;
  794. list_for_each_entry(gp, &grave_pages, list)
  795. if (gp->page_no == page_no) {
  796. if (gp->reads_done >= gp->max_reads)
  797. return 1;
  798. gp->reads_done += 1;
  799. return 0;
  800. }
  801. return 0;
  802. }
  803. static void free_lists(void)
  804. {
  805. struct list_head *pos, *n;
  806. list_for_each_safe(pos, n, &weak_blocks) {
  807. list_del(pos);
  808. kfree(list_entry(pos, struct weak_block, list));
  809. }
  810. list_for_each_safe(pos, n, &weak_pages) {
  811. list_del(pos);
  812. kfree(list_entry(pos, struct weak_page, list));
  813. }
  814. list_for_each_safe(pos, n, &grave_pages) {
  815. list_del(pos);
  816. kfree(list_entry(pos, struct grave_page, list));
  817. }
  818. kfree(erase_block_wear);
  819. }
  820. static int setup_wear_reporting(struct mtd_info *mtd)
  821. {
  822. size_t mem;
  823. if (!rptwear)
  824. return 0;
  825. wear_eb_count = div_u64(mtd->size, mtd->erasesize);
  826. mem = wear_eb_count * sizeof(unsigned long);
  827. if (mem / sizeof(unsigned long) != wear_eb_count) {
  828. NS_ERR("Too many erase blocks for wear reporting\n");
  829. return -ENOMEM;
  830. }
  831. erase_block_wear = kzalloc(mem, GFP_KERNEL);
  832. if (!erase_block_wear) {
  833. NS_ERR("Too many erase blocks for wear reporting\n");
  834. return -ENOMEM;
  835. }
  836. return 0;
  837. }
  838. static void update_wear(unsigned int erase_block_no)
  839. {
  840. unsigned long wmin = -1, wmax = 0, avg;
  841. unsigned long deciles[10], decile_max[10], tot = 0;
  842. unsigned int i;
  843. if (!erase_block_wear)
  844. return;
  845. total_wear += 1;
  846. if (total_wear == 0)
  847. NS_ERR("Erase counter total overflow\n");
  848. erase_block_wear[erase_block_no] += 1;
  849. if (erase_block_wear[erase_block_no] == 0)
  850. NS_ERR("Erase counter overflow for erase block %u\n", erase_block_no);
  851. rptwear_cnt += 1;
  852. if (rptwear_cnt < rptwear)
  853. return;
  854. rptwear_cnt = 0;
  855. /* Calc wear stats */
  856. for (i = 0; i < wear_eb_count; ++i) {
  857. unsigned long wear = erase_block_wear[i];
  858. if (wear < wmin)
  859. wmin = wear;
  860. if (wear > wmax)
  861. wmax = wear;
  862. tot += wear;
  863. }
  864. for (i = 0; i < 9; ++i) {
  865. deciles[i] = 0;
  866. decile_max[i] = (wmax * (i + 1) + 5) / 10;
  867. }
  868. deciles[9] = 0;
  869. decile_max[9] = wmax;
  870. for (i = 0; i < wear_eb_count; ++i) {
  871. int d;
  872. unsigned long wear = erase_block_wear[i];
  873. for (d = 0; d < 10; ++d)
  874. if (wear <= decile_max[d]) {
  875. deciles[d] += 1;
  876. break;
  877. }
  878. }
  879. avg = tot / wear_eb_count;
  880. /* Output wear report */
  881. NS_INFO("*** Wear Report ***\n");
  882. NS_INFO("Total numbers of erases: %lu\n", tot);
  883. NS_INFO("Number of erase blocks: %u\n", wear_eb_count);
  884. NS_INFO("Average number of erases: %lu\n", avg);
  885. NS_INFO("Maximum number of erases: %lu\n", wmax);
  886. NS_INFO("Minimum number of erases: %lu\n", wmin);
  887. for (i = 0; i < 10; ++i) {
  888. unsigned long from = (i ? decile_max[i - 1] + 1 : 0);
  889. if (from > decile_max[i])
  890. continue;
  891. NS_INFO("Number of ebs with erase counts from %lu to %lu : %lu\n",
  892. from,
  893. decile_max[i],
  894. deciles[i]);
  895. }
  896. NS_INFO("*** End of Wear Report ***\n");
  897. }
  898. /*
  899. * Returns the string representation of 'state' state.
  900. */
  901. static char *get_state_name(uint32_t state)
  902. {
  903. switch (NS_STATE(state)) {
  904. case STATE_CMD_READ0:
  905. return "STATE_CMD_READ0";
  906. case STATE_CMD_READ1:
  907. return "STATE_CMD_READ1";
  908. case STATE_CMD_PAGEPROG:
  909. return "STATE_CMD_PAGEPROG";
  910. case STATE_CMD_READOOB:
  911. return "STATE_CMD_READOOB";
  912. case STATE_CMD_READSTART:
  913. return "STATE_CMD_READSTART";
  914. case STATE_CMD_ERASE1:
  915. return "STATE_CMD_ERASE1";
  916. case STATE_CMD_STATUS:
  917. return "STATE_CMD_STATUS";
  918. case STATE_CMD_STATUS_M:
  919. return "STATE_CMD_STATUS_M";
  920. case STATE_CMD_SEQIN:
  921. return "STATE_CMD_SEQIN";
  922. case STATE_CMD_READID:
  923. return "STATE_CMD_READID";
  924. case STATE_CMD_ERASE2:
  925. return "STATE_CMD_ERASE2";
  926. case STATE_CMD_RESET:
  927. return "STATE_CMD_RESET";
  928. case STATE_CMD_RNDOUT:
  929. return "STATE_CMD_RNDOUT";
  930. case STATE_CMD_RNDOUTSTART:
  931. return "STATE_CMD_RNDOUTSTART";
  932. case STATE_ADDR_PAGE:
  933. return "STATE_ADDR_PAGE";
  934. case STATE_ADDR_SEC:
  935. return "STATE_ADDR_SEC";
  936. case STATE_ADDR_ZERO:
  937. return "STATE_ADDR_ZERO";
  938. case STATE_ADDR_COLUMN:
  939. return "STATE_ADDR_COLUMN";
  940. case STATE_DATAIN:
  941. return "STATE_DATAIN";
  942. case STATE_DATAOUT:
  943. return "STATE_DATAOUT";
  944. case STATE_DATAOUT_ID:
  945. return "STATE_DATAOUT_ID";
  946. case STATE_DATAOUT_STATUS:
  947. return "STATE_DATAOUT_STATUS";
  948. case STATE_DATAOUT_STATUS_M:
  949. return "STATE_DATAOUT_STATUS_M";
  950. case STATE_READY:
  951. return "STATE_READY";
  952. case STATE_UNKNOWN:
  953. return "STATE_UNKNOWN";
  954. }
  955. NS_ERR("get_state_name: unknown state, BUG\n");
  956. return NULL;
  957. }
  958. /*
  959. * Check if command is valid.
  960. *
  961. * RETURNS: 1 if wrong command, 0 if right.
  962. */
  963. static int check_command(int cmd)
  964. {
  965. switch (cmd) {
  966. case NAND_CMD_READ0:
  967. case NAND_CMD_READ1:
  968. case NAND_CMD_READSTART:
  969. case NAND_CMD_PAGEPROG:
  970. case NAND_CMD_READOOB:
  971. case NAND_CMD_ERASE1:
  972. case NAND_CMD_STATUS:
  973. case NAND_CMD_SEQIN:
  974. case NAND_CMD_READID:
  975. case NAND_CMD_ERASE2:
  976. case NAND_CMD_RESET:
  977. case NAND_CMD_RNDOUT:
  978. case NAND_CMD_RNDOUTSTART:
  979. return 0;
  980. case NAND_CMD_STATUS_MULTI:
  981. default:
  982. return 1;
  983. }
  984. }
  985. /*
  986. * Returns state after command is accepted by command number.
  987. */
  988. static uint32_t get_state_by_command(unsigned command)
  989. {
  990. switch (command) {
  991. case NAND_CMD_READ0:
  992. return STATE_CMD_READ0;
  993. case NAND_CMD_READ1:
  994. return STATE_CMD_READ1;
  995. case NAND_CMD_PAGEPROG:
  996. return STATE_CMD_PAGEPROG;
  997. case NAND_CMD_READSTART:
  998. return STATE_CMD_READSTART;
  999. case NAND_CMD_READOOB:
  1000. return STATE_CMD_READOOB;
  1001. case NAND_CMD_ERASE1:
  1002. return STATE_CMD_ERASE1;
  1003. case NAND_CMD_STATUS:
  1004. return STATE_CMD_STATUS;
  1005. case NAND_CMD_STATUS_MULTI:
  1006. return STATE_CMD_STATUS_M;
  1007. case NAND_CMD_SEQIN:
  1008. return STATE_CMD_SEQIN;
  1009. case NAND_CMD_READID:
  1010. return STATE_CMD_READID;
  1011. case NAND_CMD_ERASE2:
  1012. return STATE_CMD_ERASE2;
  1013. case NAND_CMD_RESET:
  1014. return STATE_CMD_RESET;
  1015. case NAND_CMD_RNDOUT:
  1016. return STATE_CMD_RNDOUT;
  1017. case NAND_CMD_RNDOUTSTART:
  1018. return STATE_CMD_RNDOUTSTART;
  1019. }
  1020. NS_ERR("get_state_by_command: unknown command, BUG\n");
  1021. return 0;
  1022. }
  1023. /*
  1024. * Move an address byte to the correspondent internal register.
  1025. */
  1026. static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
  1027. {
  1028. uint byte = (uint)bt;
  1029. if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
  1030. ns->regs.column |= (byte << 8 * ns->regs.count);
  1031. else {
  1032. ns->regs.row |= (byte << 8 * (ns->regs.count -
  1033. ns->geom.pgaddrbytes +
  1034. ns->geom.secaddrbytes));
  1035. }
  1036. return;
  1037. }
  1038. /*
  1039. * Switch to STATE_READY state.
  1040. */
  1041. static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
  1042. {
  1043. NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
  1044. ns->state = STATE_READY;
  1045. ns->nxstate = STATE_UNKNOWN;
  1046. ns->op = NULL;
  1047. ns->npstates = 0;
  1048. ns->stateidx = 0;
  1049. ns->regs.num = 0;
  1050. ns->regs.count = 0;
  1051. ns->regs.off = 0;
  1052. ns->regs.row = 0;
  1053. ns->regs.column = 0;
  1054. ns->regs.status = status;
  1055. }
  1056. /*
  1057. * If the operation isn't known yet, try to find it in the global array
  1058. * of supported operations.
  1059. *
  1060. * Operation can be unknown because of the following.
  1061. * 1. New command was accepted and this is the first call to find the
  1062. * correspondent states chain. In this case ns->npstates = 0;
  1063. * 2. There are several operations which begin with the same command(s)
  1064. * (for example program from the second half and read from the
  1065. * second half operations both begin with the READ1 command). In this
  1066. * case the ns->pstates[] array contains previous states.
  1067. *
  1068. * Thus, the function tries to find operation containing the following
  1069. * states (if the 'flag' parameter is 0):
  1070. * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
  1071. *
  1072. * If (one and only one) matching operation is found, it is accepted (
  1073. * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
  1074. * zeroed).
  1075. *
  1076. * If there are several matches, the current state is pushed to the
  1077. * ns->pstates.
  1078. *
  1079. * The operation can be unknown only while commands are input to the chip.
  1080. * As soon as address command is accepted, the operation must be known.
  1081. * In such situation the function is called with 'flag' != 0, and the
  1082. * operation is searched using the following pattern:
  1083. * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
  1084. *
  1085. * It is supposed that this pattern must either match one operation or
  1086. * none. There can't be ambiguity in that case.
  1087. *
  1088. * If no matches found, the function does the following:
  1089. * 1. if there are saved states present, try to ignore them and search
  1090. * again only using the last command. If nothing was found, switch
  1091. * to the STATE_READY state.
  1092. * 2. if there are no saved states, switch to the STATE_READY state.
  1093. *
  1094. * RETURNS: -2 - no matched operations found.
  1095. * -1 - several matches.
  1096. * 0 - operation is found.
  1097. */
  1098. static int find_operation(struct nandsim *ns, uint32_t flag)
  1099. {
  1100. int opsfound = 0;
  1101. int i, j, idx = 0;
  1102. for (i = 0; i < NS_OPER_NUM; i++) {
  1103. int found = 1;
  1104. if (!(ns->options & ops[i].reqopts))
  1105. /* Ignore operations we can't perform */
  1106. continue;
  1107. if (flag) {
  1108. if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
  1109. continue;
  1110. } else {
  1111. if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
  1112. continue;
  1113. }
  1114. for (j = 0; j < ns->npstates; j++)
  1115. if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
  1116. && (ns->options & ops[idx].reqopts)) {
  1117. found = 0;
  1118. break;
  1119. }
  1120. if (found) {
  1121. idx = i;
  1122. opsfound += 1;
  1123. }
  1124. }
  1125. if (opsfound == 1) {
  1126. /* Exact match */
  1127. ns->op = &ops[idx].states[0];
  1128. if (flag) {
  1129. /*
  1130. * In this case the find_operation function was
  1131. * called when address has just began input. But it isn't
  1132. * yet fully input and the current state must
  1133. * not be one of STATE_ADDR_*, but the STATE_ADDR_*
  1134. * state must be the next state (ns->nxstate).
  1135. */
  1136. ns->stateidx = ns->npstates - 1;
  1137. } else {
  1138. ns->stateidx = ns->npstates;
  1139. }
  1140. ns->npstates = 0;
  1141. ns->state = ns->op[ns->stateidx];
  1142. ns->nxstate = ns->op[ns->stateidx + 1];
  1143. NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
  1144. idx, get_state_name(ns->state), get_state_name(ns->nxstate));
  1145. return 0;
  1146. }
  1147. if (opsfound == 0) {
  1148. /* Nothing was found. Try to ignore previous commands (if any) and search again */
  1149. if (ns->npstates != 0) {
  1150. NS_DBG("find_operation: no operation found, try again with state %s\n",
  1151. get_state_name(ns->state));
  1152. ns->npstates = 0;
  1153. return find_operation(ns, 0);
  1154. }
  1155. NS_DBG("find_operation: no operations found\n");
  1156. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1157. return -2;
  1158. }
  1159. if (flag) {
  1160. /* This shouldn't happen */
  1161. NS_DBG("find_operation: BUG, operation must be known if address is input\n");
  1162. return -2;
  1163. }
  1164. NS_DBG("find_operation: there is still ambiguity\n");
  1165. ns->pstates[ns->npstates++] = ns->state;
  1166. return -1;
  1167. }
  1168. static void put_pages(struct nandsim *ns)
  1169. {
  1170. int i;
  1171. for (i = 0; i < ns->held_cnt; i++)
  1172. page_cache_release(ns->held_pages[i]);
  1173. }
  1174. /* Get page cache pages in advance to provide NOFS memory allocation */
  1175. static int get_pages(struct nandsim *ns, struct file *file, size_t count, loff_t pos)
  1176. {
  1177. pgoff_t index, start_index, end_index;
  1178. struct page *page;
  1179. struct address_space *mapping = file->f_mapping;
  1180. start_index = pos >> PAGE_CACHE_SHIFT;
  1181. end_index = (pos + count - 1) >> PAGE_CACHE_SHIFT;
  1182. if (end_index - start_index + 1 > NS_MAX_HELD_PAGES)
  1183. return -EINVAL;
  1184. ns->held_cnt = 0;
  1185. for (index = start_index; index <= end_index; index++) {
  1186. page = find_get_page(mapping, index);
  1187. if (page == NULL) {
  1188. page = find_or_create_page(mapping, index, GFP_NOFS);
  1189. if (page == NULL) {
  1190. write_inode_now(mapping->host, 1);
  1191. page = find_or_create_page(mapping, index, GFP_NOFS);
  1192. }
  1193. if (page == NULL) {
  1194. put_pages(ns);
  1195. return -ENOMEM;
  1196. }
  1197. unlock_page(page);
  1198. }
  1199. ns->held_pages[ns->held_cnt++] = page;
  1200. }
  1201. return 0;
  1202. }
  1203. static int set_memalloc(void)
  1204. {
  1205. if (current->flags & PF_MEMALLOC)
  1206. return 0;
  1207. current->flags |= PF_MEMALLOC;
  1208. return 1;
  1209. }
  1210. static void clear_memalloc(int memalloc)
  1211. {
  1212. if (memalloc)
  1213. current->flags &= ~PF_MEMALLOC;
  1214. }
  1215. static ssize_t read_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t *pos)
  1216. {
  1217. mm_segment_t old_fs;
  1218. ssize_t tx;
  1219. int err, memalloc;
  1220. err = get_pages(ns, file, count, *pos);
  1221. if (err)
  1222. return err;
  1223. old_fs = get_fs();
  1224. set_fs(get_ds());
  1225. memalloc = set_memalloc();
  1226. tx = vfs_read(file, (char __user *)buf, count, pos);
  1227. clear_memalloc(memalloc);
  1228. set_fs(old_fs);
  1229. put_pages(ns);
  1230. return tx;
  1231. }
  1232. static ssize_t write_file(struct nandsim *ns, struct file *file, void *buf, size_t count, loff_t *pos)
  1233. {
  1234. mm_segment_t old_fs;
  1235. ssize_t tx;
  1236. int err, memalloc;
  1237. err = get_pages(ns, file, count, *pos);
  1238. if (err)
  1239. return err;
  1240. old_fs = get_fs();
  1241. set_fs(get_ds());
  1242. memalloc = set_memalloc();
  1243. tx = vfs_write(file, (char __user *)buf, count, pos);
  1244. clear_memalloc(memalloc);
  1245. set_fs(old_fs);
  1246. put_pages(ns);
  1247. return tx;
  1248. }
  1249. /*
  1250. * Returns a pointer to the current page.
  1251. */
  1252. static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
  1253. {
  1254. return &(ns->pages[ns->regs.row]);
  1255. }
  1256. /*
  1257. * Retuns a pointer to the current byte, within the current page.
  1258. */
  1259. static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
  1260. {
  1261. return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
  1262. }
  1263. int do_read_error(struct nandsim *ns, int num)
  1264. {
  1265. unsigned int page_no = ns->regs.row;
  1266. if (read_error(page_no)) {
  1267. int i;
  1268. memset(ns->buf.byte, 0xFF, num);
  1269. for (i = 0; i < num; ++i)
  1270. ns->buf.byte[i] = random32();
  1271. NS_WARN("simulating read error in page %u\n", page_no);
  1272. return 1;
  1273. }
  1274. return 0;
  1275. }
  1276. void do_bit_flips(struct nandsim *ns, int num)
  1277. {
  1278. if (bitflips && random32() < (1 << 22)) {
  1279. int flips = 1;
  1280. if (bitflips > 1)
  1281. flips = (random32() % (int) bitflips) + 1;
  1282. while (flips--) {
  1283. int pos = random32() % (num * 8);
  1284. ns->buf.byte[pos / 8] ^= (1 << (pos % 8));
  1285. NS_WARN("read_page: flipping bit %d in page %d "
  1286. "reading from %d ecc: corrected=%u failed=%u\n",
  1287. pos, ns->regs.row, ns->regs.column + ns->regs.off,
  1288. nsmtd->ecc_stats.corrected, nsmtd->ecc_stats.failed);
  1289. }
  1290. }
  1291. }
  1292. /*
  1293. * Fill the NAND buffer with data read from the specified page.
  1294. */
  1295. static void read_page(struct nandsim *ns, int num)
  1296. {
  1297. union ns_mem *mypage;
  1298. if (ns->cfile) {
  1299. if (!ns->pages_written[ns->regs.row]) {
  1300. NS_DBG("read_page: page %d not written\n", ns->regs.row);
  1301. memset(ns->buf.byte, 0xFF, num);
  1302. } else {
  1303. loff_t pos;
  1304. ssize_t tx;
  1305. NS_DBG("read_page: page %d written, reading from %d\n",
  1306. ns->regs.row, ns->regs.column + ns->regs.off);
  1307. if (do_read_error(ns, num))
  1308. return;
  1309. pos = (loff_t)ns->regs.row * ns->geom.pgszoob + ns->regs.column + ns->regs.off;
  1310. tx = read_file(ns, ns->cfile, ns->buf.byte, num, &pos);
  1311. if (tx != num) {
  1312. NS_ERR("read_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1313. return;
  1314. }
  1315. do_bit_flips(ns, num);
  1316. }
  1317. return;
  1318. }
  1319. mypage = NS_GET_PAGE(ns);
  1320. if (mypage->byte == NULL) {
  1321. NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
  1322. memset(ns->buf.byte, 0xFF, num);
  1323. } else {
  1324. NS_DBG("read_page: page %d allocated, reading from %d\n",
  1325. ns->regs.row, ns->regs.column + ns->regs.off);
  1326. if (do_read_error(ns, num))
  1327. return;
  1328. memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
  1329. do_bit_flips(ns, num);
  1330. }
  1331. }
  1332. /*
  1333. * Erase all pages in the specified sector.
  1334. */
  1335. static void erase_sector(struct nandsim *ns)
  1336. {
  1337. union ns_mem *mypage;
  1338. int i;
  1339. if (ns->cfile) {
  1340. for (i = 0; i < ns->geom.pgsec; i++)
  1341. if (ns->pages_written[ns->regs.row + i]) {
  1342. NS_DBG("erase_sector: freeing page %d\n", ns->regs.row + i);
  1343. ns->pages_written[ns->regs.row + i] = 0;
  1344. }
  1345. return;
  1346. }
  1347. mypage = NS_GET_PAGE(ns);
  1348. for (i = 0; i < ns->geom.pgsec; i++) {
  1349. if (mypage->byte != NULL) {
  1350. NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
  1351. kmem_cache_free(ns->nand_pages_slab, mypage->byte);
  1352. mypage->byte = NULL;
  1353. }
  1354. mypage++;
  1355. }
  1356. }
  1357. /*
  1358. * Program the specified page with the contents from the NAND buffer.
  1359. */
  1360. static int prog_page(struct nandsim *ns, int num)
  1361. {
  1362. int i;
  1363. union ns_mem *mypage;
  1364. u_char *pg_off;
  1365. if (ns->cfile) {
  1366. loff_t off, pos;
  1367. ssize_t tx;
  1368. int all;
  1369. NS_DBG("prog_page: writing page %d\n", ns->regs.row);
  1370. pg_off = ns->file_buf + ns->regs.column + ns->regs.off;
  1371. off = (loff_t)ns->regs.row * ns->geom.pgszoob + ns->regs.column + ns->regs.off;
  1372. if (!ns->pages_written[ns->regs.row]) {
  1373. all = 1;
  1374. memset(ns->file_buf, 0xff, ns->geom.pgszoob);
  1375. } else {
  1376. all = 0;
  1377. pos = off;
  1378. tx = read_file(ns, ns->cfile, pg_off, num, &pos);
  1379. if (tx != num) {
  1380. NS_ERR("prog_page: read error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1381. return -1;
  1382. }
  1383. }
  1384. for (i = 0; i < num; i++)
  1385. pg_off[i] &= ns->buf.byte[i];
  1386. if (all) {
  1387. pos = (loff_t)ns->regs.row * ns->geom.pgszoob;
  1388. tx = write_file(ns, ns->cfile, ns->file_buf, ns->geom.pgszoob, &pos);
  1389. if (tx != ns->geom.pgszoob) {
  1390. NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1391. return -1;
  1392. }
  1393. ns->pages_written[ns->regs.row] = 1;
  1394. } else {
  1395. pos = off;
  1396. tx = write_file(ns, ns->cfile, pg_off, num, &pos);
  1397. if (tx != num) {
  1398. NS_ERR("prog_page: write error for page %d ret %ld\n", ns->regs.row, (long)tx);
  1399. return -1;
  1400. }
  1401. }
  1402. return 0;
  1403. }
  1404. mypage = NS_GET_PAGE(ns);
  1405. if (mypage->byte == NULL) {
  1406. NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
  1407. /*
  1408. * We allocate memory with GFP_NOFS because a flash FS may
  1409. * utilize this. If it is holding an FS lock, then gets here,
  1410. * then kernel memory alloc runs writeback which goes to the FS
  1411. * again and deadlocks. This was seen in practice.
  1412. */
  1413. mypage->byte = kmem_cache_alloc(ns->nand_pages_slab, GFP_NOFS);
  1414. if (mypage->byte == NULL) {
  1415. NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
  1416. return -1;
  1417. }
  1418. memset(mypage->byte, 0xFF, ns->geom.pgszoob);
  1419. }
  1420. pg_off = NS_PAGE_BYTE_OFF(ns);
  1421. for (i = 0; i < num; i++)
  1422. pg_off[i] &= ns->buf.byte[i];
  1423. return 0;
  1424. }
  1425. /*
  1426. * If state has any action bit, perform this action.
  1427. *
  1428. * RETURNS: 0 if success, -1 if error.
  1429. */
  1430. static int do_state_action(struct nandsim *ns, uint32_t action)
  1431. {
  1432. int num;
  1433. int busdiv = ns->busw == 8 ? 1 : 2;
  1434. unsigned int erase_block_no, page_no;
  1435. action &= ACTION_MASK;
  1436. /* Check that page address input is correct */
  1437. if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
  1438. NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
  1439. return -1;
  1440. }
  1441. switch (action) {
  1442. case ACTION_CPY:
  1443. /*
  1444. * Copy page data to the internal buffer.
  1445. */
  1446. /* Column shouldn't be very large */
  1447. if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
  1448. NS_ERR("do_state_action: column number is too large\n");
  1449. break;
  1450. }
  1451. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  1452. read_page(ns, num);
  1453. NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
  1454. num, NS_RAW_OFFSET(ns) + ns->regs.off);
  1455. if (ns->regs.off == 0)
  1456. NS_LOG("read page %d\n", ns->regs.row);
  1457. else if (ns->regs.off < ns->geom.pgsz)
  1458. NS_LOG("read page %d (second half)\n", ns->regs.row);
  1459. else
  1460. NS_LOG("read OOB of page %d\n", ns->regs.row);
  1461. NS_UDELAY(access_delay);
  1462. NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
  1463. break;
  1464. case ACTION_SECERASE:
  1465. /*
  1466. * Erase sector.
  1467. */
  1468. if (ns->lines.wp) {
  1469. NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
  1470. return -1;
  1471. }
  1472. if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
  1473. || (ns->regs.row & ~(ns->geom.secsz - 1))) {
  1474. NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
  1475. return -1;
  1476. }
  1477. ns->regs.row = (ns->regs.row <<
  1478. 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
  1479. ns->regs.column = 0;
  1480. erase_block_no = ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift);
  1481. NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
  1482. ns->regs.row, NS_RAW_OFFSET(ns));
  1483. NS_LOG("erase sector %u\n", erase_block_no);
  1484. erase_sector(ns);
  1485. NS_MDELAY(erase_delay);
  1486. if (erase_block_wear)
  1487. update_wear(erase_block_no);
  1488. if (erase_error(erase_block_no)) {
  1489. NS_WARN("simulating erase failure in erase block %u\n", erase_block_no);
  1490. return -1;
  1491. }
  1492. break;
  1493. case ACTION_PRGPAGE:
  1494. /*
  1495. * Program page - move internal buffer data to the page.
  1496. */
  1497. if (ns->lines.wp) {
  1498. NS_WARN("do_state_action: device is write-protected, programm\n");
  1499. return -1;
  1500. }
  1501. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  1502. if (num != ns->regs.count) {
  1503. NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
  1504. ns->regs.count, num);
  1505. return -1;
  1506. }
  1507. if (prog_page(ns, num) == -1)
  1508. return -1;
  1509. page_no = ns->regs.row;
  1510. NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
  1511. num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
  1512. NS_LOG("programm page %d\n", ns->regs.row);
  1513. NS_UDELAY(programm_delay);
  1514. NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
  1515. if (write_error(page_no)) {
  1516. NS_WARN("simulating write failure in page %u\n", page_no);
  1517. return -1;
  1518. }
  1519. break;
  1520. case ACTION_ZEROOFF:
  1521. NS_DBG("do_state_action: set internal offset to 0\n");
  1522. ns->regs.off = 0;
  1523. break;
  1524. case ACTION_HALFOFF:
  1525. if (!(ns->options & OPT_PAGE512_8BIT)) {
  1526. NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
  1527. "byte page size 8x chips\n");
  1528. return -1;
  1529. }
  1530. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
  1531. ns->regs.off = ns->geom.pgsz/2;
  1532. break;
  1533. case ACTION_OOBOFF:
  1534. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
  1535. ns->regs.off = ns->geom.pgsz;
  1536. break;
  1537. default:
  1538. NS_DBG("do_state_action: BUG! unknown action\n");
  1539. }
  1540. return 0;
  1541. }
  1542. /*
  1543. * Switch simulator's state.
  1544. */
  1545. static void switch_state(struct nandsim *ns)
  1546. {
  1547. if (ns->op) {
  1548. /*
  1549. * The current operation have already been identified.
  1550. * Just follow the states chain.
  1551. */
  1552. ns->stateidx += 1;
  1553. ns->state = ns->nxstate;
  1554. ns->nxstate = ns->op[ns->stateidx + 1];
  1555. NS_DBG("switch_state: operation is known, switch to the next state, "
  1556. "state: %s, nxstate: %s\n",
  1557. get_state_name(ns->state), get_state_name(ns->nxstate));
  1558. /* See, whether we need to do some action */
  1559. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1560. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1561. return;
  1562. }
  1563. } else {
  1564. /*
  1565. * We don't yet know which operation we perform.
  1566. * Try to identify it.
  1567. */
  1568. /*
  1569. * The only event causing the switch_state function to
  1570. * be called with yet unknown operation is new command.
  1571. */
  1572. ns->state = get_state_by_command(ns->regs.command);
  1573. NS_DBG("switch_state: operation is unknown, try to find it\n");
  1574. if (find_operation(ns, 0) != 0)
  1575. return;
  1576. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1577. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1578. return;
  1579. }
  1580. }
  1581. /* For 16x devices column means the page offset in words */
  1582. if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
  1583. NS_DBG("switch_state: double the column number for 16x device\n");
  1584. ns->regs.column <<= 1;
  1585. }
  1586. if (NS_STATE(ns->nxstate) == STATE_READY) {
  1587. /*
  1588. * The current state is the last. Return to STATE_READY
  1589. */
  1590. u_char status = NS_STATUS_OK(ns);
  1591. /* In case of data states, see if all bytes were input/output */
  1592. if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
  1593. && ns->regs.count != ns->regs.num) {
  1594. NS_WARN("switch_state: not all bytes were processed, %d left\n",
  1595. ns->regs.num - ns->regs.count);
  1596. status = NS_STATUS_FAILED(ns);
  1597. }
  1598. NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
  1599. switch_to_ready_state(ns, status);
  1600. return;
  1601. } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
  1602. /*
  1603. * If the next state is data input/output, switch to it now
  1604. */
  1605. ns->state = ns->nxstate;
  1606. ns->nxstate = ns->op[++ns->stateidx + 1];
  1607. ns->regs.num = ns->regs.count = 0;
  1608. NS_DBG("switch_state: the next state is data I/O, switch, "
  1609. "state: %s, nxstate: %s\n",
  1610. get_state_name(ns->state), get_state_name(ns->nxstate));
  1611. /*
  1612. * Set the internal register to the count of bytes which
  1613. * are expected to be input or output
  1614. */
  1615. switch (NS_STATE(ns->state)) {
  1616. case STATE_DATAIN:
  1617. case STATE_DATAOUT:
  1618. ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  1619. break;
  1620. case STATE_DATAOUT_ID:
  1621. ns->regs.num = ns->geom.idbytes;
  1622. break;
  1623. case STATE_DATAOUT_STATUS:
  1624. case STATE_DATAOUT_STATUS_M:
  1625. ns->regs.count = ns->regs.num = 0;
  1626. break;
  1627. default:
  1628. NS_ERR("switch_state: BUG! unknown data state\n");
  1629. }
  1630. } else if (ns->nxstate & STATE_ADDR_MASK) {
  1631. /*
  1632. * If the next state is address input, set the internal
  1633. * register to the number of expected address bytes
  1634. */
  1635. ns->regs.count = 0;
  1636. switch (NS_STATE(ns->nxstate)) {
  1637. case STATE_ADDR_PAGE:
  1638. ns->regs.num = ns->geom.pgaddrbytes;
  1639. break;
  1640. case STATE_ADDR_SEC:
  1641. ns->regs.num = ns->geom.secaddrbytes;
  1642. break;
  1643. case STATE_ADDR_ZERO:
  1644. ns->regs.num = 1;
  1645. break;
  1646. case STATE_ADDR_COLUMN:
  1647. /* Column address is always 2 bytes */
  1648. ns->regs.num = ns->geom.pgaddrbytes - ns->geom.secaddrbytes;
  1649. break;
  1650. default:
  1651. NS_ERR("switch_state: BUG! unknown address state\n");
  1652. }
  1653. } else {
  1654. /*
  1655. * Just reset internal counters.
  1656. */
  1657. ns->regs.num = 0;
  1658. ns->regs.count = 0;
  1659. }
  1660. }
  1661. static u_char ns_nand_read_byte(struct mtd_info *mtd)
  1662. {
  1663. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1664. u_char outb = 0x00;
  1665. /* Sanity and correctness checks */
  1666. if (!ns->lines.ce) {
  1667. NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
  1668. return outb;
  1669. }
  1670. if (ns->lines.ale || ns->lines.cle) {
  1671. NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
  1672. return outb;
  1673. }
  1674. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1675. NS_WARN("read_byte: unexpected data output cycle, state is %s "
  1676. "return %#x\n", get_state_name(ns->state), (uint)outb);
  1677. return outb;
  1678. }
  1679. /* Status register may be read as many times as it is wanted */
  1680. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
  1681. NS_DBG("read_byte: return %#x status\n", ns->regs.status);
  1682. return ns->regs.status;
  1683. }
  1684. /* Check if there is any data in the internal buffer which may be read */
  1685. if (ns->regs.count == ns->regs.num) {
  1686. NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
  1687. return outb;
  1688. }
  1689. switch (NS_STATE(ns->state)) {
  1690. case STATE_DATAOUT:
  1691. if (ns->busw == 8) {
  1692. outb = ns->buf.byte[ns->regs.count];
  1693. ns->regs.count += 1;
  1694. } else {
  1695. outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
  1696. ns->regs.count += 2;
  1697. }
  1698. break;
  1699. case STATE_DATAOUT_ID:
  1700. NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
  1701. outb = ns->ids[ns->regs.count];
  1702. ns->regs.count += 1;
  1703. break;
  1704. default:
  1705. BUG();
  1706. }
  1707. if (ns->regs.count == ns->regs.num) {
  1708. NS_DBG("read_byte: all bytes were read\n");
  1709. if (NS_STATE(ns->nxstate) == STATE_READY)
  1710. switch_state(ns);
  1711. }
  1712. return outb;
  1713. }
  1714. static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
  1715. {
  1716. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1717. /* Sanity and correctness checks */
  1718. if (!ns->lines.ce) {
  1719. NS_ERR("write_byte: chip is disabled, ignore write\n");
  1720. return;
  1721. }
  1722. if (ns->lines.ale && ns->lines.cle) {
  1723. NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
  1724. return;
  1725. }
  1726. if (ns->lines.cle == 1) {
  1727. /*
  1728. * The byte written is a command.
  1729. */
  1730. if (byte == NAND_CMD_RESET) {
  1731. NS_LOG("reset chip\n");
  1732. switch_to_ready_state(ns, NS_STATUS_OK(ns));
  1733. return;
  1734. }
  1735. /* Check that the command byte is correct */
  1736. if (check_command(byte)) {
  1737. NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
  1738. return;
  1739. }
  1740. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
  1741. || NS_STATE(ns->state) == STATE_DATAOUT_STATUS_M
  1742. || NS_STATE(ns->state) == STATE_DATAOUT) {
  1743. int row = ns->regs.row;
  1744. switch_state(ns);
  1745. if (byte == NAND_CMD_RNDOUT)
  1746. ns->regs.row = row;
  1747. }
  1748. /* Check if chip is expecting command */
  1749. if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
  1750. /* Do not warn if only 2 id bytes are read */
  1751. if (!(ns->regs.command == NAND_CMD_READID &&
  1752. NS_STATE(ns->state) == STATE_DATAOUT_ID && ns->regs.count == 2)) {
  1753. /*
  1754. * We are in situation when something else (not command)
  1755. * was expected but command was input. In this case ignore
  1756. * previous command(s)/state(s) and accept the last one.
  1757. */
  1758. NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
  1759. "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
  1760. }
  1761. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1762. }
  1763. NS_DBG("command byte corresponding to %s state accepted\n",
  1764. get_state_name(get_state_by_command(byte)));
  1765. ns->regs.command = byte;
  1766. switch_state(ns);
  1767. } else if (ns->lines.ale == 1) {
  1768. /*
  1769. * The byte written is an address.
  1770. */
  1771. if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
  1772. NS_DBG("write_byte: operation isn't known yet, identify it\n");
  1773. if (find_operation(ns, 1) < 0)
  1774. return;
  1775. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1776. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1777. return;
  1778. }
  1779. ns->regs.count = 0;
  1780. switch (NS_STATE(ns->nxstate)) {
  1781. case STATE_ADDR_PAGE:
  1782. ns->regs.num = ns->geom.pgaddrbytes;
  1783. break;
  1784. case STATE_ADDR_SEC:
  1785. ns->regs.num = ns->geom.secaddrbytes;
  1786. break;
  1787. case STATE_ADDR_ZERO:
  1788. ns->regs.num = 1;
  1789. break;
  1790. default:
  1791. BUG();
  1792. }
  1793. }
  1794. /* Check that chip is expecting address */
  1795. if (!(ns->nxstate & STATE_ADDR_MASK)) {
  1796. NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
  1797. "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
  1798. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1799. return;
  1800. }
  1801. /* Check if this is expected byte */
  1802. if (ns->regs.count == ns->regs.num) {
  1803. NS_ERR("write_byte: no more address bytes expected\n");
  1804. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1805. return;
  1806. }
  1807. accept_addr_byte(ns, byte);
  1808. ns->regs.count += 1;
  1809. NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
  1810. (uint)byte, ns->regs.count, ns->regs.num);
  1811. if (ns->regs.count == ns->regs.num) {
  1812. NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
  1813. switch_state(ns);
  1814. }
  1815. } else {
  1816. /*
  1817. * The byte written is an input data.
  1818. */
  1819. /* Check that chip is expecting data input */
  1820. if (!(ns->state & STATE_DATAIN_MASK)) {
  1821. NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
  1822. "switch to %s\n", (uint)byte,
  1823. get_state_name(ns->state), get_state_name(STATE_READY));
  1824. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1825. return;
  1826. }
  1827. /* Check if this is expected byte */
  1828. if (ns->regs.count == ns->regs.num) {
  1829. NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
  1830. ns->regs.num);
  1831. return;
  1832. }
  1833. if (ns->busw == 8) {
  1834. ns->buf.byte[ns->regs.count] = byte;
  1835. ns->regs.count += 1;
  1836. } else {
  1837. ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
  1838. ns->regs.count += 2;
  1839. }
  1840. }
  1841. return;
  1842. }
  1843. static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
  1844. {
  1845. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1846. ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
  1847. ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
  1848. ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
  1849. if (cmd != NAND_CMD_NONE)
  1850. ns_nand_write_byte(mtd, cmd);
  1851. }
  1852. static int ns_device_ready(struct mtd_info *mtd)
  1853. {
  1854. NS_DBG("device_ready\n");
  1855. return 1;
  1856. }
  1857. static uint16_t ns_nand_read_word(struct mtd_info *mtd)
  1858. {
  1859. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  1860. NS_DBG("read_word\n");
  1861. return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
  1862. }
  1863. static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1864. {
  1865. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1866. /* Check that chip is expecting data input */
  1867. if (!(ns->state & STATE_DATAIN_MASK)) {
  1868. NS_ERR("write_buf: data input isn't expected, state is %s, "
  1869. "switch to STATE_READY\n", get_state_name(ns->state));
  1870. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1871. return;
  1872. }
  1873. /* Check if these are expected bytes */
  1874. if (ns->regs.count + len > ns->regs.num) {
  1875. NS_ERR("write_buf: too many input bytes\n");
  1876. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1877. return;
  1878. }
  1879. memcpy(ns->buf.byte + ns->regs.count, buf, len);
  1880. ns->regs.count += len;
  1881. if (ns->regs.count == ns->regs.num) {
  1882. NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
  1883. }
  1884. }
  1885. static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  1886. {
  1887. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1888. /* Sanity and correctness checks */
  1889. if (!ns->lines.ce) {
  1890. NS_ERR("read_buf: chip is disabled\n");
  1891. return;
  1892. }
  1893. if (ns->lines.ale || ns->lines.cle) {
  1894. NS_ERR("read_buf: ALE or CLE pin is high\n");
  1895. return;
  1896. }
  1897. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1898. NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
  1899. get_state_name(ns->state));
  1900. return;
  1901. }
  1902. if (NS_STATE(ns->state) != STATE_DATAOUT) {
  1903. int i;
  1904. for (i = 0; i < len; i++)
  1905. buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
  1906. return;
  1907. }
  1908. /* Check if these are expected bytes */
  1909. if (ns->regs.count + len > ns->regs.num) {
  1910. NS_ERR("read_buf: too many bytes to read\n");
  1911. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1912. return;
  1913. }
  1914. memcpy(buf, ns->buf.byte + ns->regs.count, len);
  1915. ns->regs.count += len;
  1916. if (ns->regs.count == ns->regs.num) {
  1917. if (NS_STATE(ns->nxstate) == STATE_READY)
  1918. switch_state(ns);
  1919. }
  1920. return;
  1921. }
  1922. /*
  1923. * Module initialization function
  1924. */
  1925. static int __init ns_init_module(void)
  1926. {
  1927. struct nand_chip *chip;
  1928. struct nandsim *nand;
  1929. int retval = -ENOMEM, i;
  1930. if (bus_width != 8 && bus_width != 16) {
  1931. NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
  1932. return -EINVAL;
  1933. }
  1934. /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
  1935. nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
  1936. + sizeof(struct nandsim), GFP_KERNEL);
  1937. if (!nsmtd) {
  1938. NS_ERR("unable to allocate core structures.\n");
  1939. return -ENOMEM;
  1940. }
  1941. chip = (struct nand_chip *)(nsmtd + 1);
  1942. nsmtd->priv = (void *)chip;
  1943. nand = (struct nandsim *)(chip + 1);
  1944. chip->priv = (void *)nand;
  1945. /*
  1946. * Register simulator's callbacks.
  1947. */
  1948. chip->cmd_ctrl = ns_hwcontrol;
  1949. chip->read_byte = ns_nand_read_byte;
  1950. chip->dev_ready = ns_device_ready;
  1951. chip->write_buf = ns_nand_write_buf;
  1952. chip->read_buf = ns_nand_read_buf;
  1953. chip->read_word = ns_nand_read_word;
  1954. chip->ecc.mode = NAND_ECC_SOFT;
  1955. /* The NAND_SKIP_BBTSCAN option is necessary for 'overridesize' */
  1956. /* and 'badblocks' parameters to work */
  1957. chip->options |= NAND_SKIP_BBTSCAN;
  1958. switch (bbt) {
  1959. case 2:
  1960. chip->bbt_options |= NAND_BBT_NO_OOB;
  1961. case 1:
  1962. chip->bbt_options |= NAND_BBT_USE_FLASH;
  1963. case 0:
  1964. break;
  1965. default:
  1966. NS_ERR("bbt has to be 0..2\n");
  1967. retval = -EINVAL;
  1968. goto error;
  1969. }
  1970. /*
  1971. * Perform minimum nandsim structure initialization to handle
  1972. * the initial ID read command correctly
  1973. */
  1974. if (third_id_byte != 0xFF || fourth_id_byte != 0xFF)
  1975. nand->geom.idbytes = 4;
  1976. else
  1977. nand->geom.idbytes = 2;
  1978. nand->regs.status = NS_STATUS_OK(nand);
  1979. nand->nxstate = STATE_UNKNOWN;
  1980. nand->options |= OPT_PAGE256; /* temporary value */
  1981. nand->ids[0] = first_id_byte;
  1982. nand->ids[1] = second_id_byte;
  1983. nand->ids[2] = third_id_byte;
  1984. nand->ids[3] = fourth_id_byte;
  1985. if (bus_width == 16) {
  1986. nand->busw = 16;
  1987. chip->options |= NAND_BUSWIDTH_16;
  1988. }
  1989. nsmtd->owner = THIS_MODULE;
  1990. if ((retval = parse_weakblocks()) != 0)
  1991. goto error;
  1992. if ((retval = parse_weakpages()) != 0)
  1993. goto error;
  1994. if ((retval = parse_gravepages()) != 0)
  1995. goto error;
  1996. retval = nand_scan_ident(nsmtd, 1, NULL);
  1997. if (retval) {
  1998. NS_ERR("cannot scan NAND Simulator device\n");
  1999. if (retval > 0)
  2000. retval = -ENXIO;
  2001. goto error;
  2002. }
  2003. if (bch) {
  2004. unsigned int eccsteps, eccbytes;
  2005. if (!mtd_nand_has_bch()) {
  2006. NS_ERR("BCH ECC support is disabled\n");
  2007. retval = -EINVAL;
  2008. goto error;
  2009. }
  2010. /* use 512-byte ecc blocks */
  2011. eccsteps = nsmtd->writesize/512;
  2012. eccbytes = (bch*13+7)/8;
  2013. /* do not bother supporting small page devices */
  2014. if ((nsmtd->oobsize < 64) || !eccsteps) {
  2015. NS_ERR("bch not available on small page devices\n");
  2016. retval = -EINVAL;
  2017. goto error;
  2018. }
  2019. if ((eccbytes*eccsteps+2) > nsmtd->oobsize) {
  2020. NS_ERR("invalid bch value %u\n", bch);
  2021. retval = -EINVAL;
  2022. goto error;
  2023. }
  2024. chip->ecc.mode = NAND_ECC_SOFT_BCH;
  2025. chip->ecc.size = 512;
  2026. chip->ecc.bytes = eccbytes;
  2027. NS_INFO("using %u-bit/%u bytes BCH ECC\n", bch, chip->ecc.size);
  2028. }
  2029. retval = nand_scan_tail(nsmtd);
  2030. if (retval) {
  2031. NS_ERR("can't register NAND Simulator\n");
  2032. if (retval > 0)
  2033. retval = -ENXIO;
  2034. goto error;
  2035. }
  2036. if (overridesize) {
  2037. uint64_t new_size = (uint64_t)nsmtd->erasesize << overridesize;
  2038. if (new_size >> overridesize != nsmtd->erasesize) {
  2039. NS_ERR("overridesize is too big\n");
  2040. retval = -EINVAL;
  2041. goto err_exit;
  2042. }
  2043. /* N.B. This relies on nand_scan not doing anything with the size before we change it */
  2044. nsmtd->size = new_size;
  2045. chip->chipsize = new_size;
  2046. chip->chip_shift = ffs(nsmtd->erasesize) + overridesize - 1;
  2047. chip->pagemask = (chip->chipsize >> chip->page_shift) - 1;
  2048. }
  2049. if ((retval = setup_wear_reporting(nsmtd)) != 0)
  2050. goto err_exit;
  2051. if ((retval = init_nandsim(nsmtd)) != 0)
  2052. goto err_exit;
  2053. if ((retval = nand_default_bbt(nsmtd)) != 0)
  2054. goto err_exit;
  2055. if ((retval = parse_badblocks(nand, nsmtd)) != 0)
  2056. goto err_exit;
  2057. /* Register NAND partitions */
  2058. retval = mtd_device_register(nsmtd, &nand->partitions[0],
  2059. nand->nbparts);
  2060. if (retval != 0)
  2061. goto err_exit;
  2062. return 0;
  2063. err_exit:
  2064. free_nandsim(nand);
  2065. nand_release(nsmtd);
  2066. for (i = 0;i < ARRAY_SIZE(nand->partitions); ++i)
  2067. kfree(nand->partitions[i].name);
  2068. error:
  2069. kfree(nsmtd);
  2070. free_lists();
  2071. return retval;
  2072. }
  2073. module_init(ns_init_module);
  2074. /*
  2075. * Module clean-up function
  2076. */
  2077. static void __exit ns_cleanup_module(void)
  2078. {
  2079. struct nandsim *ns = ((struct nand_chip *)nsmtd->priv)->priv;
  2080. int i;
  2081. free_nandsim(ns); /* Free nandsim private resources */
  2082. nand_release(nsmtd); /* Unregister driver */
  2083. for (i = 0;i < ARRAY_SIZE(ns->partitions); ++i)
  2084. kfree(ns->partitions[i].name);
  2085. kfree(nsmtd); /* Free other structures */
  2086. free_lists();
  2087. }
  2088. module_exit(ns_cleanup_module);
  2089. MODULE_LICENSE ("GPL");
  2090. MODULE_AUTHOR ("Artem B. Bityuckiy");
  2091. MODULE_DESCRIPTION ("The NAND flash simulator");