davinci_nand.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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. extern struct nand_chip nand_dev_desc[CONFIG_SYS_MAX_NAND_DEVICE];
  48. static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  49. {
  50. struct nand_chip *this = mtd->priv;
  51. u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
  52. IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
  53. if (ctrl & NAND_CTRL_CHANGE) {
  54. if ( ctrl & NAND_CLE )
  55. IO_ADDR_W |= MASK_CLE;
  56. if ( ctrl & NAND_ALE )
  57. IO_ADDR_W |= MASK_ALE;
  58. this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
  59. }
  60. if (cmd != NAND_CMD_NONE)
  61. writeb(cmd, this->IO_ADDR_W);
  62. }
  63. /* Set WP on deselect, write enable on select */
  64. static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
  65. {
  66. #define GPIO_SET_DATA01 0x01c67018
  67. #define GPIO_CLR_DATA01 0x01c6701c
  68. #define GPIO_NAND_WP (1 << 4)
  69. #ifdef SONATA_BOARD_GPIOWP
  70. if (chip < 0) {
  71. REG(GPIO_CLR_DATA01) |= GPIO_NAND_WP;
  72. } else {
  73. REG(GPIO_SET_DATA01) |= GPIO_NAND_WP;
  74. }
  75. #endif
  76. }
  77. #ifdef CONFIG_SYS_NAND_HW_ECC
  78. #ifdef CONFIG_SYS_DAVINCI_BROKEN_ECC
  79. /* Linux-compatible ECC uses MTD defaults. */
  80. /* These layouts are not compatible with Linux or RBL/UBL. */
  81. #ifdef CONFIG_SYS_NAND_LARGEPAGE
  82. static struct nand_ecclayout davinci_nand_ecclayout = {
  83. .eccbytes = 12,
  84. .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
  85. .oobfree = {
  86. {.offset = 2, .length = 6},
  87. {.offset = 12, .length = 12},
  88. {.offset = 28, .length = 12},
  89. {.offset = 44, .length = 12},
  90. {.offset = 60, .length = 4}
  91. }
  92. };
  93. #elif defined(CONFIG_SYS_NAND_SMALLPAGE)
  94. static struct nand_ecclayout davinci_nand_ecclayout = {
  95. .eccbytes = 3,
  96. .eccpos = {0, 1, 2},
  97. .oobfree = {
  98. {.offset = 6, .length = 2},
  99. {.offset = 8, .length = 8}
  100. }
  101. };
  102. #else
  103. #error "Either CONFIG_SYS_NAND_LARGEPAGE or CONFIG_SYS_NAND_SMALLPAGE must be defined!"
  104. #endif
  105. #endif /* CONFIG_SYS_DAVINCI_BROKEN_ECC */
  106. static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
  107. {
  108. emifregs emif_addr;
  109. int dummy;
  110. emif_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  111. dummy = emif_addr->NANDF1ECC;
  112. dummy = emif_addr->NANDF2ECC;
  113. dummy = emif_addr->NANDF3ECC;
  114. dummy = emif_addr->NANDF4ECC;
  115. emif_addr->NANDFCR |= (1 << 8);
  116. }
  117. static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
  118. {
  119. u_int32_t ecc = 0;
  120. emifregs emif_base_addr;
  121. emif_base_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  122. if (region == 1)
  123. ecc = emif_base_addr->NANDF1ECC;
  124. else if (region == 2)
  125. ecc = emif_base_addr->NANDF2ECC;
  126. else if (region == 3)
  127. ecc = emif_base_addr->NANDF3ECC;
  128. else if (region == 4)
  129. ecc = emif_base_addr->NANDF4ECC;
  130. return(ecc);
  131. }
  132. static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  133. {
  134. u_int32_t tmp;
  135. #ifdef CONFIG_SYS_DAVINCI_BROKEN_ECC
  136. /*
  137. * This is not how you should read ECCs on large page Davinci devices.
  138. * The region parameter gets you ECCs for flash chips on different chip
  139. * selects, not the 4x512 byte pages in a 2048 byte page.
  140. *
  141. * Preserved for backwards compatibility though.
  142. */
  143. int region, n;
  144. struct nand_chip *this = mtd->priv;
  145. n = (this->ecc.size/512);
  146. region = 1;
  147. while (n--) {
  148. tmp = nand_davinci_readecc(mtd, region);
  149. *ecc_code++ = tmp;
  150. *ecc_code++ = tmp >> 16;
  151. *ecc_code++ = ((tmp >> 8) & 0x0f) | ((tmp >> 20) & 0xf0);
  152. region++;
  153. }
  154. #else
  155. const int region = 1;
  156. tmp = nand_davinci_readecc(mtd, region);
  157. /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
  158. * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
  159. tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
  160. /* Invert so that erased block ECC is correct */
  161. tmp = ~tmp;
  162. *ecc_code++ = tmp;
  163. *ecc_code++ = tmp >> 8;
  164. *ecc_code++ = tmp >> 16;
  165. #endif /* CONFIG_SYS_DAVINCI_BROKEN_ECC */
  166. return(0);
  167. }
  168. #ifdef CONFIG_SYS_DAVINCI_BROKEN_ECC
  169. static void nand_davinci_gen_true_ecc(u_int8_t *ecc_buf)
  170. {
  171. u_int32_t tmp = ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xf0) << 20) | ((ecc_buf[2] & 0x0f) << 8);
  172. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) | P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  173. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) | P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  174. ecc_buf[2] = ~( P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) | P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  175. }
  176. static int nand_davinci_compare_ecc(u_int8_t *ecc_nand, u_int8_t *ecc_calc, u_int8_t *page_data)
  177. {
  178. u_int32_t i;
  179. u_int8_t tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  180. u_int8_t comp0_bit[8], comp1_bit[8], comp2_bit[8];
  181. u_int8_t ecc_bit[24];
  182. u_int8_t ecc_sum = 0;
  183. u_int8_t find_bit = 0;
  184. u_int32_t find_byte = 0;
  185. int is_ecc_ff;
  186. is_ecc_ff = ((*ecc_nand == 0xff) && (*(ecc_nand + 1) == 0xff) && (*(ecc_nand + 2) == 0xff));
  187. nand_davinci_gen_true_ecc(ecc_nand);
  188. nand_davinci_gen_true_ecc(ecc_calc);
  189. for (i = 0; i <= 2; i++) {
  190. *(ecc_nand + i) = ~(*(ecc_nand + i));
  191. *(ecc_calc + i) = ~(*(ecc_calc + i));
  192. }
  193. for (i = 0; i < 8; i++) {
  194. tmp0_bit[i] = *ecc_nand % 2;
  195. *ecc_nand = *ecc_nand / 2;
  196. }
  197. for (i = 0; i < 8; i++) {
  198. tmp1_bit[i] = *(ecc_nand + 1) % 2;
  199. *(ecc_nand + 1) = *(ecc_nand + 1) / 2;
  200. }
  201. for (i = 0; i < 8; i++) {
  202. tmp2_bit[i] = *(ecc_nand + 2) % 2;
  203. *(ecc_nand + 2) = *(ecc_nand + 2) / 2;
  204. }
  205. for (i = 0; i < 8; i++) {
  206. comp0_bit[i] = *ecc_calc % 2;
  207. *ecc_calc = *ecc_calc / 2;
  208. }
  209. for (i = 0; i < 8; i++) {
  210. comp1_bit[i] = *(ecc_calc + 1) % 2;
  211. *(ecc_calc + 1) = *(ecc_calc + 1) / 2;
  212. }
  213. for (i = 0; i < 8; i++) {
  214. comp2_bit[i] = *(ecc_calc + 2) % 2;
  215. *(ecc_calc + 2) = *(ecc_calc + 2) / 2;
  216. }
  217. for (i = 0; i< 6; i++)
  218. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  219. for (i = 0; i < 8; i++)
  220. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  221. for (i = 0; i < 8; i++)
  222. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  223. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  224. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  225. for (i = 0; i < 24; i++)
  226. ecc_sum += ecc_bit[i];
  227. switch (ecc_sum) {
  228. case 0:
  229. /* Not reached because this function is not called if
  230. ECC values are equal */
  231. return 0;
  232. case 1:
  233. /* Uncorrectable error */
  234. MTDDEBUG (MTD_DEBUG_LEVEL0,
  235. "ECC UNCORRECTED_ERROR 1\n");
  236. return(-1);
  237. case 12:
  238. /* Correctable error */
  239. find_byte = (ecc_bit[23] << 8) +
  240. (ecc_bit[21] << 7) +
  241. (ecc_bit[19] << 6) +
  242. (ecc_bit[17] << 5) +
  243. (ecc_bit[15] << 4) +
  244. (ecc_bit[13] << 3) +
  245. (ecc_bit[11] << 2) +
  246. (ecc_bit[9] << 1) +
  247. ecc_bit[7];
  248. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  249. MTDDEBUG (MTD_DEBUG_LEVEL0, "Correcting single bit ECC "
  250. "error at offset: %d, bit: %d\n",
  251. find_byte, find_bit);
  252. page_data[find_byte] ^= (1 << find_bit);
  253. return(0);
  254. default:
  255. if (is_ecc_ff) {
  256. if (ecc_calc[0] == 0 && ecc_calc[1] == 0 && ecc_calc[2] == 0)
  257. return(0);
  258. }
  259. MTDDEBUG (MTD_DEBUG_LEVEL0,
  260. "UNCORRECTED_ERROR default\n");
  261. return(-1);
  262. }
  263. }
  264. #endif /* CONFIG_SYS_DAVINCI_BROKEN_ECC */
  265. static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
  266. {
  267. struct nand_chip *this = mtd->priv;
  268. #ifdef CONFIG_SYS_DAVINCI_BROKEN_ECC
  269. int block_count = 0, i, rc;
  270. block_count = (this->ecc.size/512);
  271. for (i = 0; i < block_count; i++) {
  272. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  273. rc = nand_davinci_compare_ecc(read_ecc, calc_ecc, dat);
  274. if (rc < 0) {
  275. return(rc);
  276. }
  277. }
  278. read_ecc += 3;
  279. calc_ecc += 3;
  280. dat += 512;
  281. }
  282. #else
  283. u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
  284. (read_ecc[2] << 16);
  285. u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
  286. (calc_ecc[2] << 16);
  287. u_int32_t diff = ecc_calc ^ ecc_nand;
  288. if (diff) {
  289. if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
  290. /* Correctable error */
  291. if ((diff >> (12 + 3)) < this->ecc.size) {
  292. uint8_t find_bit = 1 << ((diff >> 12) & 7);
  293. uint32_t find_byte = diff >> (12 + 3);
  294. dat[find_byte] ^= find_bit;
  295. MTDDEBUG(MTD_DEBUG_LEVEL0, "Correcting single "
  296. "bit ECC error at offset: %d, bit: "
  297. "%d\n", find_byte, find_bit);
  298. return 1;
  299. } else {
  300. return -1;
  301. }
  302. } else if (!(diff & (diff - 1))) {
  303. /* Single bit ECC error in the ECC itself,
  304. nothing to fix */
  305. MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
  306. "ECC.\n");
  307. return 1;
  308. } else {
  309. /* Uncorrectable error */
  310. MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  311. return -1;
  312. }
  313. }
  314. #endif /* CONFIG_SYS_DAVINCI_BROKEN_ECC */
  315. return(0);
  316. }
  317. #endif /* CONFIG_SYS_NAND_HW_ECC */
  318. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  319. {
  320. emifregs emif_addr;
  321. emif_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  322. return(emif_addr->NANDFSR & 0x1);
  323. }
  324. static int nand_davinci_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  325. {
  326. while(!nand_davinci_dev_ready(mtd)) {;}
  327. *NAND_CE0CLE = NAND_STATUS;
  328. return(*NAND_CE0DATA);
  329. }
  330. static void nand_flash_init(void)
  331. {
  332. u_int32_t acfg1 = 0x3ffffffc;
  333. emifregs emif_regs;
  334. /*------------------------------------------------------------------*
  335. * NAND FLASH CHIP TIMEOUT @ 459 MHz *
  336. * *
  337. * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
  338. * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
  339. * *
  340. *------------------------------------------------------------------*/
  341. acfg1 = 0
  342. | (0 << 31 ) /* selectStrobe */
  343. | (0 << 30 ) /* extWait */
  344. | (1 << 26 ) /* writeSetup 10 ns */
  345. | (3 << 20 ) /* writeStrobe 40 ns */
  346. | (1 << 17 ) /* writeHold 10 ns */
  347. | (1 << 13 ) /* readSetup 10 ns */
  348. | (5 << 7 ) /* readStrobe 60 ns */
  349. | (1 << 4 ) /* readHold 10 ns */
  350. | (3 << 2 ) /* turnAround ?? ns */
  351. | (0 << 0 ) /* asyncSize 8-bit bus */
  352. ;
  353. emif_regs = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  354. emif_regs->AB1CR = acfg1; /* CS2 */
  355. emif_regs->NANDFCR = 0x00000101; /* NAND flash on CS2 */
  356. }
  357. int board_nand_init(struct nand_chip *nand)
  358. {
  359. nand->IO_ADDR_R = (void __iomem *)NAND_CE0DATA;
  360. nand->IO_ADDR_W = (void __iomem *)NAND_CE0DATA;
  361. nand->chip_delay = 0;
  362. nand->select_chip = nand_davinci_select_chip;
  363. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  364. nand->options = NAND_USE_FLASH_BBT;
  365. #endif
  366. #ifdef CONFIG_SYS_NAND_HW_ECC
  367. nand->ecc.mode = NAND_ECC_HW;
  368. #ifdef CONFIG_SYS_DAVINCI_BROKEN_ECC
  369. nand->ecc.layout = &davinci_nand_ecclayout;
  370. #ifdef CONFIG_SYS_NAND_LARGEPAGE
  371. nand->ecc.size = 2048;
  372. nand->ecc.bytes = 12;
  373. #elif defined(CONFIG_SYS_NAND_SMALLPAGE)
  374. nand->ecc.size = 512;
  375. nand->ecc.bytes = 3;
  376. #else
  377. #error "Either CONFIG_SYS_NAND_LARGEPAGE or CONFIG_SYS_NAND_SMALLPAGE must be defined!"
  378. #endif
  379. #else
  380. nand->ecc.size = 512;
  381. nand->ecc.bytes = 3;
  382. #endif /* CONFIG_SYS_DAVINCI_BROKEN_ECC */
  383. nand->ecc.calculate = nand_davinci_calculate_ecc;
  384. nand->ecc.correct = nand_davinci_correct_data;
  385. nand->ecc.hwctl = nand_davinci_enable_hwecc;
  386. #else
  387. nand->ecc.mode = NAND_ECC_SOFT;
  388. #endif /* CONFIG_SYS_NAND_HW_ECC */
  389. /* Set address of hardware control function */
  390. nand->cmd_ctrl = nand_davinci_hwcontrol;
  391. nand->dev_ready = nand_davinci_dev_ready;
  392. nand->waitfunc = nand_davinci_waitfunc;
  393. nand_flash_init();
  394. return(0);
  395. }