nand.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518
  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. /*
  44. * Write buf to the DFC Controller Data Buffer
  45. */
  46. static void delta_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
  47. {
  48. unsigned long bytes_multi = len & 0xfffffffc;
  49. unsigned long rest = len & 0x3;
  50. unsigned long *long_buf;
  51. int i;
  52. if(bytes_multi) {
  53. for(i=0; i<bytes_multi; i+=4) {
  54. long_buf = (unsigned long*) &buf[i];
  55. NDDB = *long_buf;
  56. }
  57. }
  58. if(rest) {
  59. printf("delta_write_buf: ERROR, writing non 4-byte aligned data.\n");
  60. }
  61. return;
  62. }
  63. /*
  64. * These functions are quite problematic for the DFC. Luckily they are
  65. * not used in the current nand code, except for nand_command, which
  66. * we've defined our own anyway. The problem is, that we always need
  67. * to write 4 bytes to the DFC Data Buffer, but in these functions we
  68. * don't know if to buffer the bytes/half words until we've gathered 4
  69. * bytes or if to send them straight away.
  70. *
  71. * Solution: Don't use these with Mona's DFC and complain loudly.
  72. */
  73. static void delta_write_word(struct mtd_info *mtd, u16 word)
  74. {
  75. printf("delta_write_word: WARNING, this function does not work with the Monahans DFC!\n");
  76. }
  77. static void delta_write_byte(struct mtd_info *mtd, u_char byte)
  78. {
  79. printf("delta_write_byte: WARNING, this function does not work with the Monahans DFC!\n");
  80. }
  81. /* The original:
  82. * static void delta_read_buf(struct mtd_info *mtd, const u_char *buf, int len)
  83. *
  84. * Shouldn't this be "u_char * const buf" ?
  85. */
  86. static void delta_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
  87. {
  88. int i, j;
  89. /* we have to be carefull not to overflow the buffer if len is
  90. * not a multiple of 4 */
  91. unsigned long bytes_multi = len & 0xfffffffc;
  92. unsigned long rest = len & 0x3;
  93. unsigned long *long_buf;
  94. /* if there are any, first copy multiple of 4 bytes */
  95. if(bytes_multi) {
  96. for(i=0; i<bytes_multi; i+=4) {
  97. long_buf = (unsigned long*) &buf[i];
  98. *long_buf = NDDB;
  99. }
  100. }
  101. /* ...then the rest */
  102. if(rest) {
  103. unsigned long rest_data = NDDB;
  104. for(j=0;j<rest; j++)
  105. buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
  106. }
  107. return;
  108. }
  109. static void delta_read_word(struct mtd_info *mtd, u_char byte)
  110. {
  111. printf("delta_write_byte: UNIMPLEMENTED.\n");
  112. }
  113. /* global var, too bad: mk@tbd: move to ->priv pointer */
  114. static unsigned long read_buf = 0;
  115. static unsigned char bytes_read = 0;
  116. static u_char delta_read_byte(struct mtd_info *mtd)
  117. {
  118. /* struct nand_chip *this = mtd->priv; */
  119. unsigned char byte;
  120. if(bytes_read == 0) {
  121. read_buf = NDDB;
  122. printk("delta_read_byte: 0x%x.\n", read_buf);
  123. }
  124. byte = (unsigned char) (read_buf>>(8 * bytes_read++));
  125. if(bytes_read >= 4)
  126. bytes_read = 0;
  127. printf("delta_read_byte: returning 0x%x.\n", byte);
  128. return byte;
  129. }
  130. /* delay function */
  131. static void wait(unsigned long us)
  132. {
  133. #define OSCR_CLK_FREQ 3.250 /* kHz */
  134. unsigned long start = OSCR;
  135. unsigned long delta = 0, cur;
  136. us *= OSCR_CLK_FREQ;
  137. while (delta < us) {
  138. cur = OSCR;
  139. if(cur < start) /* OSCR overflowed */
  140. delta = cur + (start^0xffffffff);
  141. else
  142. delta = cur - start;
  143. }
  144. }
  145. /* poll the NAND Controller Status Register for event */
  146. static void delta_wait_event(unsigned long event)
  147. {
  148. if(!event)
  149. return;
  150. while(1) {
  151. if(NDSR & event) {
  152. NDSR |= event;
  153. break;
  154. }
  155. }
  156. }
  157. /* this is really monahans, not board specific ... */
  158. static void delta_cmdfunc(struct mtd_info *mtd, unsigned command,
  159. int column, int page_addr)
  160. {
  161. /* register struct nand_chip *this = mtd->priv; */
  162. unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
  163. unsigned long what_the_hack;
  164. /* clear the ugly byte read buffer */
  165. bytes_read = 0;
  166. read_buf = 0;
  167. /* Clear NDSR */
  168. NDSR = 0xFFF;
  169. /* apparently NDCR[NDRUN] needs to be set before writing to NDCBx */
  170. NDCR |= NDCR_ND_RUN;
  171. /* wait for write command request
  172. * hmm, might be nice if this could time-out. mk@tbd
  173. */
  174. while(1) {
  175. if(NDSR & NDSR_WRCMDREQ) {
  176. NDSR |= NDSR_WRCMDREQ; /* Ack */
  177. break;
  178. }
  179. }
  180. /* if command is a double byte cmd, we set bit double cmd bit 19 */
  181. /* command2 = (command>>8) & 0xFF; */
  182. /* ndcb0 = command | ((command2 ? 1 : 0) << 19); *\/ */
  183. switch (command) {
  184. case NAND_CMD_READ0:
  185. ndcb0 = (NAND_CMD_READ0 | (4<<16));
  186. column >>= 1; /* adjust for 16 bit bus */
  187. ndcb1 = (((column>>1) & 0xff) |
  188. ((page_addr<<8) & 0xff00) |
  189. ((page_addr<<8) & 0xff0000) |
  190. ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
  191. event = NDSR_RDDREQ;
  192. break;
  193. case NAND_CMD_READID:
  194. printk("delta_cmdfunc: NAND_CMD_READID.\n");
  195. ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
  196. event = NDSR_RDDREQ;
  197. break;
  198. case NAND_CMD_PAGEPROG:
  199. printk("delta_cmdfunc: NAND_CMD_PAGEPROG.\n");
  200. ndcb0 = (NAND_CMD_PAGEPROG | (1 << 21));
  201. break;
  202. case NAND_CMD_ERASE1:
  203. case NAND_CMD_ERASE2:
  204. printf("delta_cmdfunc: NAND_CMD_ERASEx unimplemented.\n");
  205. break;
  206. case NAND_CMD_SEQIN:
  207. /* send PAGE_PROG command(0x80) */
  208. printf("delta_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG.\n");
  209. ndcb0 = (NAND_CMD_SEQIN | (1<<21) | (3<<16));
  210. column >>= 1; /* adjust for 16 bit bus */
  211. ndcb1 = (((column>>1) & 0xff) |
  212. ((page_addr<<8) & 0xff00) |
  213. ((page_addr<<8) & 0xff0000) |
  214. ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
  215. event = NDSR_WRDREQ;
  216. break;
  217. /* case NAND_CMD_SEQIN_pointer_operation: */
  218. /* /\* This is confusing because the command names are */
  219. /* * different compared to the ones in the K9K12Q0C */
  220. /* * datasheet. Infact this has nothing to do with */
  221. /* * reading, as the but with page programming */
  222. /* * (writing). */
  223. /* * Here we send the multibyte commands */
  224. /* * cmd1=0x00, cmd2=0x80 (for programming main area) or */
  225. /* * cmd1=0x50, cmd2=0x80 (for spare area) */
  226. /* * */
  227. /* * When all data is written to the buffer, the page */
  228. /* * program command (0x10) is sent to actually write */
  229. /* * the data. */
  230. /* *\/ */
  231. /* printf("delta_cmdfunc: NAND_CMD_SEQIN pointer op called.\n"); */
  232. /* ndcb0 = (NAND_CMD_SEQIN<<8) | (1<<21) | (1<<19) | (4<<16); */
  233. /* if(column >= mtd->oobblock) { */
  234. /* /\* OOB area *\/ */
  235. /* column -= mtd->oobblock; */
  236. /* ndcb0 |= NAND_CMD_READOOB; */
  237. /* } else if (column < 256) { */
  238. /* /\* First 256 bytes --> READ0 *\/ */
  239. /* ndcb0 |= NAND_CMD_READ0; */
  240. /* } else { */
  241. /* /\* Only for 8 bit devices - not delta!!! *\/ */
  242. /* column -= 256; */
  243. /* ndcb0 |= NAND_CMD_READ1; */
  244. /* } */
  245. /* event = NDSR_WRDREQ; */
  246. /* break; */
  247. case NAND_CMD_STATUS:
  248. /* oh, this is not nice. for some reason the real
  249. * status byte is in the second read from the data
  250. * buffer. The hack is to read the first byte right
  251. * here, so the next read access by the nand code
  252. * yields the right one.
  253. */
  254. ndcb0 = (NAND_CMD_STATUS | (4<<21));
  255. event = NDSR_RDDREQ;
  256. NDCB0 = ndcb0;
  257. NDCB0 = ndcb1;
  258. NDCB0 = ndcb2;
  259. delta_wait_event(event);
  260. what_the_hack = NDDB;
  261. goto end;
  262. break;
  263. case NAND_CMD_RESET:
  264. printf("delta_cmdfunc: NAND_CMD_RESET unimplemented.\n");
  265. break;
  266. default:
  267. printk("delta_cmdfunc: error, unsupported command.\n");
  268. return;
  269. }
  270. NDCB0 = ndcb0;
  271. NDCB0 = ndcb1;
  272. NDCB0 = ndcb2;
  273. /* wait for event */
  274. delta_wait_event(event);
  275. end:
  276. return;
  277. }
  278. static void delta_dfc_gpio_init()
  279. {
  280. printf("Setting up DFC GPIO's.\n");
  281. /* no idea what is done here, see zylonite.c */
  282. GPIO4 = 0x1;
  283. DF_ALE_WE1 = 0x00000001;
  284. DF_ALE_WE2 = 0x00000001;
  285. DF_nCS0 = 0x00000001;
  286. DF_nCS1 = 0x00000001;
  287. DF_nWE = 0x00000001;
  288. DF_nRE = 0x00000001;
  289. DF_IO0 = 0x00000001;
  290. DF_IO8 = 0x00000001;
  291. DF_IO1 = 0x00000001;
  292. DF_IO9 = 0x00000001;
  293. DF_IO2 = 0x00000001;
  294. DF_IO10 = 0x00000001;
  295. DF_IO3 = 0x00000001;
  296. DF_IO11 = 0x00000001;
  297. DF_IO4 = 0x00000001;
  298. DF_IO12 = 0x00000001;
  299. DF_IO5 = 0x00000001;
  300. DF_IO13 = 0x00000001;
  301. DF_IO6 = 0x00000001;
  302. DF_IO14 = 0x00000001;
  303. DF_IO7 = 0x00000001;
  304. DF_IO15 = 0x00000001;
  305. DF_nWE = 0x1901;
  306. DF_nRE = 0x1901;
  307. DF_CLE_NOE = 0x1900;
  308. DF_ALE_WE1 = 0x1901;
  309. DF_INT_RnB = 0x1900;
  310. }
  311. /*
  312. * Board-specific NAND initialization. The following members of the
  313. * argument are board-specific (per include/linux/mtd/nand_new.h):
  314. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  315. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  316. * - hwcontrol: hardwarespecific function for accesing control-lines
  317. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  318. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  319. * only be provided if a hardware ECC is available
  320. * - eccmode: mode of ecc, see defines
  321. * - chip_delay: chip dependent delay for transfering data from array to
  322. * read regs (tR)
  323. * - options: various chip options. They can partly be set to inform
  324. * nand_scan about special functionality. See the defines for further
  325. * explanation
  326. * Members with a "?" were not set in the merged testing-NAND branch,
  327. * so they are not set here either.
  328. */
  329. void board_nand_init(struct nand_chip *nand)
  330. {
  331. unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
  332. /* set up GPIO Control Registers */
  333. delta_dfc_gpio_init();
  334. /* turn on the NAND Controller Clock (104 MHz @ D0) */
  335. CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
  336. /* wait ? */
  337. /* printf("stupid loop start...\n"); */
  338. /* wait(200); */
  339. /* printf("stupid loop end.\n"); */
  340. /* NAND Timing Parameters (in ns) */
  341. #define NAND_TIMING_tCH 10
  342. #define NAND_TIMING_tCS 0
  343. #define NAND_TIMING_tWH 20
  344. #define NAND_TIMING_tWP 40
  345. /* #define NAND_TIMING_tRH 20 */
  346. /* #define NAND_TIMING_tRP 40 */
  347. #define NAND_TIMING_tRH 25
  348. #define NAND_TIMING_tRP 50
  349. #define NAND_TIMING_tR 11123
  350. #define NAND_TIMING_tWHR 110
  351. #define NAND_TIMING_tAR 10
  352. /* Maximum values for NAND Interface Timing Registers in DFC clock
  353. * periods */
  354. #define DFC_MAX_tCH 7
  355. #define DFC_MAX_tCS 7
  356. #define DFC_MAX_tWH 7
  357. #define DFC_MAX_tWP 7
  358. #define DFC_MAX_tRH 7
  359. #define DFC_MAX_tRP 15
  360. #define DFC_MAX_tR 65535
  361. #define DFC_MAX_tWHR 15
  362. #define DFC_MAX_tAR 15
  363. #define DFC_CLOCK 104 /* DFC Clock is 104 MHz */
  364. #define DFC_CLK_PER_US DFC_CLOCK/1000 /* clock period in ns */
  365. #define MIN(x, y) ((x < y) ? x : y)
  366. tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
  367. DFC_MAX_tCH);
  368. tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
  369. DFC_MAX_tCS);
  370. tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
  371. DFC_MAX_tWH);
  372. tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
  373. DFC_MAX_tWP);
  374. tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
  375. DFC_MAX_tRH);
  376. tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
  377. DFC_MAX_tRP);
  378. tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
  379. DFC_MAX_tR);
  380. tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
  381. DFC_MAX_tWHR);
  382. tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
  383. DFC_MAX_tAR);
  384. 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);
  385. /* tRP value is split in the register */
  386. if(tRP & (1 << 4)) {
  387. tRP_high = 1;
  388. tRP &= ~(1 << 4);
  389. } else {
  390. tRP_high = 0;
  391. }
  392. NDTR0CS0 = (tCH << 19) |
  393. (tCS << 16) |
  394. (tWH << 11) |
  395. (tWP << 8) |
  396. (tRP_high << 6) |
  397. (tRH << 3) |
  398. (tRP << 0);
  399. NDTR1CS0 = (tR << 16) |
  400. (tWHR << 4) |
  401. (tAR << 0);
  402. /* If it doesn't work (unlikely) think about:
  403. * - ecc enable
  404. * - chip select don't care
  405. * - read id byte count
  406. *
  407. * Intentionally enabled by not setting bits:
  408. * - dma (DMA_EN)
  409. * - page size = 512
  410. * - cs don't care, see if we can enable later!
  411. * - row address start position (after second cycle)
  412. * - pages per block = 32
  413. * - ND_RDY : clears command buffer
  414. */
  415. NDCR = (NDCR_SPARE_EN | /* use the spare area */
  416. NDCR_DWIDTH_C | /* 16bit DFC data bus width */
  417. NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
  418. NDCR_NCSX | /* Chip select busy don't care */
  419. (7 << 16) | /* read id count = 7 ???? mk@tbd */
  420. NDCR_ND_ARB_EN | /* enable bus arbiter */
  421. NDCR_RDYM | /* flash device ready ir masked */
  422. NDCR_CS0_PAGEDM | /* ND_nCSx page done ir masked */
  423. NDCR_CS1_PAGEDM |
  424. NDCR_CS0_CMDDM | /* ND_CSx command done ir masked */
  425. NDCR_CS1_CMDDM |
  426. NDCR_CS0_BBDM | /* ND_CSx bad block detect ir masked */
  427. NDCR_CS1_BBDM |
  428. NDCR_DBERRM | /* double bit error ir masked */
  429. NDCR_SBERRM | /* single bit error ir masked */
  430. NDCR_WRDREQM | /* write data request ir masked */
  431. NDCR_RDDREQM | /* read data request ir masked */
  432. NDCR_WRCMDREQM); /* write command request ir masked */
  433. /* wait 10 us due to cmd buffer clear reset */
  434. /* wait(10); */
  435. nand->hwcontrol = delta_hwcontrol;
  436. /* nand->dev_ready = delta_device_ready; */
  437. nand->eccmode = NAND_ECC_SOFT;
  438. nand->chip_delay = NAND_DELAY_US;
  439. nand->options = NAND_BUSWIDTH_16;
  440. nand->read_byte = delta_read_byte;
  441. nand->write_byte = delta_write_byte;
  442. nand->read_word = delta_read_word;
  443. nand->write_word = delta_write_word;
  444. nand->read_buf = delta_read_buf;
  445. nand->write_buf = delta_write_buf;
  446. nand->cmdfunc = delta_cmdfunc;
  447. }
  448. #else
  449. #error "U-Boot legacy NAND support not available for delta board."
  450. #endif
  451. #endif