nand.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  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. * not required for Monahans DFC
  29. */
  30. static void delta_hwcontrol(struct mtd_info *mtdinfo, int cmd)
  31. {
  32. return;
  33. }
  34. /* read device ready pin */
  35. static int delta_device_ready(struct mtd_info *mtdinfo)
  36. {
  37. if(NDSR & NDSR_RDY)
  38. return 1;
  39. else
  40. return 0;
  41. return 0;
  42. }
  43. /* The original:
  44. * static void delta_read_buf(struct mtd_info *mtd, const u_char *buf, int len)
  45. *
  46. * Shouldn't this be "u_char * const buf" ?
  47. */
  48. static void delta_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
  49. {
  50. int i, j;
  51. /* we have to be carefull not to overflow the buffer if len is
  52. * not a multiple of 4 */
  53. unsigned long num_words = len & 0xfffffffc;
  54. unsigned long rest = len & 0x3;
  55. /* if there are any, first copy multiple of 4 bytes */
  56. if(num_words) {
  57. for(i=0; i<num_words; i+=4) {
  58. unsigned long *long_buf = &buf[i];
  59. /* ((unsigned long *) &buf[i]) = NDDB; */
  60. *long_buf = NDDB;
  61. }
  62. }
  63. /* ...then the rest */
  64. if(rest) {
  65. unsigned long rest_data = NDDB;
  66. for(j=0;j<rest;j++)
  67. buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
  68. }
  69. return;
  70. }
  71. /* global var, too bad */
  72. static unsigned long read_buf = 0;
  73. static unsigned char bytes_read = 0;
  74. /* wait for read request */
  75. static void delta_wait_event(unsigned long event)
  76. {
  77. if(!event)
  78. return;
  79. while(1) {
  80. if(NDSR & event) {
  81. NDSR |= event;
  82. break;
  83. }
  84. }
  85. }
  86. static u_char delta_read_byte(struct mtd_info *mtd)
  87. {
  88. /* struct nand_chip *this = mtd->priv; */
  89. unsigned char byte;
  90. if(bytes_read == 0) {
  91. read_buf = NDDB;
  92. printk("delta_read_byte: 0x%x.\n", read_buf);
  93. }
  94. byte = (unsigned char) (read_buf>>(8 * bytes_read++));
  95. if(bytes_read >= 4)
  96. bytes_read = 0;
  97. printf("delta_read_byte: returning 0x%x.\n", byte);
  98. return byte;
  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, event=0;
  106. /* clear the ugly byte read buffer */
  107. bytes_read = 0;
  108. read_buf = 0;
  109. /* Clear NDSR */
  110. NDSR = 0xFFF;
  111. /* apparently NDCR[NDRUN] needs to be set before writing to NDCBx */
  112. NDCR |= NDCR_ND_RUN;
  113. /* wait for write command request
  114. * hmm, might be nice if this could time-out. mk@tbd
  115. */
  116. while(1) {
  117. if(NDSR & NDSR_WRCMDREQ) {
  118. NDSR |= NDSR_WRCMDREQ; /* Ack */
  119. break;
  120. }
  121. }
  122. /* if command is a double byte cmd, we set bit double cmd bit 19 */
  123. /* command2 = (command>>8) & 0xFF; */
  124. /* ndcb0 = command | ((command2 ? 1 : 0) << 19); *\/ */
  125. switch (command) {
  126. case NAND_CMD_READ0:
  127. ndcb0 = (NAND_CMD_READ0 | (4<<16));
  128. column >>= 1; /* adjust for 16 bit bus */
  129. ndcb1 = (((column>>1) & 0xff) |
  130. ((page_addr<<8) & 0xff00) |
  131. ((page_addr<<8) & 0xff0000) |
  132. ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
  133. event = NDSR_RDDREQ;
  134. break;
  135. case NAND_CMD_READID:
  136. printk("delta_cmdfunc: NAND_CMD_READID.\n");
  137. ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
  138. event = NDSR_RDDREQ;
  139. break;
  140. case NAND_CMD_PAGEPROG:
  141. break;
  142. case NAND_CMD_ERASE1:
  143. case NAND_CMD_ERASE2:
  144. break;
  145. case NAND_CMD_SEQIN:
  146. ndcb0 = (NAND_CMD_SEQIN<<8) | (1<<19) | (4<<16);
  147. if(column >= mtd->oobblock) {
  148. /* OOB area */
  149. column -= mtd->oobblock;
  150. ndcb0 |= NAND_CMD_READOOB;
  151. } else if (column < 256) {
  152. /* First 256 bytes --> READ0 */
  153. ndcb0 |= NAND_CMD_READ0;
  154. } else {
  155. /* Only for 8 bit devices - not delta!!! */
  156. column -= 256;
  157. ndcb0 |= NAND_CMD_READ1;
  158. }
  159. break;
  160. case NAND_CMD_STATUS:
  161. return;
  162. case NAND_CMD_RESET:
  163. return;
  164. default:
  165. printk("delta_cmdfunc: error, unsupported command.\n");
  166. return;
  167. }
  168. NDCB0 = ndcb0;
  169. NDCB0 = ndcb1;
  170. NDCB0 = ndcb2;
  171. /* wait for event */
  172. delta_wait_event(event);
  173. }
  174. static void delta_dfc_gpio_init()
  175. {
  176. printf("Setting up DFC GPIO's.\n");
  177. /* no idea what is done here, see zylonite.c */
  178. GPIO4 = 0x1;
  179. DF_ALE_WE1 = 0x00000001;
  180. DF_ALE_WE2 = 0x00000001;
  181. DF_nCS0 = 0x00000001;
  182. DF_nCS1 = 0x00000001;
  183. DF_nWE = 0x00000001;
  184. DF_nRE = 0x00000001;
  185. DF_IO0 = 0x00000001;
  186. DF_IO8 = 0x00000001;
  187. DF_IO1 = 0x00000001;
  188. DF_IO9 = 0x00000001;
  189. DF_IO2 = 0x00000001;
  190. DF_IO10 = 0x00000001;
  191. DF_IO3 = 0x00000001;
  192. DF_IO11 = 0x00000001;
  193. DF_IO4 = 0x00000001;
  194. DF_IO12 = 0x00000001;
  195. DF_IO5 = 0x00000001;
  196. DF_IO13 = 0x00000001;
  197. DF_IO6 = 0x00000001;
  198. DF_IO14 = 0x00000001;
  199. DF_IO7 = 0x00000001;
  200. DF_IO15 = 0x00000001;
  201. DF_nWE = 0x1901;
  202. DF_nRE = 0x1901;
  203. DF_CLE_NOE = 0x1900;
  204. DF_ALE_WE1 = 0x1901;
  205. DF_INT_RnB = 0x1900;
  206. }
  207. /*
  208. * Board-specific NAND initialization. The following members of the
  209. * argument are board-specific (per include/linux/mtd/nand_new.h):
  210. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  211. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  212. * - hwcontrol: hardwarespecific function for accesing control-lines
  213. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  214. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  215. * only be provided if a hardware ECC is available
  216. * - eccmode: mode of ecc, see defines
  217. * - chip_delay: chip dependent delay for transfering data from array to
  218. * read regs (tR)
  219. * - options: various chip options. They can partly be set to inform
  220. * nand_scan about special functionality. See the defines for further
  221. * explanation
  222. * Members with a "?" were not set in the merged testing-NAND branch,
  223. * so they are not set here either.
  224. */
  225. void wait(unsigned long us)
  226. {
  227. #define OSCR_CLK_FREQ 3.250 /* kHz */
  228. unsigned long start = OSCR;
  229. unsigned long delta = 0, cur;
  230. us *= OSCR_CLK_FREQ;
  231. while (delta < us) {
  232. cur = OSCR;
  233. if(cur < start) /* OSCR overflowed */
  234. delta = cur + (start^0xffffffff);
  235. else
  236. delta = cur - start;
  237. }
  238. }
  239. void board_nand_init(struct nand_chip *nand)
  240. {
  241. unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
  242. /* set up GPIO Control Registers */
  243. delta_dfc_gpio_init();
  244. /* turn on the NAND Controller Clock (104 MHz @ D0) */
  245. CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
  246. /* wait ? */
  247. /* printf("stupid loop start...\n"); */
  248. /* wait(200); */
  249. /* printf("stupid loop end.\n"); */
  250. /* NAND Timing Parameters (in ns) */
  251. #define NAND_TIMING_tCH 10
  252. #define NAND_TIMING_tCS 0
  253. #define NAND_TIMING_tWH 20
  254. #define NAND_TIMING_tWP 40
  255. /* #define NAND_TIMING_tRH 20 */
  256. /* #define NAND_TIMING_tRP 40 */
  257. #define NAND_TIMING_tRH 25
  258. #define NAND_TIMING_tRP 50
  259. #define NAND_TIMING_tR 11123
  260. #define NAND_TIMING_tWHR 110
  261. #define NAND_TIMING_tAR 10
  262. /* Maximum values for NAND Interface Timing Registers in DFC clock
  263. * periods */
  264. #define DFC_MAX_tCH 7
  265. #define DFC_MAX_tCS 7
  266. #define DFC_MAX_tWH 7
  267. #define DFC_MAX_tWP 7
  268. #define DFC_MAX_tRH 7
  269. #define DFC_MAX_tRP 15
  270. #define DFC_MAX_tR 65535
  271. #define DFC_MAX_tWHR 15
  272. #define DFC_MAX_tAR 15
  273. #define DFC_CLOCK 104 /* DFC Clock is 104 MHz */
  274. #define DFC_CLK_PER_US DFC_CLOCK/1000 /* clock period in ns */
  275. #define MIN(x, y) ((x < y) ? x : y)
  276. tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
  277. DFC_MAX_tCH);
  278. tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
  279. DFC_MAX_tCS);
  280. tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
  281. DFC_MAX_tWH);
  282. tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
  283. DFC_MAX_tWP);
  284. tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
  285. DFC_MAX_tRH);
  286. tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
  287. DFC_MAX_tRP);
  288. tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
  289. DFC_MAX_tR);
  290. tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
  291. DFC_MAX_tWHR);
  292. tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
  293. DFC_MAX_tAR);
  294. printf("tCH=%u, tCS=%u, tWH=%u, tWP=%u, tRH=%u, tRP=%u, tR=%u, tWHR=%u, tAR=%u.\n", tCH, tCS, tWH, tWP, tRH, tRP, tR, tWHR, tAR);
  295. /* tRP value is split in the register */
  296. if(tRP & (1 << 4)) {
  297. tRP_high = 1;
  298. tRP &= ~(1 << 4);
  299. } else {
  300. tRP_high = 0;
  301. }
  302. NDTR0CS0 = (tCH << 19) |
  303. (tCS << 16) |
  304. (tWH << 11) |
  305. (tWP << 8) |
  306. (tRP_high << 6) |
  307. (tRH << 3) |
  308. (tRP << 0);
  309. NDTR1CS0 = (tR << 16) |
  310. (tWHR << 4) |
  311. (tAR << 0);
  312. /* If it doesn't work (unlikely) think about:
  313. * - ecc enable
  314. * - chip select don't care
  315. * - read id byte count
  316. *
  317. * Intentionally enabled by not setting bits:
  318. * - dma (DMA_EN)
  319. * - page size = 512
  320. * - cs don't care, see if we can enable later!
  321. * - row address start position (after second cycle)
  322. * - pages per block = 32
  323. * - ND_RDY : clears command buffer
  324. */
  325. NDCR = (NDCR_SPARE_EN | /* use the spare area */
  326. NDCR_DWIDTH_C | /* 16bit DFC data bus width */
  327. NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
  328. NDCR_NCSX | /* Chip select busy don't care */
  329. (7 << 16) | /* read id count = 7 ???? mk@tbd */
  330. NDCR_ND_ARB_EN | /* enable bus arbiter */
  331. NDCR_RDYM | /* flash device ready ir masked */
  332. NDCR_CS0_PAGEDM | /* ND_nCSx page done ir masked */
  333. NDCR_CS1_PAGEDM |
  334. NDCR_CS0_CMDDM | /* ND_CSx command done ir masked */
  335. NDCR_CS1_CMDDM |
  336. NDCR_CS0_BBDM | /* ND_CSx bad block detect ir masked */
  337. NDCR_CS1_BBDM |
  338. NDCR_DBERRM | /* double bit error ir masked */
  339. NDCR_SBERRM | /* single bit error ir masked */
  340. NDCR_WRDREQM | /* write data request ir masked */
  341. NDCR_RDDREQM | /* read data request ir masked */
  342. NDCR_WRCMDREQM); /* write command request ir masked */
  343. /* wait 10 us due to cmd buffer clear reset */
  344. /* wait(10); */
  345. nand->hwcontrol = delta_hwcontrol;
  346. /* nand->dev_ready = delta_device_ready; */
  347. nand->eccmode = NAND_ECC_SOFT;
  348. nand->chip_delay = NAND_DELAY_US;
  349. nand->options = NAND_BUSWIDTH_16;
  350. nand->read_byte = delta_read_byte;
  351. nand->read_buf = delta_read_buf;
  352. nand->cmdfunc = delta_cmdfunc;
  353. /* nand->options = NAND_SAMSUNG_LP_OPTIONS; */
  354. }
  355. #else
  356. #error "U-Boot legacy NAND support not available for delta board."
  357. #endif
  358. #endif