nandsim.c 67 KB

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