nand.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 defined(CONFIG_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, unsigned int ctrl)
  53. {
  54. struct nand_chip *this = mtd->priv;
  55. if (ctrl & NAND_CTRL_CHANGE) {
  56. if ( ctrl & NAND_CLE )
  57. hwctl |= 0x1;
  58. else
  59. hwctl &= ~0x1;
  60. if ( ctrl & NAND_ALE )
  61. hwctl |= 0x2;
  62. else
  63. hwctl &= ~0x2;
  64. if ( (ctrl & NAND_NCE) != NAND_NCE)
  65. writeb(0x00, &(alpr_ndfc->term));
  66. }
  67. if (cmd != NAND_CMD_NONE)
  68. writeb(cmd, this->IO_ADDR_W);
  69. }
  70. static u_char alpr_nand_read_byte(struct mtd_info *mtd)
  71. {
  72. return readb(&(alpr_ndfc->data));
  73. }
  74. static void alpr_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  75. {
  76. struct nand_chip *nand = mtd->priv;
  77. int i;
  78. for (i = 0; i < len; i++) {
  79. if (hwctl & 0x1)
  80. /*
  81. * IO_ADDR_W used as CMD[i] reg to support multiple NAND
  82. * chips.
  83. */
  84. writeb(buf[i], nand->IO_ADDR_W);
  85. else if (hwctl & 0x2)
  86. writeb(buf[i], &(alpr_ndfc->addr_wait));
  87. else
  88. writeb(buf[i], &(alpr_ndfc->data));
  89. }
  90. }
  91. static void alpr_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  92. {
  93. int i;
  94. for (i = 0; i < len; i++) {
  95. buf[i] = readb(&(alpr_ndfc->data));
  96. }
  97. }
  98. static int alpr_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  99. {
  100. int i;
  101. for (i = 0; i < len; i++)
  102. if (buf[i] != readb(&(alpr_ndfc->data)))
  103. return i;
  104. return 0;
  105. }
  106. static int alpr_nand_dev_ready(struct mtd_info *mtd)
  107. {
  108. volatile u_char val;
  109. /*
  110. * Blocking read to wait for NAND to be ready
  111. */
  112. val = readb(&(alpr_ndfc->addr_wait));
  113. /*
  114. * Return always true
  115. */
  116. return 1;
  117. }
  118. int board_nand_init(struct nand_chip *nand)
  119. {
  120. alpr_ndfc = (struct alpr_ndfc_regs *)CFG_NAND_BASE;
  121. nand->ecc.mode = NAND_ECC_SOFT;
  122. /* Reference hardware control function */
  123. nand->cmd_ctrl = alpr_nand_hwcontrol;
  124. nand->read_byte = alpr_nand_read_byte;
  125. nand->write_buf = alpr_nand_write_buf;
  126. nand->read_buf = alpr_nand_read_buf;
  127. nand->verify_buf = alpr_nand_verify_buf;
  128. nand->dev_ready = alpr_nand_dev_ready;
  129. return 0;
  130. }
  131. #endif