nand_legacy.c 41 KB

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