cmd_nand.c 49 KB

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