davinci_nand.c 7.1 KB

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