davinci_nand.c 15 KB

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