88e190268e2cbba9220fc83b6c5e286a39cad478
[platform/kernel/u-boot.git] / board / delta / nand.c
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
23 #include <common.h>
24
25 #if (CONFIG_COMMANDS & CFG_CMD_NAND)
26 #ifdef CONFIG_NEW_NAND_CODE
27
28 #include <nand.h>
29 #include <asm/arch/pxa-regs.h>
30
31 /* mk@tbd move this to pxa-regs */
32 #define OSCR_CLK_FREQ 3.250 /* MHz */
33
34 /* usefull */
35 #define CFG_DFC_DEBUG1
36 /* noisy */
37 #undef CFG_DFC_DEBUG2
38 /* wild west */
39 #undef CFG_DFC_DEBUG3
40
41
42 #ifdef CFG_DFC_DEBUG1
43 # define DFC_DEBUG1(fmt, args...) printf(fmt, ##args)
44 #else
45 # define DFC_DEBUG1(fmt, args...)
46 #endif
47
48 #ifdef CFG_DFC_DEBUG2
49 # define DFC_DEBUG2(fmt, args...) printf(fmt, ##args)
50 #else
51 # define DFC_DEBUG2(fmt, args...)
52 #endif
53
54 #ifdef CFG_DFC_DEBUG3
55 # define DFC_DEBUG3(fmt, args...) printf(fmt, ##args)
56 #else
57 # define DFC_DEBUG3(fmt, args...)
58 #endif
59
60 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
61
62 static struct nand_bbt_descr delta_bbt_descr = {
63         .options = 0,
64         .offs = 0,
65         .len = 2,
66         .pattern = scan_ff_pattern
67 };
68
69 static struct nand_oobinfo delta_oob = {
70         .useecc = MTD_NANDECC_AUTOPL_USR, /* MTD_NANDECC_PLACEONLY, */
71         .eccbytes = 6,
72         .eccpos = {2, 3, 4, 5, 6, 7},
73         .oobfree = { {8, 2}, {12, 4} }
74 };
75
76
77 /*
78  * not required for Monahans DFC
79  */
80 static void delta_hwcontrol(struct mtd_info *mtdinfo, int cmd)
81 {
82         return;
83 }
84
85 /* read device ready pin */
86 static int delta_device_ready(struct mtd_info *mtdinfo)
87 {
88         if(NDSR & NDSR_RDY)
89                 return 1;
90         else
91                 return 0;
92         return 0;
93 }
94
95 /*
96  * Write buf to the DFC Controller Data Buffer
97  */
98 static void delta_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
99 {
100         unsigned long bytes_multi = len & 0xfffffffc;
101         unsigned long rest = len & 0x3;
102         unsigned long *long_buf;
103         int i;
104         
105         DFC_DEBUG2("delta_write_buf: writing %d bytes starting with 0x%x.\n", len, *((unsigned long*) buf));
106         if(bytes_multi) {
107                 for(i=0; i<bytes_multi; i+=4) {
108                         long_buf = (unsigned long*) &buf[i];
109                         NDDB = *long_buf;
110                 }
111         }
112         if(rest) {
113                 printf("delta_write_buf: ERROR, writing non 4-byte aligned data.\n");
114         }
115         return;
116 }
117
118
119 /* 
120  * These functions are quite problematic for the DFC. Luckily they are
121  * not used in the current nand code, except for nand_command, which
122  * we've defined our own anyway. The problem is, that we always need
123  * to write 4 bytes to the DFC Data Buffer, but in these functions we
124  * don't know if to buffer the bytes/half words until we've gathered 4
125  * bytes or if to send them straight away.
126  *
127  * Solution: Don't use these with Mona's DFC and complain loudly.
128  */
129 static void delta_write_word(struct mtd_info *mtd, u16 word)
130 {
131         printf("delta_write_word: WARNING, this function does not work with the Monahans DFC!\n");
132 }
133 static void delta_write_byte(struct mtd_info *mtd, u_char byte)
134 {
135         printf("delta_write_byte: WARNING, this function does not work with the Monahans DFC!\n");
136 }
137
138 /* The original:
139  * static void delta_read_buf(struct mtd_info *mtd, const u_char *buf, int len)
140  *
141  * Shouldn't this be "u_char * const buf" ?
142  */
143 static void delta_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
144 {
145         int i, j;
146
147         /* we have to be carefull not to overflow the buffer if len is
148          * not a multiple of 4 */
149         unsigned long bytes_multi = len & 0xfffffffc;
150         unsigned long rest = len & 0x3;
151         unsigned long *long_buf;
152
153         DFC_DEBUG3("delta_read_buf: reading %d bytes.\n", len);
154         /* if there are any, first copy multiple of 4 bytes */
155         if(bytes_multi) {
156                 for(i=0; i<bytes_multi; i+=4) {
157                         long_buf = (unsigned long*) &buf[i];
158                         *long_buf = NDDB;
159                 }
160         }
161         
162         /* ...then the rest */
163         if(rest) {
164                 unsigned long rest_data = NDDB;
165                 for(j=0;j<rest; j++)
166                         buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
167         }
168
169         return;
170 }
171
172 /*
173  * read a word. Not implemented as not used in NAND code.
174  */
175 static u16 delta_read_word(struct mtd_info *mtd)
176 {
177         printf("delta_write_byte: UNIMPLEMENTED.\n");
178 }
179
180 /* global var, too bad: mk@tbd: move to ->priv pointer */
181 static unsigned long read_buf = 0;
182 static int bytes_read = -1;
183 static unsigned long last_cmd = 0;
184
185 /* read a byte from NDDB Because we can only read 4 bytes from NDDB at
186  * a time, we buffer the remaining bytes. The buffer is reset when a
187  * new command is sent to the chip.
188  */
189 static u_char delta_read_byte(struct mtd_info *mtd)
190 {
191 /*      struct nand_chip *this = mtd->priv; */
192         unsigned char byte;
193         unsigned long dummy;
194
195         if(bytes_read < 0) {
196                 read_buf = NDDB;
197                 dummy = NDDB;           
198                 bytes_read = 0;
199         }
200         byte = (unsigned char) (read_buf>>(8 * bytes_read++));
201         if(bytes_read >= 4)
202                 bytes_read = -1;
203
204         DFC_DEBUG2("delta_read_byte: byte %u: 0x%x of (0x%x).\n", bytes_read - 1, byte, read_buf);
205         return byte;
206 }
207
208 /* calculate delta between OSCR values start and now  */
209 static unsigned long get_delta(unsigned long start)
210 {
211         unsigned long cur = OSCR;
212         
213         if(cur < start) /* OSCR overflowed */
214                 return (cur + (start^0xffffffff));
215         else
216                 return (cur - start);
217 }
218
219 /* delay function, this doesn't belong here */
220 static void wait_us(unsigned long us)
221 {
222         unsigned long start = OSCR;
223         us *= OSCR_CLK_FREQ;
224
225         while (get_delta(start) < us) {
226                 /* do nothing */
227         }
228 }
229
230 static void delta_clear_nddb()
231 {
232         NDCR &= ~NDCR_ND_RUN;
233         wait_us(CFG_NAND_OTHER_TO);
234 }
235
236 /* wait_event with timeout */
237 static unsigned long delta_wait_event2(unsigned long event)
238 {
239         unsigned long ndsr, timeout, start = OSCR;
240         
241         if(!event)
242                 return 0xff000000;
243         else if(event & (NDSR_CS0_CMDD | NDSR_CS0_BBD))
244                 timeout = CFG_NAND_PROG_ERASE_TO * OSCR_CLK_FREQ;
245         else
246                 timeout = CFG_NAND_OTHER_TO * OSCR_CLK_FREQ;
247         
248         while(1) {
249                 ndsr = NDSR;
250                 if(ndsr & event) {
251                         NDSR |= event;
252                         break;
253                 }
254                 if(get_delta(start) > timeout) {
255                         DFC_DEBUG1("delta_wait_event: TIMEOUT waiting for event: 0x%x.\n", event);
256                         return 0xff000000;
257                 }
258                 
259         }
260         return ndsr;
261 }
262
263
264 #if DEADCODE
265 /* poll the NAND Controller Status Register for event */
266 static void delta_wait_event(unsigned long event)
267 {
268         if(!event)
269                 return;
270         
271         while(1) {
272                 if(NDSR & event) {
273                         NDSR |= event;
274                         break;
275                 }
276         }
277 }
278 #endif
279
280 /* we don't always wan't to do this */
281 static void delta_new_cmd()
282 {
283         int retry = 0;
284         unsigned long status;
285
286         while(retry++ <= CFG_NAND_SENDCMD_RETRY) {
287                 /* Clear NDSR */
288                 NDSR = 0xFFF;
289                 
290                 /* set NDCR[NDRUN] */
291                 if(!(NDCR & NDCR_ND_RUN))
292                         NDCR |= NDCR_ND_RUN;
293                 
294                 status = delta_wait_event2(NDSR_WRCMDREQ);
295                 
296                 if(status & NDSR_WRCMDREQ)
297                         return;
298
299                 DFC_DEBUG2("delta_new_cmd: FAILED to get WRITECMDREQ, retry: %d.\n", retry);
300                 delta_clear_nddb();
301         }
302         DFC_DEBUG1("delta_new_cmd: giving up after %d retries.\n", retry);
303                 
304 #if DEADCODE            
305         while(1) {
306                 if(NDSR & NDSR_WRCMDREQ) {
307                         NDSR |= NDSR_WRCMDREQ; /* Ack */
308                         break;
309                 }
310         }
311 #endif
312         
313 }
314 /* this function is called after Programm and Erase Operations to
315  * check for success or failure */
316 static int delta_wait(struct mtd_info *mtd, struct nand_chip *this, int state)
317 {
318         unsigned long ndsr=0, event=0;
319
320         /* mk@tbd set appropriate timeouts */
321         /*      if (state == FL_ERASING) */
322         /*              timeo = CFG_HZ * 400; */
323         /*      else */
324         /*              timeo = CFG_HZ * 20; */
325         if(state == FL_WRITING) {
326                 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
327         } else if(state == FL_ERASING) {
328                 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
329         }
330         
331         ndsr = delta_wait_event2(event);
332
333         if((ndsr & NDSR_CS0_BBD) || (ndsr & 0xff000000))
334                 return(0x1); /* Status Read error */
335         return 0;
336 }
337
338 /* cmdfunc send commands to the DFC */
339 static void delta_cmdfunc(struct mtd_info *mtd, unsigned command, 
340                           int column, int page_addr)
341 {
342         /* register struct nand_chip *this = mtd->priv; */
343         unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
344         unsigned long what_the_hack;
345
346         /* clear the ugly byte read buffer */
347         bytes_read = -1;
348         read_buf = 0;
349         last_cmd = 0;
350
351         /* if command is a double byte cmd, we set bit double cmd bit 19  */
352         /*      command2 = (command>>8) & 0xFF;  */
353         /*      ndcb0 = command | ((command2 ? 1 : 0) << 19); *\/ */
354
355         switch (command) {
356         case NAND_CMD_READ0:
357                 DFC_DEBUG3("delta_cmdfunc: NAND_CMD_READ0, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
358                 delta_new_cmd();
359                 ndcb0 = (NAND_CMD_READ0 | (4<<16));
360                 column >>= 1; /* adjust for 16 bit bus */
361                 ndcb1 = (((column>>1) & 0xff) |
362                          ((page_addr<<8) & 0xff00) |
363                          ((page_addr<<8) & 0xff0000) |
364                          ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
365                 event = NDSR_RDDREQ;
366                 goto write_cmd;
367         case NAND_CMD_READ1:
368                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_READ1 unimplemented!\n");
369                 goto end;
370         case NAND_CMD_READOOB:
371                 DFC_DEBUG1("delta_cmdfunc: NAND_CMD_READOOB unimplemented!\n");
372                 goto end;
373         case NAND_CMD_READID:
374                 last_cmd = NAND_CMD_READID;
375                 delta_new_cmd();
376                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_READID.\n");
377                 ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
378                 event = NDSR_RDDREQ;
379                 goto write_cmd;
380         case NAND_CMD_PAGEPROG:
381                 /* sent as a multicommand in NAND_CMD_SEQIN */
382                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_PAGEPROG empty due to multicmd.\n");
383                 goto end;
384         case NAND_CMD_ERASE1:
385                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_ERASE1,  page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
386                 delta_new_cmd();
387                 ndcb0 = (0xd060 | (1<<25) | (2<<21) | (1<<19) | (3<<16));
388                 ndcb1 = (page_addr & 0x00ffffff);
389                 goto write_cmd;
390         case NAND_CMD_ERASE2:
391                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_ERASE2 empty due to multicmd.\n");
392                 goto end;
393         case NAND_CMD_SEQIN:
394                 /* send PAGE_PROG command(0x1080) */
395                 delta_new_cmd();
396                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG,  page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
397                 ndcb0 = (0x1080 | (1<<25) | (1<<21) | (1<<19) | (4<<16));
398                 column >>= 1; /* adjust for 16 bit bus */
399                 ndcb1 = (((column>>1) & 0xff) |
400                          ((page_addr<<8) & 0xff00) |
401                          ((page_addr<<8) & 0xff0000) |
402                          ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
403                 event = NDSR_WRDREQ;
404                 goto write_cmd;
405 /*      case NAND_CMD_SEQIN_pointer_operation: */
406
407 /*              /\* This is confusing because the command names are */
408 /*               * different compared to the ones in the K9K12Q0C */
409 /*               * datasheet. Infact this has nothing to do with */
410 /*               * reading, as the but with page programming */
411 /*               * (writing). */
412 /*               * Here we send the multibyte commands */
413 /*               * cmd1=0x00, cmd2=0x80 (for programming main area) or */
414 /*               * cmd1=0x50, cmd2=0x80 (for spare area) */
415 /*               * */
416 /*               * When all data is written to the buffer, the page */
417 /*               * program command (0x10) is sent to actually write */
418 /*               * the data. */
419 /*               *\/ */
420
421 /*              printf("delta_cmdfunc: NAND_CMD_SEQIN pointer op called.\n"); */
422
423 /*              ndcb0 = (NAND_CMD_SEQIN<<8) | (1<<21) | (1<<19) | (4<<16); */
424 /*              if(column >= mtd->oobblock) { */
425 /*                      /\* OOB area *\/ */
426 /*                      column -= mtd->oobblock; */
427 /*                      ndcb0 |= NAND_CMD_READOOB; */
428 /*              } else if (column < 256) { */
429 /*                      /\* First 256 bytes --> READ0 *\/ */
430 /*                      ndcb0 |= NAND_CMD_READ0; */
431 /*              } else { */
432 /*                      /\* Only for 8 bit devices - not delta!!! *\/ */
433 /*                      column -= 256; */
434 /*                      ndcb0 |= NAND_CMD_READ1; */
435 /*              } */
436 /*              event = NDSR_WRDREQ; */
437 /*              break; */
438         case NAND_CMD_STATUS:
439                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_STATUS.\n");
440                 /* oh, this is not nice. for some reason the real
441                  * status byte is in the second read from the data
442                  * buffer. The hack is to read the first byte right
443                  * here, so the next read access by the nand code
444                  * yields the right one.
445                  */
446                 delta_new_cmd();
447                 ndcb0 = NAND_CMD_STATUS | (4<<21);
448                 event = NDSR_RDDREQ;
449 #undef READ_STATUS_BUG
450 #ifdef READ_STATUS_BUG
451                 NDCB0 = ndcb0;
452                 NDCB0 = ndcb1;
453                 NDCB0 = ndcb2;
454                 delta_wait_event2(event);
455                 what_the_hack = NDDB;
456                 if(what_the_hack != 0xffffffff) {
457                         DFC_DEBUG2("what the hack.\n");
458                         read_buf = what_the_hack;
459                         bytes_read = 0;
460                 }
461                 goto end;
462 #endif
463                 goto write_cmd;
464         case NAND_CMD_RESET:
465                 DFC_DEBUG2("delta_cmdfunc: NAND_CMD_RESET.\n");
466                 ndcb0 = NAND_CMD_RESET | (5<<21);
467                 event = NDSR_CS0_CMDD;
468                 goto write_cmd;
469         default:
470                 printk("delta_cmdfunc: error, unsupported command.\n");
471                 goto end;
472         }
473
474  write_cmd:
475         NDCB0 = ndcb0;
476         NDCB0 = ndcb1;
477         NDCB0 = ndcb2;
478
479  wait_event:
480         delta_wait_event2(event);
481  end:
482         return;
483 }
484
485 static void delta_dfc_gpio_init()
486 {
487         DFC_DEBUG2("Setting up DFC GPIO's.\n");
488
489         /* no idea what is done here, see zylonite.c */
490         GPIO4 = 0x1;
491         
492         DF_ALE_WE1 = 0x00000001;
493         DF_ALE_WE2 = 0x00000001;
494         DF_nCS0 = 0x00000001;
495         DF_nCS1 = 0x00000001;
496         DF_nWE = 0x00000001;
497         DF_nRE = 0x00000001;
498         DF_IO0 = 0x00000001;
499         DF_IO8 = 0x00000001;
500         DF_IO1 = 0x00000001;
501         DF_IO9 = 0x00000001;
502         DF_IO2 = 0x00000001;
503         DF_IO10 = 0x00000001;
504         DF_IO3 = 0x00000001;
505         DF_IO11 = 0x00000001;
506         DF_IO4 = 0x00000001;
507         DF_IO12 = 0x00000001;
508         DF_IO5 = 0x00000001;
509         DF_IO13 = 0x00000001;
510         DF_IO6 = 0x00000001;
511         DF_IO14 = 0x00000001;
512         DF_IO7 = 0x00000001;
513         DF_IO15 = 0x00000001;
514
515         DF_nWE = 0x1901;
516         DF_nRE = 0x1901;
517         DF_CLE_NOE = 0x1900;
518         DF_ALE_WE1 = 0x1901;
519         DF_INT_RnB = 0x1900;
520 }
521
522 /*
523  * Board-specific NAND initialization. The following members of the
524  * argument are board-specific (per include/linux/mtd/nand_new.h):
525  * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
526  * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
527  * - hwcontrol: hardwarespecific function for accesing control-lines
528  * - dev_ready: hardwarespecific function for  accesing device ready/busy line
529  * - enable_hwecc?: function to enable (reset)  hardware ecc generator. Must
530  *   only be provided if a hardware ECC is available
531  * - eccmode: mode of ecc, see defines
532  * - chip_delay: chip dependent delay for transfering data from array to
533  *   read regs (tR)
534  * - options: various chip options. They can partly be set to inform
535  *   nand_scan about special functionality. See the defines for further
536  *   explanation
537  * Members with a "?" were not set in the merged testing-NAND branch,
538  * so they are not set here either.
539  */
540 void board_nand_init(struct nand_chip *nand)
541 {
542         unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
543
544         /* set up GPIO Control Registers */
545         delta_dfc_gpio_init();
546
547         /* turn on the NAND Controller Clock (104 MHz @ D0) */
548         CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
549
550         /* wait ? */
551 /*      printf("stupid loop start...\n"); */
552 /*      wait(200); */
553 /*      printf("stupid loop end.\n"); */
554                 
555
556         /* NAND Timing Parameters (in ns) */
557 #define NAND_TIMING_tCH         10
558 #define NAND_TIMING_tCS         0
559 #define NAND_TIMING_tWH         20
560 #define NAND_TIMING_tWP         40
561
562 #define NAND_TIMING_tRH         20
563 #define NAND_TIMING_tRP         40
564
565 /* #define NAND_TIMING_tRH      25 */
566 /* #define NAND_TIMING_tRP      50 */
567
568 #define NAND_TIMING_tR          11123
569 /* #define NAND_TIMING_tWHR     110 */
570 #define NAND_TIMING_tWHR        100
571 #define NAND_TIMING_tAR         10
572
573 /* Maximum values for NAND Interface Timing Registers in DFC clock
574  * periods */
575 #define DFC_MAX_tCH             7
576 #define DFC_MAX_tCS             7
577 #define DFC_MAX_tWH             7
578 #define DFC_MAX_tWP             7
579 #define DFC_MAX_tRH             7
580 #define DFC_MAX_tRP             15
581 #define DFC_MAX_tR              65535
582 #define DFC_MAX_tWHR            15
583 #define DFC_MAX_tAR             15
584
585 #define DFC_CLOCK               104             /* DFC Clock is 104 MHz */
586 #define DFC_CLK_PER_US          DFC_CLOCK/1000  /* clock period in ns */
587 #define MIN(x, y)               ((x < y) ? x : y)
588
589
590 #undef CFG_TIMING_TIGHT
591 #ifndef CFG_TIMING_TIGHT 
592         tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1), 
593                   DFC_MAX_tCH);
594         tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1), 
595                   DFC_MAX_tCS);
596         tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
597                   DFC_MAX_tWH);
598         tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
599                   DFC_MAX_tWP);
600         tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
601                   DFC_MAX_tRH);
602         tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
603                   DFC_MAX_tRP);
604         tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
605                  DFC_MAX_tR);
606         tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
607                    DFC_MAX_tWHR);
608         tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
609                   DFC_MAX_tAR);
610 #else /* this is the tight timing */
611
612         tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US)), 
613                   DFC_MAX_tCH);
614         tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US)), 
615                   DFC_MAX_tCS);
616         tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US)),
617                   DFC_MAX_tWH);
618         tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US)),
619                   DFC_MAX_tWP);
620         tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US)),
621                   DFC_MAX_tRH);
622         tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US)),
623                   DFC_MAX_tRP);
624         tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) - tCH - 2),
625                  DFC_MAX_tR);
626         tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) - tCH - 2),
627                    DFC_MAX_tWHR);
628         tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) - 2),
629                   DFC_MAX_tAR);
630 #endif /* CFG_TIMING_TIGHT */
631
632
633         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);
634
635         /* tRP value is split in the register */
636         if(tRP & (1 << 4)) {
637                 tRP_high = 1;
638                 tRP &= ~(1 << 4);
639         } else {
640                 tRP_high = 0;
641         }
642
643         NDTR0CS0 = (tCH << 19) |
644                 (tCS << 16) |
645                 (tWH << 11) |
646                 (tWP << 8) |
647                 (tRP_high << 6) |
648                 (tRH << 3) |
649                 (tRP << 0);
650         
651         NDTR1CS0 = (tR << 16) |
652                 (tWHR << 4) |
653                 (tAR << 0);
654
655         
656
657         /* If it doesn't work (unlikely) think about:
658          *  - ecc enable
659          *  - chip select don't care
660          *  - read id byte count
661          *
662          * Intentionally enabled by not setting bits:
663          *  - dma (DMA_EN)
664          *  - page size = 512
665          *  - cs don't care, see if we can enable later!
666          *  - row address start position (after second cycle)
667          *  - pages per block = 32
668          *  - ND_RDY : clears command buffer
669          */
670         /* NDCR_NCSX |          /\* Chip select busy don't care *\/ */
671         
672         NDCR = (NDCR_SPARE_EN |         /* use the spare area */
673                 NDCR_DWIDTH_C |         /* 16bit DFC data bus width  */
674                 NDCR_DWIDTH_M |         /* 16 bit Flash device data bus width */
675                 (2 << 16) |             /* read id count = 7 ???? mk@tbd */
676                 NDCR_ND_ARB_EN |        /* enable bus arbiter */
677                 NDCR_RDYM |             /* flash device ready ir masked */
678                 NDCR_CS0_PAGEDM |       /* ND_nCSx page done ir masked */
679                 NDCR_CS1_PAGEDM |
680                 NDCR_CS0_CMDDM |        /* ND_CSx command done ir masked */
681                 NDCR_CS1_CMDDM |
682                 NDCR_CS0_BBDM |         /* ND_CSx bad block detect ir masked */
683                 NDCR_CS1_BBDM |
684                 NDCR_DBERRM |           /* double bit error ir masked */ 
685                 NDCR_SBERRM |           /* single bit error ir masked */
686                 NDCR_WRDREQM |          /* write data request ir masked */
687                 NDCR_RDDREQM |          /* read data request ir masked */
688                 NDCR_WRCMDREQM);        /* write command request ir masked */
689         
690
691         /* wait 10 us due to cmd buffer clear reset */
692         /*      wait(10); */
693         
694         
695         nand->hwcontrol = delta_hwcontrol;
696 /*      nand->dev_ready = delta_device_ready; */
697         nand->eccmode = NAND_ECC_SOFT;
698         nand->chip_delay = NAND_DELAY_US;
699         nand->options = NAND_BUSWIDTH_16;
700         nand->waitfunc = delta_wait;
701         nand->read_byte = delta_read_byte;
702         nand->write_byte = delta_write_byte;
703         nand->read_word = delta_read_word;
704         nand->write_word = delta_write_word;
705         nand->read_buf = delta_read_buf;
706         nand->write_buf = delta_write_buf;
707
708         nand->cmdfunc = delta_cmdfunc;
709         nand->autooob = &delta_oob;
710         nand->badblock_pattern = &delta_bbt_descr;
711 }
712
713 #else
714  #error "U-Boot legacy NAND support not available for delta board."
715 #endif
716 #endif