cmd_nand.c 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697
  1. /*
  2. * Driver for NAND support, Rick Bronson
  3. * borrowed heavily from:
  4. * (c) 1999 Machine Vision Holdings, Inc.
  5. * (c) 1999, 2000 David Woodhouse <dwmw2@infradead.org>
  6. */
  7. #include <common.h>
  8. #include <command.h>
  9. #include <malloc.h>
  10. #include <asm/io.h>
  11. #include <watchdog.h>
  12. #ifdef CONFIG_SHOW_BOOT_PROGRESS
  13. # include <status_led.h>
  14. # define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
  15. #else
  16. # define SHOW_BOOT_PROGRESS(arg)
  17. #endif
  18. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  19. #ifdef CONFIG_AT91RM9200DK
  20. #include <asm/arch/hardware.h>
  21. #endif
  22. #include <linux/mtd/nand.h>
  23. #include <linux/mtd/nand_ids.h>
  24. #include <jffs2/jffs2.h>
  25. #ifdef CONFIG_OMAP1510
  26. void archflashwp(void *archdata, int wp);
  27. #endif
  28. #define ROUND_DOWN(value,boundary) ((value) & (~((boundary)-1)))
  29. /*
  30. * Definition of the out of band configuration structure
  31. */
  32. struct nand_oob_config {
  33. int ecc_pos[6]; /* position of ECC bytes inside oob */
  34. int badblock_pos; /* position of bad block flag inside oob -1 = inactive */
  35. int eccvalid_pos; /* position of ECC valid flag inside oob -1 = inactive */
  36. } oob_config = { {0}, 0, 0};
  37. #undef NAND_DEBUG
  38. #undef PSYCHO_DEBUG
  39. /* ****************** WARNING *********************
  40. * When ALLOW_ERASE_BAD_DEBUG is non-zero the erase command will
  41. * erase (or at least attempt to erase) blocks that are marked
  42. * bad. This can be very handy if you are _sure_ that the block
  43. * is OK, say because you marked a good block bad to test bad
  44. * block handling and you are done testing, or if you have
  45. * accidentally marked blocks bad.
  46. *
  47. * Erasing factory marked bad blocks is a _bad_ idea. If the
  48. * erase succeeds there is no reliable way to find them again,
  49. * and attempting to program or erase bad blocks can affect
  50. * the data in _other_ (good) blocks.
  51. */
  52. #define ALLOW_ERASE_BAD_DEBUG 0
  53. #define CONFIG_MTD_NAND_ECC /* enable ECC */
  54. #define CONFIG_MTD_NAND_ECC_JFFS2
  55. /* bits for nand_rw() `cmd'; or together as needed */
  56. #define NANDRW_READ 0x01
  57. #define NANDRW_WRITE 0x00
  58. #define NANDRW_JFFS2 0x02
  59. #define NANDRW_JFFS2_SKIP 0x04
  60. /*
  61. * Function Prototypes
  62. */
  63. static void nand_print(struct nand_chip *nand);
  64. int nand_rw (struct nand_chip* nand, int cmd,
  65. size_t start, size_t len,
  66. size_t * retlen, u_char * buf);
  67. int nand_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean);
  68. static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
  69. size_t * retlen, u_char *buf, u_char *ecc_code);
  70. static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
  71. size_t * retlen, const u_char * buf, u_char * ecc_code);
  72. static void nand_print_bad(struct nand_chip *nand);
  73. static int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,
  74. size_t * retlen, u_char * buf);
  75. static int nand_write_oob(struct nand_chip* nand, size_t ofs, size_t len,
  76. size_t * retlen, const u_char * buf);
  77. static int NanD_WaitReady(struct nand_chip *nand, int ale_wait);
  78. #ifdef CONFIG_MTD_NAND_ECC
  79. static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc);
  80. static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code);
  81. #endif
  82. struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE] = {{0}};
  83. /* Current NAND Device */
  84. static int curr_device = -1;
  85. /* ------------------------------------------------------------------------- */
  86. int do_nand (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  87. {
  88. int rcode = 0;
  89. switch (argc) {
  90. case 0:
  91. case 1:
  92. printf ("Usage:\n%s\n", cmdtp->usage);
  93. return 1;
  94. case 2:
  95. if (strcmp(argv[1],"info") == 0) {
  96. int i;
  97. putc ('\n');
  98. for (i=0; i<CFG_MAX_NAND_DEVICE; ++i) {
  99. if(nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN)
  100. continue; /* list only known devices */
  101. printf ("Device %d: ", i);
  102. nand_print(&nand_dev_desc[i]);
  103. }
  104. return 0;
  105. } else if (strcmp(argv[1],"device") == 0) {
  106. if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
  107. puts ("\nno devices available\n");
  108. return 1;
  109. }
  110. printf ("\nDevice %d: ", curr_device);
  111. nand_print(&nand_dev_desc[curr_device]);
  112. return 0;
  113. } else if (strcmp(argv[1],"bad") == 0) {
  114. if ((curr_device < 0) || (curr_device >= CFG_MAX_NAND_DEVICE)) {
  115. puts ("\nno devices available\n");
  116. return 1;
  117. }
  118. printf ("\nDevice %d bad blocks:\n", curr_device);
  119. nand_print_bad(&nand_dev_desc[curr_device]);
  120. return 0;
  121. }
  122. printf ("Usage:\n%s\n", cmdtp->usage);
  123. return 1;
  124. case 3:
  125. if (strcmp(argv[1],"device") == 0) {
  126. int dev = (int)simple_strtoul(argv[2], NULL, 10);
  127. printf ("\nDevice %d: ", dev);
  128. if (dev >= CFG_MAX_NAND_DEVICE) {
  129. puts ("unknown device\n");
  130. return 1;
  131. }
  132. nand_print(&nand_dev_desc[dev]);
  133. /*nand_print (dev);*/
  134. if (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN) {
  135. return 1;
  136. }
  137. curr_device = dev;
  138. puts ("... is now current device\n");
  139. return 0;
  140. }
  141. else if (strcmp(argv[1],"erase") == 0 && strcmp(argv[2], "clean") == 0) {
  142. struct nand_chip* nand = &nand_dev_desc[curr_device];
  143. ulong off = 0;
  144. ulong size = nand->totlen;
  145. int ret;
  146. printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
  147. curr_device, off, size);
  148. ret = nand_erase (nand, off, size, 1);
  149. printf("%s\n", ret ? "ERROR" : "OK");
  150. return ret;
  151. }
  152. printf ("Usage:\n%s\n", cmdtp->usage);
  153. return 1;
  154. default:
  155. /* at least 4 args */
  156. if (strncmp(argv[1], "read", 4) == 0 ||
  157. strncmp(argv[1], "write", 5) == 0) {
  158. ulong addr = simple_strtoul(argv[2], NULL, 16);
  159. ulong off = simple_strtoul(argv[3], NULL, 16);
  160. ulong size = simple_strtoul(argv[4], NULL, 16);
  161. int cmd = (strncmp(argv[1], "read", 4) == 0) ?
  162. NANDRW_READ : NANDRW_WRITE;
  163. int ret, total;
  164. char* cmdtail = strchr(argv[1], '.');
  165. if (cmdtail && !strncmp(cmdtail, ".oob", 2)) {
  166. /* read out-of-band data */
  167. if (cmd & NANDRW_READ) {
  168. ret = nand_read_oob(nand_dev_desc + curr_device,
  169. off, size, &total,
  170. (u_char*)addr);
  171. }
  172. else {
  173. ret = nand_write_oob(nand_dev_desc + curr_device,
  174. off, size, &total,
  175. (u_char*)addr);
  176. }
  177. return ret;
  178. }
  179. else if (cmdtail && !strncmp(cmdtail, ".jffs2", 2))
  180. cmd |= NANDRW_JFFS2; /* skip bad blocks */
  181. else if (cmdtail && !strncmp(cmdtail, ".jffs2s", 2)) {
  182. cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
  183. if (cmd & NANDRW_READ)
  184. cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
  185. }
  186. #ifdef SXNI855T
  187. /* need ".e" same as ".j" for compatibility with older units */
  188. else if (cmdtail && !strcmp(cmdtail, ".e"))
  189. cmd |= NANDRW_JFFS2; /* skip bad blocks */
  190. #endif
  191. #ifdef CFG_NAND_SKIP_BAD_DOT_I
  192. /* need ".i" same as ".jffs2s" for compatibility with older units (esd) */
  193. /* ".i" for image -> read skips bad block (no 0xff) */
  194. else if (cmdtail && !strcmp(cmdtail, ".i"))
  195. cmd |= NANDRW_JFFS2; /* skip bad blocks (on read too) */
  196. if (cmd & NANDRW_READ)
  197. cmd |= NANDRW_JFFS2_SKIP; /* skip bad blocks (on read too) */
  198. #endif /* CFG_NAND_SKIP_BAD_DOT_I */
  199. else if (cmdtail) {
  200. printf ("Usage:\n%s\n", cmdtp->usage);
  201. return 1;
  202. }
  203. printf ("\nNAND %s: device %d offset %ld, size %ld ... ",
  204. (cmd & NANDRW_READ) ? "read" : "write",
  205. curr_device, off, size);
  206. ret = nand_rw(nand_dev_desc + curr_device, cmd, off, size,
  207. &total, (u_char*)addr);
  208. printf (" %d bytes %s: %s\n", total,
  209. (cmd & NANDRW_READ) ? "read" : "written",
  210. ret ? "ERROR" : "OK");
  211. return ret;
  212. } else if (strcmp(argv[1],"erase") == 0 &&
  213. (argc == 4 || strcmp("clean", argv[2]) == 0)) {
  214. int clean = argc == 5;
  215. ulong off = simple_strtoul(argv[2 + clean], NULL, 16);
  216. ulong size = simple_strtoul(argv[3 + clean], NULL, 16);
  217. int ret;
  218. printf ("\nNAND erase: device %d offset %ld, size %ld ... ",
  219. curr_device, off, size);
  220. ret = nand_erase (nand_dev_desc + curr_device, off, size, clean);
  221. printf("%s\n", ret ? "ERROR" : "OK");
  222. return ret;
  223. } else {
  224. printf ("Usage:\n%s\n", cmdtp->usage);
  225. rcode = 1;
  226. }
  227. return rcode;
  228. }
  229. }
  230. U_BOOT_CMD(
  231. nand, 5, 1, do_nand,
  232. "nand - NAND sub-system\n",
  233. "info - show available NAND devices\n"
  234. "nand device [dev] - show or set current device\n"
  235. "nand read[.jffs2[s]] addr off size\n"
  236. "nand write[.jffs2] addr off size - read/write `size' bytes starting\n"
  237. " at offset `off' to/from memory address `addr'\n"
  238. "nand erase [clean] [off size] - erase `size' bytes from\n"
  239. " offset `off' (entire device if not specified)\n"
  240. "nand bad - show bad blocks\n"
  241. "nand read.oob addr off size - read out-of-band data\n"
  242. "nand write.oob addr off size - read out-of-band data\n"
  243. );
  244. int do_nandboot (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  245. {
  246. char *boot_device = NULL;
  247. char *ep;
  248. int dev;
  249. ulong cnt;
  250. ulong addr;
  251. ulong offset = 0;
  252. image_header_t *hdr;
  253. int rcode = 0;
  254. switch (argc) {
  255. case 1:
  256. addr = CFG_LOAD_ADDR;
  257. boot_device = getenv ("bootdevice");
  258. break;
  259. case 2:
  260. addr = simple_strtoul(argv[1], NULL, 16);
  261. boot_device = getenv ("bootdevice");
  262. break;
  263. case 3:
  264. addr = simple_strtoul(argv[1], NULL, 16);
  265. boot_device = argv[2];
  266. break;
  267. case 4:
  268. addr = simple_strtoul(argv[1], NULL, 16);
  269. boot_device = argv[2];
  270. offset = simple_strtoul(argv[3], NULL, 16);
  271. break;
  272. default:
  273. printf ("Usage:\n%s\n", cmdtp->usage);
  274. SHOW_BOOT_PROGRESS (-1);
  275. return 1;
  276. }
  277. if (!boot_device) {
  278. puts ("\n** No boot device **\n");
  279. SHOW_BOOT_PROGRESS (-1);
  280. return 1;
  281. }
  282. dev = simple_strtoul(boot_device, &ep, 16);
  283. if ((dev >= CFG_MAX_NAND_DEVICE) ||
  284. (nand_dev_desc[dev].ChipID == NAND_ChipID_UNKNOWN)) {
  285. printf ("\n** Device %d not available\n", dev);
  286. SHOW_BOOT_PROGRESS (-1);
  287. return 1;
  288. }
  289. printf ("\nLoading from device %d: %s at 0x%lx (offset 0x%lx)\n",
  290. dev, nand_dev_desc[dev].name, nand_dev_desc[dev].IO_ADDR,
  291. offset);
  292. if (nand_rw (nand_dev_desc + dev, NANDRW_READ, offset,
  293. SECTORSIZE, NULL, (u_char *)addr)) {
  294. printf ("** Read error on %d\n", dev);
  295. SHOW_BOOT_PROGRESS (-1);
  296. return 1;
  297. }
  298. hdr = (image_header_t *)addr;
  299. if (ntohl(hdr->ih_magic) == IH_MAGIC) {
  300. print_image_hdr (hdr);
  301. cnt = (ntohl(hdr->ih_size) + sizeof(image_header_t));
  302. cnt -= SECTORSIZE;
  303. } else {
  304. printf ("\n** Bad Magic Number 0x%x **\n", hdr->ih_magic);
  305. SHOW_BOOT_PROGRESS (-1);
  306. return 1;
  307. }
  308. if (nand_rw (nand_dev_desc + dev, NANDRW_READ, offset + SECTORSIZE, cnt,
  309. NULL, (u_char *)(addr+SECTORSIZE))) {
  310. printf ("** Read error on %d\n", dev);
  311. SHOW_BOOT_PROGRESS (-1);
  312. return 1;
  313. }
  314. /* Loading ok, update default load address */
  315. load_addr = addr;
  316. /* Check if we should attempt an auto-start */
  317. if (((ep = getenv("autostart")) != NULL) && (strcmp(ep,"yes") == 0)) {
  318. char *local_args[2];
  319. extern int do_bootm (cmd_tbl_t *, int, int, char *[]);
  320. local_args[0] = argv[0];
  321. local_args[1] = NULL;
  322. printf ("Automatic boot of image at addr 0x%08lx ...\n", addr);
  323. do_bootm (cmdtp, 0, 1, local_args);
  324. rcode = 1;
  325. }
  326. return rcode;
  327. }
  328. U_BOOT_CMD(
  329. nboot, 4, 1, do_nandboot,
  330. "nboot - boot from NAND device\n",
  331. "loadAddr dev\n"
  332. );
  333. /* returns 0 if block containing pos is OK:
  334. * valid erase block and
  335. * not marked bad, or no bad mark position is specified
  336. * returns 1 if marked bad or otherwise invalid
  337. */
  338. int check_block(struct nand_chip* nand, unsigned long pos)
  339. {
  340. int retlen;
  341. uint8_t oob_data;
  342. int page0 = pos & (-nand->erasesize);
  343. int page1 = page0 + nand->oobblock;
  344. int badpos = oob_config.badblock_pos;
  345. if (pos >= nand->totlen)
  346. return 1;
  347. if (badpos < 0)
  348. return 0; /* no way to check, assume OK */
  349. /* Note - bad block marker can be on first or second page */
  350. if (nand_read_oob(nand, page0 + badpos, 1, &retlen, &oob_data) ||
  351. oob_data != 0xff ||
  352. nand_read_oob(nand, page1 + badpos, 1, &retlen, &oob_data) ||
  353. oob_data != 0xff)
  354. return 1;
  355. return 0;
  356. }
  357. /* print bad blocks in NAND flash */
  358. static void nand_print_bad(struct nand_chip* nand)
  359. {
  360. unsigned long pos;
  361. for (pos = 0; pos < nand->totlen; pos += nand->erasesize) {
  362. if (check_block(nand, pos))
  363. printf(" 0x%8.8lx\n", pos);
  364. }
  365. puts("\n");
  366. }
  367. /* cmd: 0: NANDRW_WRITE write, fail on bad block
  368. * 1: NANDRW_READ read, fail on bad block
  369. * 2: NANDRW_WRITE | NANDRW_JFFS2 write, skip bad blocks
  370. * 3: NANDRW_READ | NANDRW_JFFS2 read, data all 0xff for bad blocks
  371. * 7: NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP read, skip bad blocks
  372. */
  373. int nand_rw (struct nand_chip* nand, int cmd,
  374. size_t start, size_t len,
  375. size_t * retlen, u_char * buf)
  376. {
  377. int ret = 0, n, total = 0;
  378. char eccbuf[6];
  379. /* eblk (once set) is the start of the erase block containing the
  380. * data being processed.
  381. */
  382. unsigned long eblk = ~0; /* force mismatch on first pass */
  383. unsigned long erasesize = nand->erasesize;
  384. while (len) {
  385. if ((start & (-erasesize)) != eblk) {
  386. /* have crossed into new erase block, deal with
  387. * it if it is sure marked bad.
  388. */
  389. eblk = start & (-erasesize); /* start of block */
  390. if (check_block(nand, eblk)) {
  391. if (cmd == (NANDRW_READ | NANDRW_JFFS2)) {
  392. while (len > 0 &&
  393. start - eblk < erasesize) {
  394. *(buf++) = 0xff;
  395. ++start;
  396. ++total;
  397. --len;
  398. }
  399. continue;
  400. }
  401. else if (cmd == (NANDRW_READ | NANDRW_JFFS2 | NANDRW_JFFS2_SKIP)) {
  402. start += erasesize;
  403. continue;
  404. }
  405. else if (cmd == (NANDRW_WRITE | NANDRW_JFFS2)) {
  406. /* skip bad block */
  407. start += erasesize;
  408. continue;
  409. }
  410. else {
  411. ret = 1;
  412. break;
  413. }
  414. }
  415. }
  416. /* The ECC will not be calculated correctly if
  417. less than 512 is written or read */
  418. /* Is request at least 512 bytes AND it starts on a proper boundry */
  419. if((start != ROUND_DOWN(start, 0x200)) || (len < 0x200))
  420. printf("Warning block writes should be at least 512 bytes and start on a 512 byte boundry\n");
  421. if (cmd & NANDRW_READ)
  422. ret = nand_read_ecc(nand, start,
  423. min(len, eblk + erasesize - start),
  424. &n, (u_char*)buf, eccbuf);
  425. else
  426. ret = nand_write_ecc(nand, start,
  427. min(len, eblk + erasesize - start),
  428. &n, (u_char*)buf, eccbuf);
  429. if (ret)
  430. break;
  431. start += n;
  432. buf += n;
  433. total += n;
  434. len -= n;
  435. }
  436. if (retlen)
  437. *retlen = total;
  438. return ret;
  439. }
  440. static void nand_print(struct nand_chip *nand)
  441. {
  442. if (nand->numchips > 1) {
  443. printf("%s at 0x%lx,\n"
  444. "\t %d chips %s, size %d MB, \n"
  445. "\t total size %ld MB, sector size %ld kB\n",
  446. nand->name, nand->IO_ADDR, nand->numchips,
  447. nand->chips_name, 1 << (nand->chipshift - 20),
  448. nand->totlen >> 20, nand->erasesize >> 10);
  449. }
  450. else {
  451. printf("%s at 0x%lx (", nand->chips_name, nand->IO_ADDR);
  452. print_size(nand->totlen, ", ");
  453. print_size(nand->erasesize, " sector)\n");
  454. }
  455. }
  456. /* ------------------------------------------------------------------------- */
  457. static int NanD_WaitReady(struct nand_chip *nand, int ale_wait)
  458. {
  459. /* This is inline, to optimise the common case, where it's ready instantly */
  460. int ret = 0;
  461. #ifdef NAND_NO_RB /* in config file, shorter delays currently wrap accesses */
  462. if(ale_wait)
  463. NAND_WAIT_READY(nand); /* do the worst case 25us wait */
  464. else
  465. udelay(10);
  466. #else /* has functional r/b signal */
  467. NAND_WAIT_READY(nand);
  468. #endif
  469. return ret;
  470. }
  471. /* NanD_Command: Send a flash command to the flash chip */
  472. static inline int NanD_Command(struct nand_chip *nand, unsigned char command)
  473. {
  474. unsigned long nandptr = nand->IO_ADDR;
  475. /* Assert the CLE (Command Latch Enable) line to the flash chip */
  476. NAND_CTL_SETCLE(nandptr);
  477. /* Send the command */
  478. WRITE_NAND_COMMAND(command, nandptr);
  479. /* Lower the CLE line */
  480. NAND_CTL_CLRCLE(nandptr);
  481. #ifdef NAND_NO_RB
  482. if(command == NAND_CMD_RESET){
  483. u_char ret_val;
  484. NanD_Command(nand, NAND_CMD_STATUS);
  485. do{
  486. ret_val = READ_NAND(nandptr);/* wait till ready */
  487. } while((ret_val & 0x40) != 0x40);
  488. }
  489. #endif
  490. return NanD_WaitReady(nand, 0);
  491. }
  492. /* NanD_Address: Set the current address for the flash chip */
  493. static int NanD_Address(struct nand_chip *nand, int numbytes, unsigned long ofs)
  494. {
  495. unsigned long nandptr;
  496. int i;
  497. nandptr = nand->IO_ADDR;
  498. /* Assert the ALE (Address Latch Enable) line to the flash chip */
  499. NAND_CTL_SETALE(nandptr);
  500. /* Send the address */
  501. /* Devices with 256-byte page are addressed as:
  502. * Column (bits 0-7), Page (bits 8-15, 16-23, 24-31)
  503. * there is no device on the market with page256
  504. * and more than 24 bits.
  505. * Devices with 512-byte page are addressed as:
  506. * Column (bits 0-7), Page (bits 9-16, 17-24, 25-31)
  507. * 25-31 is sent only if the chip support it.
  508. * bit 8 changes the read command to be sent
  509. * (NAND_CMD_READ0 or NAND_CMD_READ1).
  510. */
  511. if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE)
  512. WRITE_NAND_ADDRESS(ofs, nandptr);
  513. ofs = ofs >> nand->page_shift;
  514. if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE)
  515. for (i = 0; i < nand->pageadrlen; i++, ofs = ofs >> 8)
  516. WRITE_NAND_ADDRESS(ofs, nandptr);
  517. /* Lower the ALE line */
  518. NAND_CTL_CLRALE(nandptr);
  519. /* Wait for the chip to respond */
  520. return NanD_WaitReady(nand, 1);
  521. }
  522. /* NanD_SelectChip: Select a given flash chip within the current floor */
  523. static inline int NanD_SelectChip(struct nand_chip *nand, int chip)
  524. {
  525. /* Wait for it to be ready */
  526. return NanD_WaitReady(nand, 0);
  527. }
  528. /* NanD_IdentChip: Identify a given NAND chip given {floor,chip} */
  529. static int NanD_IdentChip(struct nand_chip *nand, int floor, int chip)
  530. {
  531. int mfr, id, i;
  532. NAND_ENABLE_CE(nand); /* set pin low */
  533. /* Reset the chip */
  534. if (NanD_Command(nand, NAND_CMD_RESET)) {
  535. #ifdef NAND_DEBUG
  536. printf("NanD_Command (reset) for %d,%d returned true\n",
  537. floor, chip);
  538. #endif
  539. NAND_DISABLE_CE(nand); /* set pin high */
  540. return 0;
  541. }
  542. /* Read the NAND chip ID: 1. Send ReadID command */
  543. if (NanD_Command(nand, NAND_CMD_READID)) {
  544. #ifdef NAND_DEBUG
  545. printf("NanD_Command (ReadID) for %d,%d returned true\n",
  546. floor, chip);
  547. #endif
  548. NAND_DISABLE_CE(nand); /* set pin high */
  549. return 0;
  550. }
  551. /* Read the NAND chip ID: 2. Send address byte zero */
  552. NanD_Address(nand, ADDR_COLUMN, 0);
  553. /* Read the manufacturer and device id codes from the device */
  554. mfr = READ_NAND(nand->IO_ADDR);
  555. id = READ_NAND(nand->IO_ADDR);
  556. NAND_DISABLE_CE(nand); /* set pin high */
  557. /* No response - return failure */
  558. if (mfr == 0xff || mfr == 0) {
  559. #ifdef NAND_DEBUG
  560. printf("NanD_Command (ReadID) got %d %d\n", mfr, id);
  561. #endif
  562. return 0;
  563. }
  564. /* Check it's the same as the first chip we identified.
  565. * M-Systems say that any given nand_chip device should only
  566. * contain _one_ type of flash part, although that's not a
  567. * hardware restriction. */
  568. if (nand->mfr) {
  569. if (nand->mfr == mfr && nand->id == id)
  570. return 1; /* This is another the same the first */
  571. else
  572. printf("Flash chip at floor %d, chip %d is different:\n",
  573. floor, chip);
  574. }
  575. /* Print and store the manufacturer and ID codes. */
  576. for (i = 0; nand_flash_ids[i].name != NULL; i++) {
  577. if (mfr == nand_flash_ids[i].manufacture_id &&
  578. id == nand_flash_ids[i].model_id) {
  579. #ifdef NAND_DEBUG
  580. printf("Flash chip found:\n\t Manufacturer ID: 0x%2.2X, "
  581. "Chip ID: 0x%2.2X (%s)\n", mfr, id,
  582. nand_flash_ids[i].name);
  583. #endif
  584. if (!nand->mfr) {
  585. nand->mfr = mfr;
  586. nand->id = id;
  587. nand->chipshift =
  588. nand_flash_ids[i].chipshift;
  589. nand->page256 = nand_flash_ids[i].page256;
  590. nand->eccsize = 256;
  591. if (nand->page256) {
  592. nand->oobblock = 256;
  593. nand->oobsize = 8;
  594. nand->page_shift = 8;
  595. } else {
  596. nand->oobblock = 512;
  597. nand->oobsize = 16;
  598. nand->page_shift = 9;
  599. }
  600. nand->pageadrlen =
  601. nand_flash_ids[i].pageadrlen;
  602. nand->erasesize =
  603. nand_flash_ids[i].erasesize;
  604. nand->chips_name =
  605. nand_flash_ids[i].name;
  606. return 1;
  607. }
  608. return 0;
  609. }
  610. }
  611. #ifdef NAND_DEBUG
  612. /* We haven't fully identified the chip. Print as much as we know. */
  613. printf("Unknown flash chip found: %2.2X %2.2X\n",
  614. id, mfr);
  615. #endif
  616. return 0;
  617. }
  618. /* NanD_ScanChips: Find all NAND chips present in a nand_chip, and identify them */
  619. static void NanD_ScanChips(struct nand_chip *nand)
  620. {
  621. int floor, chip;
  622. int numchips[NAND_MAX_FLOORS];
  623. int maxchips = NAND_MAX_CHIPS;
  624. int ret = 1;
  625. nand->numchips = 0;
  626. nand->mfr = 0;
  627. nand->id = 0;
  628. /* For each floor, find the number of valid chips it contains */
  629. for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
  630. ret = 1;
  631. numchips[floor] = 0;
  632. for (chip = 0; chip < maxchips && ret != 0; chip++) {
  633. ret = NanD_IdentChip(nand, floor, chip);
  634. if (ret) {
  635. numchips[floor]++;
  636. nand->numchips++;
  637. }
  638. }
  639. }
  640. /* If there are none at all that we recognise, bail */
  641. if (!nand->numchips) {
  642. #ifdef NAND_DEBUG
  643. puts ("No NAND flash chips recognised.\n");
  644. #endif
  645. return;
  646. }
  647. /* Allocate an array to hold the information for each chip */
  648. nand->chips = malloc(sizeof(struct Nand) * nand->numchips);
  649. if (!nand->chips) {
  650. puts ("No memory for allocating chip info structures\n");
  651. return;
  652. }
  653. ret = 0;
  654. /* Fill out the chip array with {floor, chipno} for each
  655. * detected chip in the device. */
  656. for (floor = 0; floor < NAND_MAX_FLOORS; floor++) {
  657. for (chip = 0; chip < numchips[floor]; chip++) {
  658. nand->chips[ret].floor = floor;
  659. nand->chips[ret].chip = chip;
  660. nand->chips[ret].curadr = 0;
  661. nand->chips[ret].curmode = 0x50;
  662. ret++;
  663. }
  664. }
  665. /* Calculate and print the total size of the device */
  666. nand->totlen = nand->numchips * (1 << nand->chipshift);
  667. #ifdef NAND_DEBUG
  668. printf("%d flash chips found. Total nand_chip size: %ld MB\n",
  669. nand->numchips, nand->totlen >> 20);
  670. #endif
  671. }
  672. /* we need to be fast here, 1 us per read translates to 1 second per meg */
  673. static void NanD_ReadBuf(struct nand_chip *nand, u_char *data_buf, int cntr)
  674. {
  675. unsigned long nandptr = nand->IO_ADDR;
  676. while (cntr >= 16) {
  677. *data_buf++ = READ_NAND(nandptr);
  678. *data_buf++ = READ_NAND(nandptr);
  679. *data_buf++ = READ_NAND(nandptr);
  680. *data_buf++ = READ_NAND(nandptr);
  681. *data_buf++ = READ_NAND(nandptr);
  682. *data_buf++ = READ_NAND(nandptr);
  683. *data_buf++ = READ_NAND(nandptr);
  684. *data_buf++ = READ_NAND(nandptr);
  685. *data_buf++ = READ_NAND(nandptr);
  686. *data_buf++ = READ_NAND(nandptr);
  687. *data_buf++ = READ_NAND(nandptr);
  688. *data_buf++ = READ_NAND(nandptr);
  689. *data_buf++ = READ_NAND(nandptr);
  690. *data_buf++ = READ_NAND(nandptr);
  691. *data_buf++ = READ_NAND(nandptr);
  692. *data_buf++ = READ_NAND(nandptr);
  693. cntr -= 16;
  694. }
  695. while (cntr > 0) {
  696. *data_buf++ = READ_NAND(nandptr);
  697. cntr--;
  698. }
  699. }
  700. /*
  701. * NAND read with ECC
  702. */
  703. static int nand_read_ecc(struct nand_chip *nand, size_t start, size_t len,
  704. size_t * retlen, u_char *buf, u_char *ecc_code)
  705. {
  706. int col, page;
  707. int ecc_status = 0;
  708. #ifdef CONFIG_MTD_NAND_ECC
  709. int j;
  710. int ecc_failed = 0;
  711. u_char *data_poi;
  712. u_char ecc_calc[6];
  713. #endif
  714. /* Do not allow reads past end of device */
  715. if ((start + len) > nand->totlen) {
  716. printf ("%s: Attempt read beyond end of device %x %x %x\n", __FUNCTION__, (uint) start, (uint) len, (uint) nand->totlen);
  717. *retlen = 0;
  718. return -1;
  719. }
  720. /* First we calculate the starting page */
  721. /*page = shr(start, nand->page_shift);*/
  722. page = start >> nand->page_shift;
  723. /* Get raw starting column */
  724. col = start & (nand->oobblock - 1);
  725. /* Initialize return value */
  726. *retlen = 0;
  727. /* Select the NAND device */
  728. NAND_ENABLE_CE(nand); /* set pin low */
  729. /* Loop until all data read */
  730. while (*retlen < len) {
  731. #ifdef CONFIG_MTD_NAND_ECC
  732. /* Do we have this page in cache ? */
  733. if (nand->cache_page == page)
  734. goto readdata;
  735. /* Send the read command */
  736. NanD_Command(nand, NAND_CMD_READ0);
  737. NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
  738. /* Read in a page + oob data */
  739. NanD_ReadBuf(nand, nand->data_buf, nand->oobblock + nand->oobsize);
  740. /* copy data into cache, for read out of cache and if ecc fails */
  741. if (nand->data_cache)
  742. memcpy (nand->data_cache, nand->data_buf, nand->oobblock + nand->oobsize);
  743. /* Pick the ECC bytes out of the oob data */
  744. for (j = 0; j < 6; j++)
  745. ecc_code[j] = nand->data_buf[(nand->oobblock + oob_config.ecc_pos[j])];
  746. /* Calculate the ECC and verify it */
  747. /* If block was not written with ECC, skip ECC */
  748. if (oob_config.eccvalid_pos != -1 &&
  749. (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0x0f) != 0x0f) {
  750. nand_calculate_ecc (&nand->data_buf[0], &ecc_calc[0]);
  751. switch (nand_correct_data (&nand->data_buf[0], &ecc_code[0], &ecc_calc[0])) {
  752. case -1:
  753. printf ("%s: Failed ECC read, page 0x%08x\n", __FUNCTION__, page);
  754. ecc_failed++;
  755. break;
  756. case 1:
  757. case 2: /* transfer ECC corrected data to cache */
  758. if (nand->data_cache)
  759. memcpy (nand->data_cache, nand->data_buf, 256);
  760. break;
  761. }
  762. }
  763. if (oob_config.eccvalid_pos != -1 &&
  764. nand->oobblock == 512 && (nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] & 0xf0) != 0xf0) {
  765. nand_calculate_ecc (&nand->data_buf[256], &ecc_calc[3]);
  766. switch (nand_correct_data (&nand->data_buf[256], &ecc_code[3], &ecc_calc[3])) {
  767. case -1:
  768. printf ("%s: Failed ECC read, page 0x%08x\n", __FUNCTION__, page);
  769. ecc_failed++;
  770. break;
  771. case 1:
  772. case 2: /* transfer ECC corrected data to cache */
  773. if (nand->data_cache)
  774. memcpy (&nand->data_cache[256], &nand->data_buf[256], 256);
  775. break;
  776. }
  777. }
  778. readdata:
  779. /* Read the data from ECC data buffer into return buffer */
  780. data_poi = (nand->data_cache) ? nand->data_cache : nand->data_buf;
  781. data_poi += col;
  782. if ((*retlen + (nand->oobblock - col)) >= len) {
  783. memcpy (buf + *retlen, data_poi, len - *retlen);
  784. *retlen = len;
  785. } else {
  786. memcpy (buf + *retlen, data_poi, nand->oobblock - col);
  787. *retlen += nand->oobblock - col;
  788. }
  789. /* Set cache page address, invalidate, if ecc_failed */
  790. nand->cache_page = (nand->data_cache && !ecc_failed) ? page : -1;
  791. ecc_status += ecc_failed;
  792. ecc_failed = 0;
  793. #else
  794. /* Send the read command */
  795. NanD_Command(nand, NAND_CMD_READ0);
  796. NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
  797. /* Read the data directly into the return buffer */
  798. if ((*retlen + (nand->oobblock - col)) >= len) {
  799. NanD_ReadBuf(nand, buf + *retlen, len - *retlen);
  800. *retlen = len;
  801. /* We're done */
  802. continue;
  803. } else {
  804. NanD_ReadBuf(nand, buf + *retlen, nand->oobblock - col);
  805. *retlen += nand->oobblock - col;
  806. }
  807. #endif
  808. /* For subsequent reads align to page boundary. */
  809. col = 0;
  810. /* Increment page address */
  811. page++;
  812. }
  813. /* De-select the NAND device */
  814. NAND_DISABLE_CE(nand); /* set pin high */
  815. /*
  816. * Return success, if no ECC failures, else -EIO
  817. * fs driver will take care of that, because
  818. * retlen == desired len and result == -EIO
  819. */
  820. return ecc_status ? -1 : 0;
  821. }
  822. /*
  823. * Nand_page_program function is used for write and writev !
  824. */
  825. static int nand_write_page (struct nand_chip *nand,
  826. int page, int col, int last, u_char * ecc_code)
  827. {
  828. int i;
  829. unsigned long nandptr = nand->IO_ADDR;
  830. #ifdef CONFIG_MTD_NAND_ECC
  831. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  832. int ecc_bytes = (nand->oobblock == 512) ? 6 : 3;
  833. #endif
  834. #endif
  835. /* pad oob area */
  836. for (i = nand->oobblock; i < nand->oobblock + nand->oobsize; i++)
  837. nand->data_buf[i] = 0xff;
  838. #ifdef CONFIG_MTD_NAND_ECC
  839. /* Zero out the ECC array */
  840. for (i = 0; i < 6; i++)
  841. ecc_code[i] = 0x00;
  842. /* Read back previous written data, if col > 0 */
  843. if (col) {
  844. NanD_Command(nand, NAND_CMD_READ0);
  845. NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
  846. for (i = 0; i < col; i++)
  847. nand->data_buf[i] = READ_NAND (nandptr);
  848. }
  849. /* Calculate and write the ECC if we have enough data */
  850. if ((col < nand->eccsize) && (last >= nand->eccsize)) {
  851. nand_calculate_ecc (&nand->data_buf[0], &(ecc_code[0]));
  852. for (i = 0; i < 3; i++)
  853. nand->data_buf[(nand->oobblock + oob_config.ecc_pos[i])] = ecc_code[i];
  854. if (oob_config.eccvalid_pos != -1)
  855. nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] = 0xf0;
  856. }
  857. /* Calculate and write the second ECC if we have enough data */
  858. if ((nand->oobblock == 512) && (last == nand->oobblock)) {
  859. nand_calculate_ecc (&nand->data_buf[256], &(ecc_code[3]));
  860. for (i = 3; i < 6; i++)
  861. nand->data_buf[(nand->oobblock + oob_config.ecc_pos[i])] = ecc_code[i];
  862. if (oob_config.eccvalid_pos != -1)
  863. nand->data_buf[nand->oobblock + oob_config.eccvalid_pos] &= 0x0f;
  864. }
  865. #endif
  866. /* Prepad for partial page programming !!! */
  867. for (i = 0; i < col; i++)
  868. nand->data_buf[i] = 0xff;
  869. /* Postpad for partial page programming !!! oob is already padded */
  870. for (i = last; i < nand->oobblock; i++)
  871. nand->data_buf[i] = 0xff;
  872. /* Send command to begin auto page programming */
  873. NanD_Command(nand, NAND_CMD_READ0);
  874. NanD_Command(nand, NAND_CMD_SEQIN);
  875. NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
  876. /* Write out complete page of data */
  877. for (i = 0; i < (nand->oobblock + nand->oobsize); i++)
  878. WRITE_NAND(nand->data_buf[i], nand->IO_ADDR);
  879. /* Send command to actually program the data */
  880. NanD_Command(nand, NAND_CMD_PAGEPROG);
  881. NanD_Command(nand, NAND_CMD_STATUS);
  882. #ifdef NAND_NO_RB
  883. { u_char ret_val;
  884. do{
  885. ret_val = READ_NAND(nandptr); /* wait till ready */
  886. } while((ret_val & 0x40) != 0x40);
  887. }
  888. #endif
  889. /* See if device thinks it succeeded */
  890. if (READ_NAND(nand->IO_ADDR) & 0x01) {
  891. printf ("%s: Failed write, page 0x%08x, ", __FUNCTION__, page);
  892. return -1;
  893. }
  894. #ifdef CONFIG_MTD_NAND_VERIFY_WRITE
  895. /*
  896. * The NAND device assumes that it is always writing to
  897. * a cleanly erased page. Hence, it performs its internal
  898. * write verification only on bits that transitioned from
  899. * 1 to 0. The device does NOT verify the whole page on a
  900. * byte by byte basis. It is possible that the page was
  901. * not completely erased or the page is becoming unusable
  902. * due to wear. The read with ECC would catch the error
  903. * later when the ECC page check fails, but we would rather
  904. * catch it early in the page write stage. Better to write
  905. * no data than invalid data.
  906. */
  907. /* Send command to read back the page */
  908. if (col < nand->eccsize)
  909. NanD_Command(nand, NAND_CMD_READ0);
  910. else
  911. NanD_Command(nand, NAND_CMD_READ1);
  912. NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
  913. /* Loop through and verify the data */
  914. for (i = col; i < last; i++) {
  915. if (nand->data_buf[i] != readb (nand->IO_ADDR)) {
  916. printf ("%s: Failed write verify, page 0x%08x ", __FUNCTION__, page);
  917. return -1;
  918. }
  919. }
  920. #ifdef CONFIG_MTD_NAND_ECC
  921. /*
  922. * We also want to check that the ECC bytes wrote
  923. * correctly for the same reasons stated above.
  924. */
  925. NanD_Command(nand, NAND_CMD_READOOB);
  926. NanD_Address(nand, ADDR_COLUMN_PAGE, (page << nand->page_shift) + col);
  927. for (i = 0; i < nand->oobsize; i++)
  928. nand->data_buf[i] = readb (nand->IO_ADDR);
  929. for (i = 0; i < ecc_bytes; i++) {
  930. if ((nand->data_buf[(oob_config.ecc_pos[i])] != ecc_code[i]) && ecc_code[i]) {
  931. printf ("%s: Failed ECC write "
  932. "verify, page 0x%08x, " "%6i bytes were succesful\n", __FUNCTION__, page, i);
  933. return -1;
  934. }
  935. }
  936. #endif
  937. #endif
  938. return 0;
  939. }
  940. static int nand_write_ecc (struct nand_chip* nand, size_t to, size_t len,
  941. size_t * retlen, const u_char * buf, u_char * ecc_code)
  942. {
  943. int i, page, col, cnt, ret = 0;
  944. /* Do not allow write past end of device */
  945. if ((to + len) > nand->totlen) {
  946. printf ("%s: Attempt to write past end of page\n", __FUNCTION__);
  947. return -1;
  948. }
  949. /* Shift to get page */
  950. page = ((int) to) >> nand->page_shift;
  951. /* Get the starting column */
  952. col = to & (nand->oobblock - 1);
  953. /* Initialize return length value */
  954. *retlen = 0;
  955. /* Select the NAND device */
  956. #ifdef CONFIG_OMAP1510
  957. archflashwp(0,0);
  958. #endif
  959. NAND_ENABLE_CE(nand); /* set pin low */
  960. /* Check the WP bit */
  961. NanD_Command(nand, NAND_CMD_STATUS);
  962. if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
  963. printf ("%s: Device is write protected!!!\n", __FUNCTION__);
  964. ret = -1;
  965. goto out;
  966. }
  967. /* Loop until all data is written */
  968. while (*retlen < len) {
  969. /* Invalidate cache, if we write to this page */
  970. if (nand->cache_page == page)
  971. nand->cache_page = -1;
  972. /* Write data into buffer */
  973. if ((col + len) >= nand->oobblock)
  974. for (i = col, cnt = 0; i < nand->oobblock; i++, cnt++)
  975. nand->data_buf[i] = buf[(*retlen + cnt)];
  976. else
  977. for (i = col, cnt = 0; cnt < (len - *retlen); i++, cnt++)
  978. nand->data_buf[i] = buf[(*retlen + cnt)];
  979. /* We use the same function for write and writev !) */
  980. ret = nand_write_page (nand, page, col, i, ecc_code);
  981. if (ret)
  982. goto out;
  983. /* Next data start at page boundary */
  984. col = 0;
  985. /* Update written bytes count */
  986. *retlen += cnt;
  987. /* Increment page address */
  988. page++;
  989. }
  990. /* Return happy */
  991. *retlen = len;
  992. out:
  993. /* De-select the NAND device */
  994. NAND_DISABLE_CE(nand); /* set pin high */
  995. #ifdef CONFIG_OMAP1510
  996. archflashwp(0,1);
  997. #endif
  998. return ret;
  999. }
  1000. /* read from the 16 bytes of oob data that correspond to a 512 byte
  1001. * page or 2 256-byte pages.
  1002. */
  1003. static int nand_read_oob(struct nand_chip* nand, size_t ofs, size_t len,
  1004. size_t * retlen, u_char * buf)
  1005. {
  1006. int len256 = 0;
  1007. struct Nand *mychip;
  1008. int ret = 0;
  1009. mychip = &nand->chips[ofs >> nand->chipshift];
  1010. /* update address for 2M x 8bit devices. OOB starts on the second */
  1011. /* page to maintain compatibility with nand_read_ecc. */
  1012. if (nand->page256) {
  1013. if (!(ofs & 0x8))
  1014. ofs += 0x100;
  1015. else
  1016. ofs -= 0x8;
  1017. }
  1018. NAND_ENABLE_CE(nand); /* set pin low */
  1019. NanD_Command(nand, NAND_CMD_READOOB);
  1020. NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
  1021. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  1022. /* Note: datasheet says it should automaticaly wrap to the */
  1023. /* next OOB block, but it didn't work here. mf. */
  1024. if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
  1025. len256 = (ofs | 0x7) + 1 - ofs;
  1026. NanD_ReadBuf(nand, buf, len256);
  1027. NanD_Command(nand, NAND_CMD_READOOB);
  1028. NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
  1029. }
  1030. NanD_ReadBuf(nand, &buf[len256], len - len256);
  1031. *retlen = len;
  1032. /* Reading the full OOB data drops us off of the end of the page,
  1033. * causing the flash device to go into busy mode, so we need
  1034. * to wait until ready 11.4.1 and Toshiba TC58256FT nands */
  1035. ret = NanD_WaitReady(nand, 1);
  1036. NAND_DISABLE_CE(nand); /* set pin high */
  1037. return ret;
  1038. }
  1039. /* write to the 16 bytes of oob data that correspond to a 512 byte
  1040. * page or 2 256-byte pages.
  1041. */
  1042. static int nand_write_oob(struct nand_chip* nand, size_t ofs, size_t len,
  1043. size_t * retlen, const u_char * buf)
  1044. {
  1045. int len256 = 0;
  1046. int i;
  1047. unsigned long nandptr = nand->IO_ADDR;
  1048. #ifdef PSYCHO_DEBUG
  1049. printf("nand_write_oob(%lx, %d): %2.2X %2.2X %2.2X %2.2X ... %2.2X %2.2X .. %2.2X %2.2X\n",
  1050. (long)ofs, len, buf[0], buf[1], buf[2], buf[3],
  1051. buf[8], buf[9], buf[14],buf[15]);
  1052. #endif
  1053. NAND_ENABLE_CE(nand); /* set pin low to enable chip */
  1054. /* Reset the chip */
  1055. NanD_Command(nand, NAND_CMD_RESET);
  1056. /* issue the Read2 command to set the pointer to the Spare Data Area. */
  1057. NanD_Command(nand, NAND_CMD_READOOB);
  1058. NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
  1059. /* update address for 2M x 8bit devices. OOB starts on the second */
  1060. /* page to maintain compatibility with nand_read_ecc. */
  1061. if (nand->page256) {
  1062. if (!(ofs & 0x8))
  1063. ofs += 0x100;
  1064. else
  1065. ofs -= 0x8;
  1066. }
  1067. /* issue the Serial Data In command to initial the Page Program process */
  1068. NanD_Command(nand, NAND_CMD_SEQIN);
  1069. NanD_Address(nand, ADDR_COLUMN_PAGE, ofs);
  1070. /* treat crossing 8-byte OOB data for 2M x 8bit devices */
  1071. /* Note: datasheet says it should automaticaly wrap to the */
  1072. /* next OOB block, but it didn't work here. mf. */
  1073. if (nand->page256 && ofs + len > (ofs | 0x7) + 1) {
  1074. len256 = (ofs | 0x7) + 1 - ofs;
  1075. for (i = 0; i < len256; i++)
  1076. WRITE_NAND(buf[i], nandptr);
  1077. NanD_Command(nand, NAND_CMD_PAGEPROG);
  1078. NanD_Command(nand, NAND_CMD_STATUS);
  1079. #ifdef NAND_NO_RB
  1080. { u_char ret_val;
  1081. do{
  1082. ret_val = READ_NAND(nandptr); /* wait till ready */
  1083. }while((ret_val & 0x40) != 0x40);
  1084. }
  1085. #endif
  1086. if (READ_NAND(nandptr) & 1) {
  1087. puts ("Error programming oob data\n");
  1088. /* There was an error */
  1089. NAND_DISABLE_CE(nand); /* set pin high */
  1090. *retlen = 0;
  1091. return -1;
  1092. }
  1093. NanD_Command(nand, NAND_CMD_SEQIN);
  1094. NanD_Address(nand, ADDR_COLUMN_PAGE, ofs & (~0x1ff));
  1095. }
  1096. for (i = len256; i < len; i++)
  1097. WRITE_NAND(buf[i], nandptr);
  1098. NanD_Command(nand, NAND_CMD_PAGEPROG);
  1099. NanD_Command(nand, NAND_CMD_STATUS);
  1100. #ifdef NAND_NO_RB
  1101. { u_char ret_val;
  1102. do{
  1103. ret_val = READ_NAND(nandptr); /* wait till ready */
  1104. } while((ret_val & 0x40) != 0x40);
  1105. }
  1106. #endif
  1107. if (READ_NAND(nandptr) & 1) {
  1108. puts ("Error programming oob data\n");
  1109. /* There was an error */
  1110. NAND_DISABLE_CE(nand); /* set pin high */
  1111. *retlen = 0;
  1112. return -1;
  1113. }
  1114. NAND_DISABLE_CE(nand); /* set pin high */
  1115. *retlen = len;
  1116. return 0;
  1117. }
  1118. int nand_erase(struct nand_chip* nand, size_t ofs, size_t len, int clean)
  1119. {
  1120. /* This is defined as a structure so it will work on any system
  1121. * using native endian jffs2 (the default).
  1122. */
  1123. static struct jffs2_unknown_node clean_marker = {
  1124. JFFS2_MAGIC_BITMASK,
  1125. JFFS2_NODETYPE_CLEANMARKER,
  1126. 8 /* 8 bytes in this node */
  1127. };
  1128. unsigned long nandptr;
  1129. struct Nand *mychip;
  1130. int ret = 0;
  1131. if (ofs & (nand->erasesize-1) || len & (nand->erasesize-1)) {
  1132. printf ("Offset and size must be sector aligned, erasesize = %d\n",
  1133. (int) nand->erasesize);
  1134. return -1;
  1135. }
  1136. nandptr = nand->IO_ADDR;
  1137. /* Select the NAND device */
  1138. #ifdef CONFIG_OMAP1510
  1139. archflashwp(0,0);
  1140. #endif
  1141. NAND_ENABLE_CE(nand); /* set pin low */
  1142. /* Check the WP bit */
  1143. NanD_Command(nand, NAND_CMD_STATUS);
  1144. if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
  1145. printf ("nand_write_ecc: Device is write protected!!!\n");
  1146. ret = -1;
  1147. goto out;
  1148. }
  1149. /* Check the WP bit */
  1150. NanD_Command(nand, NAND_CMD_STATUS);
  1151. if (!(READ_NAND(nand->IO_ADDR) & 0x80)) {
  1152. printf ("%s: Device is write protected!!!\n", __FUNCTION__);
  1153. ret = -1;
  1154. goto out;
  1155. }
  1156. /* FIXME: Do nand in the background. Use timers or schedule_task() */
  1157. while(len) {
  1158. /*mychip = &nand->chips[shr(ofs, nand->chipshift)];*/
  1159. mychip = &nand->chips[ofs >> nand->chipshift];
  1160. /* always check for bad block first, genuine bad blocks
  1161. * should _never_ be erased.
  1162. */
  1163. if (ALLOW_ERASE_BAD_DEBUG || !check_block(nand, ofs)) {
  1164. /* Select the NAND device */
  1165. NAND_ENABLE_CE(nand); /* set pin low */
  1166. NanD_Command(nand, NAND_CMD_ERASE1);
  1167. NanD_Address(nand, ADDR_PAGE, ofs);
  1168. NanD_Command(nand, NAND_CMD_ERASE2);
  1169. NanD_Command(nand, NAND_CMD_STATUS);
  1170. #ifdef NAND_NO_RB
  1171. { u_char ret_val;
  1172. do{
  1173. ret_val = READ_NAND(nandptr); /* wait till ready */
  1174. } while((ret_val & 0x40) != 0x40);
  1175. }
  1176. #endif
  1177. if (READ_NAND(nandptr) & 1) {
  1178. printf ("%s: Error erasing at 0x%lx\n",
  1179. __FUNCTION__, (long)ofs);
  1180. /* There was an error */
  1181. ret = -1;
  1182. goto out;
  1183. }
  1184. if (clean) {
  1185. int n; /* return value not used */
  1186. int p, l;
  1187. /* clean marker position and size depend
  1188. * on the page size, since 256 byte pages
  1189. * only have 8 bytes of oob data
  1190. */
  1191. if (nand->page256) {
  1192. p = NAND_JFFS2_OOB8_FSDAPOS;
  1193. l = NAND_JFFS2_OOB8_FSDALEN;
  1194. }
  1195. else {
  1196. p = NAND_JFFS2_OOB16_FSDAPOS;
  1197. l = NAND_JFFS2_OOB16_FSDALEN;
  1198. }
  1199. ret = nand_write_oob(nand, ofs + p, l, &n,
  1200. (u_char *)&clean_marker);
  1201. /* quit here if write failed */
  1202. if (ret)
  1203. goto out;
  1204. }
  1205. }
  1206. ofs += nand->erasesize;
  1207. len -= nand->erasesize;
  1208. }
  1209. out:
  1210. /* De-select the NAND device */
  1211. NAND_DISABLE_CE(nand); /* set pin high */
  1212. #ifdef CONFIG_OMAP1510
  1213. archflashwp(0,1);
  1214. #endif
  1215. return ret;
  1216. }
  1217. static inline int nandcheck(unsigned long potential, unsigned long physadr)
  1218. {
  1219. return 0;
  1220. }
  1221. unsigned long nand_probe(unsigned long physadr)
  1222. {
  1223. struct nand_chip *nand = NULL;
  1224. int i = 0, ChipID = 1;
  1225. #ifdef CONFIG_MTD_NAND_ECC_JFFS2
  1226. oob_config.ecc_pos[0] = NAND_JFFS2_OOB_ECCPOS0;
  1227. oob_config.ecc_pos[1] = NAND_JFFS2_OOB_ECCPOS1;
  1228. oob_config.ecc_pos[2] = NAND_JFFS2_OOB_ECCPOS2;
  1229. oob_config.ecc_pos[3] = NAND_JFFS2_OOB_ECCPOS3;
  1230. oob_config.ecc_pos[4] = NAND_JFFS2_OOB_ECCPOS4;
  1231. oob_config.ecc_pos[5] = NAND_JFFS2_OOB_ECCPOS5;
  1232. oob_config.eccvalid_pos = 4;
  1233. #else
  1234. oob_config.ecc_pos[0] = NAND_NOOB_ECCPOS0;
  1235. oob_config.ecc_pos[1] = NAND_NOOB_ECCPOS1;
  1236. oob_config.ecc_pos[2] = NAND_NOOB_ECCPOS2;
  1237. oob_config.ecc_pos[3] = NAND_NOOB_ECCPOS3;
  1238. oob_config.ecc_pos[4] = NAND_NOOB_ECCPOS4;
  1239. oob_config.ecc_pos[5] = NAND_NOOB_ECCPOS5;
  1240. oob_config.eccvalid_pos = NAND_NOOB_ECCVPOS;
  1241. #endif
  1242. oob_config.badblock_pos = 5;
  1243. for (i=0; i<CFG_MAX_NAND_DEVICE; i++) {
  1244. if (nand_dev_desc[i].ChipID == NAND_ChipID_UNKNOWN) {
  1245. nand = &nand_dev_desc[i];
  1246. break;
  1247. }
  1248. }
  1249. if (!nand)
  1250. return (0);
  1251. memset((char *)nand, 0, sizeof(struct nand_chip));
  1252. nand->IO_ADDR = physadr;
  1253. nand->cache_page = -1; /* init the cache page */
  1254. NanD_ScanChips(nand);
  1255. if (nand->totlen == 0) {
  1256. /* no chips found, clean up and quit */
  1257. memset((char *)nand, 0, sizeof(struct nand_chip));
  1258. nand->ChipID = NAND_ChipID_UNKNOWN;
  1259. return (0);
  1260. }
  1261. nand->ChipID = ChipID;
  1262. if (curr_device == -1)
  1263. curr_device = i;
  1264. nand->data_buf = malloc (nand->oobblock + nand->oobsize);
  1265. if (!nand->data_buf) {
  1266. puts ("Cannot allocate memory for data structures.\n");
  1267. return (0);
  1268. }
  1269. return (nand->totlen);
  1270. }
  1271. #ifdef CONFIG_MTD_NAND_ECC
  1272. /*
  1273. * Pre-calculated 256-way 1 byte column parity
  1274. */
  1275. static const u_char nand_ecc_precalc_table[] = {
  1276. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00,
  1277. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  1278. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  1279. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  1280. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  1281. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  1282. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  1283. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  1284. 0x6a, 0x3f, 0x3c, 0x69, 0x33, 0x66, 0x65, 0x30, 0x30, 0x65, 0x66, 0x33, 0x69, 0x3c, 0x3f, 0x6a,
  1285. 0x0f, 0x5a, 0x59, 0x0c, 0x56, 0x03, 0x00, 0x55, 0x55, 0x00, 0x03, 0x56, 0x0c, 0x59, 0x5a, 0x0f,
  1286. 0x0c, 0x59, 0x5a, 0x0f, 0x55, 0x00, 0x03, 0x56, 0x56, 0x03, 0x00, 0x55, 0x0f, 0x5a, 0x59, 0x0c,
  1287. 0x69, 0x3c, 0x3f, 0x6a, 0x30, 0x65, 0x66, 0x33, 0x33, 0x66, 0x65, 0x30, 0x6a, 0x3f, 0x3c, 0x69,
  1288. 0x03, 0x56, 0x55, 0x00, 0x5a, 0x0f, 0x0c, 0x59, 0x59, 0x0c, 0x0f, 0x5a, 0x00, 0x55, 0x56, 0x03,
  1289. 0x66, 0x33, 0x30, 0x65, 0x3f, 0x6a, 0x69, 0x3c, 0x3c, 0x69, 0x6a, 0x3f, 0x65, 0x30, 0x33, 0x66,
  1290. 0x65, 0x30, 0x33, 0x66, 0x3c, 0x69, 0x6a, 0x3f, 0x3f, 0x6a, 0x69, 0x3c, 0x66, 0x33, 0x30, 0x65,
  1291. 0x00, 0x55, 0x56, 0x03, 0x59, 0x0c, 0x0f, 0x5a, 0x5a, 0x0f, 0x0c, 0x59, 0x03, 0x56, 0x55, 0x00
  1292. };
  1293. /*
  1294. * Creates non-inverted ECC code from line parity
  1295. */
  1296. static void nand_trans_result(u_char reg2, u_char reg3,
  1297. u_char *ecc_code)
  1298. {
  1299. u_char a, b, i, tmp1, tmp2;
  1300. /* Initialize variables */
  1301. a = b = 0x80;
  1302. tmp1 = tmp2 = 0;
  1303. /* Calculate first ECC byte */
  1304. for (i = 0; i < 4; i++) {
  1305. if (reg3 & a) /* LP15,13,11,9 --> ecc_code[0] */
  1306. tmp1 |= b;
  1307. b >>= 1;
  1308. if (reg2 & a) /* LP14,12,10,8 --> ecc_code[0] */
  1309. tmp1 |= b;
  1310. b >>= 1;
  1311. a >>= 1;
  1312. }
  1313. /* Calculate second ECC byte */
  1314. b = 0x80;
  1315. for (i = 0; i < 4; i++) {
  1316. if (reg3 & a) /* LP7,5,3,1 --> ecc_code[1] */
  1317. tmp2 |= b;
  1318. b >>= 1;
  1319. if (reg2 & a) /* LP6,4,2,0 --> ecc_code[1] */
  1320. tmp2 |= b;
  1321. b >>= 1;
  1322. a >>= 1;
  1323. }
  1324. /* Store two of the ECC bytes */
  1325. ecc_code[0] = tmp1;
  1326. ecc_code[1] = tmp2;
  1327. }
  1328. /*
  1329. * Calculate 3 byte ECC code for 256 byte block
  1330. */
  1331. static void nand_calculate_ecc (const u_char *dat, u_char *ecc_code)
  1332. {
  1333. u_char idx, reg1, reg3;
  1334. int j;
  1335. /* Initialize variables */
  1336. reg1 = reg3 = 0;
  1337. ecc_code[0] = ecc_code[1] = ecc_code[2] = 0;
  1338. /* Build up column parity */
  1339. for(j = 0; j < 256; j++) {
  1340. /* Get CP0 - CP5 from table */
  1341. idx = nand_ecc_precalc_table[dat[j]];
  1342. reg1 ^= idx;
  1343. /* All bit XOR = 1 ? */
  1344. if (idx & 0x40) {
  1345. reg3 ^= (u_char) j;
  1346. }
  1347. }
  1348. /* Create non-inverted ECC code from line parity */
  1349. nand_trans_result((reg1 & 0x40) ? ~reg3 : reg3, reg3, ecc_code);
  1350. /* Calculate final ECC code */
  1351. ecc_code[0] = ~ecc_code[0];
  1352. ecc_code[1] = ~ecc_code[1];
  1353. ecc_code[2] = ((~reg1) << 2) | 0x03;
  1354. }
  1355. /*
  1356. * Detect and correct a 1 bit error for 256 byte block
  1357. */
  1358. static int nand_correct_data (u_char *dat, u_char *read_ecc, u_char *calc_ecc)
  1359. {
  1360. u_char a, b, c, d1, d2, d3, add, bit, i;
  1361. /* Do error detection */
  1362. d1 = calc_ecc[0] ^ read_ecc[0];
  1363. d2 = calc_ecc[1] ^ read_ecc[1];
  1364. d3 = calc_ecc[2] ^ read_ecc[2];
  1365. if ((d1 | d2 | d3) == 0) {
  1366. /* No errors */
  1367. return 0;
  1368. }
  1369. else {
  1370. a = (d1 ^ (d1 >> 1)) & 0x55;
  1371. b = (d2 ^ (d2 >> 1)) & 0x55;
  1372. c = (d3 ^ (d3 >> 1)) & 0x54;
  1373. /* Found and will correct single bit error in the data */
  1374. if ((a == 0x55) && (b == 0x55) && (c == 0x54)) {
  1375. c = 0x80;
  1376. add = 0;
  1377. a = 0x80;
  1378. for (i=0; i<4; i++) {
  1379. if (d1 & c)
  1380. add |= a;
  1381. c >>= 2;
  1382. a >>= 1;
  1383. }
  1384. c = 0x80;
  1385. for (i=0; i<4; i++) {
  1386. if (d2 & c)
  1387. add |= a;
  1388. c >>= 2;
  1389. a >>= 1;
  1390. }
  1391. bit = 0;
  1392. b = 0x04;
  1393. c = 0x80;
  1394. for (i=0; i<3; i++) {
  1395. if (d3 & c)
  1396. bit |= b;
  1397. c >>= 2;
  1398. b >>= 1;
  1399. }
  1400. b = 0x01;
  1401. a = dat[add];
  1402. a ^= (b << bit);
  1403. dat[add] = a;
  1404. return 1;
  1405. }
  1406. else {
  1407. i = 0;
  1408. while (d1) {
  1409. if (d1 & 0x01)
  1410. ++i;
  1411. d1 >>= 1;
  1412. }
  1413. while (d2) {
  1414. if (d2 & 0x01)
  1415. ++i;
  1416. d2 >>= 1;
  1417. }
  1418. while (d3) {
  1419. if (d3 & 0x01)
  1420. ++i;
  1421. d3 >>= 1;
  1422. }
  1423. if (i == 1) {
  1424. /* ECC Code Error Correction */
  1425. read_ecc[0] = calc_ecc[0];
  1426. read_ecc[1] = calc_ecc[1];
  1427. read_ecc[2] = calc_ecc[2];
  1428. return 2;
  1429. }
  1430. else {
  1431. /* Uncorrectable Error */
  1432. return -1;
  1433. }
  1434. }
  1435. }
  1436. /* Should never happen */
  1437. return -1;
  1438. }
  1439. #endif
  1440. #ifdef CONFIG_JFFS2_NAND
  1441. int read_jffs2_nand(size_t start, size_t len,
  1442. size_t * retlen, u_char * buf, int nanddev)
  1443. {
  1444. return nand_rw(nand_dev_desc + nanddev, NANDRW_READ | NANDRW_JFFS2,
  1445. start, len, retlen, buf);
  1446. }
  1447. #endif /* CONFIG_JFFS2_NAND */
  1448. #endif /* (CONFIG_COMMANDS & CFG_CMD_NAND) */