spl mxc nand: Set symmetric mode
[kernel/u-boot.git] / nand_spl / nand_boot_fsl_nfc.c
1 /*
2  * (C) Copyright 2009
3  * Magnus Lilja <lilja.magnus@gmail.com>
4  *
5  * (C) Copyright 2008
6  * Maxim Artamonov, <scn1874 at yandex.ru>
7  *
8  * (C) Copyright 2006-2008
9  * Stefan Roese, DENX Software Engineering, sr at denx.de.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License as
13  * published by the Free Software Foundation; either version 2 of
14  * the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
24  * MA 02111-1307 USA
25  */
26
27 #include <common.h>
28 #include <nand.h>
29 #include <asm/arch/imx-regs.h>
30 #include <asm/io.h>
31 #include <fsl_nfc.h>
32
33 static struct fsl_nfc_regs *const nfc = (void *)NFC_BASE_ADDR;
34
35 static void nfc_wait_ready(void)
36 {
37         uint32_t tmp;
38
39         while (!(readw(&nfc->config2) & NFC_INT))
40                 ;
41
42         /* Reset interrupt flag */
43         tmp = readw(&nfc->config2);
44         tmp &= ~NFC_INT;
45         writew(tmp, &nfc->config2);
46 }
47
48 static void nfc_nand_init(void)
49 {
50 #if defined(MXC_NFC_V1_1)
51         int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
52         int config1;
53
54         writew(CONFIG_SYS_NAND_SPARE_SIZE / 2, &nfc->spare_area_size);
55
56         /* unlocking RAM Buff */
57         writew(0x2, &nfc->config);
58
59         /* hardware ECC checking and correct */
60         config1 = readw(&nfc->config1) | NFC_ECC_EN | NFC_INT_MSK |
61                         NFC_ONE_CYCLE | NFC_FP_INT;
62         /*
63          * if spare size is larger that 16 bytes per 512 byte hunk
64          * then use 8 symbol correction instead of 4
65          */
66         if (CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page > 16)
67                 config1 &= ~NFC_4_8N_ECC;
68         else
69                 config1 |= NFC_4_8N_ECC;
70         writew(config1, &nfc->config1);
71 #elif defined(MXC_NFC_V1)
72         /* unlocking RAM Buff */
73         writew(0x2, &nfc->config);
74
75         /* hardware ECC checking and correct */
76         writew(NFC_ECC_EN | NFC_INT_MSK, &nfc->config1);
77 #endif
78 }
79
80 static void nfc_nand_command(unsigned short command)
81 {
82         writew(command, &nfc->flash_cmd);
83         writew(NFC_CMD, &nfc->config2);
84         nfc_wait_ready();
85 }
86
87 static void nfc_nand_address(unsigned short address)
88 {
89         writew(address, &nfc->flash_addr);
90         writew(NFC_ADDR, &nfc->config2);
91         nfc_wait_ready();
92 }
93
94 static void nfc_nand_page_address(unsigned int page_address)
95 {
96         unsigned int page_count;
97
98         nfc_nand_address(0x00);
99
100         /* code only for large page flash */
101         if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
102                 nfc_nand_address(0x00);
103
104         page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
105
106         if (page_address <= page_count) {
107                 page_count--; /* transform 0x01000000 to 0x00ffffff */
108                 do {
109                         nfc_nand_address(page_address & 0xff);
110                         page_address = page_address >> 8;
111                         page_count = page_count >> 8;
112                 } while (page_count);
113         }
114
115         nfc_nand_address(0x00);
116 }
117
118 static void nfc_nand_data_output(void)
119 {
120 #ifdef NAND_MXC_2K_MULTI_CYCLE
121         int i;
122 #endif
123
124         writew(0, &nfc->buf_addr);
125         writew(NFC_OUTPUT, &nfc->config2);
126         nfc_wait_ready();
127 #ifdef NAND_MXC_2K_MULTI_CYCLE
128         /*
129          * This NAND controller requires multiple input commands
130          * for pages larger than 512 bytes.
131          */
132         for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
133                 writew(i, &nfc->buf_addr);
134                 writew(NFC_OUTPUT, &nfc->config2);
135                 nfc_wait_ready();
136         }
137 #endif
138 }
139
140 static int nfc_nand_check_ecc(void)
141 {
142         return readw(&nfc->ecc_status_result);
143 }
144
145 static void nfc_nand_read_page(unsigned int page_address)
146 {
147         writew(0, &nfc->buf_addr); /* read in first 0 buffer */
148         nfc_nand_command(NAND_CMD_READ0);
149         nfc_nand_page_address(page_address);
150
151         if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
152                 nfc_nand_command(NAND_CMD_READSTART);
153
154         nfc_nand_data_output(); /* fill the main buffer 0 */
155 }
156
157 static int nfc_read_page(unsigned int page_address, unsigned char *buf)
158 {
159         int i;
160         u32 *src;
161         u32 *dst;
162
163         nfc_nand_read_page(page_address);
164
165         if (nfc_nand_check_ecc())
166                 return -1;
167
168         src = (u32 *)&nfc->main_area[0][0];
169         dst = (u32 *)buf;
170
171         /* main copy loop from NAND-buffer to SDRAM memory */
172         for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
173                 writel(readl(src), dst);
174                 src++;
175                 dst++;
176         }
177
178         return 0;
179 }
180
181 static int is_badblock(int pagenumber)
182 {
183         int page = pagenumber;
184         u32 badblock;
185         u32 *src;
186
187         /* Check the first two pages for bad block markers */
188         for (page = pagenumber; page < pagenumber + 2; page++) {
189                 nfc_nand_read_page(page);
190
191                 src = (u32 *)&nfc->spare_area[0][0];
192
193                 /*
194                  * IMPORTANT NOTE: The nand flash controller uses a non-
195                  * standard layout for large page devices. This can
196                  * affect the position of the bad block marker.
197                  */
198                 /* Get the bad block marker */
199                 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
200                 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
201                 badblock &= 0xff;
202
203                 /* bad block marker verify */
204                 if (badblock != 0xff)
205                         return 1; /* potential bad block */
206         }
207
208         return 0;
209 }
210
211 static int nand_load(unsigned int from, unsigned int size, unsigned char *buf)
212 {
213         int i;
214         unsigned int page;
215         unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
216                                 CONFIG_SYS_NAND_PAGE_SIZE;
217
218         nfc_nand_init();
219
220         /* Convert to page number */
221         page = from / CONFIG_SYS_NAND_PAGE_SIZE;
222         i = 0;
223
224         while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
225                 if (nfc_read_page(page, buf) < 0)
226                         return -1;
227
228                 page++;
229                 i++;
230                 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
231
232                 /*
233                  * Check if we have crossed a block boundary, and if so
234                  * check for bad block.
235                  */
236                 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
237                         /*
238                          * Yes, new block. See if this block is good. If not,
239                          * loop until we find a good block.
240                          */
241                         while (is_badblock(page)) {
242                                 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
243                                 /* Check i we've reached the end of flash. */
244                                 if (page >= maxpages)
245                                         return -1;
246                         }
247                 }
248         }
249
250         return 0;
251 }
252
253 #if defined(CONFIG_ARM)
254 void board_init_f (ulong bootflag)
255 {
256         relocate_code (CONFIG_SYS_TEXT_BASE - TOTAL_MALLOC_LEN, NULL,
257                        CONFIG_SYS_TEXT_BASE);
258 }
259 #endif
260
261 /*
262  * The main entry for NAND booting. It's necessary that SDRAM is already
263  * configured and available since this code loads the main U-Boot image
264  * from NAND into SDRAM and starts it from there.
265  */
266 void nand_boot(void)
267 {
268         __attribute__((noreturn)) void (*uboot)(void);
269
270         /*
271          * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
272          * be aligned to full pages
273          */
274         if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
275                        (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
276                 /* Copy from NAND successful, start U-boot */
277                 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
278                 uboot();
279         } else {
280                 /* Unrecoverable error when copying from NAND */
281                 hang();
282         }
283 }
284
285 /*
286  * Called in case of an exception.
287  */
288 void hang(void)
289 {
290         /* Loop forever */
291         while (1) ;
292 }