ndfc.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. #include <nand.h>
  33. #include <linux/mtd/ndfc.h>
  34. #include <linux/mtd/nand_ecc.h>
  35. #include <asm/processor.h>
  36. #include <asm/io.h>
  37. #include <asm/ppc4xx.h>
  38. #ifndef CONFIG_SYS_NAND_BCR
  39. #define CONFIG_SYS_NAND_BCR 0x80002222
  40. #endif
  41. #ifndef CONFIG_SYS_NDFC_EBC0_CFG
  42. #define CONFIG_SYS_NDFC_EBC0_CFG 0xb8400000
  43. #endif
  44. /*
  45. * We need to store the info, which chip-select (CS) is used for the
  46. * chip number. For example on Sequoia NAND chip #0 uses
  47. * CS #3.
  48. */
  49. static int ndfc_cs[NDFC_MAX_BANKS];
  50. static void ndfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  51. {
  52. struct nand_chip *this = mtd->priv;
  53. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  54. if (cmd == NAND_CMD_NONE)
  55. return;
  56. if (ctrl & NAND_CLE)
  57. out_8((u8 *)(base + NDFC_CMD), cmd & 0xFF);
  58. else
  59. out_8((u8 *)(base + NDFC_ALE), cmd & 0xFF);
  60. }
  61. static int ndfc_dev_ready(struct mtd_info *mtdinfo)
  62. {
  63. struct nand_chip *this = mtdinfo->priv;
  64. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  65. return (in_be32((u32 *)(base + NDFC_STAT)) & NDFC_STAT_IS_READY);
  66. }
  67. static void ndfc_enable_hwecc(struct mtd_info *mtdinfo, int mode)
  68. {
  69. struct nand_chip *this = mtdinfo->priv;
  70. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  71. u32 ccr;
  72. ccr = in_be32((u32 *)(base + NDFC_CCR));
  73. ccr |= NDFC_CCR_RESET_ECC;
  74. out_be32((u32 *)(base + NDFC_CCR), ccr);
  75. }
  76. static int ndfc_calculate_ecc(struct mtd_info *mtdinfo,
  77. const u_char *dat, u_char *ecc_code)
  78. {
  79. struct nand_chip *this = mtdinfo->priv;
  80. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  81. u32 ecc;
  82. u8 *p = (u8 *)&ecc;
  83. ecc = in_be32((u32 *)(base + NDFC_ECC));
  84. /* The NDFC uses Smart Media (SMC) bytes order
  85. */
  86. ecc_code[0] = p[1];
  87. ecc_code[1] = p[2];
  88. ecc_code[2] = p[3];
  89. return 0;
  90. }
  91. /*
  92. * Speedups for buffer read/write/verify
  93. *
  94. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  95. * functions. No further checking, as nand_base will always read/write
  96. * page aligned.
  97. */
  98. static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
  99. {
  100. struct nand_chip *this = mtdinfo->priv;
  101. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  102. uint32_t *p = (uint32_t *) buf;
  103. for (;len > 0; len -= 4)
  104. *p++ = in_be32((u32 *)(base + NDFC_DATA));
  105. }
  106. #ifndef CONFIG_NAND_SPL
  107. /*
  108. * Don't use these speedup functions in NAND boot image, since the image
  109. * has to fit into 4kByte.
  110. */
  111. static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  112. {
  113. struct nand_chip *this = mtdinfo->priv;
  114. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  115. uint32_t *p = (uint32_t *) buf;
  116. for (; len > 0; len -= 4)
  117. out_be32((u32 *)(base + NDFC_DATA), *p++);
  118. }
  119. static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  120. {
  121. struct nand_chip *this = mtdinfo->priv;
  122. ulong base = (ulong) this->IO_ADDR_W & 0xffffff00;
  123. uint32_t *p = (uint32_t *) buf;
  124. for (; len > 0; len -= 4)
  125. if (*p++ != in_be32((u32 *)(base + NDFC_DATA)))
  126. return -1;
  127. return 0;
  128. }
  129. /*
  130. * Read a byte from the NDFC.
  131. */
  132. static uint8_t ndfc_read_byte(struct mtd_info *mtd)
  133. {
  134. struct nand_chip *chip = mtd->priv;
  135. #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
  136. return (uint8_t) readw(chip->IO_ADDR_R);
  137. #else
  138. return readb(chip->IO_ADDR_R);
  139. #endif
  140. }
  141. #endif /* #ifndef CONFIG_NAND_SPL */
  142. void board_nand_select_device(struct nand_chip *nand, int chip)
  143. {
  144. /*
  145. * Don't use "chip" to address the NAND device,
  146. * generate the cs from the address where it is encoded.
  147. */
  148. ulong base = (ulong)nand->IO_ADDR_W & 0xffffff00;
  149. int cs = ndfc_cs[chip];
  150. /* Set NandFlash Core Configuration Register */
  151. /* 1 col x 2 rows */
  152. out_be32((u32 *)(base + NDFC_CCR), 0x00000000 | (cs << 24));
  153. out_be32((u32 *)(base + NDFC_BCFG0 + (cs << 2)), CONFIG_SYS_NAND_BCR);
  154. }
  155. static void ndfc_select_chip(struct mtd_info *mtd, int chip)
  156. {
  157. /*
  158. * Nothing to do here!
  159. */
  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 & 0xffffff00;
  165. static int chip = 0;
  166. /*
  167. * Save chip-select for this chip #
  168. */
  169. ndfc_cs[chip] = cs;
  170. /*
  171. * Select required NAND chip in NDFC
  172. */
  173. board_nand_select_device(nand, chip);
  174. nand->IO_ADDR_R = (void __iomem *)(base + NDFC_DATA);
  175. nand->IO_ADDR_W = (void __iomem *)(base + NDFC_DATA);
  176. nand->cmd_ctrl = ndfc_hwcontrol;
  177. nand->chip_delay = 50;
  178. nand->read_buf = ndfc_read_buf;
  179. nand->dev_ready = ndfc_dev_ready;
  180. nand->ecc.correct = nand_correct_data;
  181. nand->ecc.hwctl = ndfc_enable_hwecc;
  182. nand->ecc.calculate = ndfc_calculate_ecc;
  183. nand->ecc.mode = NAND_ECC_HW;
  184. nand->ecc.size = 256;
  185. nand->ecc.bytes = 3;
  186. nand->ecc.strength = 1;
  187. nand->select_chip = ndfc_select_chip;
  188. #ifdef CONFIG_SYS_NAND_BUSWIDTH_16BIT
  189. nand->options |= NAND_BUSWIDTH_16;
  190. #endif
  191. #ifndef CONFIG_NAND_SPL
  192. nand->write_buf = ndfc_write_buf;
  193. nand->verify_buf = ndfc_verify_buf;
  194. nand->read_byte = ndfc_read_byte;
  195. chip++;
  196. #else
  197. /*
  198. * Setup EBC (CS0 only right now)
  199. */
  200. mtebc(EBC0_CFG, CONFIG_SYS_NDFC_EBC0_CFG);
  201. mtebc(PB0CR, CONFIG_SYS_EBC_PB0CR);
  202. mtebc(PB0AP, CONFIG_SYS_EBC_PB0AP);
  203. #endif
  204. return 0;
  205. }