nand.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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) | (1 << 16)); /* addr cycles*/
  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. void delta_dfc_gpio_init()
  145. {
  146. printf("Setting up DFC GPIO's.\n");
  147. /* no idea what is done here, see zylonite.c */
  148. GPIO4 = 0x1;
  149. DF_ALE_WE1 = 0x00000001;
  150. DF_ALE_WE2 = 0x00000001;
  151. DF_nCS0 = 0x00000001;
  152. DF_nCS1 = 0x00000001;
  153. DF_nWE = 0x00000001;
  154. DF_nRE = 0x00000001;
  155. DF_IO0 = 0x00000001;
  156. DF_IO8 = 0x00000001;
  157. DF_IO1 = 0x00000001;
  158. DF_IO9 = 0x00000001;
  159. DF_IO2 = 0x00000001;
  160. DF_IO10 = 0x00000001;
  161. DF_IO3 = 0x00000001;
  162. DF_IO11 = 0x00000001;
  163. DF_IO4 = 0x00000001;
  164. DF_IO12 = 0x00000001;
  165. DF_IO5 = 0x00000001;
  166. DF_IO13 = 0x00000001;
  167. DF_IO6 = 0x00000001;
  168. DF_IO14 = 0x00000001;
  169. DF_IO7 = 0x00000001;
  170. DF_IO15 = 0x00000001;
  171. DF_nWE = 0x1901;
  172. DF_nRE = 0x1901;
  173. DF_CLE_NOE = 0x1900;
  174. DF_ALE_WE1 = 0x1901;
  175. DF_INT_RnB = 0x1900;
  176. }
  177. /*
  178. * Board-specific NAND initialization. The following members of the
  179. * argument are board-specific (per include/linux/mtd/nand_new.h):
  180. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  181. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  182. * - hwcontrol: hardwarespecific function for accesing control-lines
  183. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  184. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  185. * only be provided if a hardware ECC is available
  186. * - eccmode: mode of ecc, see defines
  187. * - chip_delay: chip dependent delay for transfering data from array to
  188. * read regs (tR)
  189. * - options: various chip options. They can partly be set to inform
  190. * nand_scan about special functionality. See the defines for further
  191. * explanation
  192. * Members with a "?" were not set in the merged testing-NAND branch,
  193. * so they are not set here either.
  194. */
  195. void board_nand_init(struct nand_chip *nand)
  196. {
  197. unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
  198. /* set up GPIO Control Registers */
  199. delta_dfc_gpio_init();
  200. /* turn on the NAND Controller Clock (104 MHz @ D0) */
  201. CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
  202. /* NAND Timing Parameters (in ns) */
  203. #define NAND_TIMING_tCH 10
  204. #define NAND_TIMING_tCS 0
  205. #define NAND_TIMING_tWH 20
  206. #define NAND_TIMING_tWP 40
  207. #define NAND_TIMING_tRH 20
  208. #define NAND_TIMING_tRP 40
  209. #define NAND_TIMING_tR 11123
  210. #define NAND_TIMING_tWHR 110
  211. #define NAND_TIMING_tAR 10
  212. /* Maximum values for NAND Interface Timing Registers in DFC clock
  213. * periods */
  214. #define DFC_MAX_tCH 7
  215. #define DFC_MAX_tCS 7
  216. #define DFC_MAX_tWH 7
  217. #define DFC_MAX_tWP 7
  218. #define DFC_MAX_tRH 7
  219. #define DFC_MAX_tRP 15
  220. #define DFC_MAX_tR 65535
  221. #define DFC_MAX_tWHR 15
  222. #define DFC_MAX_tAR 15
  223. #define DFC_CLOCK 104 /* DFC Clock is 104 MHz */
  224. #define DFC_CLK_PER_US DFC_CLOCK/1000 /* clock period in ns */
  225. #define MIN(x, y) ((x < y) ? x : y)
  226. tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
  227. DFC_MAX_tCH);
  228. tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
  229. DFC_MAX_tCS);
  230. tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
  231. DFC_MAX_tWH);
  232. tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
  233. DFC_MAX_tWP);
  234. tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
  235. DFC_MAX_tRH);
  236. tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
  237. DFC_MAX_tRP);
  238. tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
  239. DFC_MAX_tR);
  240. tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
  241. DFC_MAX_tWHR);
  242. tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
  243. DFC_MAX_tAR);
  244. /* tRP value is split in the register */
  245. if(tRP & (1 << 4)) {
  246. tRP_high = 1;
  247. tRP &= ~(1 << 4);
  248. } else {
  249. tRP_high = 0;
  250. }
  251. NDTR0CS0 = (tCH << 19) |
  252. (tCS << 16) |
  253. (tWH << 11) |
  254. (tWP << 8) |
  255. (tRP_high << 6) |
  256. (tRH << 3) |
  257. (tRP << 0);
  258. NDTR1CS0 = (tR << 16) |
  259. (tWHR << 4) |
  260. (tAR << 0);
  261. /* If it doesn't work (unlikely) think about:
  262. * - ecc enable
  263. * - chip select don't care
  264. * - read id byte count
  265. *
  266. * Intentionally enabled by not setting bits:
  267. * - dma (DMA_EN)
  268. * - page size = 512
  269. * - cs don't care, see if we can enable later!
  270. * - row address start position (after second cycle)
  271. * - pages per block = 32
  272. */
  273. NDCR = (NDCR_ND_ARB_EN | /* enable bus arbiter */
  274. NDCR_SPARE_EN | /* use the spare area */
  275. NDCR_DWIDTH_C | /* 16bit DFC data bus width */
  276. NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
  277. (2 << 16) | /* read id count = 7 ???? mk@tbd */
  278. NDCE_RDYM | /* flash device ready ir masked */
  279. NDCE_CS0_PAGEDM | /* ND_nCSx page done ir masked */
  280. NDCE_CS1_PAGEDM |
  281. NDCE_CS0_CMDDM | /* ND_CSx command done ir masked */
  282. NDCE_CS1_CMDDM |
  283. NDCE_CS0_BBDM | /* ND_CSx bad block detect ir masked */
  284. NDCE_CS1_BBDM |
  285. NDCE_DBERRM | /* double bit error ir masked */
  286. NDCE_SBERRM | /* single bit error ir masked */
  287. NDCE_WRDREQM | /* write data request ir masked */
  288. NDCE_RDDREQM | /* read data request ir masked */
  289. NDCE_WRCMDREQM); /* write command request ir masked */
  290. nand->hwcontrol = delta_hwcontrol;
  291. nand->dev_ready = delta_device_ready;
  292. nand->eccmode = NAND_ECC_SOFT;
  293. nand->chip_delay = NAND_DELAY_US;
  294. nand->options = NAND_BUSWIDTH_16;
  295. nand->read_byte = delta_read_byte;
  296. nand->cmdfunc = delta_cmdfunc;
  297. /* nand->options = NAND_SAMSUNG_LP_OPTIONS; */
  298. }
  299. #else
  300. #error "U-Boot legacy NAND support not available for delta board."
  301. #endif
  302. #endif