kmeter1_nand.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * (C) Copyright 2009
  3. * Heiko Schocher, DENX Software Engineering, hs@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. #include <nand.h>
  25. #include <asm/io.h>
  26. #define CONFIG_NAND_MODE_REG (void *)(CONFIG_SYS_NAND_BASE + 0x20000)
  27. #define CONFIG_NAND_DATA_REG (void *)(CONFIG_SYS_NAND_BASE + 0x30000)
  28. #define read_mode() in_8(CONFIG_NAND_MODE_REG)
  29. #define write_mode(val) out_8(CONFIG_NAND_MODE_REG, val)
  30. #define read_data() in_8(CONFIG_NAND_DATA_REG)
  31. #define write_data(val) out_8(CONFIG_NAND_DATA_REG, val)
  32. #define KPN_RDY2 (1 << 7)
  33. #define KPN_RDY1 (1 << 6)
  34. #define KPN_WPN (1 << 4)
  35. #define KPN_CE2N (1 << 3)
  36. #define KPN_CE1N (1 << 2)
  37. #define KPN_ALE (1 << 1)
  38. #define KPN_CLE (1 << 0)
  39. #define KPN_DEFAULT_CHIP_DELAY 50
  40. static int kpn_chip_ready(void)
  41. {
  42. if (read_mode() & KPN_RDY1)
  43. return 1;
  44. return 0;
  45. }
  46. static void kpn_wait_rdy(void)
  47. {
  48. int cnt = 1000000;
  49. while (--cnt && !kpn_chip_ready())
  50. udelay(1);
  51. if (!cnt)
  52. printf ("timeout while waiting for RDY\n");
  53. }
  54. static void kpn_nand_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
  55. {
  56. u8 reg_val = read_mode();
  57. if (ctrl & NAND_CTRL_CHANGE) {
  58. reg_val = reg_val & ~(KPN_ALE + KPN_CLE);
  59. if (ctrl & NAND_CLE)
  60. reg_val = reg_val | KPN_CLE;
  61. if (ctrl & NAND_ALE)
  62. reg_val = reg_val | KPN_ALE;
  63. if (ctrl & NAND_NCE)
  64. reg_val = reg_val & ~KPN_CE1N;
  65. else
  66. reg_val = reg_val | KPN_CE1N;
  67. write_mode(reg_val);
  68. }
  69. if (cmd != NAND_CMD_NONE)
  70. write_data(cmd);
  71. /* wait until flash is ready */
  72. kpn_wait_rdy();
  73. }
  74. static u_char kpn_nand_read_byte(struct mtd_info *mtd)
  75. {
  76. return read_data();
  77. }
  78. static void kpn_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  79. {
  80. int i;
  81. for (i = 0; i < len; i++) {
  82. write_data(buf[i]);
  83. kpn_wait_rdy();
  84. }
  85. }
  86. static void kpn_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  87. {
  88. int i;
  89. for (i = 0; i < len; i++)
  90. buf[i] = read_data();
  91. }
  92. static int kpn_nand_dev_ready(struct mtd_info *mtd)
  93. {
  94. kpn_wait_rdy();
  95. return 1;
  96. }
  97. int board_nand_init(struct nand_chip *nand)
  98. {
  99. #if defined(CONFIG_NAND_ECC_BCH)
  100. nand->ecc.mode = NAND_ECC_SOFT_BCH;
  101. #else
  102. nand->ecc.mode = NAND_ECC_SOFT;
  103. #endif
  104. /* Reference hardware control function */
  105. nand->cmd_ctrl = kpn_nand_hwcontrol;
  106. nand->read_byte = kpn_nand_read_byte;
  107. nand->write_buf = kpn_nand_write_buf;
  108. nand->read_buf = kpn_nand_read_buf;
  109. nand->dev_ready = kpn_nand_dev_ready;
  110. nand->chip_delay = KPN_DEFAULT_CHIP_DELAY;
  111. /* reset mode register */
  112. write_mode(KPN_CE1N + KPN_CE2N + KPN_WPN);
  113. return 0;
  114. }