ndfc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Overview:
  3. * Platform independend driver for NDFC (NanD Flash Controller)
  4. * integrated into EP440 cores
  5. *
  6. * (C) Copyright 2006-2007
  7. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  8. *
  9. * Based on original work by
  10. * Thomas Gleixner
  11. * Copyright 2006 IBM
  12. *
  13. * See file CREDITS for list of people who contributed to this
  14. * project.
  15. *
  16. * This program is free software; you can redistribute it and/or
  17. * modify it under the terms of the GNU General Public License as
  18. * published by the Free Software Foundation; either version 2 of
  19. * the License, or (at your option) any later version.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU General Public License
  27. * along with this program; if not, write to the Free Software
  28. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  29. * MA 02111-1307 USA
  30. */
  31. #include <common.h>
  32. #if defined(CONFIG_CMD_NAND) && !defined(CONFIG_NAND_LEGACY) && \
  33. (defined(CONFIG_440EP) || defined(CONFIG_440GR) || \
  34. defined(CONFIG_440EPX) || defined(CONFIG_440GRX) || \
  35. defined(CONFIG_405EZ) || defined(CONFIG_405EX) || \
  36. defined(CONFIG_460EX) || defined(CONFIG_460GT))
  37. #include <nand.h>
  38. #include <linux/mtd/ndfc.h>
  39. #include <linux/mtd/nand_ecc.h>
  40. #include <asm/processor.h>
  41. #include <asm/io.h>
  42. #include <ppc4xx.h>
  43. /*
  44. * We need to store the info, which chip-select (CS) is used for the
  45. * chip number. For example on Sequoia NAND chip #0 uses
  46. * CS #3.
  47. */
  48. static int ndfc_cs[NDFC_MAX_BANKS];
  49. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  50. {
  51. struct nand_chip *this = mtd->priv;
  52. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  53. if (cmd == NAND_CMD_NONE)
  54. return;
  55. if (ctrl & NAND_CLE)
  56. out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF);
  57. else
  58. out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF);
  59. }
  60. static int ndfc_dev_ready(struct mtd_info *mtdinfo)
  61. {
  62. struct nand_chip *this = mtdinfo->priv;
  63. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  64. return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY);
  65. }
  66. static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
  67. {
  68. struct nand_chip *this = mtdinfo->priv;
  69. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  70. u32 ccr;
  71. ccr = in_be32((u32 *)(base + NDFC_CCR));
  72. ccr |= NDFC_CCR_RESET_ECC;
  73. out_be32((u32 *)(base + NDFC_CCR), ccr);
  74. }
  75. static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
  76. const u_char *dat, u_char *ecc_code)
  77. {
  78. struct nand_chip *this = mtdinfo->priv;
  79. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  80. u32 ecc;
  81. u8 *p = (u8 *)&ecc;
  82. ecc = in_be32((u32 *)(base + NDFC_ECC));
  83. /* The NDFC uses Smart Media (SMC) bytes order
  84. */
  85. ecc_code[0] = p[1];
  86. ecc_code[1] = p[2];
  87. ecc_code[2] = p[3];
  88. return 0;
  89. }
  90. /*
  91. * Speedups for buffer read/write/verify
  92. *
  93. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  94. * functions. No further checking, as nand_base will always read/write
  95. * page aligned.
  96. */
  97. static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
  98. {
  99. struct nand_chip *this = mtdinfo->priv;
  100. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  101. uint32_t *p = (uint32_t *) buf;
  102. for (;len > 0; len -= 4)
  103. *p++ = in_be32((u32 *)(base + NDFC_DATA));
  104. }
  105. #ifndef CONFIG_NAND_SPL
  106. /*
  107. * Don't use these speedup functions in NAND boot image, since the image
  108. * has to fit into 4kByte.
  109. */
  110. static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  111. {
  112. struct nand_chip *this = mtdinfo->priv;
  113. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  114. uint32_t *p = (uint32_t *) buf;
  115. for (; len > 0; len -= 4)
  116. out_be32((u32 *)(base + NDFC_DATA), *p++);
  117. }
  118. static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  119. {
  120. struct nand_chip *this = mtdinfo->priv;
  121. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  122. uint32_t *p = (uint32_t *) buf;
  123. for (; len > 0; len -= 4)
  124. if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
  125. return -1;
  126. return 0;
  127. }
  128. #endif /* #ifndef CONFIG_NAND_SPL */
  129. void board_nand_select_device(struct nand_chip *nand, int chip)
  130. {
  131. /*
  132. * Don't use "chip" to address the NAND device,
  133. * generate the cs from the address where it is encoded.
  134. */
  135. ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
  136. int cs = ndfc_cs[chip];
  137. /* Set NandFlash Core Configuration Register */
  138. /* 1 col x 2 rows */
  139. out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
  140. out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222);
  141. }
  142. int board_nand_init(struct nand_chip *nand)
  143. {
  144. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  145. ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
  146. static int chip = 0;
  147. /*
  148. * Save chip-select for this chip #
  149. */
  150. ndfc_cs[chip] = cs;
  151. /*
  152. * Select required NAND chip in NDFC
  153. */
  154. board_nand_select_device(nand, chip);
  155. nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA);
  156. nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA);
  157. nand->cmd_ctrl = ndfc_hwcontrol;
  158. nand->chip_delay = 50;
  159. nand->read_buf = ndfc_read_buf;
  160. nand->dev_ready = ndfc_dev_ready;
  161. nand->ecc.correct = nand_correct_data;
  162. nand->ecc.hwctl = ndfc_enable_hwecc;
  163. nand->ecc.calculate = ndfc_calculate_ecc;
  164. nand->ecc.mode = NAND_ECC_HW;
  165. nand->ecc.size = 256;
  166. nand->ecc.bytes = 3;
  167. #ifndef CONFIG_NAND_SPL
  168. nand->write_buf = ndfc_write_buf;
  169. nand->verify_buf = ndfc_verify_buf;
  170. #else
  171. /*
  172. * Setup EBC (CS0 only right now)
  173. */
  174. mtebc(EBC0_CFG, 0xb8400000);
  175. mtebc(pb0cr, CFG_EBC_PB0CR);
  176. mtebc(pb0ap, CFG_EBC_PB0AP);
  177. #endif
  178. chip++;
  179. return 0;
  180. }
  181. #endif