cmd_nand.c 42 KB

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