nand.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /*
  2. * (C) Copyright 2008
  3. * Sergei Poselenov, Emcraft Systems, sposelenov@emcraft.com.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #if defined(CONFIG_SYS_NAND_BASE)
  25. #include <nand.h>
  26. #include <asm/errno.h>
  27. #include <asm/io.h>
  28. static int state;
  29. static void nand_write_byte(struct mtd_info *mtd, u_char byte);
  30. static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len);
  31. static u_char nand_read_byte(struct mtd_info *mtd);
  32. static u16 nand_read_word(struct mtd_info *mtd);
  33. static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len);
  34. static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len);
  35. static int nand_device_ready(struct mtd_info *mtdinfo);
  36. #define FPGA_NAND_CMD_MASK (0x7 << 28)
  37. #define FPGA_NAND_CMD_COMMAND (0x0 << 28)
  38. #define FPGA_NAND_CMD_ADDR (0x1 << 28)
  39. #define FPGA_NAND_CMD_READ (0x2 << 28)
  40. #define FPGA_NAND_CMD_WRITE (0x3 << 28)
  41. #define FPGA_NAND_BUSY (0x1 << 15)
  42. #define FPGA_NAND_ENABLE (0x1 << 31)
  43. #define FPGA_NAND_DATA_SHIFT 16
  44. /**
  45. * nand_write_byte - write one byte to the chip
  46. * @mtd: MTD device structure
  47. * @byte: pointer to data byte to write
  48. */
  49. static void nand_write_byte(struct mtd_info *mtd, u_char byte)
  50. {
  51. nand_write_buf(mtd, (const uchar *)&byte, sizeof(byte));
  52. }
  53. /**
  54. * nand_write_buf - write buffer to chip
  55. * @mtd: MTD device structure
  56. * @buf: data buffer
  57. * @len: number of bytes to write
  58. */
  59. static void nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  60. {
  61. int i;
  62. struct nand_chip *this = mtd->priv;
  63. for (i = 0; i < len; i++) {
  64. out_be32(this->IO_ADDR_W,
  65. state | (buf[i] << FPGA_NAND_DATA_SHIFT));
  66. }
  67. }
  68. /**
  69. * nand_read_byte - read one byte from the chip
  70. * @mtd: MTD device structure
  71. */
  72. static u_char nand_read_byte(struct mtd_info *mtd)
  73. {
  74. u8 byte;
  75. nand_read_buf(mtd, (uchar *)&byte, sizeof(byte));
  76. return byte;
  77. }
  78. /**
  79. * nand_read_word - read one word from the chip
  80. * @mtd: MTD device structure
  81. */
  82. static u16 nand_read_word(struct mtd_info *mtd)
  83. {
  84. u16 word;
  85. nand_read_buf(mtd, (uchar *)&word, sizeof(word));
  86. return word;
  87. }
  88. /**
  89. * nand_read_buf - read chip data into buffer
  90. * @mtd: MTD device structure
  91. * @buf: buffer to store date
  92. * @len: number of bytes to read
  93. */
  94. static void nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  95. {
  96. int i;
  97. struct nand_chip *this = mtd->priv;
  98. int val;
  99. val = (state & FPGA_NAND_ENABLE) | FPGA_NAND_CMD_READ;
  100. out_be32(this->IO_ADDR_W, val);
  101. for (i = 0; i < len; i++) {
  102. buf[i] = (in_be32(this->IO_ADDR_R) >> FPGA_NAND_DATA_SHIFT) & 0xff;
  103. }
  104. }
  105. /**
  106. * nand_verify_buf - Verify chip data against buffer
  107. * @mtd: MTD device structure
  108. * @buf: buffer containing the data to compare
  109. * @len: number of bytes to compare
  110. */
  111. static int nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  112. {
  113. int i;
  114. for (i = 0; i < len; i++) {
  115. if (buf[i] != nand_read_byte(mtd));
  116. return -EFAULT;
  117. }
  118. return 0;
  119. }
  120. /**
  121. * nand_device_ready - Check the NAND device is ready for next command.
  122. * @mtd: MTD device structure
  123. */
  124. static int nand_device_ready(struct mtd_info *mtdinfo)
  125. {
  126. struct nand_chip *this = mtdinfo->priv;
  127. if (in_be32(this->IO_ADDR_W) & FPGA_NAND_BUSY)
  128. return 0; /* busy */
  129. return 1;
  130. }
  131. /**
  132. * nand_hwcontrol - NAND control functions wrapper.
  133. * @mtd: MTD device structure
  134. * @cmd: Command
  135. */
  136. static void nand_hwcontrol(struct mtd_info *mtdinfo, int cmd, unsigned int ctrl)
  137. {
  138. if (ctrl & NAND_CTRL_CHANGE) {
  139. state &= ~(FPGA_NAND_CMD_MASK | FPGA_NAND_ENABLE);
  140. switch (ctrl & (NAND_ALE | NAND_CLE)) {
  141. case 0:
  142. state |= FPGA_NAND_CMD_WRITE;
  143. break;
  144. case NAND_ALE:
  145. state |= FPGA_NAND_CMD_ADDR;
  146. break;
  147. case NAND_CLE:
  148. state |= FPGA_NAND_CMD_COMMAND;
  149. break;
  150. default:
  151. printf("%s: unknown ctrl %#x\n", __FUNCTION__, ctrl);
  152. }
  153. if (ctrl & NAND_NCE)
  154. state |= FPGA_NAND_ENABLE;
  155. }
  156. if (cmd != NAND_CMD_NONE)
  157. nand_write_byte(mtdinfo, cmd);
  158. }
  159. int board_nand_init(struct nand_chip *nand)
  160. {
  161. nand->cmd_ctrl = nand_hwcontrol;
  162. nand->ecc.mode = NAND_ECC_SOFT;
  163. nand->dev_ready = nand_device_ready;
  164. nand->read_byte = nand_read_byte;
  165. nand->read_word = nand_read_word;
  166. nand->write_buf = nand_write_buf;
  167. nand->read_buf = nand_read_buf;
  168. nand->verify_buf = nand_verify_buf;
  169. return 0;
  170. }
  171. #endif