nand.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 defined(CONFIG_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, unsigned int ctrl)
  49. {
  50. struct nand_chip *this = mtd->priv;
  51. if (ctrl & NAND_CTRL_CHANGE) {
  52. if ( ctrl & NAND_CLE )
  53. hwctl |= 0x1;
  54. else
  55. hwctl &= ~0x1;
  56. if ( ctrl & NAND_ALE )
  57. hwctl |= 0x2;
  58. else
  59. hwctl &= ~0x2;
  60. if ( (ctrl & NAND_NCE) != NAND_NCE)
  61. writeb(0x00, &(pdnb3_ndfc->term));
  62. }
  63. if (cmd != NAND_CMD_NONE)
  64. writeb(cmd, this->IO_ADDR_W);
  65. }
  66. static u_char pdnb3_nand_read_byte(struct mtd_info *mtd)
  67. {
  68. return readb(&(pdnb3_ndfc->data));
  69. }
  70. static void pdnb3_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  71. {
  72. int i;
  73. for (i = 0; i < len; i++) {
  74. if (hwctl & 0x1)
  75. writeb(buf[i], &(pdnb3_ndfc->cmd));
  76. else if (hwctl & 0x2)
  77. writeb(buf[i], &(pdnb3_ndfc->addr));
  78. else
  79. writeb(buf[i], &(pdnb3_ndfc->data));
  80. }
  81. }
  82. static void pdnb3_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  83. {
  84. int i;
  85. if (len % 4) {
  86. for (i = 0; i < len; i++)
  87. buf[i] = readb(&(pdnb3_ndfc->data));
  88. } else {
  89. ulong *ptr = (ulong *)buf;
  90. int count = len >> 2;
  91. for (i = 0; i < count; i++)
  92. *ptr++ = readl(&(pdnb3_ndfc->data));
  93. }
  94. }
  95. static int pdnb3_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  96. {
  97. int i;
  98. for (i = 0; i < len; i++)
  99. if (buf[i] != readb(&(pdnb3_ndfc->data)))
  100. return i;
  101. return 0;
  102. }
  103. static int pdnb3_nand_dev_ready(struct mtd_info *mtd)
  104. {
  105. volatile u_char val;
  106. /*
  107. * Blocking read to wait for NAND to be ready
  108. */
  109. val = readb(&(pdnb3_ndfc->wait));
  110. /*
  111. * Return always true
  112. */
  113. return 1;
  114. }
  115. int board_nand_init(struct nand_chip *nand)
  116. {
  117. pdnb3_ndfc = (struct pdnb3_ndfc_regs *)CONFIG_SYS_NAND_BASE;
  118. nand->ecc.mode = NAND_ECC_SOFT;
  119. /* Set address of NAND IO lines (Using Linear Data Access Region) */
  120. nand->IO_ADDR_R = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4);
  121. nand->IO_ADDR_W = (void __iomem *) ((ulong) pdnb3_ndfc + 0x4);
  122. /* Reference hardware control function */
  123. nand->cmd_ctrl = pdnb3_nand_hwcontrol;
  124. nand->read_byte = pdnb3_nand_read_byte;
  125. nand->write_buf = pdnb3_nand_write_buf;
  126. nand->read_buf = pdnb3_nand_read_buf;
  127. nand->verify_buf = pdnb3_nand_verify_buf;
  128. nand->dev_ready = pdnb3_nand_dev_ready;
  129. return 0;
  130. }
  131. #endif