nandsim.c 68 KB

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