s3c2410_nand.c 4.4 KB

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