ndfc.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Overview:
  3. * Platform independend driver for NDFC (NanD Flash Controller)
  4. * integrated into IBM/AMCC PPC4xx cores
  5. *
  6. * (C) Copyright 2006-2009
  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_NAND_NDFC)
  34. #include <nand.h>
  35. #include <linux/mtd/ndfc.h>
  36. #include <linux/mtd/nand_ecc.h>
  37. #include <asm/processor.h>
  38. #include <asm/io.h>
  39. #include <ppc4xx.h>
  40. /*
  41. * We need to store the info, which chip-select (CS) is used for the
  42. * chip number. For example on Sequoia NAND chip #0 uses
  43. * CS #3.
  44. */
  45. static int ndfc_cs[NDFC_MAX_BANKS];
  46. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  47. {
  48. struct nand_chip *this = mtd->priv;
  49. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  50. if (cmd == NAND_CMD_NONE)
  51. return;
  52. if (ctrl & NAND_CLE)
  53. out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF);
  54. else
  55. out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF);
  56. }
  57. static int ndfc_dev_ready(struct mtd_info *mtdinfo)
  58. {
  59. struct nand_chip *this = mtdinfo->priv;
  60. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  61. return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY);
  62. }
  63. static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
  64. {
  65. struct nand_chip *this = mtdinfo->priv;
  66. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  67. u32 ccr;
  68. ccr = in_be32((u32 *)(base + NDFC_CCR));
  69. ccr |= NDFC_CCR_RESET_ECC;
  70. out_be32((u32 *)(base + NDFC_CCR), ccr);
  71. }
  72. static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
  73. const u_char *dat, u_char *ecc_code)
  74. {
  75. struct nand_chip *this = mtdinfo->priv;
  76. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  77. u32 ecc;
  78. u8 *p = (u8 *)&ecc;
  79. ecc = in_be32((u32 *)(base + NDFC_ECC));
  80. /* The NDFC uses Smart Media (SMC) bytes order
  81. */
  82. ecc_code[0] = p[1];
  83. ecc_code[1] = p[2];
  84. ecc_code[2] = p[3];
  85. return 0;
  86. }
  87. /*
  88. * Speedups for buffer read/write/verify
  89. *
  90. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  91. * functions. No further checking, as nand_base will always read/write
  92. * page aligned.
  93. */
  94. static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
  95. {
  96. struct nand_chip *this = mtdinfo->priv;
  97. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  98. uint32_t *p = (uint32_t *) buf;
  99. for (;len > 0; len -= 4)
  100. *p++ = in_be32((u32 *)(base + NDFC_DATA));
  101. }
  102. #ifndef CONFIG_NAND_SPL
  103. /*
  104. * Don't use these speedup functions in NAND boot image, since the image
  105. * has to fit into 4kByte.
  106. */
  107. static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  108. {
  109. struct nand_chip *this = mtdinfo->priv;
  110. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  111. uint32_t *p = (uint32_t *) buf;
  112. for (; len > 0; len -= 4)
  113. out_be32((u32 *)(base + NDFC_DATA), *p++);
  114. }
  115. static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  116. {
  117. struct nand_chip *this = mtdinfo->priv;
  118. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  119. uint32_t *p = (uint32_t *) buf;
  120. for (; len > 0; len -= 4)
  121. if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
  122. return -1;
  123. return 0;
  124. }
  125. #endif /* #ifndef CONFIG_NAND_SPL */
  126. #ifndef CONFIG_SYS_NAND_BCR
  127. #define CONFIG_SYS_NAND_BCR 0x80002222
  128. #endif
  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)), CONFIG_SYS_NAND_BCR);
  141. }
  142. static void ndfc_select_chip(struct mtd_info *mtd, int chip)
  143. {
  144. /*
  145. * Nothing to do here!
  146. */
  147. }
  148. int board_nand_init(struct nand_chip *nand)
  149. {
  150. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  151. ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
  152. static int chip = 0;
  153. /*
  154. * Save chip-select for this chip #
  155. */
  156. ndfc_cs[chip] = cs;
  157. /*
  158. * Select required NAND chip in NDFC
  159. */
  160. board_nand_select_device(nand, chip);
  161. nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA);
  162. nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA);
  163. nand->cmd_ctrl = ndfc_hwcontrol;
  164. nand->chip_delay = 50;
  165. nand->read_buf = ndfc_read_buf;
  166. nand->dev_ready = ndfc_dev_ready;
  167. nand->ecc.correct = nand_correct_data;
  168. nand->ecc.hwctl = ndfc_enable_hwecc;
  169. nand->ecc.calculate = ndfc_calculate_ecc;
  170. nand->ecc.mode = NAND_ECC_HW;
  171. nand->ecc.size = 256;
  172. nand->ecc.bytes = 3;
  173. nand->select_chip = ndfc_select_chip;
  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, CONFIG_SYS_EBC_PB0CR);
  183. mtebc(pb0ap, CONFIG_SYS_EBC_PB0AP);
  184. #endif
  185. chip++;
  186. return 0;
  187. }
  188. #endif