ndfc.c 5.7 KB

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