nand_legacy.c 41 KB

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