nandsim.c 67 KB

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