nand.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  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 u16 delta_read_word(struct mtd_info *mtd)
  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. static unsigned long delta_wait_event2(unsigned long event)
  158. {
  159. unsigned long ndsr;
  160. if(!event)
  161. return;
  162. while(1) {
  163. ndsr = NDSR;
  164. if(ndsr & event) {
  165. NDSR |= event;
  166. break;
  167. }
  168. }
  169. return ndsr;
  170. }
  171. /* we don't always wan't to do this */
  172. static void delta_new_cmd()
  173. {
  174. /* Clear NDSR */
  175. NDSR = 0xFFF;
  176. /* apparently NDCR[NDRUN] needs to be set before writing to NDCBx */
  177. if(!(NDCR & NDCR_ND_RUN)) {
  178. NDCR |= NDCR_ND_RUN;
  179. while(1) {
  180. if(NDSR & NDSR_WRCMDREQ) {
  181. NDSR |= NDSR_WRCMDREQ; /* Ack */
  182. break;
  183. }
  184. }
  185. }
  186. }
  187. static int delta_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
  188. {
  189. /* unsigned long timeo; */
  190. unsigned long ndsr=0, event=0;
  191. /* mk@tbd set appropriate timeouts */
  192. /* if (state == FL_ERASING) */
  193. /* timeo = CFG_HZ * 400; */
  194. /* else */
  195. /* timeo = CFG_HZ * 20; */
  196. if(state == FL_WRITING) {
  197. event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
  198. } else if(state == FL_ERASING) {
  199. event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
  200. }
  201. ndsr = delta_wait_event2(event);
  202. if(ndsr & NDSR_CS0_BBD)
  203. return(0x1); /* Status Read error */
  204. return 0;
  205. }
  206. /* this is really monahans, not board specific ... */
  207. static void delta_cmdfunc(struct mtd_info *mtd, unsigned command,
  208. int column, int page_addr)
  209. {
  210. /* register struct nand_chip *this = mtd->priv; */
  211. unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
  212. unsigned long what_the_hack;
  213. /* clear the ugly byte read buffer */
  214. bytes_read = 0;
  215. read_buf = 0;
  216. /* if command is a double byte cmd, we set bit double cmd bit 19 */
  217. /* command2 = (command>>8) & 0xFF; */
  218. /* ndcb0 = command | ((command2 ? 1 : 0) << 19); *\/ */
  219. switch (command) {
  220. case NAND_CMD_READ0:
  221. delta_new_cmd();
  222. ndcb0 = (NAND_CMD_READ0 | (4<<16));
  223. column >>= 1; /* adjust for 16 bit bus */
  224. ndcb1 = (((column>>1) & 0xff) |
  225. ((page_addr<<8) & 0xff00) |
  226. ((page_addr<<8) & 0xff0000) |
  227. ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
  228. event = NDSR_RDDREQ;
  229. break;
  230. case NAND_CMD_READID:
  231. delta_new_cmd();
  232. printk("delta_cmdfunc: NAND_CMD_READID.\n");
  233. ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
  234. event = NDSR_RDDREQ;
  235. break;
  236. case NAND_CMD_PAGEPROG:
  237. /* sent as a multicommand in NAND_CMD_SEQIN */
  238. printk("delta_cmdfunc: NAND_CMD_PAGEPROG.\n");
  239. goto end;
  240. case NAND_CMD_ERASE1:
  241. printf("delta_cmdfunc: NAND_CMD_ERASE1.\n");
  242. delta_new_cmd();
  243. ndcb0 = (0xd060 | (1<<25) | (2<<21) | (1<<19) | (3<<16));
  244. ndcb1 = (page_addr & 0x00ffffff);
  245. break;
  246. case NAND_CMD_ERASE2:
  247. printf("delta_cmdfunc: NAND_CMD_ERASE1 empty due to multicmd.\n");
  248. goto end;
  249. case NAND_CMD_SEQIN:
  250. /* send PAGE_PROG command(0x1080) */
  251. delta_new_cmd();
  252. printf("delta_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG.\n");
  253. ndcb0 = (0x1080 | (1<<25) | (1<<21) | (1<<19) | (4<<16));
  254. column >>= 1; /* adjust for 16 bit bus */
  255. ndcb1 = (((column>>1) & 0xff) |
  256. ((page_addr<<8) & 0xff00) |
  257. ((page_addr<<8) & 0xff0000) |
  258. ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
  259. event = NDSR_WRDREQ;
  260. break;
  261. /* case NAND_CMD_SEQIN_pointer_operation: */
  262. /* /\* This is confusing because the command names are */
  263. /* * different compared to the ones in the K9K12Q0C */
  264. /* * datasheet. Infact this has nothing to do with */
  265. /* * reading, as the but with page programming */
  266. /* * (writing). */
  267. /* * Here we send the multibyte commands */
  268. /* * cmd1=0x00, cmd2=0x80 (for programming main area) or */
  269. /* * cmd1=0x50, cmd2=0x80 (for spare area) */
  270. /* * */
  271. /* * When all data is written to the buffer, the page */
  272. /* * program command (0x10) is sent to actually write */
  273. /* * the data. */
  274. /* *\/ */
  275. /* printf("delta_cmdfunc: NAND_CMD_SEQIN pointer op called.\n"); */
  276. /* ndcb0 = (NAND_CMD_SEQIN<<8) | (1<<21) | (1<<19) | (4<<16); */
  277. /* if(column >= mtd->oobblock) { */
  278. /* /\* OOB area *\/ */
  279. /* column -= mtd->oobblock; */
  280. /* ndcb0 |= NAND_CMD_READOOB; */
  281. /* } else if (column < 256) { */
  282. /* /\* First 256 bytes --> READ0 *\/ */
  283. /* ndcb0 |= NAND_CMD_READ0; */
  284. /* } else { */
  285. /* /\* Only for 8 bit devices - not delta!!! *\/ */
  286. /* column -= 256; */
  287. /* ndcb0 |= NAND_CMD_READ1; */
  288. /* } */
  289. /* event = NDSR_WRDREQ; */
  290. /* break; */
  291. case NAND_CMD_STATUS:
  292. /* oh, this is not nice. for some reason the real
  293. * status byte is in the second read from the data
  294. * buffer. The hack is to read the first byte right
  295. * here, so the next read access by the nand code
  296. * yields the right one.
  297. */
  298. delta_new_cmd();
  299. ndcb0 = (NAND_CMD_STATUS | (4<<21));
  300. event = NDSR_RDDREQ;
  301. /* #define READ_STATUS_BUG 1 */
  302. #ifdef READ_STATUS_BUG
  303. NDCB0 = ndcb0;
  304. NDCB0 = ndcb1;
  305. NDCB0 = ndcb2;
  306. delta_wait_event(event);
  307. what_the_hack = NDDB;
  308. goto end;
  309. #endif
  310. break;
  311. case NAND_CMD_RESET:
  312. printf("delta_cmdfunc: NAND_CMD_RESET unimplemented.\n");
  313. break;
  314. default:
  315. printk("delta_cmdfunc: error, unsupported command.\n");
  316. return;
  317. }
  318. NDCB0 = ndcb0;
  319. NDCB0 = ndcb1;
  320. NDCB0 = ndcb2;
  321. wait_event:
  322. delta_wait_event(event);
  323. end:
  324. return;
  325. }
  326. static void delta_dfc_gpio_init()
  327. {
  328. printf("Setting up DFC GPIO's.\n");
  329. /* no idea what is done here, see zylonite.c */
  330. GPIO4 = 0x1;
  331. DF_ALE_WE1 = 0x00000001;
  332. DF_ALE_WE2 = 0x00000001;
  333. DF_nCS0 = 0x00000001;
  334. DF_nCS1 = 0x00000001;
  335. DF_nWE = 0x00000001;
  336. DF_nRE = 0x00000001;
  337. DF_IO0 = 0x00000001;
  338. DF_IO8 = 0x00000001;
  339. DF_IO1 = 0x00000001;
  340. DF_IO9 = 0x00000001;
  341. DF_IO2 = 0x00000001;
  342. DF_IO10 = 0x00000001;
  343. DF_IO3 = 0x00000001;
  344. DF_IO11 = 0x00000001;
  345. DF_IO4 = 0x00000001;
  346. DF_IO12 = 0x00000001;
  347. DF_IO5 = 0x00000001;
  348. DF_IO13 = 0x00000001;
  349. DF_IO6 = 0x00000001;
  350. DF_IO14 = 0x00000001;
  351. DF_IO7 = 0x00000001;
  352. DF_IO15 = 0x00000001;
  353. DF_nWE = 0x1901;
  354. DF_nRE = 0x1901;
  355. DF_CLE_NOE = 0x1900;
  356. DF_ALE_WE1 = 0x1901;
  357. DF_INT_RnB = 0x1900;
  358. }
  359. /*
  360. * Board-specific NAND initialization. The following members of the
  361. * argument are board-specific (per include/linux/mtd/nand_new.h):
  362. * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
  363. * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
  364. * - hwcontrol: hardwarespecific function for accesing control-lines
  365. * - dev_ready: hardwarespecific function for accesing device ready/busy line
  366. * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
  367. * only be provided if a hardware ECC is available
  368. * - eccmode: mode of ecc, see defines
  369. * - chip_delay: chip dependent delay for transfering data from array to
  370. * read regs (tR)
  371. * - options: various chip options. They can partly be set to inform
  372. * nand_scan about special functionality. See the defines for further
  373. * explanation
  374. * Members with a "?" were not set in the merged testing-NAND branch,
  375. * so they are not set here either.
  376. */
  377. void board_nand_init(struct nand_chip *nand)
  378. {
  379. unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
  380. /* set up GPIO Control Registers */
  381. delta_dfc_gpio_init();
  382. /* turn on the NAND Controller Clock (104 MHz @ D0) */
  383. CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
  384. /* wait ? */
  385. /* printf("stupid loop start...\n"); */
  386. /* wait(200); */
  387. /* printf("stupid loop end.\n"); */
  388. /* NAND Timing Parameters (in ns) */
  389. #define NAND_TIMING_tCH 10
  390. #define NAND_TIMING_tCS 0
  391. #define NAND_TIMING_tWH 20
  392. #define NAND_TIMING_tWP 40
  393. /* #define NAND_TIMING_tRH 20 */
  394. /* #define NAND_TIMING_tRP 40 */
  395. #define NAND_TIMING_tRH 25
  396. #define NAND_TIMING_tRP 50
  397. #define NAND_TIMING_tR 11123
  398. #define NAND_TIMING_tWHR 110
  399. #define NAND_TIMING_tAR 10
  400. /* Maximum values for NAND Interface Timing Registers in DFC clock
  401. * periods */
  402. #define DFC_MAX_tCH 7
  403. #define DFC_MAX_tCS 7
  404. #define DFC_MAX_tWH 7
  405. #define DFC_MAX_tWP 7
  406. #define DFC_MAX_tRH 7
  407. #define DFC_MAX_tRP 15
  408. #define DFC_MAX_tR 65535
  409. #define DFC_MAX_tWHR 15
  410. #define DFC_MAX_tAR 15
  411. #define DFC_CLOCK 104 /* DFC Clock is 104 MHz */
  412. #define DFC_CLK_PER_US DFC_CLOCK/1000 /* clock period in ns */
  413. #define MIN(x, y) ((x < y) ? x : y)
  414. tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
  415. DFC_MAX_tCH);
  416. tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
  417. DFC_MAX_tCS);
  418. tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
  419. DFC_MAX_tWH);
  420. tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
  421. DFC_MAX_tWP);
  422. tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
  423. DFC_MAX_tRH);
  424. tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
  425. DFC_MAX_tRP);
  426. tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
  427. DFC_MAX_tR);
  428. tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
  429. DFC_MAX_tWHR);
  430. tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
  431. DFC_MAX_tAR);
  432. 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);
  433. /* tRP value is split in the register */
  434. if(tRP & (1 << 4)) {
  435. tRP_high = 1;
  436. tRP &= ~(1 << 4);
  437. } else {
  438. tRP_high = 0;
  439. }
  440. NDTR0CS0 = (tCH << 19) |
  441. (tCS << 16) |
  442. (tWH << 11) |
  443. (tWP << 8) |
  444. (tRP_high << 6) |
  445. (tRH << 3) |
  446. (tRP << 0);
  447. NDTR1CS0 = (tR << 16) |
  448. (tWHR << 4) |
  449. (tAR << 0);
  450. /* If it doesn't work (unlikely) think about:
  451. * - ecc enable
  452. * - chip select don't care
  453. * - read id byte count
  454. *
  455. * Intentionally enabled by not setting bits:
  456. * - dma (DMA_EN)
  457. * - page size = 512
  458. * - cs don't care, see if we can enable later!
  459. * - row address start position (after second cycle)
  460. * - pages per block = 32
  461. * - ND_RDY : clears command buffer
  462. */
  463. /* NDCR_NCSX | /\* Chip select busy don't care *\/ */
  464. NDCR = (NDCR_SPARE_EN | /* use the spare area */
  465. NDCR_DWIDTH_C | /* 16bit DFC data bus width */
  466. NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
  467. (7 << 16) | /* read id count = 7 ???? mk@tbd */
  468. NDCR_ND_ARB_EN | /* enable bus arbiter */
  469. NDCR_RDYM | /* flash device ready ir masked */
  470. NDCR_CS0_PAGEDM | /* ND_nCSx page done ir masked */
  471. NDCR_CS1_PAGEDM |
  472. NDCR_CS0_CMDDM | /* ND_CSx command done ir masked */
  473. NDCR_CS1_CMDDM |
  474. NDCR_CS0_BBDM | /* ND_CSx bad block detect ir masked */
  475. NDCR_CS1_BBDM |
  476. NDCR_DBERRM | /* double bit error ir masked */
  477. NDCR_SBERRM | /* single bit error ir masked */
  478. NDCR_WRDREQM | /* write data request ir masked */
  479. NDCR_RDDREQM | /* read data request ir masked */
  480. NDCR_WRCMDREQM); /* write command request ir masked */
  481. /* wait 10 us due to cmd buffer clear reset */
  482. /* wait(10); */
  483. nand->hwcontrol = delta_hwcontrol;
  484. /* nand->dev_ready = delta_device_ready; */
  485. nand->eccmode = NAND_ECC_SOFT;
  486. nand->chip_delay = NAND_DELAY_US;
  487. nand->options = NAND_BUSWIDTH_16;
  488. nand->waitfunc = delta_wait;
  489. nand->read_byte = delta_read_byte;
  490. nand->write_byte = delta_write_byte;
  491. nand->read_word = delta_read_word;
  492. nand->write_word = delta_write_word;
  493. nand->read_buf = delta_read_buf;
  494. nand->write_buf = delta_write_buf;
  495. nand->cmdfunc = delta_cmdfunc;
  496. }
  497. #else
  498. #error "U-Boot legacy NAND support not available for delta board."
  499. #endif
  500. #endif