davinci_nand.c 15 KB

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