eeb17c1d00cf9c3f005846e47fe19820aee95616
[pandora-x-loader.git] / drivers / k9f1g08r0a.c
1 /*
2  * (C) Copyright 2004 Texas Instruments
3  * Jian Zhang <jzhang@ti.com>
4  *
5  *  Samsung K9F1G08R0AQ0C NAND chip driver for an OMAP2420 board
6  * 
7  * This file is based on the following u-boot file:
8  *      common/cmd_nand.c
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 #include <common.h>
30
31 #include <asm/arch/sys_proto.h>
32 #include <asm/arch/sys_info.h>
33
34 #ifdef CFG_NAND_K9F1G08R0A
35
36 #define K9F1G08R0A_MFR          0xec  /* Samsung */
37 #define K9F1G08R0A_ID           0xa1  /* part # */
38
39 /* Since Micron and Samsung parts are similar in geometry and bus width
40  * we can use the same driver. Need to revisit to make this file independent
41  * of part/manufacturer
42  */
43 #define MT29F1G_MFR             0x2c  /* Micron */
44 #define MT29F1G_MFR2            0x20  /* numonyx */
45 #define MT29F1G_MFR3            0xad  /* Hynix */
46 #define MT29F1G_ID              0xa1  /* x8, 1GiB */
47 #define MT29F2G_ID              0xba  /* x16, 2GiB */
48 #define MT29F4G_ID              0xbc  /* x16, 4GiB */
49
50 #define ADDR_COLUMN             1          
51 #define ADDR_PAGE               2             
52 #define ADDR_COLUMN_PAGE        (ADDR_COLUMN | ADDR_PAGE)
53
54 #define ADDR_OOB                (0x4 | ADDR_COLUMN_PAGE) 
55
56 #define PAGE_SIZE               2048
57 #define OOB_SIZE                64
58 #define MAX_NUM_PAGES           64
59
60 #define ECC_CHECK_ENABLE
61 #define ECC_SIZE                24
62 #define ECC_STEPS               3
63
64 /*******************************************************
65  * Routine: delay
66  * Description: spinning delay to use before udelay works
67  ******************************************************/
68 static inline void delay (unsigned long loops)
69 {
70         __asm__ volatile ("1:\n"
71                                           "subs %0, %0, #1\n"
72                                           "bne 1b":"=r" (loops):"0" (loops));
73 }
74
75 static int nand_read_page(u_char *buf, ulong page_addr);
76 static int nand_read_oob(u_char * buf, ulong page_addr);
77
78 /* JFFS2 large page layout for 3-byte ECC per 256 bytes ECC layout */
79 /* This is the only SW ECC supported by u-boot. So to load u-boot 
80  * this should be supported */
81 static u_char ecc_pos[] = 
82                 {40, 41, 42, 43, 44, 45, 46, 47,
83                 48, 49, 50, 51, 52, 53, 54, 55,
84                 56, 57, 58, 59, 60, 61, 62, 63};
85
86 static unsigned long chipsize = (256 << 20);
87
88 #ifdef NAND_16BIT
89 static int bus_width = 16;
90 #else
91 static int bus_width = 8;
92 #endif
93
94 /* NanD_Command: Send a flash command to the flash chip */
95 static int NanD_Command(unsigned char command)
96 {
97         NAND_CTL_SETCLE(NAND_ADDR);
98
99         WRITE_NAND_COMMAND(command, NAND_ADDR);
100         NAND_CTL_CLRCLE(NAND_ADDR);
101
102         if(command == NAND_CMD_RESET){
103                 unsigned char ret_val;
104                 NanD_Command(NAND_CMD_STATUS);
105                 do{
106                         ret_val = READ_NAND(NAND_ADDR);/* wait till ready */
107                 } while((ret_val & 0x40) != 0x40);
108         }
109         
110         NAND_WAIT_READY();
111         return 0;
112 }
113
114
115 /* NanD_Address: Set the current address for the flash chip */
116 static int NanD_Address(unsigned int numbytes, unsigned long ofs)
117 {
118         uchar u;
119
120         NAND_CTL_SETALE(NAND_ADDR);
121
122         if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE 
123                                 || numbytes == ADDR_OOB)
124         {
125                 ushort col = ofs;
126
127                 u = col  & 0xff;
128                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
129
130                 u = (col >> 8) & 0x07;
131                 if (numbytes == ADDR_OOB)
132                         u = u | ((bus_width == 16) ? (1 << 2) : (1 << 3));
133                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
134         }
135
136         if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE
137                                 || numbytes == ADDR_OOB)
138         {
139                 u = (ofs >> 11) & 0xff;
140                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
141                 u = (ofs >> 19) & 0xff;
142                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
143
144                 /* One more address cycle for devices > 128MiB */
145                 if (chipsize > (128 << 20)) {
146                         u = (ofs >> 27) & 0xff;
147                         WRITE_NAND_ADDRESS(u, NAND_ADDR);
148                 }
149         }
150
151         NAND_CTL_CLRALE(NAND_ADDR);
152
153         NAND_WAIT_READY();
154         return 0;
155 }
156
157 int nand_readid(int *mfr, int *id)
158 {
159         NAND_ENABLE_CE();
160
161         if (NanD_Command(NAND_CMD_RESET)) {
162                 NAND_DISABLE_CE();
163                 return 1;
164         }
165  
166         if (NanD_Command(NAND_CMD_READID)) {
167                 NAND_DISABLE_CE();
168                 return 1;
169         }
170  
171         NanD_Address(ADDR_COLUMN, 0);
172
173         *mfr = READ_NAND(NAND_ADDR);
174         *id = READ_NAND(NAND_ADDR);
175
176         NAND_DISABLE_CE();
177         return 0;
178 }
179
180 /* reads parameter page */
181 int nand_read_param_page(unsigned char *data, unsigned int size)
182 {
183         unsigned int i;
184
185         NAND_ENABLE_CE();
186
187         if (NanD_Command(NAND_CMD_RESET)) {
188                 NAND_DISABLE_CE();
189                 return 1;
190         }
191
192         if (NanD_Command(NAND_CMD_READPARAM)) {
193                 NAND_DISABLE_CE();
194                 return 1;
195         }
196
197         NanD_Address(ADDR_COLUMN, 0);
198         delay(10000);
199
200         for (i = 0; i < size; i++) {
201                 data[i] = READ_NAND(NAND_ADDR);
202                 delay(10);
203         }
204
205         NAND_DISABLE_CE();
206         return 0;
207 }
208
209 /* read chip mfr and id
210  * return 0 if they match board config
211  * return 1 if not
212  */
213 int nand_chip()
214 {
215         int mfr, id;
216
217         NAND_ENABLE_CE();
218
219         if (NanD_Command(NAND_CMD_RESET)) {
220                 printf("Err: RESET\n");
221                 NAND_DISABLE_CE();   
222                 return 1;
223         }
224  
225         if (NanD_Command(NAND_CMD_READID)) {
226                 printf("Err: READID\n");
227                 NAND_DISABLE_CE();
228                 return 1;
229         }
230  
231         NanD_Address(ADDR_COLUMN, 0);
232
233         mfr = READ_NAND(NAND_ADDR);
234         id = READ_NAND(NAND_ADDR);
235
236         NAND_DISABLE_CE();
237
238         if (((mfr == MT29F1G_MFR || mfr == MT29F1G_MFR2 || mfr == MT29F1G_MFR3) &&
239                 (id == MT29F1G_ID || id == MT29F2G_ID || id == MT29F4G_ID)) ||
240              (mfr == K9F1G08R0A_MFR && (id == K9F1G08R0A_ID))) {
241                 return 0;
242         } else {
243                 if ((mfr == 0) && (id == 0)) {
244                         printf("No NAND detected\n");
245                         return 0;
246                 } else {
247                         printf("Unknown chip: mfr was 0x%02x, id was 0x%02x\n", mfr, id);
248                         return 1;
249                 }
250         }
251 }
252
253 /* read a block data to buf
254  * return 1 if the block is bad or ECC error can't be corrected for any page
255  * return 0 on sucess
256  */ 
257 int nand_read_block(unsigned char *buf, ulong block_addr)
258 {
259         int i, offset = 0;
260
261 #ifdef ECC_CHECK_ENABLE
262         u16 oob_buf[OOB_SIZE >> 1];
263         
264         /* check bad block */
265         /* 0th word in spare area needs be 0xff */
266         if (nand_read_oob((u_char *)oob_buf, block_addr) || (oob_buf[0] & 0xff) != 0xff){
267                 printf("Skipped bad block at 0x%x\n", block_addr);
268                 return 1;    /* skip bad block */
269         }
270 #endif
271         /* read the block page by page*/
272         for (i=0; i<MAX_NUM_PAGES; i++){
273                 if (nand_read_page(buf+offset, block_addr + offset))
274                         return 1;
275                 offset += PAGE_SIZE;
276         }
277
278         return 0;
279 }
280
281 /* read a page with ECC */
282 static int nand_read_page(u_char *buf, ulong page_addr)
283 {
284 #ifdef ECC_CHECK_ENABLE
285         u_char ecc_code[ECC_SIZE];
286         u_char ecc_calc[ECC_STEPS];
287         u_char oob_buf[OOB_SIZE];
288         int count;
289 #endif
290         u16 val;
291         int cntr;
292         int len;
293
294 #ifdef NAND_16BIT
295         u16 *p;
296 #else
297         u_char *p;
298 #endif
299
300         NAND_ENABLE_CE();   
301         NanD_Command(NAND_CMD_READ0);
302         NanD_Address(ADDR_COLUMN_PAGE, page_addr);
303         NanD_Command(NAND_CMD_READSTART);
304         NAND_WAIT_READY();
305
306         /* A delay seems to be helping here. needs more investigation */
307         delay(10000);
308         len = (bus_width == 16) ? PAGE_SIZE >> 1 : PAGE_SIZE;
309         p = (void *)buf;
310         for (cntr = 0; cntr < len; cntr++){
311                 *p++ = READ_NAND(NAND_ADDR);
312                 delay(10);
313         }
314         
315 #ifdef ECC_CHECK_ENABLE
316         p = (void *)oob_buf;
317         len = (bus_width == 16) ? OOB_SIZE >> 1 : OOB_SIZE;
318         for (cntr = 0; cntr < len; cntr++){
319                 *p++ = READ_NAND(NAND_ADDR);
320                 delay(10);
321         }
322         NAND_DISABLE_CE();  /* set pin high */
323
324         /* Pick the ECC bytes out of the oob data */
325         for (cntr = 0; cntr < ECC_SIZE; cntr++)
326                 ecc_code[cntr] =  oob_buf[ecc_pos[cntr]];
327
328         for(count = 0; count < ECC_SIZE; count += ECC_STEPS) {
329                 nand_calculate_ecc (buf, &ecc_calc[0]);
330                 if (nand_correct_data (buf, &ecc_code[count], &ecc_calc[0]) == -1) {
331                         printf ("ECC Failed, page 0x%08x\n", page_addr);
332                         for (val=0; val <256; val++)
333                                 printf("%x ", buf[val]);
334                         printf("\n");
335                         for (;;);
336                         return 1;
337                 }
338                 buf += 256;
339                 page_addr += 256;
340         }
341 #endif  
342         return 0;
343 }
344
345 /* read from the 16 bytes of oob data that correspond to a 512 / 2048 byte page.
346  */
347 static int nand_read_oob(u_char *buf, ulong page_addr)
348 {
349         int cntr;
350         int len;
351
352 #ifdef NAND_16BIT
353         u16 *p;
354 #else
355         u_char *p;
356 #endif
357         p = (void *)buf;
358         len = (bus_width == 16) ? OOB_SIZE >> 1 : OOB_SIZE;
359
360         NAND_ENABLE_CE();  /* set pin low */
361         NanD_Command(NAND_CMD_READ0);
362         NanD_Address(ADDR_OOB, page_addr);
363         NanD_Command(NAND_CMD_READSTART);
364         NAND_WAIT_READY();
365
366         /* A delay seems to be helping here. needs more investigation */
367         delay(10000);
368         for (cntr = 0; cntr < len; cntr++)
369                 *p++ = READ_NAND(NAND_ADDR);
370   
371         NAND_WAIT_READY();
372         NAND_DISABLE_CE();  /* set pin high */
373
374         return 0;
375 }
376
377 #endif