cmd_nand.c 49 KB

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