nand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. #ifdef CFG_USE_NAND
  45. #if !defined(CONFIG_NAND_LEGACY)
  46. #include <nand.h>
  47. #include <asm/arch/nand_defs.h>
  48. #include <asm/arch/emif_defs.h>
  49. extern struct nand_chip nand_dev_desc[CFG_MAX_NAND_DEVICE];
  50. static void nand_davinci_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  51. {
  52. struct nand_chip *this = mtd->priv;
  53. u_int32_t IO_ADDR_W = (u_int32_t)this->IO_ADDR_W;
  54. IO_ADDR_W &= ~(MASK_ALE|MASK_CLE);
  55. if (ctrl & NAND_CTRL_CHANGE) {
  56. if ( ctrl & NAND_CLE )
  57. IO_ADDR_W |= MASK_CLE;
  58. if ( ctrl & NAND_ALE )
  59. IO_ADDR_W |= MASK_ALE;
  60. this->IO_ADDR_W = (void __iomem *) IO_ADDR_W;
  61. }
  62. if (cmd != NAND_CMD_NONE)
  63. writeb(cmd, this->IO_ADDR_W);
  64. }
  65. /* Set WP on deselect, write enable on select */
  66. static void nand_davinci_select_chip(struct mtd_info *mtd, int chip)
  67. {
  68. #define GPIO_SET_DATA01 0x01c67018
  69. #define GPIO_CLR_DATA01 0x01c6701c
  70. #define GPIO_NAND_WP (1 << 4)
  71. #ifdef SONATA_BOARD_GPIOWP
  72. if (chip < 0) {
  73. REG(GPIO_CLR_DATA01) |= GPIO_NAND_WP;
  74. } else {
  75. REG(GPIO_SET_DATA01) |= GPIO_NAND_WP;
  76. }
  77. #endif
  78. }
  79. #ifdef CFG_NAND_HW_ECC
  80. #ifdef CFG_NAND_LARGEPAGE
  81. static struct nand_ecclayout davinci_nand_ecclayout = {
  82. .eccbytes = 12,
  83. .eccpos = {8, 9, 10, 24, 25, 26, 40, 41, 42, 56, 57, 58},
  84. .oobfree = {
  85. {.offset = 2, .length = 6},
  86. {.offset = 12, .length = 12},
  87. {.offset = 28, .length = 12},
  88. {.offset = 44, .length = 12},
  89. {.offset = 60, .length = 4}
  90. }
  91. };
  92. #elif defined(CFG_NAND_SMALLPAGE)
  93. static struct nand_ecclayout davinci_nand_ecclayout = {
  94. .eccbytes = 3,
  95. .eccpos = {0, 1, 2},
  96. .oobfree = {
  97. {.offset = 6, .length = 2},
  98. {.offset = 8, .length = 8}
  99. }
  100. };
  101. #else
  102. #error "Either CFG_NAND_LARGEPAGE or CFG_NAND_SMALLPAGE must be defined!"
  103. #endif
  104. static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
  105. {
  106. emifregs emif_addr;
  107. int dummy;
  108. emif_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  109. dummy = emif_addr->NANDF1ECC;
  110. dummy = emif_addr->NANDF2ECC;
  111. dummy = emif_addr->NANDF3ECC;
  112. dummy = emif_addr->NANDF4ECC;
  113. emif_addr->NANDFCR |= (1 << 8);
  114. }
  115. static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
  116. {
  117. u_int32_t ecc = 0;
  118. emifregs emif_base_addr;
  119. emif_base_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  120. if (region == 1)
  121. ecc = emif_base_addr->NANDF1ECC;
  122. else if (region == 2)
  123. ecc = emif_base_addr->NANDF2ECC;
  124. else if (region == 3)
  125. ecc = emif_base_addr->NANDF3ECC;
  126. else if (region == 4)
  127. ecc = emif_base_addr->NANDF4ECC;
  128. return(ecc);
  129. }
  130. static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  131. {
  132. u_int32_t tmp;
  133. int region, n;
  134. struct nand_chip *this = mtd->priv;
  135. n = (this->ecc.size/512);
  136. region = 1;
  137. while (n--) {
  138. tmp = nand_davinci_readecc(mtd, region);
  139. *ecc_code++ = tmp;
  140. *ecc_code++ = tmp >> 16;
  141. *ecc_code++ = ((tmp >> 8) & 0x0f) | ((tmp >> 20) & 0xf0);
  142. region++;
  143. }
  144. return(0);
  145. }
  146. static void nand_davinci_gen_true_ecc(u_int8_t *ecc_buf)
  147. {
  148. u_int32_t tmp = ecc_buf[0] | (ecc_buf[1] << 16) | ((ecc_buf[2] & 0xf0) << 20) | ((ecc_buf[2] & 0x0f) << 8);
  149. ecc_buf[0] = ~(P64o(tmp) | P64e(tmp) | P32o(tmp) | P32e(tmp) | P16o(tmp) | P16e(tmp) | P8o(tmp) | P8e(tmp));
  150. ecc_buf[1] = ~(P1024o(tmp) | P1024e(tmp) | P512o(tmp) | P512e(tmp) | P256o(tmp) | P256e(tmp) | P128o(tmp) | P128e(tmp));
  151. ecc_buf[2] = ~( P4o(tmp) | P4e(tmp) | P2o(tmp) | P2e(tmp) | P1o(tmp) | P1e(tmp) | P2048o(tmp) | P2048e(tmp));
  152. }
  153. static int nand_davinci_compare_ecc(u_int8_t *ecc_nand, u_int8_t *ecc_calc, u_int8_t *page_data)
  154. {
  155. u_int32_t i;
  156. u_int8_t tmp0_bit[8], tmp1_bit[8], tmp2_bit[8];
  157. u_int8_t comp0_bit[8], comp1_bit[8], comp2_bit[8];
  158. u_int8_t ecc_bit[24];
  159. u_int8_t ecc_sum = 0;
  160. u_int8_t find_bit = 0;
  161. u_int32_t find_byte = 0;
  162. int is_ecc_ff;
  163. is_ecc_ff = ((*ecc_nand == 0xff) && (*(ecc_nand + 1) == 0xff) && (*(ecc_nand + 2) == 0xff));
  164. nand_davinci_gen_true_ecc(ecc_nand);
  165. nand_davinci_gen_true_ecc(ecc_calc);
  166. for (i = 0; i <= 2; i++) {
  167. *(ecc_nand + i) = ~(*(ecc_nand + i));
  168. *(ecc_calc + i) = ~(*(ecc_calc + i));
  169. }
  170. for (i = 0; i < 8; i++) {
  171. tmp0_bit[i] = *ecc_nand % 2;
  172. *ecc_nand = *ecc_nand / 2;
  173. }
  174. for (i = 0; i < 8; i++) {
  175. tmp1_bit[i] = *(ecc_nand + 1) % 2;
  176. *(ecc_nand + 1) = *(ecc_nand + 1) / 2;
  177. }
  178. for (i = 0; i < 8; i++) {
  179. tmp2_bit[i] = *(ecc_nand + 2) % 2;
  180. *(ecc_nand + 2) = *(ecc_nand + 2) / 2;
  181. }
  182. for (i = 0; i < 8; i++) {
  183. comp0_bit[i] = *ecc_calc % 2;
  184. *ecc_calc = *ecc_calc / 2;
  185. }
  186. for (i = 0; i < 8; i++) {
  187. comp1_bit[i] = *(ecc_calc + 1) % 2;
  188. *(ecc_calc + 1) = *(ecc_calc + 1) / 2;
  189. }
  190. for (i = 0; i < 8; i++) {
  191. comp2_bit[i] = *(ecc_calc + 2) % 2;
  192. *(ecc_calc + 2) = *(ecc_calc + 2) / 2;
  193. }
  194. for (i = 0; i< 6; i++)
  195. ecc_bit[i] = tmp2_bit[i + 2] ^ comp2_bit[i + 2];
  196. for (i = 0; i < 8; i++)
  197. ecc_bit[i + 6] = tmp0_bit[i] ^ comp0_bit[i];
  198. for (i = 0; i < 8; i++)
  199. ecc_bit[i + 14] = tmp1_bit[i] ^ comp1_bit[i];
  200. ecc_bit[22] = tmp2_bit[0] ^ comp2_bit[0];
  201. ecc_bit[23] = tmp2_bit[1] ^ comp2_bit[1];
  202. for (i = 0; i < 24; i++)
  203. ecc_sum += ecc_bit[i];
  204. switch (ecc_sum) {
  205. case 0:
  206. /* Not reached because this function is not called if
  207. ECC values are equal */
  208. return 0;
  209. case 1:
  210. /* Uncorrectable error */
  211. MTDDEBUG (MTD_DEBUG_LEVEL0,
  212. "ECC UNCORRECTED_ERROR 1\n");
  213. return(-1);
  214. case 12:
  215. /* Correctable error */
  216. find_byte = (ecc_bit[23] << 8) +
  217. (ecc_bit[21] << 7) +
  218. (ecc_bit[19] << 6) +
  219. (ecc_bit[17] << 5) +
  220. (ecc_bit[15] << 4) +
  221. (ecc_bit[13] << 3) +
  222. (ecc_bit[11] << 2) +
  223. (ecc_bit[9] << 1) +
  224. ecc_bit[7];
  225. find_bit = (ecc_bit[5] << 2) + (ecc_bit[3] << 1) + ecc_bit[1];
  226. MTDDEBUG (MTD_DEBUG_LEVEL0, "Correcting single bit ECC "
  227. "error at offset: %d, bit: %d\n",
  228. find_byte, find_bit);
  229. page_data[find_byte] ^= (1 << find_bit);
  230. return(0);
  231. default:
  232. if (is_ecc_ff) {
  233. if (ecc_calc[0] == 0 && ecc_calc[1] == 0 && ecc_calc[2] == 0)
  234. return(0);
  235. }
  236. MTDDEBUG (MTD_DEBUG_LEVEL0,
  237. "UNCORRECTED_ERROR default\n");
  238. return(-1);
  239. }
  240. }
  241. static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
  242. {
  243. struct nand_chip *this;
  244. int block_count = 0, i, rc;
  245. this = mtd->priv;
  246. block_count = (this->ecc.size/512);
  247. for (i = 0; i < block_count; i++) {
  248. if (memcmp(read_ecc, calc_ecc, 3) != 0) {
  249. rc = nand_davinci_compare_ecc(read_ecc, calc_ecc, dat);
  250. if (rc < 0) {
  251. return(rc);
  252. }
  253. }
  254. read_ecc += 3;
  255. calc_ecc += 3;
  256. dat += 512;
  257. }
  258. return(0);
  259. }
  260. #endif
  261. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  262. {
  263. emifregs emif_addr;
  264. emif_addr = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  265. return(emif_addr->NANDFSR & 0x1);
  266. }
  267. static int nand_davinci_waitfunc(struct mtd_info *mtd, struct nand_chip *this)
  268. {
  269. while(!nand_davinci_dev_ready(mtd)) {;}
  270. *NAND_CE0CLE = NAND_STATUS;
  271. return(*NAND_CE0DATA);
  272. }
  273. static void nand_flash_init(void)
  274. {
  275. u_int32_t acfg1 = 0x3ffffffc;
  276. u_int32_t acfg2 = 0x3ffffffc;
  277. u_int32_t acfg3 = 0x3ffffffc;
  278. u_int32_t acfg4 = 0x3ffffffc;
  279. emifregs emif_regs;
  280. /*------------------------------------------------------------------*
  281. * NAND FLASH CHIP TIMEOUT @ 459 MHz *
  282. * *
  283. * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
  284. * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
  285. * *
  286. *------------------------------------------------------------------*/
  287. acfg1 = 0
  288. | (0 << 31 ) /* selectStrobe */
  289. | (0 << 30 ) /* extWait */
  290. | (1 << 26 ) /* writeSetup 10 ns */
  291. | (3 << 20 ) /* writeStrobe 40 ns */
  292. | (1 << 17 ) /* writeHold 10 ns */
  293. | (1 << 13 ) /* readSetup 10 ns */
  294. | (5 << 7 ) /* readStrobe 60 ns */
  295. | (1 << 4 ) /* readHold 10 ns */
  296. | (3 << 2 ) /* turnAround ?? ns */
  297. | (0 << 0 ) /* asyncSize 8-bit bus */
  298. ;
  299. emif_regs = (emifregs)DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  300. emif_regs->AWCCR |= 0x10000000;
  301. emif_regs->AB1CR = acfg1; /* 0x08244128 */;
  302. emif_regs->AB2CR = acfg2;
  303. emif_regs->AB3CR = acfg3;
  304. emif_regs->AB4CR = acfg4;
  305. emif_regs->NANDFCR = 0x00000101;
  306. }
  307. int board_nand_init(struct nand_chip *nand)
  308. {
  309. nand->IO_ADDR_R = (void __iomem *)NAND_CE0DATA;
  310. nand->IO_ADDR_W = (void __iomem *)NAND_CE0DATA;
  311. nand->chip_delay = 0;
  312. nand->select_chip = nand_davinci_select_chip;
  313. #ifdef CFG_NAND_USE_FLASH_BBT
  314. nand->options = NAND_USE_FLASH_BBT;
  315. #endif
  316. #ifdef CFG_NAND_HW_ECC
  317. nand->ecc.mode = NAND_ECC_HW;
  318. #ifdef CFG_NAND_LARGEPAGE
  319. nand->ecc.size = 2048;
  320. nand->ecc.bytes = 12;
  321. #elif defined(CFG_NAND_SMALLPAGE)
  322. nand->ecc.size = 512;
  323. nand->ecc.bytes = 3;
  324. #else
  325. #error "Either CFG_NAND_LARGEPAGE or CFG_NAND_SMALLPAGE must be defined!"
  326. #endif
  327. nand->ecc.layout = &davinci_nand_ecclayout;
  328. nand->ecc.calculate = nand_davinci_calculate_ecc;
  329. nand->ecc.correct = nand_davinci_correct_data;
  330. nand->ecc.hwctl = nand_davinci_enable_hwecc;
  331. #else
  332. nand->ecc.mode = NAND_ECC_SOFT;
  333. #endif
  334. /* Set address of hardware control function */
  335. nand->cmd_ctrl = nand_davinci_hwcontrol;
  336. nand->dev_ready = nand_davinci_dev_ready;
  337. nand->waitfunc = nand_davinci_waitfunc;
  338. nand_flash_init();
  339. return(0);
  340. }
  341. #else
  342. #error "U-Boot legacy NAND support not available for DaVinci chips"
  343. #endif
  344. #endif /* CFG_USE_NAND */