s3c64xx.c 7.9 KB

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