ndfc.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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(CFG_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. static u8 hwctl = 0;
  44. static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
  45. {
  46. switch (cmd) {
  47. case NAND_CTL_SETCLE:
  48. hwctl |= 0x1;
  49. break;
  50. case NAND_CTL_CLRCLE:
  51. hwctl &= ~0x1;
  52. break;
  53. case NAND_CTL_SETALE:
  54. hwctl |= 0x2;
  55. break;
  56. case NAND_CTL_CLRALE:
  57. hwctl &= ~0x2;
  58. break;
  59. }
  60. }
  61. static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte)
  62. {
  63. struct nand_chip *this = mtdinfo->priv;
  64. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  65. if (hwctl & 0x1)
  66. out_8((u8 *)(base + NDFC_CMD), byte);
  67. else if (hwctl & 0x2)
  68. out_8((u8 *)(base + NDFC_ALE), byte);
  69. else
  70. out_8((u8 *)(base + NDFC_DATA), byte);
  71. }
  72. static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
  73. {
  74. struct nand_chip *this = mtdinfo->priv;
  75. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  76. return (in_8((u8 *)(base + NDFC_DATA)));
  77. }
  78. static int ndfc_dev_ready(struct mtd_info *mtdinfo)
  79. {
  80. struct nand_chip *this = mtdinfo->priv;
  81. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  82. while (!(in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY))
  83. ;
  84. return 1;
  85. }
  86. static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
  87. {
  88. struct nand_chip *this = mtdinfo->priv;
  89. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  90. u32 ccr;
  91. ccr = in_be32((u32 *)(base + NDFC_CCR));
  92. ccr |= NDFC_CCR_RESET_ECC;
  93. out_be32((u32 *)(base + NDFC_CCR), ccr);
  94. }
  95. static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
  96. const u_char *dat, u_char *ecc_code)
  97. {
  98. struct nand_chip *this = mtdinfo->priv;
  99. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  100. u32 ecc;
  101. u8 *p = (u8 *)&ecc;
  102. ecc = in_be32((u32 *)(base + NDFC_ECC));
  103. /* The NDFC uses Smart Media (SMC) bytes order
  104. */
  105. ecc_code[0] = p[1];
  106. ecc_code[1] = p[2];
  107. ecc_code[2] = p[3];
  108. return 0;
  109. }
  110. /*
  111. * Speedups for buffer read/write/verify
  112. *
  113. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  114. * functions. No further checking, as nand_base will always read/write
  115. * page aligned.
  116. */
  117. static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
  118. {
  119. struct nand_chip *this = mtdinfo->priv;
  120. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  121. uint32_t *p = (uint32_t *) buf;
  122. for (;len > 0; len -= 4)
  123. *p++ = in_be32((u32 *)(base + NDFC_DATA));
  124. }
  125. #ifndef CONFIG_NAND_SPL
  126. /*
  127. * Don't use these speedup functions in NAND boot image, since the image
  128. * has to fit into 4kByte.
  129. */
  130. static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  131. {
  132. struct nand_chip *this = mtdinfo->priv;
  133. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  134. uint32_t *p = (uint32_t *) buf;
  135. for (; len > 0; len -= 4)
  136. out_be32((u32 *)(base + NDFC_DATA), *p++);
  137. }
  138. static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  139. {
  140. struct nand_chip *this = mtdinfo->priv;
  141. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  142. uint32_t *p = (uint32_t *) buf;
  143. for (; len > 0; len -= 4)
  144. if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
  145. return -1;
  146. return 0;
  147. }
  148. #endif /* #ifndef CONFIG_NAND_SPL */
  149. void board_nand_select_device(struct nand_chip *nand, int chip)
  150. {
  151. /*
  152. * Don't use "chip" to address the NAND device,
  153. * generate the cs from the address where it is encoded.
  154. */
  155. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  156. ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
  157. /* Set NandFlash Core Configuration Register */
  158. /* 1 col x 2 rows */
  159. out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
  160. }
  161. int board_nand_init(struct nand_chip *nand)
  162. {
  163. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  164. ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
  165. nand->hwcontrol = ndfc_hwcontrol;
  166. nand->read_byte = ndfc_read_byte;
  167. nand->read_buf = ndfc_read_buf;
  168. nand->write_byte = ndfc_write_byte;
  169. nand->dev_ready = ndfc_dev_ready;
  170. nand->eccmode = NAND_ECC_HW3_256;
  171. nand->enable_hwecc = ndfc_enable_hwecc;
  172. nand->calculate_ecc = ndfc_calculate_ecc;
  173. nand->correct_data = nand_correct_data;
  174. #ifndef CONFIG_NAND_SPL
  175. nand->write_buf = ndfc_write_buf;
  176. nand->verify_buf = ndfc_verify_buf;
  177. #else
  178. /*
  179. * Setup EBC (CS0 only right now)
  180. */
  181. mtebc(EBC0_CFG, 0xb8400000);
  182. mtebc(pb0cr, CFG_EBC_PB0CR);
  183. mtebc(pb0ap, CFG_EBC_PB0AP);
  184. #endif
  185. /*
  186. * Select required NAND chip in NDFC
  187. */
  188. board_nand_select_device(nand, cs);
  189. out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), 0x80002222);
  190. return 0;
  191. }
  192. #endif