nandsim.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607
  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. * $Id: nandsim.c,v 1.8 2005/03/19 15:33:56 dedekind Exp $
  26. */
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/module.h>
  30. #include <linux/moduleparam.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/slab.h>
  33. #include <linux/errno.h>
  34. #include <linux/string.h>
  35. #include <linux/mtd/mtd.h>
  36. #include <linux/mtd/nand.h>
  37. #include <linux/mtd/partitions.h>
  38. #include <linux/delay.h>
  39. /* Default simulator parameters values */
  40. #if !defined(CONFIG_NANDSIM_FIRST_ID_BYTE) || \
  41. !defined(CONFIG_NANDSIM_SECOND_ID_BYTE) || \
  42. !defined(CONFIG_NANDSIM_THIRD_ID_BYTE) || \
  43. !defined(CONFIG_NANDSIM_FOURTH_ID_BYTE)
  44. #define CONFIG_NANDSIM_FIRST_ID_BYTE 0x98
  45. #define CONFIG_NANDSIM_SECOND_ID_BYTE 0x39
  46. #define CONFIG_NANDSIM_THIRD_ID_BYTE 0xFF /* No byte */
  47. #define CONFIG_NANDSIM_FOURTH_ID_BYTE 0xFF /* No byte */
  48. #endif
  49. #ifndef CONFIG_NANDSIM_ACCESS_DELAY
  50. #define CONFIG_NANDSIM_ACCESS_DELAY 25
  51. #endif
  52. #ifndef CONFIG_NANDSIM_PROGRAMM_DELAY
  53. #define CONFIG_NANDSIM_PROGRAMM_DELAY 200
  54. #endif
  55. #ifndef CONFIG_NANDSIM_ERASE_DELAY
  56. #define CONFIG_NANDSIM_ERASE_DELAY 2
  57. #endif
  58. #ifndef CONFIG_NANDSIM_OUTPUT_CYCLE
  59. #define CONFIG_NANDSIM_OUTPUT_CYCLE 40
  60. #endif
  61. #ifndef CONFIG_NANDSIM_INPUT_CYCLE
  62. #define CONFIG_NANDSIM_INPUT_CYCLE 50
  63. #endif
  64. #ifndef CONFIG_NANDSIM_BUS_WIDTH
  65. #define CONFIG_NANDSIM_BUS_WIDTH 8
  66. #endif
  67. #ifndef CONFIG_NANDSIM_DO_DELAYS
  68. #define CONFIG_NANDSIM_DO_DELAYS 0
  69. #endif
  70. #ifndef CONFIG_NANDSIM_LOG
  71. #define CONFIG_NANDSIM_LOG 0
  72. #endif
  73. #ifndef CONFIG_NANDSIM_DBG
  74. #define CONFIG_NANDSIM_DBG 0
  75. #endif
  76. static uint first_id_byte = CONFIG_NANDSIM_FIRST_ID_BYTE;
  77. static uint second_id_byte = CONFIG_NANDSIM_SECOND_ID_BYTE;
  78. static uint third_id_byte = CONFIG_NANDSIM_THIRD_ID_BYTE;
  79. static uint fourth_id_byte = CONFIG_NANDSIM_FOURTH_ID_BYTE;
  80. static uint access_delay = CONFIG_NANDSIM_ACCESS_DELAY;
  81. static uint programm_delay = CONFIG_NANDSIM_PROGRAMM_DELAY;
  82. static uint erase_delay = CONFIG_NANDSIM_ERASE_DELAY;
  83. static uint output_cycle = CONFIG_NANDSIM_OUTPUT_CYCLE;
  84. static uint input_cycle = CONFIG_NANDSIM_INPUT_CYCLE;
  85. static uint bus_width = CONFIG_NANDSIM_BUS_WIDTH;
  86. static uint do_delays = CONFIG_NANDSIM_DO_DELAYS;
  87. static uint log = CONFIG_NANDSIM_LOG;
  88. static uint dbg = CONFIG_NANDSIM_DBG;
  89. module_param(first_id_byte, uint, 0400);
  90. module_param(second_id_byte, uint, 0400);
  91. module_param(third_id_byte, uint, 0400);
  92. module_param(fourth_id_byte, uint, 0400);
  93. module_param(access_delay, uint, 0400);
  94. module_param(programm_delay, uint, 0400);
  95. module_param(erase_delay, uint, 0400);
  96. module_param(output_cycle, uint, 0400);
  97. module_param(input_cycle, uint, 0400);
  98. module_param(bus_width, uint, 0400);
  99. module_param(do_delays, uint, 0400);
  100. module_param(log, uint, 0400);
  101. module_param(dbg, uint, 0400);
  102. MODULE_PARM_DESC(first_id_byte, "The fist byte returned by NAND Flash 'read ID' command (manufaturer ID)");
  103. MODULE_PARM_DESC(second_id_byte, "The second byte returned by NAND Flash 'read ID' command (chip ID)");
  104. MODULE_PARM_DESC(third_id_byte, "The third byte returned by NAND Flash 'read ID' command");
  105. MODULE_PARM_DESC(fourth_id_byte, "The fourth byte returned by NAND Flash 'read ID' command");
  106. MODULE_PARM_DESC(access_delay, "Initial page access delay (microiseconds)");
  107. MODULE_PARM_DESC(programm_delay, "Page programm delay (microseconds");
  108. MODULE_PARM_DESC(erase_delay, "Sector erase delay (milliseconds)");
  109. MODULE_PARM_DESC(output_cycle, "Word output (from flash) time (nanodeconds)");
  110. MODULE_PARM_DESC(input_cycle, "Word input (to flash) time (nanodeconds)");
  111. MODULE_PARM_DESC(bus_width, "Chip's bus width (8- or 16-bit)");
  112. MODULE_PARM_DESC(do_delays, "Simulate NAND delays using busy-waits if not zero");
  113. MODULE_PARM_DESC(log, "Perform logging if not zero");
  114. MODULE_PARM_DESC(dbg, "Output debug information if not zero");
  115. /* The largest possible page size */
  116. #define NS_LARGEST_PAGE_SIZE 2048
  117. /* The prefix for simulator output */
  118. #define NS_OUTPUT_PREFIX "[nandsim]"
  119. /* Simulator's output macros (logging, debugging, warning, error) */
  120. #define NS_LOG(args...) \
  121. do { if (log) printk(KERN_DEBUG NS_OUTPUT_PREFIX " log: " args); } while(0)
  122. #define NS_DBG(args...) \
  123. do { if (dbg) printk(KERN_DEBUG NS_OUTPUT_PREFIX " debug: " args); } while(0)
  124. #define NS_WARN(args...) \
  125. do { printk(KERN_WARNING NS_OUTPUT_PREFIX " warnig: " args); } while(0)
  126. #define NS_ERR(args...) \
  127. do { printk(KERN_ERR NS_OUTPUT_PREFIX " errorr: " args); } while(0)
  128. /* Busy-wait delay macros (microseconds, milliseconds) */
  129. #define NS_UDELAY(us) \
  130. do { if (do_delays) udelay(us); } while(0)
  131. #define NS_MDELAY(us) \
  132. do { if (do_delays) mdelay(us); } while(0)
  133. /* Is the nandsim structure initialized ? */
  134. #define NS_IS_INITIALIZED(ns) ((ns)->geom.totsz != 0)
  135. /* Good operation completion status */
  136. #define NS_STATUS_OK(ns) (NAND_STATUS_READY | (NAND_STATUS_WP * ((ns)->lines.wp == 0)))
  137. /* Operation failed completion status */
  138. #define NS_STATUS_FAILED(ns) (NAND_STATUS_FAIL | NS_STATUS_OK(ns))
  139. /* Calculate the page offset in flash RAM image by (row, column) address */
  140. #define NS_RAW_OFFSET(ns) \
  141. (((ns)->regs.row << (ns)->geom.pgshift) + ((ns)->regs.row * (ns)->geom.oobsz) + (ns)->regs.column)
  142. /* Calculate the OOB offset in flash RAM image by (row, column) address */
  143. #define NS_RAW_OFFSET_OOB(ns) (NS_RAW_OFFSET(ns) + ns->geom.pgsz)
  144. /* After a command is input, the simulator goes to one of the following states */
  145. #define STATE_CMD_READ0 0x00000001 /* read data from the beginning of page */
  146. #define STATE_CMD_READ1 0x00000002 /* read data from the second half of page */
  147. #define STATE_CMD_READSTART 0x00000003 /* read data second command (large page devices) */
  148. #define STATE_CMD_PAGEPROG 0x00000004 /* start page programm */
  149. #define STATE_CMD_READOOB 0x00000005 /* read OOB area */
  150. #define STATE_CMD_ERASE1 0x00000006 /* sector erase first command */
  151. #define STATE_CMD_STATUS 0x00000007 /* read status */
  152. #define STATE_CMD_STATUS_M 0x00000008 /* read multi-plane status (isn't implemented) */
  153. #define STATE_CMD_SEQIN 0x00000009 /* sequential data imput */
  154. #define STATE_CMD_READID 0x0000000A /* read ID */
  155. #define STATE_CMD_ERASE2 0x0000000B /* sector erase second command */
  156. #define STATE_CMD_RESET 0x0000000C /* reset */
  157. #define STATE_CMD_MASK 0x0000000F /* command states mask */
  158. /* After an addres is input, the simulator goes to one of these states */
  159. #define STATE_ADDR_PAGE 0x00000010 /* full (row, column) address is accepted */
  160. #define STATE_ADDR_SEC 0x00000020 /* sector address was accepted */
  161. #define STATE_ADDR_ZERO 0x00000030 /* one byte zero address was accepted */
  162. #define STATE_ADDR_MASK 0x00000030 /* address states mask */
  163. /* Durind data input/output the simulator is in these states */
  164. #define STATE_DATAIN 0x00000100 /* waiting for data input */
  165. #define STATE_DATAIN_MASK 0x00000100 /* data input states mask */
  166. #define STATE_DATAOUT 0x00001000 /* waiting for page data output */
  167. #define STATE_DATAOUT_ID 0x00002000 /* waiting for ID bytes output */
  168. #define STATE_DATAOUT_STATUS 0x00003000 /* waiting for status output */
  169. #define STATE_DATAOUT_STATUS_M 0x00004000 /* waiting for multi-plane status output */
  170. #define STATE_DATAOUT_MASK 0x00007000 /* data output states mask */
  171. /* Previous operation is done, ready to accept new requests */
  172. #define STATE_READY 0x00000000
  173. /* This state is used to mark that the next state isn't known yet */
  174. #define STATE_UNKNOWN 0x10000000
  175. /* Simulator's actions bit masks */
  176. #define ACTION_CPY 0x00100000 /* copy page/OOB to the internal buffer */
  177. #define ACTION_PRGPAGE 0x00200000 /* programm the internal buffer to flash */
  178. #define ACTION_SECERASE 0x00300000 /* erase sector */
  179. #define ACTION_ZEROOFF 0x00400000 /* don't add any offset to address */
  180. #define ACTION_HALFOFF 0x00500000 /* add to address half of page */
  181. #define ACTION_OOBOFF 0x00600000 /* add to address OOB offset */
  182. #define ACTION_MASK 0x00700000 /* action mask */
  183. #define NS_OPER_NUM 12 /* Number of operations supported by the simulator */
  184. #define NS_OPER_STATES 6 /* Maximum number of states in operation */
  185. #define OPT_ANY 0xFFFFFFFF /* any chip supports this operation */
  186. #define OPT_PAGE256 0x00000001 /* 256-byte page chips */
  187. #define OPT_PAGE512 0x00000002 /* 512-byte page chips */
  188. #define OPT_PAGE2048 0x00000008 /* 2048-byte page chips */
  189. #define OPT_SMARTMEDIA 0x00000010 /* SmartMedia technology chips */
  190. #define OPT_AUTOINCR 0x00000020 /* page number auto inctimentation is possible */
  191. #define OPT_PAGE512_8BIT 0x00000040 /* 512-byte page chips with 8-bit bus width */
  192. #define OPT_LARGEPAGE (OPT_PAGE2048) /* 2048-byte page chips */
  193. #define OPT_SMALLPAGE (OPT_PAGE256 | OPT_PAGE512) /* 256 and 512-byte page chips */
  194. /* Remove action bits ftom state */
  195. #define NS_STATE(x) ((x) & ~ACTION_MASK)
  196. /*
  197. * Maximum previous states which need to be saved. Currently saving is
  198. * only needed for page programm operation with preceeded read command
  199. * (which is only valid for 512-byte pages).
  200. */
  201. #define NS_MAX_PREVSTATES 1
  202. /*
  203. * A union to represent flash memory contents and flash buffer.
  204. */
  205. union ns_mem {
  206. u_char *byte; /* for byte access */
  207. uint16_t *word; /* for 16-bit word access */
  208. };
  209. /*
  210. * The structure which describes all the internal simulator data.
  211. */
  212. struct nandsim {
  213. struct mtd_partition part;
  214. uint busw; /* flash chip bus width (8 or 16) */
  215. u_char ids[4]; /* chip's ID bytes */
  216. uint32_t options; /* chip's characteristic bits */
  217. uint32_t state; /* current chip state */
  218. uint32_t nxstate; /* next expected state */
  219. uint32_t *op; /* current operation, NULL operations isn't known yet */
  220. uint32_t pstates[NS_MAX_PREVSTATES]; /* previous states */
  221. uint16_t npstates; /* number of previous states saved */
  222. uint16_t stateidx; /* current state index */
  223. /* The simulated NAND flash pages array */
  224. union ns_mem *pages;
  225. /* Internal buffer of page + OOB size bytes */
  226. union ns_mem buf;
  227. /* NAND flash "geometry" */
  228. struct nandsin_geometry {
  229. uint32_t totsz; /* total flash size, bytes */
  230. uint32_t secsz; /* flash sector (erase block) size, bytes */
  231. uint pgsz; /* NAND flash page size, bytes */
  232. uint oobsz; /* page OOB area size, bytes */
  233. uint32_t totszoob; /* total flash size including OOB, bytes */
  234. uint pgszoob; /* page size including OOB , bytes*/
  235. uint secszoob; /* sector size including OOB, bytes */
  236. uint pgnum; /* total number of pages */
  237. uint pgsec; /* number of pages per sector */
  238. uint secshift; /* bits number in sector size */
  239. uint pgshift; /* bits number in page size */
  240. uint oobshift; /* bits number in OOB size */
  241. uint pgaddrbytes; /* bytes per page address */
  242. uint secaddrbytes; /* bytes per sector address */
  243. uint idbytes; /* the number ID bytes that this chip outputs */
  244. } geom;
  245. /* NAND flash internal registers */
  246. struct nandsim_regs {
  247. unsigned command; /* the command register */
  248. u_char status; /* the status register */
  249. uint row; /* the page number */
  250. uint column; /* the offset within page */
  251. uint count; /* internal counter */
  252. uint num; /* number of bytes which must be processed */
  253. uint off; /* fixed page offset */
  254. } regs;
  255. /* NAND flash lines state */
  256. struct ns_lines_status {
  257. int ce; /* chip Enable */
  258. int cle; /* command Latch Enable */
  259. int ale; /* address Latch Enable */
  260. int wp; /* write Protect */
  261. } lines;
  262. };
  263. /*
  264. * Operations array. To perform any operation the simulator must pass
  265. * through the correspondent states chain.
  266. */
  267. static struct nandsim_operations {
  268. uint32_t reqopts; /* options which are required to perform the operation */
  269. uint32_t states[NS_OPER_STATES]; /* operation's states */
  270. } ops[NS_OPER_NUM] = {
  271. /* Read page + OOB from the beginning */
  272. {OPT_SMALLPAGE, {STATE_CMD_READ0 | ACTION_ZEROOFF, STATE_ADDR_PAGE | ACTION_CPY,
  273. STATE_DATAOUT, STATE_READY}},
  274. /* Read page + OOB from the second half */
  275. {OPT_PAGE512_8BIT, {STATE_CMD_READ1 | ACTION_HALFOFF, STATE_ADDR_PAGE | ACTION_CPY,
  276. STATE_DATAOUT, STATE_READY}},
  277. /* Read OOB */
  278. {OPT_SMALLPAGE, {STATE_CMD_READOOB | ACTION_OOBOFF, STATE_ADDR_PAGE | ACTION_CPY,
  279. STATE_DATAOUT, STATE_READY}},
  280. /* Programm page starting from the beginning */
  281. {OPT_ANY, {STATE_CMD_SEQIN, STATE_ADDR_PAGE, STATE_DATAIN,
  282. STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  283. /* Programm page starting from the beginning */
  284. {OPT_SMALLPAGE, {STATE_CMD_READ0, STATE_CMD_SEQIN | ACTION_ZEROOFF, STATE_ADDR_PAGE,
  285. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  286. /* Programm page starting from the second half */
  287. {OPT_PAGE512, {STATE_CMD_READ1, STATE_CMD_SEQIN | ACTION_HALFOFF, STATE_ADDR_PAGE,
  288. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  289. /* Programm OOB */
  290. {OPT_SMALLPAGE, {STATE_CMD_READOOB, STATE_CMD_SEQIN | ACTION_OOBOFF, STATE_ADDR_PAGE,
  291. STATE_DATAIN, STATE_CMD_PAGEPROG | ACTION_PRGPAGE, STATE_READY}},
  292. /* Erase sector */
  293. {OPT_ANY, {STATE_CMD_ERASE1, STATE_ADDR_SEC, STATE_CMD_ERASE2 | ACTION_SECERASE, STATE_READY}},
  294. /* Read status */
  295. {OPT_ANY, {STATE_CMD_STATUS, STATE_DATAOUT_STATUS, STATE_READY}},
  296. /* Read multi-plane status */
  297. {OPT_SMARTMEDIA, {STATE_CMD_STATUS_M, STATE_DATAOUT_STATUS_M, STATE_READY}},
  298. /* Read ID */
  299. {OPT_ANY, {STATE_CMD_READID, STATE_ADDR_ZERO, STATE_DATAOUT_ID, STATE_READY}},
  300. /* Large page devices read page */
  301. {OPT_LARGEPAGE, {STATE_CMD_READ0, STATE_ADDR_PAGE, STATE_CMD_READSTART | ACTION_CPY,
  302. STATE_DATAOUT, STATE_READY}}
  303. };
  304. /* MTD structure for NAND controller */
  305. static struct mtd_info *nsmtd;
  306. static u_char ns_verify_buf[NS_LARGEST_PAGE_SIZE];
  307. /*
  308. * Allocate array of page pointers and initialize the array to NULL
  309. * pointers.
  310. *
  311. * RETURNS: 0 if success, -ENOMEM if memory alloc fails.
  312. */
  313. static int alloc_device(struct nandsim *ns)
  314. {
  315. int i;
  316. ns->pages = vmalloc(ns->geom.pgnum * sizeof(union ns_mem));
  317. if (!ns->pages) {
  318. NS_ERR("alloc_map: unable to allocate page array\n");
  319. return -ENOMEM;
  320. }
  321. for (i = 0; i < ns->geom.pgnum; i++) {
  322. ns->pages[i].byte = NULL;
  323. }
  324. return 0;
  325. }
  326. /*
  327. * Free any allocated pages, and free the array of page pointers.
  328. */
  329. static void free_device(struct nandsim *ns)
  330. {
  331. int i;
  332. if (ns->pages) {
  333. for (i = 0; i < ns->geom.pgnum; i++) {
  334. if (ns->pages[i].byte)
  335. kfree(ns->pages[i].byte);
  336. }
  337. vfree(ns->pages);
  338. }
  339. }
  340. /*
  341. * Initialize the nandsim structure.
  342. *
  343. * RETURNS: 0 if success, -ERRNO if failure.
  344. */
  345. static int init_nandsim(struct mtd_info *mtd)
  346. {
  347. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  348. struct nandsim *ns = (struct nandsim *)(chip->priv);
  349. int i;
  350. if (NS_IS_INITIALIZED(ns)) {
  351. NS_ERR("init_nandsim: nandsim is already initialized\n");
  352. return -EIO;
  353. }
  354. /* Force mtd to not do delays */
  355. chip->chip_delay = 0;
  356. /* Initialize the NAND flash parameters */
  357. ns->busw = chip->options & NAND_BUSWIDTH_16 ? 16 : 8;
  358. ns->geom.totsz = mtd->size;
  359. ns->geom.pgsz = mtd->writesize;
  360. ns->geom.oobsz = mtd->oobsize;
  361. ns->geom.secsz = mtd->erasesize;
  362. ns->geom.pgszoob = ns->geom.pgsz + ns->geom.oobsz;
  363. ns->geom.pgnum = ns->geom.totsz / ns->geom.pgsz;
  364. ns->geom.totszoob = ns->geom.totsz + ns->geom.pgnum * ns->geom.oobsz;
  365. ns->geom.secshift = ffs(ns->geom.secsz) - 1;
  366. ns->geom.pgshift = chip->page_shift;
  367. ns->geom.oobshift = ffs(ns->geom.oobsz) - 1;
  368. ns->geom.pgsec = ns->geom.secsz / ns->geom.pgsz;
  369. ns->geom.secszoob = ns->geom.secsz + ns->geom.oobsz * ns->geom.pgsec;
  370. ns->options = 0;
  371. if (ns->geom.pgsz == 256) {
  372. ns->options |= OPT_PAGE256;
  373. }
  374. else if (ns->geom.pgsz == 512) {
  375. ns->options |= (OPT_PAGE512 | OPT_AUTOINCR);
  376. if (ns->busw == 8)
  377. ns->options |= OPT_PAGE512_8BIT;
  378. } else if (ns->geom.pgsz == 2048) {
  379. ns->options |= OPT_PAGE2048;
  380. } else {
  381. NS_ERR("init_nandsim: unknown page size %u\n", ns->geom.pgsz);
  382. return -EIO;
  383. }
  384. if (ns->options & OPT_SMALLPAGE) {
  385. if (ns->geom.totsz < (64 << 20)) {
  386. ns->geom.pgaddrbytes = 3;
  387. ns->geom.secaddrbytes = 2;
  388. } else {
  389. ns->geom.pgaddrbytes = 4;
  390. ns->geom.secaddrbytes = 3;
  391. }
  392. } else {
  393. if (ns->geom.totsz <= (128 << 20)) {
  394. ns->geom.pgaddrbytes = 4;
  395. ns->geom.secaddrbytes = 2;
  396. } else {
  397. ns->geom.pgaddrbytes = 5;
  398. ns->geom.secaddrbytes = 3;
  399. }
  400. }
  401. /* Detect how many ID bytes the NAND chip outputs */
  402. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  403. if (second_id_byte != nand_flash_ids[i].id)
  404. continue;
  405. if (!(nand_flash_ids[i].options & NAND_NO_AUTOINCR))
  406. ns->options |= OPT_AUTOINCR;
  407. }
  408. if (ns->busw == 16)
  409. NS_WARN("16-bit flashes support wasn't tested\n");
  410. printk("flash size: %u MiB\n", ns->geom.totsz >> 20);
  411. printk("page size: %u bytes\n", ns->geom.pgsz);
  412. printk("OOB area size: %u bytes\n", ns->geom.oobsz);
  413. printk("sector size: %u KiB\n", ns->geom.secsz >> 10);
  414. printk("pages number: %u\n", ns->geom.pgnum);
  415. printk("pages per sector: %u\n", ns->geom.pgsec);
  416. printk("bus width: %u\n", ns->busw);
  417. printk("bits in sector size: %u\n", ns->geom.secshift);
  418. printk("bits in page size: %u\n", ns->geom.pgshift);
  419. printk("bits in OOB size: %u\n", ns->geom.oobshift);
  420. printk("flash size with OOB: %u KiB\n", ns->geom.totszoob >> 10);
  421. printk("page address bytes: %u\n", ns->geom.pgaddrbytes);
  422. printk("sector address bytes: %u\n", ns->geom.secaddrbytes);
  423. printk("options: %#x\n", ns->options);
  424. if (alloc_device(ns) != 0)
  425. goto error;
  426. /* Allocate / initialize the internal buffer */
  427. ns->buf.byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  428. if (!ns->buf.byte) {
  429. NS_ERR("init_nandsim: unable to allocate %u bytes for the internal buffer\n",
  430. ns->geom.pgszoob);
  431. goto error;
  432. }
  433. memset(ns->buf.byte, 0xFF, ns->geom.pgszoob);
  434. /* Fill the partition_info structure */
  435. ns->part.name = "NAND simulator partition";
  436. ns->part.offset = 0;
  437. ns->part.size = ns->geom.totsz;
  438. return 0;
  439. error:
  440. free_device(ns);
  441. return -ENOMEM;
  442. }
  443. /*
  444. * Free the nandsim structure.
  445. */
  446. static void free_nandsim(struct nandsim *ns)
  447. {
  448. kfree(ns->buf.byte);
  449. free_device(ns);
  450. return;
  451. }
  452. /*
  453. * Returns the string representation of 'state' state.
  454. */
  455. static char *get_state_name(uint32_t state)
  456. {
  457. switch (NS_STATE(state)) {
  458. case STATE_CMD_READ0:
  459. return "STATE_CMD_READ0";
  460. case STATE_CMD_READ1:
  461. return "STATE_CMD_READ1";
  462. case STATE_CMD_PAGEPROG:
  463. return "STATE_CMD_PAGEPROG";
  464. case STATE_CMD_READOOB:
  465. return "STATE_CMD_READOOB";
  466. case STATE_CMD_READSTART:
  467. return "STATE_CMD_READSTART";
  468. case STATE_CMD_ERASE1:
  469. return "STATE_CMD_ERASE1";
  470. case STATE_CMD_STATUS:
  471. return "STATE_CMD_STATUS";
  472. case STATE_CMD_STATUS_M:
  473. return "STATE_CMD_STATUS_M";
  474. case STATE_CMD_SEQIN:
  475. return "STATE_CMD_SEQIN";
  476. case STATE_CMD_READID:
  477. return "STATE_CMD_READID";
  478. case STATE_CMD_ERASE2:
  479. return "STATE_CMD_ERASE2";
  480. case STATE_CMD_RESET:
  481. return "STATE_CMD_RESET";
  482. case STATE_ADDR_PAGE:
  483. return "STATE_ADDR_PAGE";
  484. case STATE_ADDR_SEC:
  485. return "STATE_ADDR_SEC";
  486. case STATE_ADDR_ZERO:
  487. return "STATE_ADDR_ZERO";
  488. case STATE_DATAIN:
  489. return "STATE_DATAIN";
  490. case STATE_DATAOUT:
  491. return "STATE_DATAOUT";
  492. case STATE_DATAOUT_ID:
  493. return "STATE_DATAOUT_ID";
  494. case STATE_DATAOUT_STATUS:
  495. return "STATE_DATAOUT_STATUS";
  496. case STATE_DATAOUT_STATUS_M:
  497. return "STATE_DATAOUT_STATUS_M";
  498. case STATE_READY:
  499. return "STATE_READY";
  500. case STATE_UNKNOWN:
  501. return "STATE_UNKNOWN";
  502. }
  503. NS_ERR("get_state_name: unknown state, BUG\n");
  504. return NULL;
  505. }
  506. /*
  507. * Check if command is valid.
  508. *
  509. * RETURNS: 1 if wrong command, 0 if right.
  510. */
  511. static int check_command(int cmd)
  512. {
  513. switch (cmd) {
  514. case NAND_CMD_READ0:
  515. case NAND_CMD_READSTART:
  516. case NAND_CMD_PAGEPROG:
  517. case NAND_CMD_READOOB:
  518. case NAND_CMD_ERASE1:
  519. case NAND_CMD_STATUS:
  520. case NAND_CMD_SEQIN:
  521. case NAND_CMD_READID:
  522. case NAND_CMD_ERASE2:
  523. case NAND_CMD_RESET:
  524. case NAND_CMD_READ1:
  525. return 0;
  526. case NAND_CMD_STATUS_MULTI:
  527. default:
  528. return 1;
  529. }
  530. }
  531. /*
  532. * Returns state after command is accepted by command number.
  533. */
  534. static uint32_t get_state_by_command(unsigned command)
  535. {
  536. switch (command) {
  537. case NAND_CMD_READ0:
  538. return STATE_CMD_READ0;
  539. case NAND_CMD_READ1:
  540. return STATE_CMD_READ1;
  541. case NAND_CMD_PAGEPROG:
  542. return STATE_CMD_PAGEPROG;
  543. case NAND_CMD_READSTART:
  544. return STATE_CMD_READSTART;
  545. case NAND_CMD_READOOB:
  546. return STATE_CMD_READOOB;
  547. case NAND_CMD_ERASE1:
  548. return STATE_CMD_ERASE1;
  549. case NAND_CMD_STATUS:
  550. return STATE_CMD_STATUS;
  551. case NAND_CMD_STATUS_MULTI:
  552. return STATE_CMD_STATUS_M;
  553. case NAND_CMD_SEQIN:
  554. return STATE_CMD_SEQIN;
  555. case NAND_CMD_READID:
  556. return STATE_CMD_READID;
  557. case NAND_CMD_ERASE2:
  558. return STATE_CMD_ERASE2;
  559. case NAND_CMD_RESET:
  560. return STATE_CMD_RESET;
  561. }
  562. NS_ERR("get_state_by_command: unknown command, BUG\n");
  563. return 0;
  564. }
  565. /*
  566. * Move an address byte to the correspondent internal register.
  567. */
  568. static inline void accept_addr_byte(struct nandsim *ns, u_char bt)
  569. {
  570. uint byte = (uint)bt;
  571. if (ns->regs.count < (ns->geom.pgaddrbytes - ns->geom.secaddrbytes))
  572. ns->regs.column |= (byte << 8 * ns->regs.count);
  573. else {
  574. ns->regs.row |= (byte << 8 * (ns->regs.count -
  575. ns->geom.pgaddrbytes +
  576. ns->geom.secaddrbytes));
  577. }
  578. return;
  579. }
  580. /*
  581. * Switch to STATE_READY state.
  582. */
  583. static inline void switch_to_ready_state(struct nandsim *ns, u_char status)
  584. {
  585. NS_DBG("switch_to_ready_state: switch to %s state\n", get_state_name(STATE_READY));
  586. ns->state = STATE_READY;
  587. ns->nxstate = STATE_UNKNOWN;
  588. ns->op = NULL;
  589. ns->npstates = 0;
  590. ns->stateidx = 0;
  591. ns->regs.num = 0;
  592. ns->regs.count = 0;
  593. ns->regs.off = 0;
  594. ns->regs.row = 0;
  595. ns->regs.column = 0;
  596. ns->regs.status = status;
  597. }
  598. /*
  599. * If the operation isn't known yet, try to find it in the global array
  600. * of supported operations.
  601. *
  602. * Operation can be unknown because of the following.
  603. * 1. New command was accepted and this is the firs call to find the
  604. * correspondent states chain. In this case ns->npstates = 0;
  605. * 2. There is several operations which begin with the same command(s)
  606. * (for example program from the second half and read from the
  607. * second half operations both begin with the READ1 command). In this
  608. * case the ns->pstates[] array contains previous states.
  609. *
  610. * Thus, the function tries to find operation containing the following
  611. * states (if the 'flag' parameter is 0):
  612. * ns->pstates[0], ... ns->pstates[ns->npstates], ns->state
  613. *
  614. * If (one and only one) matching operation is found, it is accepted (
  615. * ns->ops, ns->state, ns->nxstate are initialized, ns->npstate is
  616. * zeroed).
  617. *
  618. * If there are several maches, the current state is pushed to the
  619. * ns->pstates.
  620. *
  621. * The operation can be unknown only while commands are input to the chip.
  622. * As soon as address command is accepted, the operation must be known.
  623. * In such situation the function is called with 'flag' != 0, and the
  624. * operation is searched using the following pattern:
  625. * ns->pstates[0], ... ns->pstates[ns->npstates], <address input>
  626. *
  627. * It is supposed that this pattern must either match one operation on
  628. * none. There can't be ambiguity in that case.
  629. *
  630. * If no matches found, the functions does the following:
  631. * 1. if there are saved states present, try to ignore them and search
  632. * again only using the last command. If nothing was found, switch
  633. * to the STATE_READY state.
  634. * 2. if there are no saved states, switch to the STATE_READY state.
  635. *
  636. * RETURNS: -2 - no matched operations found.
  637. * -1 - several matches.
  638. * 0 - operation is found.
  639. */
  640. static int find_operation(struct nandsim *ns, uint32_t flag)
  641. {
  642. int opsfound = 0;
  643. int i, j, idx = 0;
  644. for (i = 0; i < NS_OPER_NUM; i++) {
  645. int found = 1;
  646. if (!(ns->options & ops[i].reqopts))
  647. /* Ignore operations we can't perform */
  648. continue;
  649. if (flag) {
  650. if (!(ops[i].states[ns->npstates] & STATE_ADDR_MASK))
  651. continue;
  652. } else {
  653. if (NS_STATE(ns->state) != NS_STATE(ops[i].states[ns->npstates]))
  654. continue;
  655. }
  656. for (j = 0; j < ns->npstates; j++)
  657. if (NS_STATE(ops[i].states[j]) != NS_STATE(ns->pstates[j])
  658. && (ns->options & ops[idx].reqopts)) {
  659. found = 0;
  660. break;
  661. }
  662. if (found) {
  663. idx = i;
  664. opsfound += 1;
  665. }
  666. }
  667. if (opsfound == 1) {
  668. /* Exact match */
  669. ns->op = &ops[idx].states[0];
  670. if (flag) {
  671. /*
  672. * In this case the find_operation function was
  673. * called when address has just began input. But it isn't
  674. * yet fully input and the current state must
  675. * not be one of STATE_ADDR_*, but the STATE_ADDR_*
  676. * state must be the next state (ns->nxstate).
  677. */
  678. ns->stateidx = ns->npstates - 1;
  679. } else {
  680. ns->stateidx = ns->npstates;
  681. }
  682. ns->npstates = 0;
  683. ns->state = ns->op[ns->stateidx];
  684. ns->nxstate = ns->op[ns->stateidx + 1];
  685. NS_DBG("find_operation: operation found, index: %d, state: %s, nxstate %s\n",
  686. idx, get_state_name(ns->state), get_state_name(ns->nxstate));
  687. return 0;
  688. }
  689. if (opsfound == 0) {
  690. /* Nothing was found. Try to ignore previous commands (if any) and search again */
  691. if (ns->npstates != 0) {
  692. NS_DBG("find_operation: no operation found, try again with state %s\n",
  693. get_state_name(ns->state));
  694. ns->npstates = 0;
  695. return find_operation(ns, 0);
  696. }
  697. NS_DBG("find_operation: no operations found\n");
  698. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  699. return -2;
  700. }
  701. if (flag) {
  702. /* This shouldn't happen */
  703. NS_DBG("find_operation: BUG, operation must be known if address is input\n");
  704. return -2;
  705. }
  706. NS_DBG("find_operation: there is still ambiguity\n");
  707. ns->pstates[ns->npstates++] = ns->state;
  708. return -1;
  709. }
  710. /*
  711. * Returns a pointer to the current page.
  712. */
  713. static inline union ns_mem *NS_GET_PAGE(struct nandsim *ns)
  714. {
  715. return &(ns->pages[ns->regs.row]);
  716. }
  717. /*
  718. * Retuns a pointer to the current byte, within the current page.
  719. */
  720. static inline u_char *NS_PAGE_BYTE_OFF(struct nandsim *ns)
  721. {
  722. return NS_GET_PAGE(ns)->byte + ns->regs.column + ns->regs.off;
  723. }
  724. /*
  725. * Fill the NAND buffer with data read from the specified page.
  726. */
  727. static void read_page(struct nandsim *ns, int num)
  728. {
  729. union ns_mem *mypage;
  730. mypage = NS_GET_PAGE(ns);
  731. if (mypage->byte == NULL) {
  732. NS_DBG("read_page: page %d not allocated\n", ns->regs.row);
  733. memset(ns->buf.byte, 0xFF, num);
  734. } else {
  735. NS_DBG("read_page: page %d allocated, reading from %d\n",
  736. ns->regs.row, ns->regs.column + ns->regs.off);
  737. memcpy(ns->buf.byte, NS_PAGE_BYTE_OFF(ns), num);
  738. }
  739. }
  740. /*
  741. * Erase all pages in the specified sector.
  742. */
  743. static void erase_sector(struct nandsim *ns)
  744. {
  745. union ns_mem *mypage;
  746. int i;
  747. mypage = NS_GET_PAGE(ns);
  748. for (i = 0; i < ns->geom.pgsec; i++) {
  749. if (mypage->byte != NULL) {
  750. NS_DBG("erase_sector: freeing page %d\n", ns->regs.row+i);
  751. kfree(mypage->byte);
  752. mypage->byte = NULL;
  753. }
  754. mypage++;
  755. }
  756. }
  757. /*
  758. * Program the specified page with the contents from the NAND buffer.
  759. */
  760. static int prog_page(struct nandsim *ns, int num)
  761. {
  762. int i;
  763. union ns_mem *mypage;
  764. u_char *pg_off;
  765. mypage = NS_GET_PAGE(ns);
  766. if (mypage->byte == NULL) {
  767. NS_DBG("prog_page: allocating page %d\n", ns->regs.row);
  768. mypage->byte = kmalloc(ns->geom.pgszoob, GFP_KERNEL);
  769. if (mypage->byte == NULL) {
  770. NS_ERR("prog_page: error allocating memory for page %d\n", ns->regs.row);
  771. return -1;
  772. }
  773. memset(mypage->byte, 0xFF, ns->geom.pgszoob);
  774. }
  775. pg_off = NS_PAGE_BYTE_OFF(ns);
  776. for (i = 0; i < num; i++)
  777. pg_off[i] &= ns->buf.byte[i];
  778. return 0;
  779. }
  780. /*
  781. * If state has any action bit, perform this action.
  782. *
  783. * RETURNS: 0 if success, -1 if error.
  784. */
  785. static int do_state_action(struct nandsim *ns, uint32_t action)
  786. {
  787. int num;
  788. int busdiv = ns->busw == 8 ? 1 : 2;
  789. action &= ACTION_MASK;
  790. /* Check that page address input is correct */
  791. if (action != ACTION_SECERASE && ns->regs.row >= ns->geom.pgnum) {
  792. NS_WARN("do_state_action: wrong page number (%#x)\n", ns->regs.row);
  793. return -1;
  794. }
  795. switch (action) {
  796. case ACTION_CPY:
  797. /*
  798. * Copy page data to the internal buffer.
  799. */
  800. /* Column shouldn't be very large */
  801. if (ns->regs.column >= (ns->geom.pgszoob - ns->regs.off)) {
  802. NS_ERR("do_state_action: column number is too large\n");
  803. break;
  804. }
  805. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  806. read_page(ns, num);
  807. NS_DBG("do_state_action: (ACTION_CPY:) copy %d bytes to int buf, raw offset %d\n",
  808. num, NS_RAW_OFFSET(ns) + ns->regs.off);
  809. if (ns->regs.off == 0)
  810. NS_LOG("read page %d\n", ns->regs.row);
  811. else if (ns->regs.off < ns->geom.pgsz)
  812. NS_LOG("read page %d (second half)\n", ns->regs.row);
  813. else
  814. NS_LOG("read OOB of page %d\n", ns->regs.row);
  815. NS_UDELAY(access_delay);
  816. NS_UDELAY(input_cycle * ns->geom.pgsz / 1000 / busdiv);
  817. break;
  818. case ACTION_SECERASE:
  819. /*
  820. * Erase sector.
  821. */
  822. if (ns->lines.wp) {
  823. NS_ERR("do_state_action: device is write-protected, ignore sector erase\n");
  824. return -1;
  825. }
  826. if (ns->regs.row >= ns->geom.pgnum - ns->geom.pgsec
  827. || (ns->regs.row & ~(ns->geom.secsz - 1))) {
  828. NS_ERR("do_state_action: wrong sector address (%#x)\n", ns->regs.row);
  829. return -1;
  830. }
  831. ns->regs.row = (ns->regs.row <<
  832. 8 * (ns->geom.pgaddrbytes - ns->geom.secaddrbytes)) | ns->regs.column;
  833. ns->regs.column = 0;
  834. NS_DBG("do_state_action: erase sector at address %#x, off = %d\n",
  835. ns->regs.row, NS_RAW_OFFSET(ns));
  836. NS_LOG("erase sector %d\n", ns->regs.row >> (ns->geom.secshift - ns->geom.pgshift));
  837. erase_sector(ns);
  838. NS_MDELAY(erase_delay);
  839. break;
  840. case ACTION_PRGPAGE:
  841. /*
  842. * Programm page - move internal buffer data to the page.
  843. */
  844. if (ns->lines.wp) {
  845. NS_WARN("do_state_action: device is write-protected, programm\n");
  846. return -1;
  847. }
  848. num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  849. if (num != ns->regs.count) {
  850. NS_ERR("do_state_action: too few bytes were input (%d instead of %d)\n",
  851. ns->regs.count, num);
  852. return -1;
  853. }
  854. if (prog_page(ns, num) == -1)
  855. return -1;
  856. NS_DBG("do_state_action: copy %d bytes from int buf to (%#x, %#x), raw off = %d\n",
  857. num, ns->regs.row, ns->regs.column, NS_RAW_OFFSET(ns) + ns->regs.off);
  858. NS_LOG("programm page %d\n", ns->regs.row);
  859. NS_UDELAY(programm_delay);
  860. NS_UDELAY(output_cycle * ns->geom.pgsz / 1000 / busdiv);
  861. break;
  862. case ACTION_ZEROOFF:
  863. NS_DBG("do_state_action: set internal offset to 0\n");
  864. ns->regs.off = 0;
  865. break;
  866. case ACTION_HALFOFF:
  867. if (!(ns->options & OPT_PAGE512_8BIT)) {
  868. NS_ERR("do_state_action: BUG! can't skip half of page for non-512"
  869. "byte page size 8x chips\n");
  870. return -1;
  871. }
  872. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz/2);
  873. ns->regs.off = ns->geom.pgsz/2;
  874. break;
  875. case ACTION_OOBOFF:
  876. NS_DBG("do_state_action: set internal offset to %d\n", ns->geom.pgsz);
  877. ns->regs.off = ns->geom.pgsz;
  878. break;
  879. default:
  880. NS_DBG("do_state_action: BUG! unknown action\n");
  881. }
  882. return 0;
  883. }
  884. /*
  885. * Switch simulator's state.
  886. */
  887. static void switch_state(struct nandsim *ns)
  888. {
  889. if (ns->op) {
  890. /*
  891. * The current operation have already been identified.
  892. * Just follow the states chain.
  893. */
  894. ns->stateidx += 1;
  895. ns->state = ns->nxstate;
  896. ns->nxstate = ns->op[ns->stateidx + 1];
  897. NS_DBG("switch_state: operation is known, switch to the next state, "
  898. "state: %s, nxstate: %s\n",
  899. get_state_name(ns->state), get_state_name(ns->nxstate));
  900. /* See, whether we need to do some action */
  901. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  902. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  903. return;
  904. }
  905. } else {
  906. /*
  907. * We don't yet know which operation we perform.
  908. * Try to identify it.
  909. */
  910. /*
  911. * The only event causing the switch_state function to
  912. * be called with yet unknown operation is new command.
  913. */
  914. ns->state = get_state_by_command(ns->regs.command);
  915. NS_DBG("switch_state: operation is unknown, try to find it\n");
  916. if (find_operation(ns, 0) != 0)
  917. return;
  918. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  919. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  920. return;
  921. }
  922. }
  923. /* For 16x devices column means the page offset in words */
  924. if ((ns->nxstate & STATE_ADDR_MASK) && ns->busw == 16) {
  925. NS_DBG("switch_state: double the column number for 16x device\n");
  926. ns->regs.column <<= 1;
  927. }
  928. if (NS_STATE(ns->nxstate) == STATE_READY) {
  929. /*
  930. * The current state is the last. Return to STATE_READY
  931. */
  932. u_char status = NS_STATUS_OK(ns);
  933. /* In case of data states, see if all bytes were input/output */
  934. if ((ns->state & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK))
  935. && ns->regs.count != ns->regs.num) {
  936. NS_WARN("switch_state: not all bytes were processed, %d left\n",
  937. ns->regs.num - ns->regs.count);
  938. status = NS_STATUS_FAILED(ns);
  939. }
  940. NS_DBG("switch_state: operation complete, switch to STATE_READY state\n");
  941. switch_to_ready_state(ns, status);
  942. return;
  943. } else if (ns->nxstate & (STATE_DATAIN_MASK | STATE_DATAOUT_MASK)) {
  944. /*
  945. * If the next state is data input/output, switch to it now
  946. */
  947. ns->state = ns->nxstate;
  948. ns->nxstate = ns->op[++ns->stateidx + 1];
  949. ns->regs.num = ns->regs.count = 0;
  950. NS_DBG("switch_state: the next state is data I/O, switch, "
  951. "state: %s, nxstate: %s\n",
  952. get_state_name(ns->state), get_state_name(ns->nxstate));
  953. /*
  954. * Set the internal register to the count of bytes which
  955. * are expected to be input or output
  956. */
  957. switch (NS_STATE(ns->state)) {
  958. case STATE_DATAIN:
  959. case STATE_DATAOUT:
  960. ns->regs.num = ns->geom.pgszoob - ns->regs.off - ns->regs.column;
  961. break;
  962. case STATE_DATAOUT_ID:
  963. ns->regs.num = ns->geom.idbytes;
  964. break;
  965. case STATE_DATAOUT_STATUS:
  966. case STATE_DATAOUT_STATUS_M:
  967. ns->regs.count = ns->regs.num = 0;
  968. break;
  969. default:
  970. NS_ERR("switch_state: BUG! unknown data state\n");
  971. }
  972. } else if (ns->nxstate & STATE_ADDR_MASK) {
  973. /*
  974. * If the next state is address input, set the internal
  975. * register to the number of expected address bytes
  976. */
  977. ns->regs.count = 0;
  978. switch (NS_STATE(ns->nxstate)) {
  979. case STATE_ADDR_PAGE:
  980. ns->regs.num = ns->geom.pgaddrbytes;
  981. break;
  982. case STATE_ADDR_SEC:
  983. ns->regs.num = ns->geom.secaddrbytes;
  984. break;
  985. case STATE_ADDR_ZERO:
  986. ns->regs.num = 1;
  987. break;
  988. default:
  989. NS_ERR("switch_state: BUG! unknown address state\n");
  990. }
  991. } else {
  992. /*
  993. * Just reset internal counters.
  994. */
  995. ns->regs.num = 0;
  996. ns->regs.count = 0;
  997. }
  998. }
  999. static u_char ns_nand_read_byte(struct mtd_info *mtd)
  1000. {
  1001. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1002. u_char outb = 0x00;
  1003. /* Sanity and correctness checks */
  1004. if (!ns->lines.ce) {
  1005. NS_ERR("read_byte: chip is disabled, return %#x\n", (uint)outb);
  1006. return outb;
  1007. }
  1008. if (ns->lines.ale || ns->lines.cle) {
  1009. NS_ERR("read_byte: ALE or CLE pin is high, return %#x\n", (uint)outb);
  1010. return outb;
  1011. }
  1012. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1013. NS_WARN("read_byte: unexpected data output cycle, state is %s "
  1014. "return %#x\n", get_state_name(ns->state), (uint)outb);
  1015. return outb;
  1016. }
  1017. /* Status register may be read as many times as it is wanted */
  1018. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS) {
  1019. NS_DBG("read_byte: return %#x status\n", ns->regs.status);
  1020. return ns->regs.status;
  1021. }
  1022. /* Check if there is any data in the internal buffer which may be read */
  1023. if (ns->regs.count == ns->regs.num) {
  1024. NS_WARN("read_byte: no more data to output, return %#x\n", (uint)outb);
  1025. return outb;
  1026. }
  1027. switch (NS_STATE(ns->state)) {
  1028. case STATE_DATAOUT:
  1029. if (ns->busw == 8) {
  1030. outb = ns->buf.byte[ns->regs.count];
  1031. ns->regs.count += 1;
  1032. } else {
  1033. outb = (u_char)cpu_to_le16(ns->buf.word[ns->regs.count >> 1]);
  1034. ns->regs.count += 2;
  1035. }
  1036. break;
  1037. case STATE_DATAOUT_ID:
  1038. NS_DBG("read_byte: read ID byte %d, total = %d\n", ns->regs.count, ns->regs.num);
  1039. outb = ns->ids[ns->regs.count];
  1040. ns->regs.count += 1;
  1041. break;
  1042. default:
  1043. BUG();
  1044. }
  1045. if (ns->regs.count == ns->regs.num) {
  1046. NS_DBG("read_byte: all bytes were read\n");
  1047. /*
  1048. * The OPT_AUTOINCR allows to read next conseqitive pages without
  1049. * new read operation cycle.
  1050. */
  1051. if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) {
  1052. ns->regs.count = 0;
  1053. if (ns->regs.row + 1 < ns->geom.pgnum)
  1054. ns->regs.row += 1;
  1055. NS_DBG("read_byte: switch to the next page (%#x)\n", ns->regs.row);
  1056. do_state_action(ns, ACTION_CPY);
  1057. }
  1058. else if (NS_STATE(ns->nxstate) == STATE_READY)
  1059. switch_state(ns);
  1060. }
  1061. return outb;
  1062. }
  1063. static void ns_nand_write_byte(struct mtd_info *mtd, u_char byte)
  1064. {
  1065. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1066. /* Sanity and correctness checks */
  1067. if (!ns->lines.ce) {
  1068. NS_ERR("write_byte: chip is disabled, ignore write\n");
  1069. return;
  1070. }
  1071. if (ns->lines.ale && ns->lines.cle) {
  1072. NS_ERR("write_byte: ALE and CLE pins are high simultaneously, ignore write\n");
  1073. return;
  1074. }
  1075. if (ns->lines.cle == 1) {
  1076. /*
  1077. * The byte written is a command.
  1078. */
  1079. if (byte == NAND_CMD_RESET) {
  1080. NS_LOG("reset chip\n");
  1081. switch_to_ready_state(ns, NS_STATUS_OK(ns));
  1082. return;
  1083. }
  1084. /*
  1085. * Chip might still be in STATE_DATAOUT
  1086. * (if OPT_AUTOINCR feature is supported), STATE_DATAOUT_STATUS or
  1087. * STATE_DATAOUT_STATUS_M state. If so, switch state.
  1088. */
  1089. if (NS_STATE(ns->state) == STATE_DATAOUT_STATUS
  1090. || NS_STATE(ns->state) == STATE_DATAOUT_STATUS_M
  1091. || ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT))
  1092. switch_state(ns);
  1093. /* Check if chip is expecting command */
  1094. if (NS_STATE(ns->nxstate) != STATE_UNKNOWN && !(ns->nxstate & STATE_CMD_MASK)) {
  1095. /*
  1096. * We are in situation when something else (not command)
  1097. * was expected but command was input. In this case ignore
  1098. * previous command(s)/state(s) and accept the last one.
  1099. */
  1100. NS_WARN("write_byte: command (%#x) wasn't expected, expected state is %s, "
  1101. "ignore previous states\n", (uint)byte, get_state_name(ns->nxstate));
  1102. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1103. }
  1104. /* Check that the command byte is correct */
  1105. if (check_command(byte)) {
  1106. NS_ERR("write_byte: unknown command %#x\n", (uint)byte);
  1107. return;
  1108. }
  1109. NS_DBG("command byte corresponding to %s state accepted\n",
  1110. get_state_name(get_state_by_command(byte)));
  1111. ns->regs.command = byte;
  1112. switch_state(ns);
  1113. } else if (ns->lines.ale == 1) {
  1114. /*
  1115. * The byte written is an address.
  1116. */
  1117. if (NS_STATE(ns->nxstate) == STATE_UNKNOWN) {
  1118. NS_DBG("write_byte: operation isn't known yet, identify it\n");
  1119. if (find_operation(ns, 1) < 0)
  1120. return;
  1121. if ((ns->state & ACTION_MASK) && do_state_action(ns, ns->state) < 0) {
  1122. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1123. return;
  1124. }
  1125. ns->regs.count = 0;
  1126. switch (NS_STATE(ns->nxstate)) {
  1127. case STATE_ADDR_PAGE:
  1128. ns->regs.num = ns->geom.pgaddrbytes;
  1129. break;
  1130. case STATE_ADDR_SEC:
  1131. ns->regs.num = ns->geom.secaddrbytes;
  1132. break;
  1133. case STATE_ADDR_ZERO:
  1134. ns->regs.num = 1;
  1135. break;
  1136. default:
  1137. BUG();
  1138. }
  1139. }
  1140. /* Check that chip is expecting address */
  1141. if (!(ns->nxstate & STATE_ADDR_MASK)) {
  1142. NS_ERR("write_byte: address (%#x) isn't expected, expected state is %s, "
  1143. "switch to STATE_READY\n", (uint)byte, get_state_name(ns->nxstate));
  1144. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1145. return;
  1146. }
  1147. /* Check if this is expected byte */
  1148. if (ns->regs.count == ns->regs.num) {
  1149. NS_ERR("write_byte: no more address bytes expected\n");
  1150. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1151. return;
  1152. }
  1153. accept_addr_byte(ns, byte);
  1154. ns->regs.count += 1;
  1155. NS_DBG("write_byte: address byte %#x was accepted (%d bytes input, %d expected)\n",
  1156. (uint)byte, ns->regs.count, ns->regs.num);
  1157. if (ns->regs.count == ns->regs.num) {
  1158. NS_DBG("address (%#x, %#x) is accepted\n", ns->regs.row, ns->regs.column);
  1159. switch_state(ns);
  1160. }
  1161. } else {
  1162. /*
  1163. * The byte written is an input data.
  1164. */
  1165. /* Check that chip is expecting data input */
  1166. if (!(ns->state & STATE_DATAIN_MASK)) {
  1167. NS_ERR("write_byte: data input (%#x) isn't expected, state is %s, "
  1168. "switch to %s\n", (uint)byte,
  1169. get_state_name(ns->state), get_state_name(STATE_READY));
  1170. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1171. return;
  1172. }
  1173. /* Check if this is expected byte */
  1174. if (ns->regs.count == ns->regs.num) {
  1175. NS_WARN("write_byte: %u input bytes has already been accepted, ignore write\n",
  1176. ns->regs.num);
  1177. return;
  1178. }
  1179. if (ns->busw == 8) {
  1180. ns->buf.byte[ns->regs.count] = byte;
  1181. ns->regs.count += 1;
  1182. } else {
  1183. ns->buf.word[ns->regs.count >> 1] = cpu_to_le16((uint16_t)byte);
  1184. ns->regs.count += 2;
  1185. }
  1186. }
  1187. return;
  1188. }
  1189. static void ns_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int bitmask)
  1190. {
  1191. struct nandsim *ns = ((struct nand_chip *)mtd->priv)->priv;
  1192. ns->lines.cle = bitmask & NAND_CLE ? 1 : 0;
  1193. ns->lines.ale = bitmask & NAND_ALE ? 1 : 0;
  1194. ns->lines.ce = bitmask & NAND_NCE ? 1 : 0;
  1195. if (cmd != NAND_CMD_NONE)
  1196. ns_nand_write_byte(mtd, cmd);
  1197. }
  1198. static int ns_device_ready(struct mtd_info *mtd)
  1199. {
  1200. NS_DBG("device_ready\n");
  1201. return 1;
  1202. }
  1203. static uint16_t ns_nand_read_word(struct mtd_info *mtd)
  1204. {
  1205. struct nand_chip *chip = (struct nand_chip *)mtd->priv;
  1206. NS_DBG("read_word\n");
  1207. return chip->read_byte(mtd) | (chip->read_byte(mtd) << 8);
  1208. }
  1209. static void ns_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1210. {
  1211. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1212. /* Check that chip is expecting data input */
  1213. if (!(ns->state & STATE_DATAIN_MASK)) {
  1214. NS_ERR("write_buf: data input isn't expected, state is %s, "
  1215. "switch to STATE_READY\n", get_state_name(ns->state));
  1216. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1217. return;
  1218. }
  1219. /* Check if these are expected bytes */
  1220. if (ns->regs.count + len > ns->regs.num) {
  1221. NS_ERR("write_buf: too many input bytes\n");
  1222. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1223. return;
  1224. }
  1225. memcpy(ns->buf.byte + ns->regs.count, buf, len);
  1226. ns->regs.count += len;
  1227. if (ns->regs.count == ns->regs.num) {
  1228. NS_DBG("write_buf: %d bytes were written\n", ns->regs.count);
  1229. }
  1230. }
  1231. static void ns_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  1232. {
  1233. struct nandsim *ns = (struct nandsim *)((struct nand_chip *)mtd->priv)->priv;
  1234. /* Sanity and correctness checks */
  1235. if (!ns->lines.ce) {
  1236. NS_ERR("read_buf: chip is disabled\n");
  1237. return;
  1238. }
  1239. if (ns->lines.ale || ns->lines.cle) {
  1240. NS_ERR("read_buf: ALE or CLE pin is high\n");
  1241. return;
  1242. }
  1243. if (!(ns->state & STATE_DATAOUT_MASK)) {
  1244. NS_WARN("read_buf: unexpected data output cycle, current state is %s\n",
  1245. get_state_name(ns->state));
  1246. return;
  1247. }
  1248. if (NS_STATE(ns->state) != STATE_DATAOUT) {
  1249. int i;
  1250. for (i = 0; i < len; i++)
  1251. buf[i] = ((struct nand_chip *)mtd->priv)->read_byte(mtd);
  1252. return;
  1253. }
  1254. /* Check if these are expected bytes */
  1255. if (ns->regs.count + len > ns->regs.num) {
  1256. NS_ERR("read_buf: too many bytes to read\n");
  1257. switch_to_ready_state(ns, NS_STATUS_FAILED(ns));
  1258. return;
  1259. }
  1260. memcpy(buf, ns->buf.byte + ns->regs.count, len);
  1261. ns->regs.count += len;
  1262. if (ns->regs.count == ns->regs.num) {
  1263. if ((ns->options & OPT_AUTOINCR) && NS_STATE(ns->state) == STATE_DATAOUT) {
  1264. ns->regs.count = 0;
  1265. if (ns->regs.row + 1 < ns->geom.pgnum)
  1266. ns->regs.row += 1;
  1267. NS_DBG("read_buf: switch to the next page (%#x)\n", ns->regs.row);
  1268. do_state_action(ns, ACTION_CPY);
  1269. }
  1270. else if (NS_STATE(ns->nxstate) == STATE_READY)
  1271. switch_state(ns);
  1272. }
  1273. return;
  1274. }
  1275. static int ns_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  1276. {
  1277. ns_nand_read_buf(mtd, (u_char *)&ns_verify_buf[0], len);
  1278. if (!memcmp(buf, &ns_verify_buf[0], len)) {
  1279. NS_DBG("verify_buf: the buffer is OK\n");
  1280. return 0;
  1281. } else {
  1282. NS_DBG("verify_buf: the buffer is wrong\n");
  1283. return -EFAULT;
  1284. }
  1285. }
  1286. /*
  1287. * Module initialization function
  1288. */
  1289. static int __init ns_init_module(void)
  1290. {
  1291. struct nand_chip *chip;
  1292. struct nandsim *nand;
  1293. int retval = -ENOMEM;
  1294. if (bus_width != 8 && bus_width != 16) {
  1295. NS_ERR("wrong bus width (%d), use only 8 or 16\n", bus_width);
  1296. return -EINVAL;
  1297. }
  1298. /* Allocate and initialize mtd_info, nand_chip and nandsim structures */
  1299. nsmtd = kzalloc(sizeof(struct mtd_info) + sizeof(struct nand_chip)
  1300. + sizeof(struct nandsim), GFP_KERNEL);
  1301. if (!nsmtd) {
  1302. NS_ERR("unable to allocate core structures.\n");
  1303. return -ENOMEM;
  1304. }
  1305. chip = (struct nand_chip *)(nsmtd + 1);
  1306. nsmtd->priv = (void *)chip;
  1307. nand = (struct nandsim *)(chip + 1);
  1308. chip->priv = (void *)nand;
  1309. /*
  1310. * Register simulator's callbacks.
  1311. */
  1312. chip->cmd_ctrl = ns_hwcontrol;
  1313. chip->read_byte = ns_nand_read_byte;
  1314. chip->dev_ready = ns_device_ready;
  1315. chip->write_buf = ns_nand_write_buf;
  1316. chip->read_buf = ns_nand_read_buf;
  1317. chip->verify_buf = ns_nand_verify_buf;
  1318. chip->read_word = ns_nand_read_word;
  1319. chip->ecc.mode = NAND_ECC_SOFT;
  1320. chip->options |= NAND_SKIP_BBTSCAN;
  1321. /*
  1322. * Perform minimum nandsim structure initialization to handle
  1323. * the initial ID read command correctly
  1324. */
  1325. if (third_id_byte != 0xFF || fourth_id_byte != 0xFF)
  1326. nand->geom.idbytes = 4;
  1327. else
  1328. nand->geom.idbytes = 2;
  1329. nand->regs.status = NS_STATUS_OK(nand);
  1330. nand->nxstate = STATE_UNKNOWN;
  1331. nand->options |= OPT_PAGE256; /* temporary value */
  1332. nand->ids[0] = first_id_byte;
  1333. nand->ids[1] = second_id_byte;
  1334. nand->ids[2] = third_id_byte;
  1335. nand->ids[3] = fourth_id_byte;
  1336. if (bus_width == 16) {
  1337. nand->busw = 16;
  1338. chip->options |= NAND_BUSWIDTH_16;
  1339. }
  1340. nsmtd->owner = THIS_MODULE;
  1341. if ((retval = nand_scan(nsmtd, 1)) != 0) {
  1342. NS_ERR("can't register NAND Simulator\n");
  1343. if (retval > 0)
  1344. retval = -ENXIO;
  1345. goto error;
  1346. }
  1347. if ((retval = init_nandsim(nsmtd)) != 0) {
  1348. NS_ERR("scan_bbt: can't initialize the nandsim structure\n");
  1349. goto error;
  1350. }
  1351. if ((retval = nand_default_bbt(nsmtd)) != 0) {
  1352. free_nandsim(nand);
  1353. goto error;
  1354. }
  1355. /* Register NAND as one big partition */
  1356. add_mtd_partitions(nsmtd, &nand->part, 1);
  1357. return 0;
  1358. error:
  1359. kfree(nsmtd);
  1360. return retval;
  1361. }
  1362. module_init(ns_init_module);
  1363. /*
  1364. * Module clean-up function
  1365. */
  1366. static void __exit ns_cleanup_module(void)
  1367. {
  1368. struct nandsim *ns = (struct nandsim *)(((struct nand_chip *)nsmtd->priv)->priv);
  1369. free_nandsim(ns); /* Free nandsim private resources */
  1370. nand_release(nsmtd); /* Unregisterd drived */
  1371. kfree(nsmtd); /* Free other structures */
  1372. }
  1373. module_exit(ns_cleanup_module);
  1374. MODULE_LICENSE ("GPL");
  1375. MODULE_AUTHOR ("Artem B. Bityuckiy");
  1376. MODULE_DESCRIPTION ("The NAND flash simulator");