Remove legacy NAND and disk on chip code.
[platform/kernel/u-boot.git] / board / zylonite / 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 defined(CONFIG_CMD_NAND)
26 #ifdef CONFIG_NEW_NAND_CODE
27
28 #include <nand.h>
29 #include <asm/arch/pxa-regs.h>
30
31 #ifdef CONFIG_SYS_DFC_DEBUG1
32 # define DFC_DEBUG1(fmt, args...) printf(fmt, ##args)
33 #else
34 # define DFC_DEBUG1(fmt, args...)
35 #endif
36
37 #ifdef CONFIG_SYS_DFC_DEBUG2
38 # define DFC_DEBUG2(fmt, args...) printf(fmt, ##args)
39 #else
40 # define DFC_DEBUG2(fmt, args...)
41 #endif
42
43 #ifdef CONFIG_SYS_DFC_DEBUG3
44 # define DFC_DEBUG3(fmt, args...) printf(fmt, ##args)
45 #else
46 # define DFC_DEBUG3(fmt, args...)
47 #endif
48
49 /* These really don't belong here, as they are specific to the NAND Model */
50 static uint8_t scan_ff_pattern[] = { 0xff, 0xff };
51
52 static struct nand_bbt_descr delta_bbt_descr = {
53         .options = 0,
54         .offs = 0,
55         .len = 2,
56         .pattern = scan_ff_pattern
57 };
58
59 static struct nand_ecclayout delta_oob = {
60         .eccbytes = 6,
61         .eccpos = {2, 3, 4, 5, 6, 7},
62         .oobfree = { {8, 2}, {12, 4} }
63 };
64
65 /*
66  * not required for Monahans DFC
67  */
68 static void dfc_hwcontrol(struct mtd_info *mtd, int cmd, unsigned int ctrl)
69 {
70         return;
71 }
72
73 #if 0
74 /* read device ready pin */
75 static int dfc_device_ready(struct mtd_info *mtdinfo)
76 {
77         if(NDSR & NDSR_RDY)
78                 return 1;
79         else
80                 return 0;
81         return 0;
82 }
83 #endif
84
85 /*
86  * Write buf to the DFC Controller Data Buffer
87  */
88 static void dfc_write_buf(struct mtd_info *mtd, const u_char *buf, int len)
89 {
90         unsigned long bytes_multi = len & 0xfffffffc;
91         unsigned long rest = len & 0x3;
92         unsigned long *long_buf;
93         int i;
94
95         DFC_DEBUG2("dfc_write_buf: writing %d bytes starting with 0x%x.\n", len, *((unsigned long*) buf));
96         if(bytes_multi) {
97                 for(i=0; i<bytes_multi; i+=4) {
98                         long_buf = (unsigned long*) &buf[i];
99                         NDDB = *long_buf;
100                 }
101         }
102         if(rest) {
103                 printf("dfc_write_buf: ERROR, writing non 4-byte aligned data.\n");
104         }
105         return;
106 }
107
108
109 /* The original:
110  * static void dfc_read_buf(struct mtd_info *mtd, const u_char *buf, int len)
111  *
112  * Shouldn't this be "u_char * const buf" ?
113  */
114 static void dfc_read_buf(struct mtd_info *mtd, u_char* const buf, int len)
115 {
116         int i=0, j;
117
118         /* we have to be carefull not to overflow the buffer if len is
119          * not a multiple of 4 */
120         unsigned long bytes_multi = len & 0xfffffffc;
121         unsigned long rest = len & 0x3;
122         unsigned long *long_buf;
123
124         DFC_DEBUG3("dfc_read_buf: reading %d bytes.\n", len);
125         /* if there are any, first copy multiple of 4 bytes */
126         if(bytes_multi) {
127                 for(i=0; i<bytes_multi; i+=4) {
128                         long_buf = (unsigned long*) &buf[i];
129                         *long_buf = NDDB;
130                 }
131         }
132
133         /* ...then the rest */
134         if(rest) {
135                 unsigned long rest_data = NDDB;
136                 for(j=0;j<rest; j++)
137                         buf[i+j] = (u_char) ((rest_data>>j) & 0xff);
138         }
139
140         return;
141 }
142
143 /*
144  * read a word. Not implemented as not used in NAND code.
145  */
146 static u16 dfc_read_word(struct mtd_info *mtd)
147 {
148         printf("dfc_read_word: UNIMPLEMENTED.\n");
149         return 0;
150 }
151
152 /* global var, too bad: mk@tbd: move to ->priv pointer */
153 static unsigned long read_buf = 0;
154 static int bytes_read = -1;
155
156 /*
157  * read a byte from NDDB Because we can only read 4 bytes from NDDB at
158  * a time, we buffer the remaining bytes. The buffer is reset when a
159  * new command is sent to the chip.
160  *
161  * WARNING:
162  * This function is currently only used to read status and id
163  * bytes. For these commands always 8 bytes need to be read from
164  * NDDB. So we read and discard these bytes right now. In case this
165  * function is used for anything else in the future, we must check
166  * what was the last command issued and read the appropriate amount of
167  * bytes respectively.
168  */
169 static u_char dfc_read_byte(struct mtd_info *mtd)
170 {
171         unsigned char byte;
172         unsigned long dummy;
173
174         if(bytes_read < 0) {
175                 read_buf = NDDB;
176                 dummy = NDDB;
177                 bytes_read = 0;
178         }
179         byte = (unsigned char) (read_buf>>(8 * bytes_read++));
180         if(bytes_read >= 4)
181                 bytes_read = -1;
182
183         DFC_DEBUG2("dfc_read_byte: byte %u: 0x%x of (0x%x).\n", bytes_read - 1, byte, read_buf);
184         return byte;
185 }
186
187 /* calculate delta between OSCR values start and now  */
188 static unsigned long get_delta(unsigned long start)
189 {
190         unsigned long cur = OSCR;
191
192         if(cur < start) /* OSCR overflowed */
193                 return (cur + (start^0xffffffff));
194         else
195                 return (cur - start);
196 }
197
198 /* delay function, this doesn't belong here */
199 static void wait_us(unsigned long us)
200 {
201         unsigned long start = OSCR;
202         us *= OSCR_CLK_FREQ;
203
204         while (get_delta(start) < us) {
205                 /* do nothing */
206         }
207 }
208
209 static void dfc_clear_nddb(void)
210 {
211         NDCR &= ~NDCR_ND_RUN;
212         wait_us(CONFIG_SYS_NAND_OTHER_TO);
213 }
214
215 /* wait_event with timeout */
216 static unsigned long dfc_wait_event(unsigned long event)
217 {
218         unsigned long ndsr, timeout, start = OSCR;
219
220         if(!event)
221                 return 0xff000000;
222         else if(event & (NDSR_CS0_CMDD | NDSR_CS0_BBD))
223                 timeout = CONFIG_SYS_NAND_PROG_ERASE_TO * OSCR_CLK_FREQ;
224         else
225                 timeout = CONFIG_SYS_NAND_OTHER_TO * OSCR_CLK_FREQ;
226
227         while(1) {
228                 ndsr = NDSR;
229                 if(ndsr & event) {
230                         NDSR |= event;
231                         break;
232                 }
233                 if(get_delta(start) > timeout) {
234                         DFC_DEBUG1("dfc_wait_event: TIMEOUT waiting for event: 0x%lx.\n", event);
235                         return 0xff000000;
236                 }
237
238         }
239         return ndsr;
240 }
241
242 /* we don't always wan't to do this */
243 static void dfc_new_cmd(void)
244 {
245         int retry = 0;
246         unsigned long status;
247
248         while(retry++ <= CONFIG_SYS_NAND_SENDCMD_RETRY) {
249                 /* Clear NDSR */
250                 NDSR = 0xFFF;
251
252                 /* set NDCR[NDRUN] */
253                 if(!(NDCR & NDCR_ND_RUN))
254                         NDCR |= NDCR_ND_RUN;
255
256                 status = dfc_wait_event(NDSR_WRCMDREQ);
257
258                 if(status & NDSR_WRCMDREQ)
259                         return;
260
261                 DFC_DEBUG2("dfc_new_cmd: FAILED to get WRITECMDREQ, retry: %d.\n", retry);
262                 dfc_clear_nddb();
263         }
264         DFC_DEBUG1("dfc_new_cmd: giving up after %d retries.\n", retry);
265 }
266
267 /* this function is called after Programm and Erase Operations to
268  * check for success or failure */
269 static int dfc_wait(struct mtd_info *mtd, struct nand_chip *this)
270 {
271         unsigned long ndsr=0, event=0;
272         int state = this->state;
273
274         if(state == FL_WRITING) {
275                 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
276         } else if(state == FL_ERASING) {
277                 event = NDSR_CS0_CMDD | NDSR_CS0_BBD;
278         }
279
280         ndsr = dfc_wait_event(event);
281
282         if((ndsr & NDSR_CS0_BBD) || (ndsr & 0xff000000))
283                 return(0x1); /* Status Read error */
284         return 0;
285 }
286
287 /* cmdfunc send commands to the DFC */
288 static void dfc_cmdfunc(struct mtd_info *mtd, unsigned command,
289                         int column, int page_addr)
290 {
291         /* register struct nand_chip *this = mtd->priv; */
292         unsigned long ndcb0=0, ndcb1=0, ndcb2=0, event=0;
293
294         /* clear the ugly byte read buffer */
295         bytes_read = -1;
296         read_buf = 0;
297
298         switch (command) {
299         case NAND_CMD_READ0:
300                 DFC_DEBUG3("dfc_cmdfunc: NAND_CMD_READ0, page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
301                 dfc_new_cmd();
302                 ndcb0 = (NAND_CMD_READ0 | (4<<16));
303                 column >>= 1; /* adjust for 16 bit bus */
304                 ndcb1 = (((column>>1) & 0xff) |
305                          ((page_addr<<8) & 0xff00) |
306                          ((page_addr<<8) & 0xff0000) |
307                          ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
308                 event = NDSR_RDDREQ;
309                 goto write_cmd;
310         case NAND_CMD_READ1:
311                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READ1 unimplemented!\n");
312                 goto end;
313         case NAND_CMD_READOOB:
314                 DFC_DEBUG1("dfc_cmdfunc: NAND_CMD_READOOB unimplemented!\n");
315                 goto end;
316         case NAND_CMD_READID:
317                 dfc_new_cmd();
318                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_READID.\n");
319                 ndcb0 = (NAND_CMD_READID | (3 << 21) | (1 << 16)); /* addr cycles*/
320                 event = NDSR_RDDREQ;
321                 goto write_cmd;
322         case NAND_CMD_PAGEPROG:
323                 /* sent as a multicommand in NAND_CMD_SEQIN */
324                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_PAGEPROG empty due to multicmd.\n");
325                 goto end;
326         case NAND_CMD_ERASE1:
327                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE1,  page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
328                 dfc_new_cmd();
329                 ndcb0 = (0xd060 | (1<<25) | (2<<21) | (1<<19) | (3<<16));
330                 ndcb1 = (page_addr & 0x00ffffff);
331                 goto write_cmd;
332         case NAND_CMD_ERASE2:
333                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_ERASE2 empty due to multicmd.\n");
334                 goto end;
335         case NAND_CMD_SEQIN:
336                 /* send PAGE_PROG command(0x1080) */
337                 dfc_new_cmd();
338                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_SEQIN/PAGE_PROG,  page_addr: 0x%x, column: 0x%x.\n", page_addr, (column>>1));
339                 ndcb0 = (0x1080 | (1<<25) | (1<<21) | (1<<19) | (4<<16));
340                 column >>= 1; /* adjust for 16 bit bus */
341                 ndcb1 = (((column>>1) & 0xff) |
342                          ((page_addr<<8) & 0xff00) |
343                          ((page_addr<<8) & 0xff0000) |
344                          ((page_addr<<8) & 0xff000000)); /* make this 0x01000000 ? */
345                 event = NDSR_WRDREQ;
346                 goto write_cmd;
347         case NAND_CMD_STATUS:
348                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_STATUS.\n");
349                 dfc_new_cmd();
350                 ndcb0 = NAND_CMD_STATUS | (4<<21);
351                 event = NDSR_RDDREQ;
352                 goto write_cmd;
353         case NAND_CMD_RESET:
354                 DFC_DEBUG2("dfc_cmdfunc: NAND_CMD_RESET.\n");
355                 ndcb0 = NAND_CMD_RESET | (5<<21);
356                 event = NDSR_CS0_CMDD;
357                 goto write_cmd;
358         default:
359                 printk("dfc_cmdfunc: error, unsupported command.\n");
360                 goto end;
361         }
362
363  write_cmd:
364         NDCB0 = ndcb0;
365         NDCB0 = ndcb1;
366         NDCB0 = ndcb2;
367
368         /*  wait_event: */
369         dfc_wait_event(event);
370  end:
371         return;
372 }
373
374 static void dfc_gpio_init(void)
375 {
376         DFC_DEBUG2("Setting up DFC GPIO's.\n");
377
378         /* no idea what is done here, see zylonite.c */
379         GPIO4 = 0x1;
380
381         DF_ALE_WE1 = 0x00000001;
382         DF_ALE_WE2 = 0x00000001;
383         DF_nCS0 = 0x00000001;
384         DF_nCS1 = 0x00000001;
385         DF_nWE = 0x00000001;
386         DF_nRE = 0x00000001;
387         DF_IO0 = 0x00000001;
388         DF_IO8 = 0x00000001;
389         DF_IO1 = 0x00000001;
390         DF_IO9 = 0x00000001;
391         DF_IO2 = 0x00000001;
392         DF_IO10 = 0x00000001;
393         DF_IO3 = 0x00000001;
394         DF_IO11 = 0x00000001;
395         DF_IO4 = 0x00000001;
396         DF_IO12 = 0x00000001;
397         DF_IO5 = 0x00000001;
398         DF_IO13 = 0x00000001;
399         DF_IO6 = 0x00000001;
400         DF_IO14 = 0x00000001;
401         DF_IO7 = 0x00000001;
402         DF_IO15 = 0x00000001;
403
404         DF_nWE = 0x1901;
405         DF_nRE = 0x1901;
406         DF_CLE_NOE = 0x1900;
407         DF_ALE_WE1 = 0x1901;
408         DF_INT_RnB = 0x1900;
409 }
410
411 /*
412  * Board-specific NAND initialization. The following members of the
413  * argument are board-specific (per include/linux/mtd/nand_new.h):
414  * - IO_ADDR_R?: address to read the 8 I/O lines of the flash device
415  * - IO_ADDR_W?: address to write the 8 I/O lines of the flash device
416  * - cmd_ctrl: hardwarespecific function for accesing control-lines
417  * - dev_ready: hardwarespecific function for  accesing device ready/busy line
418  * - enable_hwecc?: function to enable (reset)  hardware ecc generator. Must
419  *   only be provided if a hardware ECC is available
420  * - ecc.mode: mode of ecc, see defines
421  * - chip_delay: chip dependent delay for transfering data from array to
422  *   read regs (tR)
423  * - options: various chip options. They can partly be set to inform
424  *   nand_scan about special functionality. See the defines for further
425  *   explanation
426  * Members with a "?" were not set in the merged testing-NAND branch,
427  * so they are not set here either.
428  */
429 int board_nand_init(struct nand_chip *nand)
430 {
431         unsigned long tCH, tCS, tWH, tWP, tRH, tRP, tRP_high, tR, tWHR, tAR;
432
433         /* set up GPIO Control Registers */
434         dfc_gpio_init();
435
436         /* turn on the NAND Controller Clock (104 MHz @ D0) */
437         CKENA |= (CKENA_4_NAND | CKENA_9_SMC);
438
439 #undef CONFIG_SYS_TIMING_TIGHT
440 #ifndef CONFIG_SYS_TIMING_TIGHT
441         tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US) + 1),
442                   DFC_MAX_tCH);
443         tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US) + 1),
444                   DFC_MAX_tCS);
445         tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US) + 1),
446                   DFC_MAX_tWH);
447         tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US) + 1),
448                   DFC_MAX_tWP);
449         tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US) + 1),
450                   DFC_MAX_tRH);
451         tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US) + 1),
452                   DFC_MAX_tRP);
453         tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) + 1),
454                  DFC_MAX_tR);
455         tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) + 1),
456                    DFC_MAX_tWHR);
457         tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) + 1),
458                   DFC_MAX_tAR);
459 #else /* this is the tight timing */
460
461         tCH = MIN(((unsigned long) (NAND_TIMING_tCH * DFC_CLK_PER_US)),
462                   DFC_MAX_tCH);
463         tCS = MIN(((unsigned long) (NAND_TIMING_tCS * DFC_CLK_PER_US)),
464                   DFC_MAX_tCS);
465         tWH = MIN(((unsigned long) (NAND_TIMING_tWH * DFC_CLK_PER_US)),
466                   DFC_MAX_tWH);
467         tWP = MIN(((unsigned long) (NAND_TIMING_tWP * DFC_CLK_PER_US)),
468                   DFC_MAX_tWP);
469         tRH = MIN(((unsigned long) (NAND_TIMING_tRH * DFC_CLK_PER_US)),
470                   DFC_MAX_tRH);
471         tRP = MIN(((unsigned long) (NAND_TIMING_tRP * DFC_CLK_PER_US)),
472                   DFC_MAX_tRP);
473         tR = MIN(((unsigned long) (NAND_TIMING_tR * DFC_CLK_PER_US) - tCH - 2),
474                  DFC_MAX_tR);
475         tWHR = MIN(((unsigned long) (NAND_TIMING_tWHR * DFC_CLK_PER_US) - tCH - 2),
476                    DFC_MAX_tWHR);
477         tAR = MIN(((unsigned long) (NAND_TIMING_tAR * DFC_CLK_PER_US) - 2),
478                   DFC_MAX_tAR);
479 #endif /* CONFIG_SYS_TIMING_TIGHT */
480
481
482         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);
483
484         /* tRP value is split in the register */
485         if(tRP & (1 << 4)) {
486                 tRP_high = 1;
487                 tRP &= ~(1 << 4);
488         } else {
489                 tRP_high = 0;
490         }
491
492         NDTR0CS0 = (tCH << 19) |
493                 (tCS << 16) |
494                 (tWH << 11) |
495                 (tWP << 8) |
496                 (tRP_high << 6) |
497                 (tRH << 3) |
498                 (tRP << 0);
499
500         NDTR1CS0 = (tR << 16) |
501                 (tWHR << 4) |
502                 (tAR << 0);
503
504         /* If it doesn't work (unlikely) think about:
505          *  - ecc enable
506          *  - chip select don't care
507          *  - read id byte count
508          *
509          * Intentionally enabled by not setting bits:
510          *  - dma (DMA_EN)
511          *  - page size = 512
512          *  - cs don't care, see if we can enable later!
513          *  - row address start position (after second cycle)
514          *  - pages per block = 32
515          *  - ND_RDY : clears command buffer
516          */
517         /* NDCR_NCSX |          /\* Chip select busy don't care *\/ */
518
519         NDCR = (NDCR_SPARE_EN |         /* use the spare area */
520                 NDCR_DWIDTH_C |         /* 16bit DFC data bus width  */
521                 NDCR_DWIDTH_M |         /* 16 bit Flash device data bus width */
522                 (2 << 16) |             /* read id count = 7 ???? mk@tbd */
523                 NDCR_ND_ARB_EN |        /* enable bus arbiter */
524                 NDCR_RDYM |             /* flash device ready ir masked */
525                 NDCR_CS0_PAGEDM |       /* ND_nCSx page done ir masked */
526                 NDCR_CS1_PAGEDM |
527                 NDCR_CS0_CMDDM |        /* ND_CSx command done ir masked */
528                 NDCR_CS1_CMDDM |
529                 NDCR_CS0_BBDM |         /* ND_CSx bad block detect ir masked */
530                 NDCR_CS1_BBDM |
531                 NDCR_DBERRM |           /* double bit error ir masked */
532                 NDCR_SBERRM |           /* single bit error ir masked */
533                 NDCR_WRDREQM |          /* write data request ir masked */
534                 NDCR_RDDREQM |          /* read data request ir masked */
535                 NDCR_WRCMDREQM);        /* write command request ir masked */
536
537
538         /* wait 10 us due to cmd buffer clear reset */
539         /*      wait(10); */
540
541         nand->cmd_ctrl = dfc_hwcontrol;
542 /*      nand->dev_ready = dfc_device_ready; */
543         nand->ecc.mode = NAND_ECC_SOFT;
544         nand->ecc.layout = &delta_oob;
545         nand->options = NAND_BUSWIDTH_16;
546         nand->waitfunc = dfc_wait;
547         nand->read_byte = dfc_read_byte;
548         nand->read_word = dfc_read_word;
549         nand->read_buf = dfc_read_buf;
550         nand->write_buf = dfc_write_buf;
551
552         nand->cmdfunc = dfc_cmdfunc;
553         nand->badblock_pattern = &delta_bbt_descr;
554         return 0;
555 }
556
557 #else
558  #error "U-Boot legacy NAND support not available for Monahans DFC."
559 #endif
560 #endif