cmd_nand.c 44 KB

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