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. #include <nand.h>
  36. #include <linux/mtd/ndfc.h>
  37. #include <asm/processor.h>
  38. #include <ppc440.h>
  39. static u8 hwctl = 0;
  40. static void ndfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
  41. {
  42. switch (cmd) {
  43. case NAND_CTL_SETCLE:
  44. hwctl |= 0x1;
  45. break;
  46. case NAND_CTL_CLRCLE:
  47. hwctl &= ~0x1;
  48. break;
  49. case NAND_CTL_SETALE:
  50. hwctl |= 0x2;
  51. break;
  52. case NAND_CTL_CLRALE:
  53. hwctl &= ~0x2;
  54. break;
  55. }
  56. }
  57. static void ndfc_write_byte(struct mtd_info *mtdinfo, u_char byte)
  58. {
  59. struct nand_chip *this = mtdinfo->priv;
  60. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  61. if (hwctl & 0x1)
  62. out8(base + NDFC_CMD, byte);
  63. else if (hwctl & 0x2)
  64. out8(base + NDFC_ALE, byte);
  65. else
  66. out8(base + NDFC_DATA, byte);
  67. }
  68. static u_char ndfc_read_byte(struct mtd_info *mtdinfo)
  69. {
  70. struct nand_chip *this = mtdinfo->priv;
  71. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  72. return (in8(base + NDFC_DATA));
  73. }
  74. static int ndfc_dev_ready(struct mtd_info *mtdinfo)
  75. {
  76. struct nand_chip *this = mtdinfo->priv;
  77. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  78. while (!(in32(base + NDFC_STAT) & NDFC_STAT_IS_READY))
  79. ;
  80. return 1;
  81. }
  82. #ifndef CONFIG_NAND_SPL
  83. /*
  84. * Don't use these speedup functions in NAND boot image, since the image
  85. * has to fit into 4kByte.
  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 & 0xfffffffc;
  98. uint32_t *p = (uint32_t *) buf;
  99. for (;len > 0; len -= 4)
  100. *p++ = in32(base + NDFC_DATA);
  101. }
  102. static void ndfc_write_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  103. {
  104. struct nand_chip *this = mtdinfo->priv;
  105. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  106. uint32_t *p = (uint32_t *) buf;
  107. for (; len > 0; len -= 4)
  108. out32(base + NDFC_DATA, *p++);
  109. }
  110. static int ndfc_verify_buf(struct mtd_info *mtdinfo, const uint8_t *buf, int len)
  111. {
  112. struct nand_chip *this = mtdinfo->priv;
  113. ulong base = (ulong) this->IO_ADDR_W & 0xfffffffc;
  114. uint32_t *p = (uint32_t *) buf;
  115. for (; len > 0; len -= 4)
  116. if (*p++ != in32(base + NDFC_DATA))
  117. return -1;
  118. return 0;
  119. }
  120. #endif /* #ifndef CONFIG_NAND_SPL */
  121. void board_nand_select_device(struct nand_chip *nand, int chip)
  122. {
  123. /*
  124. * Don't use "chip" to address the NAND device,
  125. * generate the cs from the address where it is encoded.
  126. */
  127. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  128. ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
  129. /* Set NandFlash Core Configuration Register */
  130. /* 1col x 2 rows */
  131. out32(base + NDFC_CCR, 0x00000000 | (cs << 24));
  132. }
  133. int board_nand_init(struct nand_chip *nand)
  134. {
  135. int cs = (ulong)nand->IO_ADDR_W & 0x00000003;
  136. ulong base = (ulong)nand->IO_ADDR_W & 0xfffffffc;
  137. nand->eccmode = NAND_ECC_SOFT;
  138. nand->hwcontrol = ndfc_hwcontrol;
  139. nand->read_byte = ndfc_read_byte;
  140. nand->write_byte = ndfc_write_byte;
  141. nand->dev_ready = ndfc_dev_ready;
  142. #ifndef CONFIG_NAND_SPL
  143. nand->write_buf = ndfc_write_buf;
  144. nand->read_buf = ndfc_read_buf;
  145. nand->verify_buf = ndfc_verify_buf;
  146. #else
  147. /*
  148. * Setup EBC (CS0 only right now)
  149. */
  150. mtdcr(ebccfga, xbcfg);
  151. mtdcr(ebccfgd, 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