nand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #if 0
  22. #define DEBUGN printf
  23. #else
  24. #define DEBUGN(x, args ...) {}
  25. #endif
  26. #if defined(CONFIG_CMD_NAND)
  27. #if !defined(CONFIG_NAND_LEGACY)
  28. #include <nand.h>
  29. #include <s3c2410.h>
  30. #define __REGb(x) (*(volatile unsigned char *)(x))
  31. #define __REGi(x) (*(volatile unsigned int *)(x))
  32. #define NF_BASE 0x4e000000
  33. #define NFCONF __REGi(NF_BASE + 0x0)
  34. #define NFCMD __REGb(NF_BASE + 0x4)
  35. #define NFADDR __REGb(NF_BASE + 0x8)
  36. #define NFDATA __REGb(NF_BASE + 0xc)
  37. #define NFSTAT __REGb(NF_BASE + 0x10)
  38. #define NFECC0 __REGb(NF_BASE + 0x14)
  39. #define NFECC1 __REGb(NF_BASE + 0x15)
  40. #define NFECC2 __REGb(NF_BASE + 0x16)
  41. #define S3C2410_NFCONF_EN (1<<15)
  42. #define S3C2410_NFCONF_512BYTE (1<<14)
  43. #define S3C2410_NFCONF_4STEP (1<<13)
  44. #define S3C2410_NFCONF_INITECC (1<<12)
  45. #define S3C2410_NFCONF_nFCE (1<<11)
  46. #define S3C2410_NFCONF_TACLS(x) ((x)<<8)
  47. #define S3C2410_NFCONF_TWRPH0(x) ((x)<<4)
  48. #define S3C2410_NFCONF_TWRPH1(x) ((x)<<0)
  49. static void s3c2410_hwcontrol(struct mtd_info *mtd, int cmd)
  50. {
  51. struct nand_chip *chip = mtd->priv;
  52. DEBUGN("hwcontrol(): 0x%02x: ", cmd);
  53. switch (cmd) {
  54. case NAND_CTL_SETNCE:
  55. NFCONF &= ~S3C2410_NFCONF_nFCE;
  56. DEBUGN("NFCONF=0x%08x\n", NFCONF);
  57. break;
  58. case NAND_CTL_CLRNCE:
  59. NFCONF |= S3C2410_NFCONF_nFCE;
  60. DEBUGN("NFCONF=0x%08x\n", NFCONF);
  61. break;
  62. case NAND_CTL_SETALE:
  63. chip->IO_ADDR_W = NF_BASE + 0x8;
  64. DEBUGN("SETALE\n");
  65. break;
  66. case NAND_CTL_SETCLE:
  67. chip->IO_ADDR_W = NF_BASE + 0x4;
  68. DEBUGN("SETCLE\n");
  69. break;
  70. default:
  71. chip->IO_ADDR_W = NF_BASE + 0xc;
  72. break;
  73. }
  74. return;
  75. }
  76. static int s3c2410_dev_ready(struct mtd_info *mtd)
  77. {
  78. DEBUGN("dev_ready\n");
  79. return (NFSTAT & 0x01);
  80. }
  81. #ifdef CONFIG_S3C2410_NAND_HWECC
  82. void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  83. {
  84. DEBUGN("s3c2410_nand_enable_hwecc(%p, %d)\n", mtd ,mode);
  85. NFCONF |= S3C2410_NFCONF_INITECC;
  86. }
  87. static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  88. u_char *ecc_code)
  89. {
  90. ecc_code[0] = NFECC0;
  91. ecc_code[1] = NFECC1;
  92. ecc_code[2] = NFECC2;
  93. DEBUGN("s3c2410_nand_calculate_hwecc(%p,): 0x%02x 0x%02x 0x%02x\n",
  94. mtd , ecc_code[0], ecc_code[1], ecc_code[2]);
  95. return 0;
  96. }
  97. static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  98. u_char *read_ecc, u_char *calc_ecc)
  99. {
  100. if (read_ecc[0] == calc_ecc[0] &&
  101. read_ecc[1] == calc_ecc[1] &&
  102. read_ecc[2] == calc_ecc[2])
  103. return 0;
  104. printf("s3c2410_nand_correct_data: not implemented\n");
  105. return -1;
  106. }
  107. #endif
  108. int board_nand_init(struct nand_chip *nand)
  109. {
  110. u_int32_t cfg;
  111. u_int8_t tacls, twrph0, twrph1;
  112. S3C24X0_CLOCK_POWER * const clk_power = S3C24X0_GetBase_CLOCK_POWER();
  113. DEBUGN("board_nand_init()\n");
  114. clk_power->CLKCON |= (1 << 4);
  115. /* initialize hardware */
  116. twrph0 = 3; twrph1 = 0; tacls = 0;
  117. cfg = S3C2410_NFCONF_EN;
  118. cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
  119. cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
  120. cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
  121. NFCONF = cfg;
  122. /* initialize nand_chip data structure */
  123. nand->IO_ADDR_R = nand->IO_ADDR_W = 0x4e00000c;
  124. /* read_buf and write_buf are default */
  125. /* read_byte and write_byte are default */
  126. /* hwcontrol always must be implemented */
  127. nand->hwcontrol = s3c2410_hwcontrol;
  128. nand->dev_ready = s3c2410_dev_ready;
  129. #ifdef CONFIG_S3C2410_NAND_HWECC
  130. nand->enable_hwecc = s3c2410_nand_enable_hwecc;
  131. nand->calculate_ecc = s3c2410_nand_calculate_ecc;
  132. nand->correct_data = s3c2410_nand_correct_data;
  133. nand->eccmode = NAND_ECC_HW3_512;
  134. #else
  135. nand->eccmode = NAND_ECC_SOFT;
  136. #endif
  137. #ifdef CONFIG_S3C2410_NAND_BBT
  138. nand->options = NAND_USE_FLASH_BBT;
  139. #else
  140. nand->options = 0;
  141. #endif
  142. DEBUGN("end of nand_init\n");
  143. return 0;
  144. }
  145. #else
  146. #error "U-Boot legacy NAND support not available for S3C2410"
  147. #endif
  148. #endif