ndfc.c 5.5 KB

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