nand.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * (C) Copyright 2006
  3. * Heiko Schocher, DENX Software Engineering, hs@denx.de
  4. *
  5. * (C) Copyright 2006
  6. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  7. *
  8. * See file CREDITS for list of people who contributed to this
  9. * project.
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2 of
  14. * the License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  24. * MA 02111-1307 USA
  25. */
  26. #include <common.h>
  27. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  28. #include <asm/processor.h>
  29. #include <nand.h>
  30. struct alpr_ndfc_regs {
  31. u8 cmd[4];
  32. u8 addr_wait;
  33. u8 term;
  34. u8 dummy;
  35. u8 dummy2;
  36. u8 data;
  37. };
  38. static u8 hwctl;
  39. static struct alpr_ndfc_regs *alpr_ndfc = NULL;
  40. #define readb(addr) (u8)(*(volatile u8 *)(addr))
  41. #define writeb(d,addr) *(volatile u8 *)(addr) = ((u8)(d))
  42. /*
  43. * The ALPR has a NAND Flash Controller (NDFC) that handles all accesses to
  44. * the NAND devices. The NDFC has command, address and data registers that
  45. * when accessed will set up the NAND flash pins appropriately. We'll use the
  46. * hwcontrol function to save the configuration in a global variable.
  47. * We can then use this information in the read and write functions to
  48. * determine which NDFC register to access.
  49. *
  50. * There are 2 NAND devices on the board, a Hynix HY27US08561A (1 GByte).
  51. */
  52. static void alpr_nand_hwcontrol(struct mtd_info *mtd, int cmd)
  53. {
  54. switch (cmd) {
  55. case NAND_CTL_SETCLE:
  56. hwctl |= 0x1;
  57. break;
  58. case NAND_CTL_CLRCLE:
  59. hwctl &= ~0x1;
  60. break;
  61. case NAND_CTL_SETALE:
  62. hwctl |= 0x2;
  63. break;
  64. case NAND_CTL_CLRALE:
  65. hwctl &= ~0x2;
  66. break;
  67. case NAND_CTL_SETNCE:
  68. break;
  69. case NAND_CTL_CLRNCE:
  70. writeb(0x00, &(alpr_ndfc->term));
  71. break;
  72. }
  73. }
  74. static void alpr_nand_write_byte(struct mtd_info *mtd, u_char byte)
  75. {
  76. struct nand_chip *nand = mtd->priv;
  77. if (hwctl & 0x1)
  78. /*
  79. * IO_ADDR_W used as CMD[i] reg to support multiple NAND
  80. * chips.
  81. */
  82. writeb(byte, nand->IO_ADDR_W);
  83. else if (hwctl & 0x2) {
  84. writeb(byte, &(alpr_ndfc->addr_wait));
  85. } else
  86. writeb(byte, &(alpr_ndfc->data));
  87. }
  88. static u_char alpr_nand_read_byte(struct mtd_info *mtd)
  89. {
  90. return readb(&(alpr_ndfc->data));
  91. }
  92. static void alpr_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  93. {
  94. struct nand_chip *nand = mtd->priv;
  95. int i;
  96. for (i = 0; i < len; i++) {
  97. if (hwctl & 0x1)
  98. /*
  99. * IO_ADDR_W used as CMD[i] reg to support multiple NAND
  100. * chips.
  101. */
  102. writeb(buf[i], nand->IO_ADDR_W);
  103. else if (hwctl & 0x2)
  104. writeb(buf[i], &(alpr_ndfc->addr_wait));
  105. else
  106. writeb(buf[i], &(alpr_ndfc->data));
  107. }
  108. }
  109. static void alpr_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  110. {
  111. int i;
  112. for (i = 0; i < len; i++) {
  113. buf[i] = readb(&(alpr_ndfc->data));
  114. }
  115. }
  116. static int alpr_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  117. {
  118. int i;
  119. for (i = 0; i < len; i++)
  120. if (buf[i] != readb(&(alpr_ndfc->data)))
  121. return i;
  122. return 0;
  123. }
  124. static int alpr_nand_dev_ready(struct mtd_info *mtd)
  125. {
  126. volatile u_char val;
  127. /*
  128. * Blocking read to wait for NAND to be ready
  129. */
  130. val = readb(&(alpr_ndfc->addr_wait));
  131. /*
  132. * Return always true
  133. */
  134. return 1;
  135. }
  136. void board_nand_init(struct nand_chip *nand)
  137. {
  138. alpr_ndfc = (struct alpr_ndfc_regs *)CFG_NAND_BASE;
  139. nand->eccmode = NAND_ECC_SOFT;
  140. /* Reference hardware control function */
  141. nand->hwcontrol = alpr_nand_hwcontrol;
  142. /* Set command delay time */
  143. nand->write_byte = alpr_nand_write_byte;
  144. nand->read_byte = alpr_nand_read_byte;
  145. nand->write_buf = alpr_nand_write_buf;
  146. nand->read_buf = alpr_nand_read_buf;
  147. nand->verify_buf = alpr_nand_verify_buf;
  148. nand->dev_ready = alpr_nand_dev_ready;
  149. }
  150. #endif