2 * (C) Copyright 2006 DENX Software Engineering
4 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
26 #ifdef CONFIG_NEW_NAND_CODE
29 #include <asm/arch/pxa-regs.h>
32 # define DFC_DEBUG1(fmt, args...) printf(fmt, ##args)
34 # define DFC_DEBUG1(fmt, args...)
38 # define DFC_DEBUG2(fmt, args...) printf(fmt, ##args)
40 # define DFC_DEBUG2(fmt, args...)
44 # define DFC_DEBUG3(fmt, args...) printf(fmt, ##args)
46 # define DFC_DEBUG3(fmt, args...)
49 #define MIN(x, y) ((x < y) ? x : y)
51 /* These really don't belong here, as they are specific to the NAND Model */
52 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
54 static struct nand_bbt_descr delta_bbt_descr = {
58 .pattern = scan_ff_pattern
61 static struct nand_oobinfo delta_oob = {
62 .useecc = MTD_NANDECC_AUTOPL_USR, /* MTD_NANDECC_PLACEONLY, */
64 .eccpos = {2, 3, 4, 5, 6, 7},
65 .oobfree = { {8, 2}, {12, 4} }
70 * not required for Monahans DFC
72 static void dfc_hwcontrol(struct mtd_info *mtdinfo, int cmd)
78 /* read device ready pin */
79 static int dfc_device_ready(struct mtd_info *mtdinfo)
90 * Write buf to the DFC Controller Data Buffer
92 static void dfc_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
94 unsigned long bytes_multi = len & 0xfffffffc;
95 unsigned long rest = len & 0x3;
96 unsigned long *long_buf;
99 DFC_DEBUG2("dfc_write_buf: writing %d bytes starting with 0x%x.\n", len, *((unsigned long*) buf));
101 for(i=0; i<bytes_multi; i+=4) {
102 long_buf = (unsigned long*) &buf[i];
107 printf("dfc_write_buf: ERROR, writing non 4-byte aligned data.\n");
114 * These functions are quite problematic for the DFC. Luckily they are
115 * not used in the current nand code, except for nand_command, which
116 * we've defined our own anyway. The problem is, that we always need
117 * to write 4 bytes to the DFC Data Buffer, but in these functions we
118 * don't know if to buffer the bytes/half words until we've gathered 4
119 * bytes or if to send them straight away.
121 * Solution: Don't use these with Mona's DFC and complain loudly.
123 static void dfc_write_word(struct mtd_info *mtd, u16 word)
125 printf("dfc_write_word: WARNING, this function does not work with the Monahans DFC!\n");
127 static void dfc_write_byte(struct mtd_info *mtd, u_char byte)
129 printf("dfc_write_byte: WARNING, this function does not work with the Monahans DFC!\n");
133 * static void dfc_read_buf(struct mtd_info *mtd, const u_char *buf, int len)
135 * Shouldn't this be "u_char * const buf" ?
137 static void dfc_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
141 /* we have to be carefull not to overflow the buffer if len is
142 * not a multiple of 4 */
143 unsigned long bytes_multi = len & 0xfffffffc;
144 unsigned long rest = len & 0x3;
145 unsigned long *long_buf;
147 DFC_DEBUG3("dfc_read_buf: reading %d bytes.\n", len);
148 /* if there are any, first copy multiple of 4 bytes */
150 for(i=0; i<bytes_multi; i+=4) {
151 long_buf = (unsigned long*) &buf[i];
156 /* ...then the rest */
158 unsigned long rest_data = NDDB;
160 buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
167 * read a word. Not implemented as not used in NAND code.
169 static u16 dfc_read_word(struct mtd_info *mtd)
171 printf("dfc_write_byte: UNIMPLEMENTED.\n");
175 /* global var, too bad: mk@tbd: move to ->priv pointer */
176 static unsigned long read_buf = 0;
177 static int bytes_read = -1;
180 * read a byte from NDDB Because we can only read 4 bytes from NDDB at
181 * a time, we buffer the remaining bytes. The buffer is reset when a
182 * new command is sent to the chip.
185 * This function is currently only used to read status and id
186 * bytes. For these commands always 8 bytes need to be read from
187 * NDDB. So we read and discard these bytes right now. In case this
188 * function is used for anything else in the future, we must check
189 * what was the last command issued and read the appropriate amount of
190 * bytes respectively.
192 static u_char dfc_read_byte(struct mtd_info *mtd)
202 byte = (unsigned char) (read_buf>>(8 * bytes_read++));
206 DFC_DEBUG2("dfc_read_byte: byte %u: 0x%x of (0x%x).\n", bytes_read - 1, byte, read_buf);
210 /* calculate delta between OSCR values start and now */
211 static unsigned long get_delta(unsigned long start)
213 unsigned long cur = OSCR;
215 if(cur < start) /* OSCR overflowed */
216 return (cur + (start^0xffffffff));
218 return (cur - start);
221 /* delay function, this doesn't belong here */
222 static void wait_us(unsigned long us)
224 unsigned long start = OSCR;
227 while (get_delta(start) < us) {
232 static void dfc_clear_nddb(void)
234 NDCR &= ~NDCR_ND_RUN;
235 wait_us(CFG_NAND_OTHER_TO);
238 /* wait_event with timeout */
239 static unsigned long dfc_wait_event(unsigned long event)
241 unsigned long ndsr, timeout, start = OSCR;
245 else if(event & (NDSR_CS0_CMDD | NDSR_CS0_BBD))
246 timeout = CFG_NAND_PROG_ERASE_TO * OSCR_CLK_FREQ;
248 timeout = CFG_NAND_OTHER_TO * OSCR_CLK_FREQ;
256 if(get_delta(start) > timeout) {
257 DFC_DEBUG1("dfc_wait_event: TIMEOUT waiting for event: 0x%x.\n", event);
265 /* we don't always wan't to do this */
266 static void dfc_new_cmd(void)
269 unsigned long status;
271 while(retry++ <= CFG_NAND_SENDCMD_RETRY) {
275 /* set NDCR[NDRUN] */
276 if(!(NDCR & NDCR_ND_RUN))
279 status = dfc_wait_event(NDSR_WRCMDREQ);
281 if(status & NDSR_WRCMDREQ)
284 DFC_DEBUG2("dfc_new_cmd: FAILED to get WRITECMDREQ, retry: %d.\n", retry);
287 DFC_DEBUG1("dfc_new_cmd: giving up after %d retries.\n", retry);
290 /* this function is called after Programm and Erase Operations to
291 * check for success or failure */
292 static int dfc_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
294 unsigned long ndsr=0, event=0;
296 if(state == FL_WRITING) {
297 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
298 } else if(state == FL_ERASING) {
299 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
302 ndsr = dfc_wait_event(event);
304 if((ndsr & NDSR_CS0_BBD) || (ndsr & 0xff000000))
305 return(0x1); /* Status Read error */
309 /* cmdfunc send commands to the DFC */
310 static void dfc_cmdfunc(struct mtd_info *mtd, unsigned command,
311 int column, int page_addr)
313 /* register struct nand_chip *this = mtd->priv; */
314 unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
316 /* clear the ugly byte read buffer */
322 DFC_DEBUG3("dfc_cmdfunc: NAND_CMD_READ0, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
324 ndcb0 = (NAND_CMD_READ0 | (4<<16));
325 column >>= 1; /* adjust for 16 bit bus */
326 ndcb1 = (((column>>1) & 0xff) |
327 ((page_addr<<8) & 0xff00) |
328 ((page_addr<<8) & 0xff0000) |
329 ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
333 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READ1 unimplemented!\n");
335 case NAND_CMD_READOOB:
336 DFC_DEBUG1("dfc_cmdfunc: NAND_CMD_READOOB unimplemented!\n");
338 case NAND_CMD_READID:
340 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READID.\n");
341 ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
344 case NAND_CMD_PAGEPROG:
345 /* sent as a multicommand in NAND_CMD_SEQIN */
346 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_PAGEPROG empty due to multicmd.\n");
348 case NAND_CMD_ERASE1:
349 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE1, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
351 ndcb0 = (0xd060 | (1<<25) | (2<<21) | (1<<19) | (3<<16));
352 ndcb1 = (page_addr & 0x00ffffff);
354 case NAND_CMD_ERASE2:
355 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE2 empty due to multicmd.\n");
358 /* send PAGE_PROG command(0x1080) */
360 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
361 ndcb0 = (0x1080 | (1<<25) | (1<<21) | (1<<19) | (4<<16));
362 column >>= 1; /* adjust for 16 bit bus */
363 ndcb1 = (((column>>1) & 0xff) |
364 ((page_addr<<8) & 0xff00) |
365 ((page_addr<<8) & 0xff0000) |
366 ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
369 case NAND_CMD_STATUS:
370 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_STATUS.\n");
372 ndcb0 = NAND_CMD_STATUS | (4<<21);
376 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_RESET.\n");
377 ndcb0 = NAND_CMD_RESET | (5<<21);
378 event = NDSR_CS0_CMDD;
381 printk("dfc_cmdfunc: error, unsupported command.\n");
391 dfc_wait_event(event);
396 static void dfc_gpio_init(void)
398 DFC_DEBUG2("Setting up DFC GPIO's.\n");
400 /* no idea what is done here, see zylonite.c */
403 DF_ALE_WE1 = 0x00000001;
404 DF_ALE_WE2 = 0x00000001;
405 DF_nCS0 = 0x00000001;
406 DF_nCS1 = 0x00000001;
414 DF_IO10 = 0x00000001;
416 DF_IO11 = 0x00000001;
418 DF_IO12 = 0x00000001;
420 DF_IO13 = 0x00000001;
422 DF_IO14 = 0x00000001;
424 DF_IO15 = 0x00000001;
434 * Board-specific NAND initialization. The following members of the
435 * argument are board-specific (per include/linux/mtd/nand_new.h):
436 * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
437 * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
438 * - hwcontrol: hardwarespecific function for accesing control-lines
439 * - dev_ready: hardwarespecific function for accesing device ready/busy line
440 * - enable_hwecc?: function to enable (reset) hardware ecc generator. Must
441 * only be provided if a hardware ECC is available
442 * - eccmode: mode of ecc, see defines
443 * - chip_delay: chip dependent delay for transfering data from array to
445 * - options: various chip options. They can partly be set to inform
446 * nand_scan about special functionality. See the defines for further
448 * Members with a "?" were not set in the merged testing-NAND branch,
449 * so they are not set here either.
451 void board_nand_init(struct nand_chip *nand)
453 unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
455 /* set up GPIO Control Registers */
458 /* turn on the NAND Controller Clock (104 MHz @ D0) */
459 CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
461 #undef CFG_TIMING_TIGHT
462 #ifndef CFG_TIMING_TIGHT
463 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
465 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
467 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
469 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
471 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
473 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
475 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
477 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
479 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
481 #else /* this is the tight timing */
483 tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US)),
485 tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US)),
487 tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US)),
489 tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US)),
491 tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US)),
493 tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US)),
495 tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) - tCH - 2),
497 tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) - tCH - 2),
499 tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) - 2),
501 #endif /* CFG_TIMING_TIGHT */
504 DFC_DEBUG2("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);
506 /* tRP value is split in the register */
514 NDTR0CS0 = (tCH << 19) |
522 NDTR1CS0 = (tR << 16) |
526 /* If it doesn't work (unlikely) think about:
528 * - chip select don't care
529 * - read id byte count
531 * Intentionally enabled by not setting bits:
534 * - cs don't care, see if we can enable later!
535 * - row address start position (after second cycle)
536 * - pages per block = 32
537 * - ND_RDY : clears command buffer
539 /* NDCR_NCSX | /\* Chip select busy don't care *\/ */
541 NDCR = (NDCR_SPARE_EN | /* use the spare area */
542 NDCR_DWIDTH_C | /* 16bit DFC data bus width */
543 NDCR_DWIDTH_M | /* 16 bit Flash device data bus width */
544 (2 << 16) | /* read id count = 7 ???? mk@tbd */
545 NDCR_ND_ARB_EN | /* enable bus arbiter */
546 NDCR_RDYM | /* flash device ready ir masked */
547 NDCR_CS0_PAGEDM | /* ND_nCSx page done ir masked */
549 NDCR_CS0_CMDDM | /* ND_CSx command done ir masked */
551 NDCR_CS0_BBDM | /* ND_CSx bad block detect ir masked */
553 NDCR_DBERRM | /* double bit error ir masked */
554 NDCR_SBERRM | /* single bit error ir masked */
555 NDCR_WRDREQM | /* write data request ir masked */
556 NDCR_RDDREQM | /* read data request ir masked */
557 NDCR_WRCMDREQM); /* write command request ir masked */
560 /* wait 10 us due to cmd buffer clear reset */
564 nand->hwcontrol = dfc_hwcontrol;
565 /* nand->dev_ready = dfc_device_ready; */
566 nand->eccmode = NAND_ECC_SOFT;
567 nand->options = NAND_BUSWIDTH_16;
568 nand->waitfunc = dfc_wait;
569 nand->read_byte = dfc_read_byte;
570 nand->write_byte = dfc_write_byte;
571 nand->read_word = dfc_read_word;
572 nand->write_word = dfc_write_word;
573 nand->read_buf = dfc_read_buf;
574 nand->write_buf = dfc_write_buf;
576 nand->cmdfunc = dfc_cmdfunc;
577 nand->autooob = &delta_oob;
578 nand->badblock_pattern = &delta_bbt_descr;
582 #error "U-Boot legacy NAND support not available for Monahans DFC."