davinci_nand.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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. static emif_registers *const emif_regs = (void *) DAVINCI_ASYNC_EMIF_CNTRL_BASE;
  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. #ifdef CONFIG_SYS_NAND_HW_ECC
  64. static void nand_davinci_enable_hwecc(struct mtd_info *mtd, int mode)
  65. {
  66. int dummy;
  67. dummy = emif_regs->NANDF1ECC;
  68. /* FIXME: only chipselect 0 is supported for now */
  69. emif_regs->NANDFCR |= 1 << 8;
  70. }
  71. static u_int32_t nand_davinci_readecc(struct mtd_info *mtd, u_int32_t region)
  72. {
  73. u_int32_t ecc = 0;
  74. if (region == 1)
  75. ecc = emif_regs->NANDF1ECC;
  76. else if (region == 2)
  77. ecc = emif_regs->NANDF2ECC;
  78. else if (region == 3)
  79. ecc = emif_regs->NANDF3ECC;
  80. else if (region == 4)
  81. ecc = emif_regs->NANDF4ECC;
  82. return(ecc);
  83. }
  84. static int nand_davinci_calculate_ecc(struct mtd_info *mtd, const u_char *dat, u_char *ecc_code)
  85. {
  86. u_int32_t tmp;
  87. const int region = 1;
  88. tmp = nand_davinci_readecc(mtd, region);
  89. /* Squeeze 4 bytes ECC into 3 bytes by removing RESERVED bits
  90. * and shifting. RESERVED bits are 31 to 28 and 15 to 12. */
  91. tmp = (tmp & 0x00000fff) | ((tmp & 0x0fff0000) >> 4);
  92. /* Invert so that erased block ECC is correct */
  93. tmp = ~tmp;
  94. *ecc_code++ = tmp;
  95. *ecc_code++ = tmp >> 8;
  96. *ecc_code++ = tmp >> 16;
  97. /* NOTE: the above code matches mainline Linux:
  98. * .PQR.stu ==> ~PQRstu
  99. *
  100. * MontaVista/TI kernels encode those bytes differently, use
  101. * complicated (and allegedly sometimes-wrong) correction code,
  102. * and usually shipped with U-Boot that uses software ECC:
  103. * .PQR.stu ==> PsQRtu
  104. *
  105. * If you need MV/TI compatible NAND I/O in U-Boot, it should
  106. * be possible to (a) change the mangling above, (b) reverse
  107. * that mangling in nand_davinci_correct_data() below.
  108. */
  109. return 0;
  110. }
  111. static int nand_davinci_correct_data(struct mtd_info *mtd, u_char *dat, u_char *read_ecc, u_char *calc_ecc)
  112. {
  113. struct nand_chip *this = mtd->priv;
  114. u_int32_t ecc_nand = read_ecc[0] | (read_ecc[1] << 8) |
  115. (read_ecc[2] << 16);
  116. u_int32_t ecc_calc = calc_ecc[0] | (calc_ecc[1] << 8) |
  117. (calc_ecc[2] << 16);
  118. u_int32_t diff = ecc_calc ^ ecc_nand;
  119. if (diff) {
  120. if ((((diff >> 12) ^ diff) & 0xfff) == 0xfff) {
  121. /* Correctable error */
  122. if ((diff >> (12 + 3)) < this->ecc.size) {
  123. uint8_t find_bit = 1 << ((diff >> 12) & 7);
  124. uint32_t find_byte = diff >> (12 + 3);
  125. dat[find_byte] ^= find_bit;
  126. MTDDEBUG(MTD_DEBUG_LEVEL0, "Correcting single "
  127. "bit ECC error at offset: %d, bit: "
  128. "%d\n", find_byte, find_bit);
  129. return 1;
  130. } else {
  131. return -1;
  132. }
  133. } else if (!(diff & (diff - 1))) {
  134. /* Single bit ECC error in the ECC itself,
  135. nothing to fix */
  136. MTDDEBUG(MTD_DEBUG_LEVEL0, "Single bit ECC error in "
  137. "ECC.\n");
  138. return 1;
  139. } else {
  140. /* Uncorrectable error */
  141. MTDDEBUG(MTD_DEBUG_LEVEL0, "ECC UNCORRECTED_ERROR 1\n");
  142. return -1;
  143. }
  144. }
  145. return(0);
  146. }
  147. #endif /* CONFIG_SYS_NAND_HW_ECC */
  148. static int nand_davinci_dev_ready(struct mtd_info *mtd)
  149. {
  150. return emif_regs->NANDFSR & 0x1;
  151. }
  152. static void nand_flash_init(void)
  153. {
  154. /* This is for DM6446 EVM and *very* similar. DO NOT GROW THIS!
  155. * Instead, have your board_init() set EMIF timings, based on its
  156. * knowledge of the clocks and what devices are hooked up ... and
  157. * don't even do that unless no UBL handled it.
  158. */
  159. #ifdef CONFIG_SOC_DM6446
  160. u_int32_t acfg1 = 0x3ffffffc;
  161. /*------------------------------------------------------------------*
  162. * NAND FLASH CHIP TIMEOUT @ 459 MHz *
  163. * *
  164. * AEMIF.CLK freq = PLL1/6 = 459/6 = 76.5 MHz *
  165. * AEMIF.CLK period = 1/76.5 MHz = 13.1 ns *
  166. * *
  167. *------------------------------------------------------------------*/
  168. acfg1 = 0
  169. | (0 << 31 ) /* selectStrobe */
  170. | (0 << 30 ) /* extWait */
  171. | (1 << 26 ) /* writeSetup 10 ns */
  172. | (3 << 20 ) /* writeStrobe 40 ns */
  173. | (1 << 17 ) /* writeHold 10 ns */
  174. | (1 << 13 ) /* readSetup 10 ns */
  175. | (5 << 7 ) /* readStrobe 60 ns */
  176. | (1 << 4 ) /* readHold 10 ns */
  177. | (3 << 2 ) /* turnAround ?? ns */
  178. | (0 << 0 ) /* asyncSize 8-bit bus */
  179. ;
  180. emif_regs->AB1CR = acfg1; /* CS2 */
  181. emif_regs->NANDFCR = 0x00000101; /* NAND flash on CS2 */
  182. #endif
  183. }
  184. void davinci_nand_init(struct nand_chip *nand)
  185. {
  186. nand->chip_delay = 0;
  187. #ifdef CONFIG_SYS_NAND_USE_FLASH_BBT
  188. nand->options = NAND_USE_FLASH_BBT;
  189. #endif
  190. #ifdef CONFIG_SYS_NAND_HW_ECC
  191. nand->ecc.mode = NAND_ECC_HW;
  192. nand->ecc.size = 512;
  193. nand->ecc.bytes = 3;
  194. nand->ecc.calculate = nand_davinci_calculate_ecc;
  195. nand->ecc.correct = nand_davinci_correct_data;
  196. nand->ecc.hwctl = nand_davinci_enable_hwecc;
  197. #else
  198. nand->ecc.mode = NAND_ECC_SOFT;
  199. #endif /* CONFIG_SYS_NAND_HW_ECC */
  200. /* Set address of hardware control function */
  201. nand->cmd_ctrl = nand_davinci_hwcontrol;
  202. nand->dev_ready = nand_davinci_dev_ready;
  203. nand_flash_init();
  204. }
  205. int board_nand_init(struct nand_chip *chip) __attribute__((weak));
  206. int board_nand_init(struct nand_chip *chip)
  207. {
  208. davinci_nand_init(chip);
  209. return 0;
  210. }