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