nandsim.c 67 KB

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