cmd_nand.c 41 KB

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