ndfc.c 5.5 KB

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