s3c2410_nand.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * (C) Copyright 2006 OpenMoko, Inc.
  3. * Author: Harald Welte <laforge@openmoko.org>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of
  8. * the License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  18. * MA 02111-1307 USA
  19. */
  20. #include <common.h>
  21. #include <nand.h>
  22. #include <asm/arch/s3c24x0_cpu.h>
  23. #include <asm/io.h>
  24. #define S3C2410_NFCONF_EN (1<<15)
  25. #define S3C2410_NFCONF_512BYTE (1<<14)
  26. #define S3C2410_NFCONF_4STEP (1<<13)
  27. #define S3C2410_NFCONF_INITECC (1<<12)
  28. #define S3C2410_NFCONF_nFCE (1<<11)
  29. #define S3C2410_NFCONF_TACLS(x) ((x)<<8)
  30. #define S3C2410_NFCONF_TWRPH0(x) ((x)<<4)
  31. #define S3C2410_NFCONF_TWRPH1(x) ((x)<<0)
  32. #define S3C2410_ADDR_NALE 4
  33. #define S3C2410_ADDR_NCLE 8
  34. #ifdef CONFIG_NAND_SPL
  35. /* in the early stage of NAND flash booting, printf() is not available */
  36. #define printf(fmt, args...)
  37. static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  38. {
  39. int i;
  40. struct nand_chip *this = mtd->priv;
  41. for (i = 0; i < len; i++)
  42. buf[i] = readb(this->IO_ADDR_R);
  43. }
  44. #endif
  45. static void s3c2410_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  46. {
  47. struct nand_chip *chip = mtd->priv;
  48. struct s3c2410_nand *nand = s3c2410_get_base_nand();
  49. debug("hwcontrol(): 0x%02x 0x%02x\n", cmd, ctrl);
  50. if (ctrl & NAND_CTRL_CHANGE) {
  51. ulong IO_ADDR_W = (ulong)nand;
  52. if (!(ctrl & NAND_CLE))
  53. IO_ADDR_W |= S3C2410_ADDR_NCLE;
  54. if (!(ctrl & NAND_ALE))
  55. IO_ADDR_W |= S3C2410_ADDR_NALE;
  56. chip->IO_ADDR_W = (void *)IO_ADDR_W;
  57. if (ctrl & NAND_NCE)
  58. writel(readl(&nand->nfconf) & ~S3C2410_NFCONF_nFCE,
  59. &nand->nfconf);
  60. else
  61. writel(readl(&nand->nfconf) | S3C2410_NFCONF_nFCE,
  62. &nand->nfconf);
  63. }
  64. if (cmd != NAND_CMD_NONE)
  65. writeb(cmd, chip->IO_ADDR_W);
  66. }
  67. static int s3c2410_dev_ready(struct mtd_info *mtd)
  68. {
  69. struct s3c2410_nand *nand = s3c2410_get_base_nand();
  70. debug("dev_ready\n");
  71. return readl(&nand->nfstat) & 0x01;
  72. }
  73. #ifdef CONFIG_S3C2410_NAND_HWECC
  74. void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  75. {
  76. struct s3c2410_nand *nand = s3c2410_get_base_nand();
  77. debug("s3c2410_nand_enable_hwecc(%p, %d)\n", mtd, mode);
  78. writel(readl(&nand->nfconf) | S3C2410_NFCONF_INITECC, &nand->nfconf);
  79. }
  80. static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  81. u_char *ecc_code)
  82. {
  83. struct s3c2410_nand *nand = s3c2410_get_base_nand();
  84. ecc_code[0] = readb(&nand->nfecc);
  85. ecc_code[1] = readb(&nand->nfecc + 1);
  86. ecc_code[2] = readb(&nand->nfecc + 2);
  87. debug("s3c2410_nand_calculate_hwecc(%p,): 0x%02x 0x%02x 0x%02x\n",
  88. mtd , ecc_code[0], ecc_code[1], ecc_code[2]);
  89. return 0;
  90. }
  91. static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  92. u_char *read_ecc, u_char *calc_ecc)
  93. {
  94. if (read_ecc[0] == calc_ecc[0] &&
  95. read_ecc[1] == calc_ecc[1] &&
  96. read_ecc[2] == calc_ecc[2])
  97. return 0;
  98. printf("s3c2410_nand_correct_data: not implemented\n");
  99. return -1;
  100. }
  101. #endif
  102. int board_nand_init(struct nand_chip *nand)
  103. {
  104. u_int32_t cfg;
  105. u_int8_t tacls, twrph0, twrph1;
  106. struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
  107. struct s3c2410_nand *nand_reg = s3c2410_get_base_nand();
  108. debug("board_nand_init()\n");
  109. writel(readl(&clk_power->clkcon) | (1 << 4), &clk_power->clkcon);
  110. /* initialize hardware */
  111. #if defined(CONFIG_S3C24XX_CUSTOM_NAND_TIMING)
  112. tacls = CONFIG_S3C24XX_TACLS;
  113. twrph0 = CONFIG_S3C24XX_TWRPH0;
  114. twrph1 = CONFIG_S3C24XX_TWRPH1;
  115. #else
  116. tacls = 4;
  117. twrph0 = 8;
  118. twrph1 = 8;
  119. #endif
  120. cfg = S3C2410_NFCONF_EN;
  121. cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
  122. cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
  123. cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
  124. writel(cfg, &nand_reg->nfconf);
  125. /* initialize nand_chip data structure */
  126. nand->IO_ADDR_R = (void *)&nand_reg->nfdata;
  127. nand->IO_ADDR_W = (void *)&nand_reg->nfdata;
  128. nand->select_chip = NULL;
  129. /* read_buf and write_buf are default */
  130. /* read_byte and write_byte are default */
  131. #ifdef CONFIG_NAND_SPL
  132. nand->read_buf = nand_read_buf;
  133. #endif
  134. /* hwcontrol always must be implemented */
  135. nand->cmd_ctrl = s3c2410_hwcontrol;
  136. nand->dev_ready = s3c2410_dev_ready;
  137. #ifdef CONFIG_S3C2410_NAND_HWECC
  138. nand->ecc.hwctl = s3c2410_nand_enable_hwecc;
  139. nand->ecc.calculate = s3c2410_nand_calculate_ecc;
  140. nand->ecc.correct = s3c2410_nand_correct_data;
  141. nand->ecc.mode = NAND_ECC_HW;
  142. nand->ecc.size = CONFIG_SYS_NAND_ECCSIZE;
  143. nand->ecc.bytes = CONFIG_SYS_NAND_ECCBYTES;
  144. nand->ecc.strength = 1;
  145. #else
  146. nand->ecc.mode = NAND_ECC_SOFT;
  147. #endif
  148. #ifdef CONFIG_S3C2410_NAND_BBT
  149. nand->bbt_options |= NAND_BBT_USE_FLASH;
  150. #endif
  151. debug("end of nand_init\n");
  152. return 0;
  153. }