cmd_nand.c 42 KB

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