nand.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. /*
  2. * (C) Copyright 2006
  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 <asm/io.h>
  25. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  26. #include <nand.h>
  27. #if 0
  28. #define HS_printf(fmt,arg...) \
  29. printf("HS %s %s: " fmt,__FILE__, __FUNCTION__, ##arg)
  30. #else
  31. #define HS_printf(fmt,arg...) \
  32. do { } while (0)
  33. #endif
  34. #if 0
  35. #define CPLD_REG uchar
  36. #else
  37. #define CPLD_REG u16
  38. #endif
  39. struct alpr_ndfc_regs {
  40. CPLD_REG cmd[4];
  41. CPLD_REG addr_wait;
  42. CPLD_REG term;
  43. CPLD_REG dummy;
  44. uchar dum2[2];
  45. CPLD_REG data;
  46. };
  47. static u8 hwctl;
  48. static struct alpr_ndfc_regs *alpr_ndfc;
  49. static int alpr_chip = 0;
  50. #if 1
  51. static int pdnb3_nand_dev_ready(struct mtd_info *mtd);
  52. #if 1
  53. static u_char alpr_read (void *padr) {
  54. return (u_char )*((u16 *)(padr));
  55. }
  56. #else
  57. static u_char alpr_read (void *padr) {
  58. u16 hilf;
  59. u_char ret = 0;
  60. hilf = *((u16 *)(padr));
  61. ret = hilf;
  62. printf("%p hilf: %x ret: %x\n", padr, hilf, ret);
  63. return ret;
  64. }
  65. #endif
  66. static void alpr_write (u_char byte, void *padr) {
  67. HS_printf("%p Byte: %x\n", padr, byte);
  68. *(volatile u16 *)padr = (u16)(byte);
  69. }
  70. #elif 0
  71. #define alpr_read(a) (*(volatile u16 *) (a))
  72. #define alpr_write(a, b) ((*(volatile u16 *) (a)) = (b))
  73. #else
  74. #define alpr_read(a) readw(a)
  75. #define alpr_write(a, b) writew(a, b)
  76. #endif
  77. /*
  78. * The ALPR has a NAND Flash Controller (NDFC) that handles all accesses to
  79. * the NAND devices. The NDFC has command, address and data registers that
  80. * when accessed will set up the NAND flash pins appropriately. We'll use the
  81. * hwcontrol function to save the configuration in a global variable.
  82. * We can then use this information in the read and write functions to
  83. * determine which NDFC register to access.
  84. *
  85. * There are 2 NAND devices on the board, a Hynix HY27US08561A (32 MByte).
  86. */
  87. static void pdnb3_nand_hwcontrol(struct mtd_info *mtd, int cmd)
  88. {
  89. HS_printf("cmd: %x\n", cmd);
  90. switch (cmd) {
  91. case NAND_CTL_SETCLE:
  92. hwctl |= 0x1;
  93. break;
  94. case NAND_CTL_CLRCLE:
  95. hwctl &= ~0x1;
  96. break;
  97. case NAND_CTL_SETALE:
  98. hwctl |= 0x2;
  99. break;
  100. case NAND_CTL_CLRALE:
  101. hwctl &= ~0x2;
  102. break;
  103. case NAND_CTL_SETNCE:
  104. break;
  105. case NAND_CTL_CLRNCE:
  106. alpr_write(0x00, &(alpr_ndfc->term));
  107. break;
  108. }
  109. }
  110. static void pdnb3_nand_write_byte(struct mtd_info *mtd, u_char byte)
  111. {
  112. HS_printf("hwctl: %x %x %x %x\n", hwctl, byte, &(alpr_ndfc->cmd[alpr_chip]), &(alpr_ndfc->addr_wait));
  113. if (hwctl & 0x1)
  114. alpr_write(byte, &(alpr_ndfc->cmd[alpr_chip]));
  115. else if (hwctl & 0x2) {
  116. alpr_write(byte, &(alpr_ndfc->addr_wait));
  117. } else
  118. alpr_write(byte, &(alpr_ndfc->data));
  119. }
  120. static u_char pdnb3_nand_read_byte(struct mtd_info *mtd)
  121. {
  122. return alpr_read(&(alpr_ndfc->data));
  123. }
  124. static void pdnb3_nand_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  125. {
  126. int i;
  127. /*printf("%s chip:%d hwctl:%x size:%d\n", __FUNCTION__, alpr_chip, hwctl, len);*/
  128. for (i = 0; i < len; i++) {
  129. if (hwctl & 0x1)
  130. alpr_write(buf[i], &(alpr_ndfc->cmd[alpr_chip]));
  131. else if (hwctl & 0x2) {
  132. alpr_write(buf[i], &(alpr_ndfc->addr_wait));
  133. } else {
  134. alpr_write(buf[i], &(alpr_ndfc->data));
  135. /*printf("i: %d\n", i);*/
  136. }
  137. }
  138. }
  139. static void pdnb3_nand_read_buf(struct mtd_info *mtd, u_char *buf, int len)
  140. {
  141. int i;
  142. for (i = 0; i < len; i++) {
  143. buf[i] = alpr_read(&(alpr_ndfc->data));
  144. }
  145. }
  146. static int pdnb3_nand_verify_buf(struct mtd_info *mtd, const u_char *buf, int len)
  147. {
  148. int i;
  149. for (i = 0; i < len; i++)
  150. if (buf[i] != alpr_read(&(alpr_ndfc->data)))
  151. return i;
  152. return 0;
  153. }
  154. static int pdnb3_nand_dev_ready(struct mtd_info *mtd)
  155. {
  156. #if 1
  157. volatile u_char val;
  158. /*printf("%s aufruf\n", __FUNCTION__);*/
  159. /*
  160. * Blocking read to wait for NAND to be ready
  161. */
  162. val = alpr_read(&(alpr_ndfc->addr_wait));
  163. /*
  164. * Return always true
  165. */
  166. return 1;
  167. #else
  168. u8 hwctl_org = hwctl;
  169. unsigned long timeo;
  170. u8 val;
  171. hwctl = 0x01;
  172. pdnb3_nand_write_byte (mtd, NAND_CMD_STATUS);
  173. hwctl = hwctl_org;
  174. reset_timer();
  175. while (1) {
  176. if (get_timer(0) > timeo) {
  177. printf("Timeout!");
  178. return 0;
  179. }
  180. val = pdnb3_nand_read_byte(mtd);
  181. /*printf("%s val: %x\n", __FUNCTION__, val);*/
  182. if (val & NAND_STATUS_READY)
  183. break;
  184. }
  185. return 1;
  186. #endif
  187. }
  188. static void alpr_select_chip(struct mtd_info *mtd, int chip)
  189. {
  190. alpr_chip = chip;
  191. }
  192. static int alpr_nand_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
  193. {
  194. unsigned long timeo;
  195. if (state == FL_ERASING)
  196. timeo = CFG_HZ * 400;
  197. else
  198. timeo = CFG_HZ * 20;
  199. if ((state == FL_ERASING) && (this->options & NAND_IS_AND))
  200. this->cmdfunc(mtd, NAND_CMD_STATUS_MULTI, -1, -1);
  201. else
  202. this->cmdfunc(mtd, NAND_CMD_STATUS, -1, -1);
  203. reset_timer();
  204. while (1) {
  205. if (get_timer(0) > timeo) {
  206. printf("Timeout!");
  207. return 0;
  208. }
  209. if (this->read_byte(mtd) & NAND_STATUS_READY)
  210. break;
  211. }
  212. return this->read_byte(mtd);
  213. }
  214. void board_nand_init(struct nand_chip *nand)
  215. {
  216. alpr_ndfc = (struct alpr_ndfc_regs *)CFG_NAND_BASE;
  217. nand->eccmode = NAND_ECC_SOFT;
  218. /* Set address of NAND IO lines (Using Linear Data Access Region) */
  219. nand->IO_ADDR_R = (void __iomem *) ((ulong) alpr_ndfc + 0x10);
  220. nand->IO_ADDR_W = (void __iomem *) ((ulong) alpr_ndfc + 0x10);
  221. /* Reference hardware control function */
  222. nand->hwcontrol = pdnb3_nand_hwcontrol;
  223. /* Set command delay time */
  224. nand->hwcontrol = pdnb3_nand_hwcontrol;
  225. nand->write_byte = pdnb3_nand_write_byte;
  226. nand->read_byte = pdnb3_nand_read_byte;
  227. nand->write_buf = pdnb3_nand_write_buf;
  228. nand->read_buf = pdnb3_nand_read_buf;
  229. nand->verify_buf = pdnb3_nand_verify_buf;
  230. nand->dev_ready = pdnb3_nand_dev_ready;
  231. nand->select_chip = alpr_select_chip;
  232. nand->waitfunc = alpr_nand_wait;
  233. }
  234. #endif