nand_legacy.c 41 KB

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