cmd_nand.c 41 KB

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