cmd_nand.c 44 KB

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