s3c2410_nand.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 <s3c2410.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. static void s3c2410_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  35. {
  36. struct nand_chip *chip = mtd->priv;
  37. struct s3c2410_nand *nand = s3c2410_get_base_nand();
  38. debugX(1, "hwcontrol(): 0x%02x 0x%02x\n", cmd, ctrl);
  39. if (ctrl & NAND_CTRL_CHANGE) {
  40. ulong IO_ADDR_W = (ulong)nand;
  41. if (!(ctrl & NAND_CLE))
  42. IO_ADDR_W |= S3C2410_ADDR_NCLE;
  43. if (!(ctrl & NAND_ALE))
  44. IO_ADDR_W |= S3C2410_ADDR_NALE;
  45. chip->IO_ADDR_W = (void *)IO_ADDR_W;
  46. if (ctrl & NAND_NCE)
  47. writel(readl(&nand->NFCONF) & ~S3C2410_NFCONF_nFCE,
  48. &nand->NFCONF);
  49. else
  50. writel(readl(&nand->NFCONF) | S3C2410_NFCONF_nFCE,
  51. &nand->NFCONF);
  52. }
  53. if (cmd != NAND_CMD_NONE)
  54. writeb(cmd, chip->IO_ADDR_W);
  55. }
  56. static int s3c2410_dev_ready(struct mtd_info *mtd)
  57. {
  58. struct s3c2410_nand *nand = s3c2410_get_base_nand();
  59. debugX(1, "dev_ready\n");
  60. return readl(&nand->NFSTAT) & 0x01;
  61. }
  62. #ifdef CONFIG_S3C2410_NAND_HWECC
  63. void s3c2410_nand_enable_hwecc(struct mtd_info *mtd, int mode)
  64. {
  65. struct s3c2410_nand *nand = s3c2410_get_base_nand();
  66. debugX(1, "s3c2410_nand_enable_hwecc(%p, %d)\n", mtd, mode);
  67. writel(readl(&nand->NFCONF) | S3C2410_NFCONF_INITECC, &nand->NFCONF);
  68. }
  69. static int s3c2410_nand_calculate_ecc(struct mtd_info *mtd, const u_char *dat,
  70. u_char *ecc_code)
  71. {
  72. ecc_code[0] = NFECC0;
  73. ecc_code[1] = NFECC1;
  74. ecc_code[2] = NFECC2;
  75. debugX(1, "s3c2410_nand_calculate_hwecc(%p,): 0x%02x 0x%02x 0x%02x\n",
  76. mtd , ecc_code[0], ecc_code[1], ecc_code[2]);
  77. return 0;
  78. }
  79. static int s3c2410_nand_correct_data(struct mtd_info *mtd, u_char *dat,
  80. u_char *read_ecc, u_char *calc_ecc)
  81. {
  82. if (read_ecc[0] == calc_ecc[0] &&
  83. read_ecc[1] == calc_ecc[1] &&
  84. read_ecc[2] == calc_ecc[2])
  85. return 0;
  86. printf("s3c2410_nand_correct_data: not implemented\n");
  87. return -1;
  88. }
  89. #endif
  90. int board_nand_init(struct nand_chip *nand)
  91. {
  92. u_int32_t cfg;
  93. u_int8_t tacls, twrph0, twrph1;
  94. struct s3c24x0_clock_power *clk_power = s3c24x0_get_base_clock_power();
  95. struct s3c2410_nand *nand_reg = s3c2410_get_base_nand();
  96. debugX(1, "board_nand_init()\n");
  97. writel(readl(&clk_power->CLKCON) | (1 << 4), &clk_power->CLKCON);
  98. /* initialize hardware */
  99. twrph0 = 3;
  100. twrph1 = 0;
  101. tacls = 0;
  102. cfg = S3C2410_NFCONF_EN;
  103. cfg |= S3C2410_NFCONF_TACLS(tacls - 1);
  104. cfg |= S3C2410_NFCONF_TWRPH0(twrph0 - 1);
  105. cfg |= S3C2410_NFCONF_TWRPH1(twrph1 - 1);
  106. writel(cfg, &nand_reg->NFCONF);
  107. /* initialize nand_chip data structure */
  108. nand->IO_ADDR_R = nand->IO_ADDR_W = (void *)&nand_reg->NFDATA;
  109. /* read_buf and write_buf are default */
  110. /* read_byte and write_byte are default */
  111. /* hwcontrol always must be implemented */
  112. nand->cmd_ctrl = s3c2410_hwcontrol;
  113. nand->dev_ready = s3c2410_dev_ready;
  114. #ifdef CONFIG_S3C2410_NAND_HWECC
  115. nand->ecc.hwctl = s3c2410_nand_enable_hwecc;
  116. nand->ecc.calculate = s3c2410_nand_calculate_ecc;
  117. nand->ecc.correct = s3c2410_nand_correct_data;
  118. nand->ecc.mode = NAND_ECC_HW3_512;
  119. #else
  120. nand->ecc.mode = NAND_ECC_SOFT;
  121. #endif
  122. #ifdef CONFIG_S3C2410_NAND_BBT
  123. nand->options = NAND_USE_FLASH_BBT;
  124. #else
  125. nand->options = 0;
  126. #endif
  127. debugX(1, "end of nand_init\n");
  128. return 0;
  129. }