ndfc.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * Overview:
  3. * Platform independend driver for NDFC (NanD Flash Controller)
  4. * integrated into EP440 cores
  5. *
  6. * (C) Copyright 2006
  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 <asm/processor.h>
  39. #include <ppc4xx.h>
  40. static u8 hwctl = 0;
  41. static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
  42. {
  43. switch (cmd) {
  44. case NAND_CTL_SETCLE:
  45. hwctl |= 0x1;
  46. break;
  47. case NAND_CTL_CLRCLE:
  48. hwctl &= ~0x1;
  49. break;
  50. case NAND_CTL_SETALE:
  51. hwctl |= 0x2;
  52. break;
  53. case NAND_CTL_CLRALE:
  54. hwctl &= ~0x2;
  55. break;
  56. }
  57. }
  58. static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte)
  59. {
  60. struct nand_chip *this = mtdinfo->priv;
  61. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  62. if (hwctl & 0x1)
  63. out8(base + NDFC_CMD, byte);
  64. else if (hwctl & 0x2)
  65. out8(base + NDFC_ALE, byte);
  66. else
  67. out8(base + NDFC_DATA, byte);
  68. }
  69. static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
  70. {
  71. struct nand_chip *this = mtdinfo->priv;
  72. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  73. return (in8(base + NDFC_DATA));
  74. }
  75. static int ndfc_dev_ready(struct mtd_info *mtdinfo)
  76. {
  77. struct nand_chip *this = mtdinfo->priv;
  78. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  79. while (!(in32(base + NDFC_STAT) & NDFC_STAT_IS_READY))
  80. ;
  81. return 1;
  82. }
  83. #ifndef CONFIG_NAND_SPL
  84. /*
  85. * Don't use these speedup functions in NAND boot image, since the image
  86. * has to fit into 4kByte.
  87. */
  88. /*
  89. * Speedups for buffer read/write/verify
  90. *
  91. * NDFC allows 32bit read/write of data. So we can speed up the buffer
  92. * functions. No further checking, as nand_base will always read/write
  93. * page aligned.
  94. */
  95. static void ndfc_read_buf(struct mtd_info *mtdinfo, uint8_t *buf, int len)
  96. {
  97. struct nand_chip *this = mtdinfo->priv;
  98. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  99. uint32_t *p = (uint32_t *) buf;
  100. for (;len > 0; len -= 4)
  101. *p++ = in32(base + NDFC_DATA);
  102. }
  103. static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  104. {
  105. struct nand_chip *this = mtdinfo->priv;
  106. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  107. uint32_t *p = (uint32_t *) buf;
  108. for (; len > 0; len -= 4)
  109. out32(base + NDFC_DATA, *p++);
  110. }
  111. static int ndfc_verify_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 & 0xfffffffc;
  115. uint32_t *p = (uint32_t *) buf;
  116. for (; len > 0; len -= 4)
  117. if (*p++ != in32(base + NDFC_DATA))
  118. return -1;
  119. return 0;
  120. }
  121. #endif /* #ifndef CONFIG_NAND_SPL */
  122. void board_nand_select_device(struct nand_chip *nand, int chip)
  123. {
  124. /*
  125. * Don't use "chip" to address the NAND device,
  126. * generate the cs from the address where it is encoded.
  127. */
  128. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  129. ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
  130. /* Set NandFlash Core Configuration Register */
  131. /* 1col x 2 rows */
  132. out32(base + NDFC_CCR, 0x00000000 | (cs << 24));
  133. }
  134. int board_nand_init(struct nand_chip *nand)
  135. {
  136. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  137. ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
  138. nand->eccmode = NAND_ECC_SOFT;
  139. nand->hwcontrol = ndfc_hwcontrol;
  140. nand->read_byte = ndfc_read_byte;
  141. nand->write_byte = ndfc_write_byte;
  142. nand->dev_ready = ndfc_dev_ready;
  143. #ifndef CONFIG_NAND_SPL
  144. nand->write_buf = ndfc_write_buf;
  145. nand->read_buf = ndfc_read_buf;
  146. nand->verify_buf = ndfc_verify_buf;
  147. #else
  148. /*
  149. * Setup EBC (CS0 only right now)
  150. */
  151. mtebc(EBC0_CFG, 0xb8400000);
  152. mtebc(pb0cr, CFG_EBC_PB0CR);
  153. mtebc(pb0ap, CFG_EBC_PB0AP);
  154. #endif
  155. /*
  156. * Select required NAND chip in NDFC
  157. */
  158. board_nand_select_device(nand, cs);
  159. out32(base + NDFC_BCFG0 + (cs << 2), 0x80002222);
  160. return 0;
  161. }
  162. #endif