nand.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * (C) Copyright 2006
  3. * Stefan Roese, DENX Software Engineering, sr@denx.de.
  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 (CONFIG_COMMANDS & CFG_CMD_NAND)
  25. #include <nand.h>
  26. struct pdnb3_ndfc_regs {
  27. uchar cmd;
  28. uchar wait;
  29. uchar addr;
  30. uchar term;
  31. uchar data;
  32. };
  33. static u8 hwctl;
  34. static struct pdnb3_ndfc_regs *pdnb3_ndfc;
  35. #define readb(addr) *(volatile u_char *)(addr)
  36. #define readl(addr) *(volatile u_long *)(addr)
  37. #define writeb(d,addr) *(volatile u_char *)(addr) = (d)
  38. /*
  39. * The PDNB3 has a NAND Flash Controller (NDFC) that handles all accesses to
  40. * the NAND devices. The NDFC has command, address and data registers that
  41. * when accessed will set up the NAND flash pins appropriately. We'll use the
  42. * hwcontrol function to save the configuration in a global variable.
  43. * We can then use this information in the read and write functions to
  44. * determine which NDFC register to access.
  45. *
  46. * There is one NAND devices on the board, a Hynix HY27US08561A (32 MByte).
  47. */
  48. static void pdnb3_nand_hwcontrol(struct mtd_info *mtd, int cmd)
  49. {
  50. switch (cmd) {
  51. case NAND_CTL_SETCLE:
  52. hwctl |= 0x1;
  53. break;
  54. case NAND_CTL_CLRCLE:
  55. hwctl &= ~0x1;
  56. break;
  57. case NAND_CTL_SETALE:
  58. hwctl |= 0x2;
  59. break;
  60. case NAND_CTL_CLRALE:
  61. hwctl &= ~0x2;
  62. break;
  63. case NAND_CTL_SETNCE:
  64. break;
  65. case NAND_CTL_CLRNCE:
  66. writeb(0x00, &(pdnb3_ndfc->term));
  67. break;
  68. }
  69. }
  70. static void pdnb3_nand_write_byte(struct mtd_info *mtd, u_char byte)
  71. {
  72. if (hwctl & 0x1)
  73. writeb(byte, &(pdnb3_ndfc->cmd));
  74. else if (hwctl & 0x2)
  75. writeb(byte, &(pdnb3_ndfc->addr));
  76. else
  77. writeb(byte, &(pdnb3_ndfc->data));
  78. }
  79. static u_char pdnb3_nand_read_byte(struct mtd_info *mtd)
  80. {
  81. return readb(&(pdnb3_ndfc->data));
  82. }
  83. static void pdnb3_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  84. {
  85. int i;
  86. for (i = 0; i < len; i++) {
  87. if (hwctl & 0x1)
  88. writeb(buf[i], &(pdnb3_ndfc->cmd));
  89. else if (hwctl & 0x2)
  90. writeb(buf[i], &(pdnb3_ndfc->addr));
  91. else
  92. writeb(buf[i], &(pdnb3_ndfc->data));
  93. }
  94. }
  95. static void pdnb3_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  96. {
  97. int i;
  98. if (len % 4) {
  99. for (i = 0; i < len; i++)
  100. buf[i] = readb(&(pdnb3_ndfc->data));
  101. } else {
  102. ulong *ptr = (ulong *)buf;
  103. int count = len >> 2;
  104. for (i = 0; i < count; i++)
  105. *ptr++ = readl(&(pdnb3_ndfc->data));
  106. }
  107. }
  108. static int pdnb3_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  109. {
  110. int i;
  111. for (i = 0; i < len; i++)
  112. if (buf[i] != readb(&(pdnb3_ndfc->data)))
  113. return i;
  114. return 0;
  115. }
  116. static int pdnb3_nand_dev_ready(struct mtd_info *mtd)
  117. {
  118. volatile u_char val;
  119. /*
  120. * Blocking read to wait for NAND to be ready
  121. */
  122. val = readb(&(pdnb3_ndfc->wait));
  123. /*
  124. * Return always true
  125. */
  126. return 1;
  127. }
  128. void board_nand_init(struct nand_chip *nand)
  129. {
  130. pdnb3_ndfc = (struct pdnb3_ndfc_regs *)CFG_NAND_BASE;
  131. nand->eccmode = NAND_ECC_SOFT;
  132. /* Set address of NAND IO lines (Using Linear Data Access Region) */
  133. nand->IO_ADDR_R = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4);
  134. nand->IO_ADDR_W = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4);
  135. /* Reference hardware control function */
  136. nand->hwcontrol = pdnb3_nand_hwcontrol;
  137. /* Set command delay time */
  138. nand->hwcontrol = pdnb3_nand_hwcontrol;
  139. nand->write_byte = pdnb3_nand_write_byte;
  140. nand->read_byte = pdnb3_nand_read_byte;
  141. nand->write_buf = pdnb3_nand_write_buf;
  142. nand->read_buf = pdnb3_nand_read_buf;
  143. nand->verify_buf = pdnb3_nand_verify_buf;
  144. nand->dev_ready = pdnb3_nand_dev_ready;
  145. }
  146. #endif