nand_legacy.c 41 KB

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