nand_legacy.c 42 KB

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