TI tarball had execute permissions set on a number of source files -- change these...
[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_ID              0xa1  /* x8, 1GiB */
45 #define MT29F2G_ID      0xba  /* x16, 2GiB */
46
47 #define ADDR_COLUMN             1          
48 #define ADDR_PAGE               2             
49 #define ADDR_COLUMN_PAGE        (ADDR_COLUMN | ADDR_PAGE)
50
51 #define ADDR_OOB                (0x4 | ADDR_COLUMN_PAGE) 
52
53 #define PAGE_SIZE               2048
54 #define OOB_SIZE                64
55 #define MAX_NUM_PAGES           64
56
57 #define ECC_CHECK_ENABLE
58 #define ECC_SIZE                24
59 #define ECC_STEPS               3
60
61 /*******************************************************
62  * Routine: delay
63  * Description: spinning delay to use before udelay works
64  ******************************************************/
65 static inline void delay (unsigned long loops)
66 {
67         __asm__ volatile ("1:\n"
68                                           "subs %0, %0, #1\n"
69                                           "bne 1b":"=r" (loops):"0" (loops));
70 }
71
72 static int nand_read_page(u_char *buf, ulong page_addr);
73 static int nand_read_oob(u_char * buf, ulong page_addr);
74
75 /* JFFS2 large page layout for 3-byte ECC per 256 bytes ECC layout */
76 /* This is the only SW ECC supported by u-boot. So to load u-boot 
77  * this should be supported */
78 static u_char ecc_pos[] = 
79                 {40, 41, 42, 43, 44, 45, 46, 47,
80                 48, 49, 50, 51, 52, 53, 54, 55,
81                 56, 57, 58, 59, 60, 61, 62, 63};
82 static u_char eccvalid_pos = 4;
83
84 static unsigned long chipsize = (256 << 20);
85
86 #ifdef NAND_16BIT
87 static int bus_width = 16;
88 #else
89 static int bus_width = 8;
90 #endif
91
92 /* NanD_Command: Send a flash command to the flash chip */
93 static int NanD_Command(unsigned char command)
94 {
95         NAND_CTL_SETCLE(NAND_ADDR);
96
97         WRITE_NAND_COMMAND(command, NAND_ADDR);
98         NAND_CTL_CLRCLE(NAND_ADDR);
99
100         if(command == NAND_CMD_RESET){
101                 unsigned char ret_val;
102                 NanD_Command(NAND_CMD_STATUS);
103                 do{
104                         ret_val = READ_NAND(NAND_ADDR);/* wait till ready */
105                 } while((ret_val & 0x40) != 0x40);
106         }
107         
108         NAND_WAIT_READY();
109         return 0;
110 }
111
112
113 /* NanD_Address: Set the current address for the flash chip */
114 static int NanD_Address(unsigned int numbytes, unsigned long ofs)
115 {
116         uchar u;
117
118         NAND_CTL_SETALE(NAND_ADDR);
119
120         if (numbytes == ADDR_COLUMN || numbytes == ADDR_COLUMN_PAGE 
121                                 || numbytes == ADDR_OOB)
122         {
123                 ushort col = ofs;
124
125                 u = col  & 0xff;
126                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
127
128                 u = (col >> 8) & 0x07;
129                 if (numbytes == ADDR_OOB)
130                         u = u | ((bus_width == 16) ? (1 << 2) : (1 << 3));
131                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
132         }
133
134         if (numbytes == ADDR_PAGE || numbytes == ADDR_COLUMN_PAGE
135                                 || numbytes == ADDR_OOB)
136         {
137                 u = (ofs >> 11) & 0xff;
138                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
139                 u = (ofs >> 19) & 0xff;
140                 WRITE_NAND_ADDRESS(u, NAND_ADDR);
141
142                 /* One more address cycle for devices > 128MiB */
143                 if (chipsize > (128 << 20)) {
144                         u = (ofs >> 27) & 0xff;
145                         WRITE_NAND_ADDRESS(u, NAND_ADDR);
146                 }
147         }
148
149         NAND_CTL_CLRALE(NAND_ADDR);
150
151         NAND_WAIT_READY();
152         return 0;
153 }
154
155 /* read chip mfr and id
156  * return 0 if they match board config
157  * return 1 if not
158  */
159 int nand_chip()
160 {
161         int mfr, id;
162
163         NAND_ENABLE_CE();
164
165         if (NanD_Command(NAND_CMD_RESET)) {
166                 printf("Err: RESET\n");
167                 NAND_DISABLE_CE();   
168                 return 1;
169         }
170  
171         if (NanD_Command(NAND_CMD_READID)) {
172                 printf("Err: READID\n");
173                 NAND_DISABLE_CE();
174                 return 1;
175         }
176  
177         NanD_Address(ADDR_COLUMN, 0);
178
179         mfr = READ_NAND(NAND_ADDR);
180         id = READ_NAND(NAND_ADDR);
181
182         NAND_DISABLE_CE();
183
184         if (get_cpu_rev() == CPU_3430_ES2)
185                 return (mfr != MT29F1G_MFR || !(id == MT29F1G_ID || id == MT29F2G_ID));
186         else
187                 return (mfr != K9F1G08R0A_MFR || id != K9F1G08R0A_ID);
188 }
189
190 /* read a block data to buf
191  * return 1 if the block is bad or ECC error can't be corrected for any page
192  * return 0 on sucess
193  */ 
194 int nand_read_block(unsigned char *buf, ulong block_addr)
195 {
196         int i, offset = 0;
197
198 #ifdef ECC_CHECK_ENABLE
199         u16 oob_buf[OOB_SIZE >> 1];
200         
201         /* check bad block */
202         /* 0th word in spare area needs be 0xff */
203         if (nand_read_oob(oob_buf, block_addr) || (oob_buf[0] & 0xff) != 0xff){
204                 printf("Skipped bad block at 0x%x\n", block_addr);
205                 return 1;    /* skip bad block */
206         }
207 #endif
208         /* read the block page by page*/
209         for (i=0; i<MAX_NUM_PAGES; i++){
210                 if (nand_read_page(buf+offset, block_addr + offset))
211                         return 1;
212                 offset += PAGE_SIZE;
213         }
214
215         return 0;
216 }
217 static count = 0;
218 /* read a page with ECC */
219 static int nand_read_page(u_char *buf, ulong page_addr)
220 {
221 #ifdef ECC_CHECK_ENABLE
222         u_char ecc_code[ECC_SIZE];
223         u_char ecc_calc[ECC_STEPS];
224         u_char oob_buf[OOB_SIZE];
225 #endif
226         u16 val;
227         int cntr;
228         int len;
229
230 #ifdef NAND_16BIT
231         u16 *p;
232 #else
233         u_char *p;
234 #endif
235
236         NAND_ENABLE_CE();   
237         NanD_Command(NAND_CMD_READ0);
238         NanD_Address(ADDR_COLUMN_PAGE, page_addr);
239         NanD_Command(NAND_CMD_READSTART);
240         NAND_WAIT_READY();
241
242         /* A delay seems to be helping here. needs more investigation */
243         delay(10000);
244         len = (bus_width == 16) ? PAGE_SIZE >> 1 : PAGE_SIZE;
245         p = buf;
246         for (cntr = 0; cntr < len; cntr++){
247                 *p++ = READ_NAND(NAND_ADDR);
248                 delay(10);
249         }
250         
251 #ifdef ECC_CHECK_ENABLE
252         p = oob_buf;
253         len = (bus_width == 16) ? OOB_SIZE >> 1 : OOB_SIZE;
254         for (cntr = 0; cntr < len; cntr++){
255                 *p++ = READ_NAND(NAND_ADDR);
256                 delay(10);
257         }
258         count = 0;
259         NAND_DISABLE_CE();  /* set pin high */
260
261         /* Pick the ECC bytes out of the oob data */
262         for (cntr = 0; cntr < ECC_SIZE; cntr++)
263                 ecc_code[cntr] =  oob_buf[ecc_pos[cntr]];
264
265         for(count = 0; count < ECC_SIZE; count += ECC_STEPS) {
266                 nand_calculate_ecc (buf, &ecc_calc[0]);
267                 if (nand_correct_data (buf, &ecc_code[count], &ecc_calc[0]) == -1) {
268                         printf ("ECC Failed, page 0x%08x\n", page_addr);
269                         for (val=0; val <256; val++)
270                                 printf("%x ", buf[val]);
271                         printf("\n");
272                         for (;;);
273                         return 1;
274                 }
275                 buf += 256;
276                 page_addr += 256;
277         }
278 #endif  
279         return 0;
280 }
281
282 /* read from the 16 bytes of oob data that correspond to a 512 / 2048 byte page.
283  */
284 static int nand_read_oob(u_char *buf, ulong page_addr)
285 {
286         u16 val;
287         int cntr;
288         int len;
289
290 #ifdef NAND_16BIT
291         u16 *p;
292 #else
293         u_char *p;
294 #endif
295         p = buf;
296         len = (bus_width == 16) ? OOB_SIZE >> 1 : OOB_SIZE;
297
298         NAND_ENABLE_CE();  /* set pin low */
299         NanD_Command(NAND_CMD_READ0);
300         NanD_Address(ADDR_OOB, page_addr);
301         NanD_Command(NAND_CMD_READSTART);
302         NAND_WAIT_READY();
303
304         /* A delay seems to be helping here. needs more investigation */
305         delay(10000);
306         for (cntr = 0; cntr < len; cntr++)
307                 *p++ = READ_NAND(NAND_ADDR);
308   
309         NAND_WAIT_READY();
310         NAND_DISABLE_CE();  /* set pin high */
311
312         return 0;
313 }
314
315 #endif