davinci_nand.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*
  2. * NAND driver for TI DaVinci based boards.
  3. *
  4. * Copyright (C) 2007 Sergey Kubushyn <ksi@koi8.net>
  5. *
  6. * Based on Linux DaVinci NAND driver by TI. Original copyright follows:
  7. */
  8. /*
  9. *
  10. * linux/drivers/mtd/nand/nand_davinci.c
  11. *
  12. * NAND Flash Driver
  13. *
  14. * Copyright (C) 2006 Texas Instruments.
  15. *
  16. * ----------------------------------------------------------------------------
  17. *
  18. * This program is free software; you can redistribute it and/or modify
  19. * it under the terms of the GNU General Public License as published by
  20. * the Free Software Foundation; either version 2 of the License, or
  21. * (at your option) any later version.
  22. *
  23. * This program is distributed in the hope that it will be useful,
  24. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  26. * GNU General Public License for more details.
  27. *
  28. * You should have received a copy of the GNU General Public License
  29. * along with this program; if not, write to the Free Software
  30. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  31. * ----------------------------------------------------------------------------
  32. *
  33. * Overview:
  34. * This is a device driver for the NAND flash device found on the
  35. * DaVinci board which utilizes the Samsung k9k2g08 part.
  36. *
  37. Modifications:
  38. ver. 1.0: Feb 2005, Vinod/Sudhakar
  39. -
  40. *
  41. */
  42. #include <common.h>
  43. #include <asm/io.h>
  44. #include <nand.h>
  45. #include <asm/arch/nand_defs.h>
  46. #include <asm/arch/emif_defs.h>
  47. /* Definitions for 4-bit hardware ECC */
  48. #define NAND_TIMEOUT 10240
  49. #define NAND_ECC_BUSY 0xC
  50. #define NAND_4BITECC_MASK 0x03FF03FF
  51. #define EMIF_NANDFSR_ECC_STATE_MASK 0x00000F00
  52. #define ECC_STATE_NO_ERR 0x0
  53. #define ECC_STATE_TOO_MANY_ERRS 0x1
  54. #define ECC_STATE_ERR_CORR_COMP_P 0x2
  55. #define ECC_STATE_ERR_CORR_COMP_N 0x3
  56. static emif_registers *const emif_regs = (void *) DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  57. static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  58. {
  59. struct nand_chip *this = mtd->priv;
  60. u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
  61. IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
  62. if (ctrl & NAND_CTRL_CHANGE) {
  63. if ( ctrl & NAND_CLE )
  64. IO_ADDR_W |= MASK_CLE;
  65. if ( ctrl & NAND_ALE )
  66. IO_ADDR_W |= MASK_ALE;
  67. this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
  68. }
  69. if (cmd != NAND_CMD_NONE)
  70. writeb(cmd, this->IO_ADDR_W);
  71. }
  72. #ifdef CONFIG_SYS_NAND_HW_ECC
  73. static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
  74. {
  75. u_int32_t val;
  76. (void)readl(&(emif_regs->NANDFECC[CONFIG_SYS_NAND_CS - 2]));
  77. val = readl(&emif_regs->NANDFCR);
  78. val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
  79. val |= DAVINCI_NANDFCR_1BIT_ECC_START(CONFIG_SYS_NAND_CS);
  80. writel(val, &emif_regs->NANDFCR);
  81. }
  82. static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
  83. {
  84. u_int32_t ecc = 0;
  85. ecc = readl(&(emif_regs->NANDFECC[region - 1]));
  86. return(ecc);
  87. }
  88. static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  89. {
  90. u_int32_t tmp;
  91. const int region = 1;
  92. tmp = nand_davinci_readecc(mtd, region);
  93. /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
  94. * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
  95. tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
  96. /* Invert so that erased block ECC is correct */
  97. tmp = ~tmp;
  98. *ecc_code++ = tmp;
  99. *ecc_code++ = tmp >> 8;
  100. *ecc_code++ = tmp >> 16;
  101. /* NOTE: the above code matches mainline Linux:
  102. * .PQR.stu ==> ~PQRstu
  103. *
  104. * MontaVista/TI kernels encode those bytes differently, use
  105. * complicated (and allegedly sometimes-wrong) correction code,
  106. * and usually shipped with U-Boot that uses software ECC:
  107. * .PQR.stu ==> PsQRtu
  108. *
  109. * If you need MV/TI compatible NAND I/O in U-Boot, it should
  110. * be possible to (a) change the mangling above, (b) reverse
  111. * that mangling in nand_davinci_correct_data() below.
  112. */
  113. return 0;
  114. }
  115. static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
  116. {
  117. struct nand_chip *this = mtd->priv;
  118. u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
  119. (read_ecc[2] << 16);
  120. u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
  121. (calc_ecc[2] << 16);
  122. u_int32_t diff = ecc_calc ^ ecc_nand;
  123. if (diff) {
  124. if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
  125. /* Correctable error */
  126. if ((diff >> (12 + 3)) < this->ecc.size) {
  127. uint8_t find_bit = 1 << ((diff >> 12) & 7);
  128. uint32_t find_byte = diff >> (12 + 3);
  129. dat[find_byte] ^= find_bit;
  130. MTDDEBUG(MTD_DEBUG_LEVEL0, "Correcting single "
  131. "bit ECC error at offset: %d, bit: "
  132. "%d\n", find_byte, find_bit);
  133. return 1;
  134. } else {
  135. return -1;
  136. }
  137. } else if (!(diff & (diff - 1))) {
  138. /* Single bit ECC error in the ECC itself,
  139. nothing to fix */
  140. MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
  141. "ECC.\n");
  142. return 1;
  143. } else {
  144. /* Uncorrectable error */
  145. MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  146. return -1;
  147. }
  148. }
  149. return(0);
  150. }
  151. #endif /* CONFIG_SYS_NAND_HW_ECC */
  152. #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
  153. static struct nand_ecclayout nand_davinci_4bit_layout_oobfirst = {
  154. #if defined(CONFIG_SYS_NAND_PAGE_2K)
  155. .eccbytes = 40,
  156. .eccpos = {
  157. 24, 25, 26, 27, 28,
  158. 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  159. 39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
  160. 49, 50, 51, 52, 53, 54, 55, 56, 57, 58,
  161. 59, 60, 61, 62, 63,
  162. },
  163. .oobfree = {
  164. {.offset = 2, .length = 22, },
  165. },
  166. #elif defined(CONFIG_SYS_NAND_PAGE_4K)
  167. .eccbytes = 80,
  168. .eccpos = {
  169. 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,
  170. 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
  171. 68, 69, 70, 71, 72, 73, 74, 75, 76, 77,
  172. 78, 79, 80, 81, 82, 83, 84, 85, 86, 87,
  173. 88, 89, 90, 91, 92, 93, 94, 95, 96, 97,
  174. 98, 99, 100, 101, 102, 103, 104, 105, 106, 107,
  175. 108, 109, 110, 111, 112, 113, 114, 115, 116, 117,
  176. 118, 119, 120, 121, 122, 123, 124, 125, 126, 127,
  177. },
  178. .oobfree = {
  179. {.offset = 2, .length = 46, },
  180. },
  181. #endif
  182. };
  183. static void nand_davinci_4bit_enable_hwecc(struct mtd_info *mtd, int mode)
  184. {
  185. u32 val;
  186. switch (mode) {
  187. case NAND_ECC_WRITE:
  188. case NAND_ECC_READ:
  189. /*
  190. * Start a new ECC calculation for reading or writing 512 bytes
  191. * of data.
  192. */
  193. val = readl(&emif_regs->NANDFCR);
  194. val &= ~DAVINCI_NANDFCR_4BIT_ECC_SEL_MASK;
  195. val |= DAVINCI_NANDFCR_NAND_ENABLE(CONFIG_SYS_NAND_CS);
  196. val |= DAVINCI_NANDFCR_4BIT_ECC_SEL(CONFIG_SYS_NAND_CS);
  197. val |= DAVINCI_NANDFCR_4BIT_ECC_START;
  198. writel(val, &emif_regs->NANDFCR);
  199. break;
  200. case NAND_ECC_READSYN:
  201. val = emif_regs->NAND4BITECC1;
  202. break;
  203. default:
  204. break;
  205. }
  206. }
  207. static u32 nand_davinci_4bit_readecc(struct mtd_info *mtd, unsigned int ecc[4])
  208. {
  209. ecc[0] = emif_regs->NAND4BITECC1 & NAND_4BITECC_MASK;
  210. ecc[1] = emif_regs->NAND4BITECC2 & NAND_4BITECC_MASK;
  211. ecc[2] = emif_regs->NAND4BITECC3 & NAND_4BITECC_MASK;
  212. ecc[3] = emif_regs->NAND4BITECC4 & NAND_4BITECC_MASK;
  213. return 0;
  214. }
  215. static int nand_davinci_4bit_calculate_ecc(struct mtd_info *mtd,
  216. const uint8_t *dat,
  217. uint8_t *ecc_code)
  218. {
  219. unsigned int hw_4ecc[4] = { 0, 0, 0, 0 };
  220. unsigned int const1 = 0, const2 = 0;
  221. unsigned char count1 = 0;
  222. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  223. /*Convert 10 bit ecc value to 8 bit */
  224. for (count1 = 0; count1 < 2; count1++) {
  225. const2 = count1 * 5;
  226. const1 = count1 * 2;
  227. /* Take first 8 bits from val1 (count1=0) or val5 (count1=1) */
  228. ecc_code[const2] = hw_4ecc[const1] & 0xFF;
  229. /*
  230. * Take 2 bits as LSB bits from val1 (count1=0) or val5
  231. * (count1=1) and 6 bits from val2 (count1=0) or
  232. * val5 (count1=1)
  233. */
  234. ecc_code[const2 + 1] =
  235. ((hw_4ecc[const1] >> 8) & 0x3) | ((hw_4ecc[const1] >> 14) &
  236. 0xFC);
  237. /*
  238. * Take 4 bits from val2 (count1=0) or val5 (count1=1) and
  239. * 4 bits from val3 (count1=0) or val6 (count1=1)
  240. */
  241. ecc_code[const2 + 2] =
  242. ((hw_4ecc[const1] >> 22) & 0xF) |
  243. ((hw_4ecc[const1 + 1] << 4) & 0xF0);
  244. /*
  245. * Take 6 bits from val3(count1=0) or val6 (count1=1) and
  246. * 2 bits from val4 (count1=0) or val7 (count1=1)
  247. */
  248. ecc_code[const2 + 3] =
  249. ((hw_4ecc[const1 + 1] >> 4) & 0x3F) |
  250. ((hw_4ecc[const1 + 1] >> 10) & 0xC0);
  251. /* Take 8 bits from val4 (count1=0) or val7 (count1=1) */
  252. ecc_code[const2 + 4] = (hw_4ecc[const1 + 1] >> 18) & 0xFF;
  253. }
  254. return 0;
  255. }
  256. static int nand_davinci_4bit_correct_data(struct mtd_info *mtd, uint8_t *dat,
  257. uint8_t *read_ecc, uint8_t *calc_ecc)
  258. {
  259. unsigned short ecc_10bit[8] = { 0, 0, 0, 0, 0, 0, 0, 0 };
  260. int i;
  261. unsigned int hw_4ecc[4] = { 0, 0, 0, 0 }, iserror = 0;
  262. unsigned short *pspare = NULL, *pspare1 = NULL;
  263. unsigned int numerrors, erroraddress, errorvalue;
  264. u32 val;
  265. /*
  266. * Check for an ECC where all bytes are 0xFF. If this is the case, we
  267. * will assume we are looking at an erased page and we should ignore
  268. * the ECC.
  269. */
  270. for (i = 0; i < 10; i++) {
  271. if (read_ecc[i] != 0xFF)
  272. break;
  273. }
  274. if (i == 10)
  275. return 0;
  276. /* Convert 8 bit in to 10 bit */
  277. pspare = (unsigned short *)&read_ecc[2];
  278. pspare1 = (unsigned short *)&read_ecc[0];
  279. /* Take 10 bits from 0th and 1st bytes */
  280. ecc_10bit[0] = (*pspare1) & 0x3FF;
  281. /* Take 6 bits from 1st byte and 4 bits from 2nd byte */
  282. ecc_10bit[1] = (((*pspare1) >> 10) & 0x3F)
  283. | (((pspare[0]) << 6) & 0x3C0);
  284. /* Take 4 bits form 2nd bytes and 6 bits from 3rd bytes */
  285. ecc_10bit[2] = ((pspare[0]) >> 4) & 0x3FF;
  286. /*Take 2 bits from 3rd byte and 8 bits from 4th byte */
  287. ecc_10bit[3] = (((pspare[0]) >> 14) & 0x3)
  288. | ((((pspare[1])) << 2) & 0x3FC);
  289. /* Take 8 bits from 5th byte and 2 bits from 6th byte */
  290. ecc_10bit[4] = ((pspare[1]) >> 8)
  291. | ((((pspare[2])) << 8) & 0x300);
  292. /* Take 6 bits from 6th byte and 4 bits from 7th byte */
  293. ecc_10bit[5] = (pspare[2] >> 2) & 0x3FF;
  294. /* Take 4 bits from 7th byte and 6 bits from 8th byte */
  295. ecc_10bit[6] = (((pspare[2]) >> 12) & 0xF)
  296. | ((((pspare[3])) << 4) & 0x3F0);
  297. /*Take 2 bits from 8th byte and 8 bits from 9th byte */
  298. ecc_10bit[7] = ((pspare[3]) >> 6) & 0x3FF;
  299. /*
  300. * Write the parity values in the NAND Flash 4-bit ECC Load register.
  301. * Write each parity value one at a time starting from 4bit_ecc_val8
  302. * to 4bit_ecc_val1.
  303. */
  304. for (i = 7; i >= 0; i--)
  305. emif_regs->NAND4BITECCLOAD = ecc_10bit[i];
  306. /*
  307. * Perform a dummy read to the EMIF Revision Code and Status register.
  308. * This is required to ensure time for syndrome calculation after
  309. * writing the ECC values in previous step.
  310. */
  311. val = emif_regs->NANDFSR;
  312. /*
  313. * Read the syndrome from the NAND Flash 4-Bit ECC 1-4 registers.
  314. * A syndrome value of 0 means no bit errors. If the syndrome is
  315. * non-zero then go further otherwise return.
  316. */
  317. nand_davinci_4bit_readecc(mtd, hw_4ecc);
  318. if (hw_4ecc[0] == ECC_STATE_NO_ERR && hw_4ecc[1] == ECC_STATE_NO_ERR &&
  319. hw_4ecc[2] == ECC_STATE_NO_ERR && hw_4ecc[3] == ECC_STATE_NO_ERR)
  320. return 0;
  321. /*
  322. * Clear any previous address calculation by doing a dummy read of an
  323. * error address register.
  324. */
  325. val = emif_regs->NANDERRADD1;
  326. /*
  327. * Set the addr_calc_st bit(bit no 13) in the NAND Flash Control
  328. * register to 1.
  329. */
  330. emif_regs->NANDFCR |= 1 << 13;
  331. /*
  332. * Wait for the corr_state field (bits 8 to 11)in the
  333. * NAND Flash Status register to be equal to 0x0, 0x1, 0x2, or 0x3.
  334. */
  335. i = NAND_TIMEOUT;
  336. do {
  337. val = emif_regs->NANDFSR;
  338. val &= 0xc00;
  339. i--;
  340. } while ((i > 0) && val);
  341. iserror = emif_regs->NANDFSR;
  342. iserror &= EMIF_NANDFSR_ECC_STATE_MASK;
  343. iserror = iserror >> 8;
  344. /*
  345. * ECC_STATE_TOO_MANY_ERRS (0x1) means errors cannot be
  346. * corrected (five or more errors). The number of errors
  347. * calculated (err_num field) differs from the number of errors
  348. * searched. ECC_STATE_ERR_CORR_COMP_P (0x2) means error
  349. * correction complete (errors on bit 8 or 9).
  350. * ECC_STATE_ERR_CORR_COMP_N (0x3) means error correction
  351. * complete (error exists).
  352. */
  353. if (iserror == ECC_STATE_NO_ERR) {
  354. val = emif_regs->NANDERRVAL1;
  355. return 0;
  356. } else if (iserror == ECC_STATE_TOO_MANY_ERRS) {
  357. val = emif_regs->NANDERRVAL1;
  358. return -1;
  359. }
  360. numerrors = ((emif_regs->NANDFSR >> 16) & 0x3) + 1;
  361. /* Read the error address, error value and correct */
  362. for (i = 0; i < numerrors; i++) {
  363. if (i > 1) {
  364. erroraddress =
  365. ((emif_regs->NANDERRADD2 >>
  366. (16 * (i & 1))) & 0x3FF);
  367. erroraddress = ((512 + 7) - erroraddress);
  368. errorvalue =
  369. ((emif_regs->NANDERRVAL2 >>
  370. (16 * (i & 1))) & 0xFF);
  371. } else {
  372. erroraddress =
  373. ((emif_regs->NANDERRADD1 >>
  374. (16 * (i & 1))) & 0x3FF);
  375. erroraddress = ((512 + 7) - erroraddress);
  376. errorvalue =
  377. ((emif_regs->NANDERRVAL1 >>
  378. (16 * (i & 1))) & 0xFF);
  379. }
  380. /* xor the corrupt data with error value */
  381. if (erroraddress < 512)
  382. dat[erroraddress] ^= errorvalue;
  383. }
  384. return numerrors;
  385. }
  386. #endif /* CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST */
  387. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  388. {
  389. return emif_regs->NANDFSR & 0x1;
  390. }
  391. static void nand_flash_init(void)
  392. {
  393. /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
  394. * Instead, have your board_init() set EMIF timings, based on its
  395. * knowledge of the clocks and what devices are hooked up ... and
  396. * don't even do that unless no UBL handled it.
  397. */
  398. #ifdef CONFIG_SOC_DM644X
  399. u_int32_t acfg1 = 0x3ffffffc;
  400. /*------------------------------------------------------------------*
  401. * NAND FLASH CHIP TIMEOUT @ 459 MHz *
  402. * *
  403. * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
  404. * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
  405. * *
  406. *------------------------------------------------------------------*/
  407. acfg1 = 0
  408. | (0 << 31 ) /* selectStrobe */
  409. | (0 << 30 ) /* extWait */
  410. | (1 << 26 ) /* writeSetup 10 ns */
  411. | (3 << 20 ) /* writeStrobe 40 ns */
  412. | (1 << 17 ) /* writeHold 10 ns */
  413. | (1 << 13 ) /* readSetup 10 ns */
  414. | (5 << 7 ) /* readStrobe 60 ns */
  415. | (1 << 4 ) /* readHold 10 ns */
  416. | (3 << 2 ) /* turnAround ?? ns */
  417. | (0 << 0 ) /* asyncSize 8-bit bus */
  418. ;
  419. emif_regs->AB1CR = acfg1; /* CS2 */
  420. emif_regs->NANDFCR = 0x00000101; /* NAND flash on CS2 */
  421. #endif
  422. }
  423. void davinci_nand_init(struct nand_chip *nand)
  424. {
  425. nand->chip_delay = 0;
  426. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  427. nand->options |= NAND_USE_FLASH_BBT;
  428. #endif
  429. #ifdef CONFIG_SYS_NAND_HW_ECC
  430. nand->ecc.mode = NAND_ECC_HW;
  431. nand->ecc.size = 512;
  432. nand->ecc.bytes = 3;
  433. nand->ecc.calculate = nand_davinci_calculate_ecc;
  434. nand->ecc.correct = nand_davinci_correct_data;
  435. nand->ecc.hwctl = nand_davinci_enable_hwecc;
  436. #else
  437. nand->ecc.mode = NAND_ECC_SOFT;
  438. #endif /* CONFIG_SYS_NAND_HW_ECC */
  439. #ifdef CONFIG_SYS_NAND_4BIT_HW_ECC_OOBFIRST
  440. nand->ecc.mode = NAND_ECC_HW_OOB_FIRST;
  441. nand->ecc.size = 512;
  442. nand->ecc.bytes = 10;
  443. nand->ecc.calculate = nand_davinci_4bit_calculate_ecc;
  444. nand->ecc.correct = nand_davinci_4bit_correct_data;
  445. nand->ecc.hwctl = nand_davinci_4bit_enable_hwecc;
  446. nand->ecc.layout = &nand_davinci_4bit_layout_oobfirst;
  447. #endif
  448. /* Set address of hardware control function */
  449. nand->cmd_ctrl = nand_davinci_hwcontrol;
  450. nand->dev_ready = nand_davinci_dev_ready;
  451. nand_flash_init();
  452. }
  453. int board_nand_init(struct nand_chip *chip) __attribute__((weak));
  454. int board_nand_init(struct nand_chip *chip)
  455. {
  456. davinci_nand_init(chip);
  457. return 0;
  458. }