cmd_nand.c 42 KB

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