s3c64xx.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /*
  2. * (C) Copyright 2006 DENX Software Engineering
  3. *
  4. * Implementation for U-Boot 1.1.6 by Samsung
  5. *
  6. * (C) Copyright 2008
  7. * Guennadi Liakhovetki, DENX Software Engineering, <lg@denx.de>
  8. *
  9. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <common.h>
  28. #include <nand.h>
  29. #include <linux/mtd/nand.h>
  30. #include <asm/arch/s3c6400.h>
  31. #include <asm/io.h>
  32. #include <asm/errno.h>
  33. #define MAX_CHIPS 2
  34. static int nand_cs[MAX_CHIPS] = {0, 1};
  35. #ifdef CONFIG_NAND_SPL
  36. #define printf(arg...) do {} while (0)
  37. #endif
  38. /* Nand flash definition values by jsgood */
  39. #ifdef S3C_NAND_DEBUG
  40. /*
  41. * Function to print out oob buffer for debugging
  42. * Written by jsgood
  43. */
  44. static void print_oob(const char *header, struct mtd_info *mtd)
  45. {
  46. int i;
  47. struct nand_chip *chip = mtd->priv;
  48. printf("%s:\t", header);
  49. for (i = 0; i < 64; i++)
  50. printf("%02x ", chip->oob_poi[i]);
  51. printf("\n");
  52. }
  53. #endif /* S3C_NAND_DEBUG */
  54. static void s3c_nand_select_chip(struct mtd_info *mtd, int chip)
  55. {
  56. int ctrl = readl(NFCONT);
  57. switch (chip) {
  58. case -1:
  59. ctrl |= 6;
  60. break;
  61. case 0:
  62. ctrl &= ~2;
  63. break;
  64. case 1:
  65. ctrl &= ~4;
  66. break;
  67. default:
  68. return;
  69. }
  70. writel(ctrl, NFCONT);
  71. }
  72. /*
  73. * Hardware specific access to control-lines function
  74. * Written by jsgood
  75. */
  76. static void s3c_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  77. {
  78. struct nand_chip *this = mtd->priv;
  79. if (ctrl & NAND_CTRL_CHANGE) {
  80. if (ctrl & NAND_CLE)
  81. this->IO_ADDR_W = (void __iomem *)NFCMMD;
  82. else if (ctrl & NAND_ALE)
  83. this->IO_ADDR_W = (void __iomem *)NFADDR;
  84. else
  85. this->IO_ADDR_W = (void __iomem *)NFDATA;
  86. if (ctrl & NAND_NCE)
  87. s3c_nand_select_chip(mtd, *(int *)this->priv);
  88. else
  89. s3c_nand_select_chip(mtd, -1);
  90. }
  91. if (cmd != NAND_CMD_NONE)
  92. writeb(cmd, this->IO_ADDR_W);
  93. }
  94. /*
  95. * Function for checking device ready pin
  96. * Written by jsgood
  97. */
  98. static int s3c_nand_device_ready(struct mtd_info *mtdinfo)
  99. {
  100. return !!(readl(NFSTAT) & NFSTAT_RnB);
  101. }
  102. #ifdef CONFIG_SYS_S3C_NAND_HWECC
  103. /*
  104. * This function is called before encoding ecc codes to ready ecc engine.
  105. * Written by jsgood
  106. */
  107. static void s3c_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  108. {
  109. u_long nfcont, nfconf;
  110. /*
  111. * The original driver used 4-bit ECC for "new" MLC chips, i.e., for
  112. * those with non-zero ID[3][3:2], which anyway only holds for ST
  113. * (Numonyx) chips
  114. */
  115. nfconf = readl(NFCONF) & ~NFCONF_ECC_4BIT;
  116. writel(nfconf, NFCONF);
  117. /* Initialize & unlock */
  118. nfcont = readl(NFCONT);
  119. nfcont |= NFCONT_INITECC;
  120. nfcont &= ~NFCONT_MECCLOCK;
  121. if (mode == NAND_ECC_WRITE)
  122. nfcont |= NFCONT_ECC_ENC;
  123. else if (mode == NAND_ECC_READ)
  124. nfcont &= ~NFCONT_ECC_ENC;
  125. writel(nfcont, NFCONT);
  126. }
  127. /*
  128. * This function is called immediately after encoding ecc codes.
  129. * This function returns encoded ecc codes.
  130. * Written by jsgood
  131. */
  132. static int s3c_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  133. u_char *ecc_code)
  134. {
  135. u_long nfcont, nfmecc0;
  136. /* Lock */
  137. nfcont = readl(NFCONT);
  138. nfcont |= NFCONT_MECCLOCK;
  139. writel(nfcont, NFCONT);
  140. nfmecc0 = readl(NFMECC0);
  141. ecc_code[0] = nfmecc0 & 0xff;
  142. ecc_code[1] = (nfmecc0 >> 8) & 0xff;
  143. ecc_code[2] = (nfmecc0 >> 16) & 0xff;
  144. ecc_code[3] = (nfmecc0 >> 24) & 0xff;
  145. return 0;
  146. }
  147. /*
  148. * This function determines whether read data is good or not.
  149. * If SLC, must write ecc codes to controller before reading status bit.
  150. * If MLC, status bit is already set, so only reading is needed.
  151. * If status bit is good, return 0.
  152. * If correctable errors occured, do that.
  153. * If uncorrectable errors occured, return -1.
  154. * Written by jsgood
  155. */
  156. static int s3c_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  157. u_char *read_ecc, u_char *calc_ecc)
  158. {
  159. int ret = -1;
  160. u_long nfestat0, nfmeccdata0, nfmeccdata1, err_byte_addr;
  161. u_char err_type, repaired;
  162. /* SLC: Write ecc to compare */
  163. nfmeccdata0 = (calc_ecc[1] << 16) | calc_ecc[0];
  164. nfmeccdata1 = (calc_ecc[3] << 16) | calc_ecc[2];
  165. writel(nfmeccdata0, NFMECCDATA0);
  166. writel(nfmeccdata1, NFMECCDATA1);
  167. /* Read ecc status */
  168. nfestat0 = readl(NFESTAT0);
  169. err_type = nfestat0 & 0x3;
  170. switch (err_type) {
  171. case 0: /* No error */
  172. ret = 0;
  173. break;
  174. case 1:
  175. /*
  176. * 1 bit error (Correctable)
  177. * (nfestat0 >> 7) & 0x7ff :error byte number
  178. * (nfestat0 >> 4) & 0x7 :error bit number
  179. */
  180. err_byte_addr = (nfestat0 >> 7) & 0x7ff;
  181. repaired = dat[err_byte_addr] ^ (1 << ((nfestat0 >> 4) & 0x7));
  182. printf("S3C NAND: 1 bit error detected at byte %ld. "
  183. "Correcting from 0x%02x to 0x%02x...OK\n",
  184. err_byte_addr, dat[err_byte_addr], repaired);
  185. dat[err_byte_addr] = repaired;
  186. ret = 1;
  187. break;
  188. case 2: /* Multiple error */
  189. case 3: /* ECC area error */
  190. printf("S3C NAND: ECC uncorrectable error detected. "
  191. "Not correctable.\n");
  192. ret = -1;
  193. break;
  194. }
  195. return ret;
  196. }
  197. #endif /* CONFIG_SYS_S3C_NAND_HWECC */
  198. /*
  199. * Board-specific NAND initialization. The following members of the
  200. * argument are board-specific (per include/linux/mtd/nand.h):
  201. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  202. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  203. * - hwcontrol: hardwarespecific function for accesing control-lines
  204. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  205. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  206. * only be provided if a hardware ECC is available
  207. * - eccmode: mode of ecc, see defines
  208. * - chip_delay: chip dependent delay for transfering data from array to
  209. * read regs (tR)
  210. * - options: various chip options. They can partly be set to inform
  211. * nand_scan about special functionality. See the defines for further
  212. * explanation
  213. * Members with a "?" were not set in the merged testing-NAND branch,
  214. * so they are not set here either.
  215. */
  216. int board_nand_init(struct nand_chip *nand)
  217. {
  218. static int chip_n;
  219. if (chip_n >= MAX_CHIPS)
  220. return -ENODEV;
  221. NFCONT_REG = (NFCONT_REG & ~NFCONT_WP) | NFCONT_ENABLE | 0x6;
  222. nand->IO_ADDR_R = (void __iomem *)NFDATA;
  223. nand->IO_ADDR_W = (void __iomem *)NFDATA;
  224. nand->cmd_ctrl = s3c_nand_hwcontrol;
  225. nand->dev_ready = s3c_nand_device_ready;
  226. nand->select_chip = s3c_nand_select_chip;
  227. nand->options = 0;
  228. #ifdef CONFIG_NAND_SPL
  229. nand->read_byte = nand_read_byte;
  230. nand->write_buf = nand_write_buf;
  231. nand->read_buf = nand_read_buf;
  232. #endif
  233. #ifdef CONFIG_SYS_S3C_NAND_HWECC
  234. nand->ecc.hwctl = s3c_nand_enable_hwecc;
  235. nand->ecc.calculate = s3c_nand_calculate_ecc;
  236. nand->ecc.correct = s3c_nand_correct_data;
  237. /*
  238. * If you get more than 1 NAND-chip with different page-sizes on the
  239. * board one day, it will get more complicated...
  240. */
  241. nand->ecc.mode = NAND_ECC_HW;
  242. nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
  243. nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
  244. #else
  245. nand->ecc.mode = NAND_ECC_SOFT;
  246. #endif /* ! CONFIG_SYS_S3C_NAND_HWECC */
  247. nand->priv = nand_cs + chip_n++;
  248. return 0;
  249. }