blob: a3f0f6ba51add514801f9ed84f33fac484258f50 [file] [log] [blame]
Magnus Lilja4133f652009-06-13 20:50:01 +02001/*
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>
John Rigby4c94c452010-01-26 19:24:17 -070029#ifdef CONFIG_MX31
Peter Tyser133c0fe2010-04-12 22:28:07 -050030#include <asm/arch/mx31-regs.h>
John Rigby4c94c452010-01-26 19:24:17 -070031#else
Peter Tyser133c0fe2010-04-12 22:28:07 -050032#include <asm/arch/imx-regs.h>
John Rigby4c94c452010-01-26 19:24:17 -070033#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020034#include <asm/io.h>
35#include <fsl_nfc.h>
36
Heiko Schocher26cc10a2010-09-17 13:10:38 +020037static struct fsl_nfc_regs *const nfc = (void *)NFC_BASE_ADDR;
Magnus Lilja4133f652009-06-13 20:50:01 +020038
39static void nfc_wait_ready(void)
40{
41 uint32_t tmp;
42
43 while (!(readw(&nfc->nand_flash_config2) & NFC_INT))
44 ;
45
46 /* Reset interrupt flag */
47 tmp = readw(&nfc->nand_flash_config2);
48 tmp &= ~NFC_INT;
49 writew(tmp, &nfc->nand_flash_config2);
50}
51
John Rigby4c94c452010-01-26 19:24:17 -070052void nfc_nand_init(void)
Magnus Lilja4133f652009-06-13 20:50:01 +020053{
John Rigby4c94c452010-01-26 19:24:17 -070054#if defined(MXC_NFC_V1_1)
55 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
56 int config1;
57
58 writew(CONFIG_SYS_NAND_SPARE_SIZE / 2, &nfc->spare_area_size);
59
60 /* unlocking RAM Buff */
61 writew(0x2, &nfc->configuration);
62
63 /* hardware ECC checking and correct */
64 config1 = readw(&nfc->nand_flash_config1) | NFC_ECC_EN | 0x800;
65 /*
66 * if spare size is larger that 16 bytes per 512 byte hunk
67 * then use 8 symbol correction instead of 4
68 */
69 if ((CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page) > 16)
70 config1 &= ~NFC_4_8N_ECC;
71 else
72 config1 |= NFC_4_8N_ECC;
73 writew(config1, &nfc->nand_flash_config1);
74#elif defined(MXC_NFC_V1)
Magnus Lilja4133f652009-06-13 20:50:01 +020075 /* unlocking RAM Buff */
76 writew(0x2, &nfc->configuration);
77
78 /* hardware ECC checking and correct */
79 writew(NFC_ECC_EN, &nfc->nand_flash_config1);
John Rigby4c94c452010-01-26 19:24:17 -070080#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020081}
82
83static void nfc_nand_command(unsigned short command)
84{
85 writew(command, &nfc->flash_cmd);
86 writew(NFC_CMD, &nfc->nand_flash_config2);
87 nfc_wait_ready();
88}
89
90static void nfc_nand_page_address(unsigned int page_address)
91{
92 unsigned int page_count;
93
John Rigby4c94c452010-01-26 19:24:17 -070094 writew(0x00, &nfc->flash_add);
Magnus Lilja4133f652009-06-13 20:50:01 +020095 writew(NFC_ADDR, &nfc->nand_flash_config2);
96 nfc_wait_ready();
97
John Rigby4c94c452010-01-26 19:24:17 -070098 /* code only for large page flash */
99 if (CONFIG_SYS_NAND_PAGE_SIZE > 512) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200100 writew(0x00, &nfc->flash_add);
101 writew(NFC_ADDR, &nfc->nand_flash_config2);
102 nfc_wait_ready();
103 }
104
105 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
106
107 if (page_address <= page_count) {
108 page_count--; /* transform 0x01000000 to 0x00ffffff */
109 do {
110 writew(page_address & 0xff, &nfc->flash_add);
111 writew(NFC_ADDR, &nfc->nand_flash_config2);
112 nfc_wait_ready();
113 page_address = page_address >> 8;
114 page_count = page_count >> 8;
115 } while (page_count);
116 }
John Rigby4c94c452010-01-26 19:24:17 -0700117
118 writew(0x00, &nfc->flash_add);
119 writew(NFC_ADDR, &nfc->nand_flash_config2);
120 nfc_wait_ready();
Magnus Lilja4133f652009-06-13 20:50:01 +0200121}
122
123static void nfc_nand_data_output(void)
124{
John Rigby4c94c452010-01-26 19:24:17 -0700125 int config1 = readw(&nfc->nand_flash_config1);
126#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200127 int i;
John Rigby4c94c452010-01-26 19:24:17 -0700128#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200129
John Rigby4c94c452010-01-26 19:24:17 -0700130 config1 |= NFC_ECC_EN | NFC_INT_MSK;
131 writew(config1, &nfc->nand_flash_config1);
132 writew(0, &nfc->buffer_address);
133 writew(NFC_OUTPUT, &nfc->nand_flash_config2);
134 nfc_wait_ready();
135#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200136 /*
John Rigby4c94c452010-01-26 19:24:17 -0700137 * This NAND controller requires multiple input commands
138 * for pages larger than 512 bytes.
Magnus Lilja4133f652009-06-13 20:50:01 +0200139 */
John Rigby4c94c452010-01-26 19:24:17 -0700140 for (i = 1; i < (CONFIG_SYS_NAND_PAGE_SIZE / 512); i++) {
141 config1 = readw(&nfc->nand_flash_config1);
142 config1 |= NFC_ECC_EN | NFC_INT_MSK;
143 writew(config1, &nfc->nand_flash_config1);
144 writew(i, &nfc->buffer_address);
Magnus Lilja4133f652009-06-13 20:50:01 +0200145 writew(NFC_OUTPUT, &nfc->nand_flash_config2);
146 nfc_wait_ready();
147 }
John Rigby4c94c452010-01-26 19:24:17 -0700148#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200149}
150
151static int nfc_nand_check_ecc(void)
152{
153 return readw(&nfc->ecc_status_result);
154}
155
156static int nfc_read_page(unsigned int page_address, unsigned char *buf)
157{
158 int i;
159 u32 *src;
160 u32 *dst;
161
162 writew(0, &nfc->buffer_address); /* read in first 0 buffer */
163 nfc_nand_command(NAND_CMD_READ0);
164 nfc_nand_page_address(page_address);
165
John Rigby4c94c452010-01-26 19:24:17 -0700166 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200167 nfc_nand_command(NAND_CMD_READSTART);
168
169 nfc_nand_data_output(); /* fill the main buffer 0 */
170
171 if (nfc_nand_check_ecc())
172 return -1;
173
John Rigby4c94c452010-01-26 19:24:17 -0700174 src = &nfc->main_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200175 dst = (u32 *)buf;
176
177 /* main copy loop from NAND-buffer to SDRAM memory */
178 for (i = 0; i < (CONFIG_SYS_NAND_PAGE_SIZE / 4); i++) {
179 writel(readl(src), dst);
180 src++;
181 dst++;
182 }
183
184 return 0;
185}
186
187static int is_badblock(int pagenumber)
188{
189 int page = pagenumber;
190 u32 badblock;
191 u32 *src;
192
193 /* Check the first two pages for bad block markers */
194 for (page = pagenumber; page < pagenumber + 2; page++) {
195 writew(0, &nfc->buffer_address); /* read in first 0 buffer */
196 nfc_nand_command(NAND_CMD_READ0);
197 nfc_nand_page_address(page);
198
John Rigby4c94c452010-01-26 19:24:17 -0700199 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200200 nfc_nand_command(NAND_CMD_READSTART);
201
202 nfc_nand_data_output(); /* fill the main buffer 0 */
203
John Rigby4c94c452010-01-26 19:24:17 -0700204 src = &nfc->spare_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200205
206 /*
207 * IMPORTANT NOTE: The nand flash controller uses a non-
208 * standard layout for large page devices. This can
209 * affect the position of the bad block marker.
210 */
211 /* Get the bad block marker */
212 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
213 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
214 badblock &= 0xff;
215
216 /* bad block marker verify */
217 if (badblock != 0xff)
218 return 1; /* potential bad block */
219 }
220
221 return 0;
222}
223
224static int nand_load(unsigned int from, unsigned int size, unsigned char *buf)
225{
226 int i;
227 unsigned int page;
228 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
229 CONFIG_SYS_NAND_PAGE_SIZE;
230
Magnus Lilja4133f652009-06-13 20:50:01 +0200231 nfc_nand_init();
232
233 /* Convert to page number */
234 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
235 i = 0;
236
237 while (i < (size / CONFIG_SYS_NAND_PAGE_SIZE)) {
238 if (nfc_read_page(page, buf) < 0)
239 return -1;
240
241 page++;
242 i++;
243 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
244
245 /*
246 * Check if we have crossed a block boundary, and if so
247 * check for bad block.
248 */
249 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
250 /*
251 * Yes, new block. See if this block is good. If not,
John Rigby4c94c452010-01-26 19:24:17 -0700252 * loop until we find a good block.
Magnus Lilja4133f652009-06-13 20:50:01 +0200253 */
254 while (is_badblock(page)) {
255 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
256 /* Check i we've reached the end of flash. */
257 if (page >= maxpages)
258 return -1;
259 }
260 }
261 }
262
263 return 0;
264}
265
Wolfgang Denk627857d2010-10-28 20:35:36 +0200266#if defined(CONFIG_ARM)
Heiko Schocheraeb29912010-09-17 13:10:39 +0200267void board_init_f (ulong bootflag)
268{
Wolfgang Denkfb2759c2010-10-18 23:43:37 +0200269 relocate_code (CONFIG_SYS_TEXT_BASE - TOTAL_MALLOC_LEN, NULL,
270 CONFIG_SYS_TEXT_BASE);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200271}
272#endif
273
Magnus Lilja4133f652009-06-13 20:50:01 +0200274/*
275 * The main entry for NAND booting. It's necessary that SDRAM is already
276 * configured and available since this code loads the main U-Boot image
277 * from NAND into SDRAM and starts it from there.
278 */
279void nand_boot(void)
280{
281 __attribute__((noreturn)) void (*uboot)(void);
282
Magnus Lilja4133f652009-06-13 20:50:01 +0200283 /*
284 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
285 * be aligned to full pages
286 */
287 if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
288 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
289 /* Copy from NAND successful, start U-boot */
290 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
291 uboot();
292 } else {
293 /* Unrecoverable error when copying from NAND */
294 hang();
295 }
296}
297
298/*
299 * Called in case of an exception.
300 */
301void hang(void)
302{
303 /* Loop forever */
304 while (1) ;
305}