nand.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /*
  2. * (C) Copyright 2006 DENX Software Engineering
  3. *
  4. * See file CREDITS for list of people who contributed to this
  5. * project.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License as
  9. * published by the Free Software Foundation; either version 2 of
  10. * the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. */
  22. #include <common.h>
  23. #if (CONFIG_COMMANDS & CFG_CMD_NAND)
  24. #ifdef CONFIG_NEW_NAND_CODE
  25. #include <nand.h>
  26. #include <asm/arch/pxa-regs.h>
  27. /*
  28. * hardware specific access to control-lines
  29. * function borrowed from Linux 2.6 (drivers/mtd/nand/ppchameleonevb.c)
  30. */
  31. static void delta_hwcontrol(struct mtd_info *mtdinfo, int cmd)
  32. {
  33. #if 0
  34. struct nand_chip *this = mtdinfo->priv;
  35. ulong base = (ulong) this->IO_ADDR_W;
  36. switch(cmd) {
  37. case NAND_CTL_SETCLE:
  38. MACRO_NAND_CTL_SETCLE((unsigned long)base);
  39. break;
  40. case NAND_CTL_CLRCLE:
  41. MACRO_NAND_CTL_CLRCLE((unsigned long)base);
  42. break;
  43. case NAND_CTL_SETALE:
  44. MACRO_NAND_CTL_SETALE((unsigned long)base);
  45. break;
  46. case NAND_CTL_CLRALE:
  47. MACRO_NAND_CTL_CLRALE((unsigned long)base);
  48. break;
  49. case NAND_CTL_SETNCE:
  50. MACRO_NAND_ENABLE_CE((unsigned long)base);
  51. break;
  52. case NAND_CTL_CLRNCE:
  53. MACRO_NAND_DISABLE_CE((unsigned long)base);
  54. break;
  55. }
  56. #endif
  57. }
  58. /* read device ready pin */
  59. static int delta_device_ready(struct mtd_info *mtdinfo)
  60. {
  61. if(NDSR & NDSR_RDY)
  62. return 1;
  63. else
  64. return 0;
  65. #if 0
  66. struct nand_chip *this = mtdinfo->priv;
  67. ulong rb_gpio_pin;
  68. /* use the base addr to find out which chip are we dealing with */
  69. switch((ulong) this->IO_ADDR_W) {
  70. case CFG_NAND0_BASE:
  71. rb_gpio_pin = CFG_NAND0_RDY;
  72. break;
  73. case CFG_NAND1_BASE:
  74. rb_gpio_pin = CFG_NAND1_RDY;
  75. break;
  76. default: /* this should never happen */
  77. return 0;
  78. break;
  79. }
  80. if (in32(GPIO0_IR) & rb_gpio_pin)
  81. return 1;
  82. #endif
  83. return 0;
  84. }
  85. static u_char delta_read_byte(struct mtd_info *mtd)
  86. {
  87. /* struct nand_chip *this = mtd->priv; */
  88. unsigned long tmp;
  89. /* wait for read request */
  90. while(1) {
  91. if(NDSR & NDSR_RDDREQ) {
  92. NDSR |= NDSR_RDDREQ;
  93. break;
  94. }
  95. }
  96. tmp = NDDB;
  97. printk("delta_read_byte: 0x%x.\n", tmp);
  98. return (u_char) tmp;
  99. }
  100. /* this is really monahans, not board specific ... */
  101. static void delta_cmdfunc(struct mtd_info *mtd, unsigned command,
  102. int column, int page_addr)
  103. {
  104. /* register struct nand_chip *this = mtd->priv; */
  105. unsigned long ndcb0=0, ndcb1=0, ndcb2=0;
  106. uchar command2;
  107. /* Clear NDSR */
  108. NDSR = 0xFFF;
  109. /* apparently NDCR[NDRUN] needs to be set before writing to NDCBx */
  110. NDCR |= NDCR_ND_RUN;
  111. /* wait for write command request
  112. * hmm, might be nice if this could time-out. mk@tbd
  113. */
  114. while(1) {
  115. if(NDSR & NDSR_WRCMDREQ) {
  116. NDSR |= NDSR_WRCMDREQ; /* Ack */
  117. break;
  118. }
  119. }
  120. /* if command is a double byte cmd, we set bit double cmd bit 19 */
  121. command2 = (command>>8) & 0xFF;
  122. ndcb0 = command | ((command2 ? 1 : 0) << 19);
  123. switch (command) {
  124. case NAND_CMD_READID:
  125. printk("delta_cmdfunc: NAND_CMD_READID.\n");
  126. ndcb0 |= ((3 << 21) | (2 << 16));
  127. break;
  128. case NAND_CMD_PAGEPROG:
  129. case NAND_CMD_ERASE1:
  130. case NAND_CMD_ERASE2:
  131. case NAND_CMD_SEQIN:
  132. case NAND_CMD_STATUS:
  133. return;
  134. case NAND_CMD_RESET:
  135. return;
  136. default:
  137. printk("delta_cmdfunc: error, unkown command issued.\n");
  138. return;
  139. }
  140. NDCB0 = ndcb0;
  141. NDCB1 = ndcb1;
  142. NDCB2 = ndcb2;
  143. }
  144. /*
  145. * Board-specific NAND initialization. The following members of the
  146. * argument are board-specific (per include/linux/mtd/nand_new.h):
  147. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  148. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  149. * - hwcontrol: hardwarespecific function for accesing control-lines
  150. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  151. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  152. * only be provided if a hardware ECC is available
  153. * - eccmode: mode of ecc, see defines
  154. * - chip_delay: chip dependent delay for transfering data from array to
  155. * read regs (tR)
  156. * - options: various chip options. They can partly be set to inform
  157. * nand_scan about special functionality. See the defines for further
  158. * explanation
  159. * Members with a "?" were not set in the merged testing-NAND branch,
  160. * so they are not set here either.
  161. */
  162. void board_nand_init(struct nand_chip *nand)
  163. {
  164. unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
  165. /* set up GPIO Control Registers */
  166. /* turn on the NAND Controller Clock (104 MHz @ D0) */
  167. CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
  168. /* NAND Timing Parameters (in ns) */
  169. #define NAND_TIMING_tCH 10
  170. #define NAND_TIMING_tCS 0
  171. #define NAND_TIMING_tWH 20
  172. #define NAND_TIMING_tWP 40
  173. #define NAND_TIMING_tRH 20
  174. #define NAND_TIMING_tRP 40
  175. #define NAND_TIMING_tR 11123
  176. #define NAND_TIMING_tWHR 110
  177. #define NAND_TIMING_tAR 10
  178. /* Maximum values for NAND Interface Timing Registers in DFC clock
  179. * periods */
  180. #define DFC_MAX_tCH 7
  181. #define DFC_MAX_tCS 7
  182. #define DFC_MAX_tWH 7
  183. #define DFC_MAX_tWP 7
  184. #define DFC_MAX_tRH 7
  185. #define DFC_MAX_tRP 15
  186. #define DFC_MAX_tR 65535
  187. #define DFC_MAX_tWHR 15
  188. #define DFC_MAX_tAR 15
  189. #define DFC_CLOCK 104 /* DFC Clock is 104 MHz */
  190. #define DFC_CLK_PER_US DFC_CLOCK/1000 /* clock period in ns */
  191. #define MIN(x, y) ((x < y) ? x : y)
  192. tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
  193. DFC_MAX_tCH);
  194. tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
  195. DFC_MAX_tCS);
  196. tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
  197. DFC_MAX_tWH);
  198. tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
  199. DFC_MAX_tWP);
  200. tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
  201. DFC_MAX_tRH);
  202. tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
  203. DFC_MAX_tRP);
  204. tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
  205. DFC_MAX_tR);
  206. tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
  207. DFC_MAX_tWHR);
  208. tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
  209. DFC_MAX_tAR);
  210. /* tRP value is split in the register */
  211. if(tRP & (1 << 4)) {
  212. tRP_high = 1;
  213. tRP &= ~(1 << 4);
  214. } else {
  215. tRP_high = 0;
  216. }
  217. NDTR0CS0 = (tCH << 19) |
  218. (tCS << 16) |
  219. (tWH << 11) |
  220. (tWP << 8) |
  221. (tRP_high << 6) |
  222. (tRH << 3) |
  223. (tRP << 0);
  224. NDTR1CS0 = (tR << 16) |
  225. (tWHR << 4) |
  226. (tAR << 0);
  227. /* If it doesn't work (unlikely) think about:
  228. * - ecc enable
  229. * - chip select don't care
  230. * - read id byte count
  231. *
  232. * Intentionally enabled by not setting bits:
  233. * - dma (DMA_EN)
  234. * - page size = 512
  235. * - cs don't care, see if we can enable later!
  236. * - row address start position (after second cycle)
  237. * - pages per block = 32
  238. */
  239. NDCR = (NDCR_ND_ARB_EN | /* enable bus arbiter */
  240. NDCR_SPARE_EN | /* use the spare area */
  241. NDCR_DWIDTH_C | /* 16bit DFC data bus width */
  242. NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
  243. (2 << 16) | /* read id count = 7 ???? mk@tbd */
  244. NDCE_RDYM | /* flash device ready ir masked */
  245. NDCE_CS0_PAGEDM | /* ND_nCSx page done ir masked */
  246. NDCE_CS1_PAGEDM |
  247. NDCE_CS0_CMDDM | /* ND_CSx command done ir masked */
  248. NDCE_CS1_CMDDM |
  249. NDCE_CS0_BBDM | /* ND_CSx bad block detect ir masked */
  250. NDCE_CS1_BBDM |
  251. NDCE_DBERRM | /* double bit error ir masked */
  252. NDCE_SBERRM | /* single bit error ir masked */
  253. NDCE_WRDREQM | /* write data request ir masked */
  254. NDCE_RDDREQM | /* read data request ir masked */
  255. NDCE_WRCMDREQM); /* write command request ir masked */
  256. nand->hwcontrol = delta_hwcontrol;
  257. nand->dev_ready = delta_device_ready;
  258. nand->eccmode = NAND_ECC_SOFT;
  259. nand->chip_delay = NAND_DELAY_US;
  260. nand->options = NAND_BUSWIDTH_16;
  261. nand->read_byte = delta_read_byte;
  262. nand->cmdfunc = delta_cmdfunc;
  263. /* nand->options = NAND_SAMSUNG_LP_OPTIONS; */
  264. }
  265. #else
  266. #error "U-Boot legacy NAND support not available for delta board."
  267. #endif
  268. #endif