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