cmd_nand.c 45 KB

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