blob: b90f93e3b85916d18a2b2839ec62365246034a4f [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>
Peter Tyser133c0fe2010-04-12 22:28:07 -050029#include <asm/arch/imx-regs.h>
Magnus Lilja4133f652009-06-13 20:50:01 +020030#include <asm/io.h>
31#include <fsl_nfc.h>
32
Heiko Schocher26cc10a2010-09-17 13:10:38 +020033static struct fsl_nfc_regs *const nfc = (void *)NFC_BASE_ADDR;
Magnus Lilja4133f652009-06-13 20:50:01 +020034
35static void nfc_wait_ready(void)
36{
37 uint32_t tmp;
38
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020039 while (!(readw(&nfc->config2) & NFC_INT))
Magnus Lilja4133f652009-06-13 20:50:01 +020040 ;
41
42 /* Reset interrupt flag */
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020043 tmp = readw(&nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +020044 tmp &= ~NFC_INT;
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020045 writew(tmp, &nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +020046}
47
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020048static void nfc_nand_init(void)
Magnus Lilja4133f652009-06-13 20:50:01 +020049{
John Rigby4c94c452010-01-26 19:24:17 -070050#if defined(MXC_NFC_V1_1)
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020051 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
John Rigby4c94c452010-01-26 19:24:17 -070052 int config1;
53
54 writew(CONFIG_SYS_NAND_SPARE_SIZE / 2, &nfc->spare_area_size);
55
56 /* unlocking RAM Buff */
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020057 writew(0x2, &nfc->config);
John Rigby4c94c452010-01-26 19:24:17 -070058
59 /* hardware ECC checking and correct */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020060 config1 = readw(&nfc->config1) | NFC_ECC_EN | NFC_FP_INT;
John Rigby4c94c452010-01-26 19:24:17 -070061 /*
62 * if spare size is larger that 16 bytes per 512 byte hunk
63 * then use 8 symbol correction instead of 4
64 */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020065 if (CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page > 16)
John Rigby4c94c452010-01-26 19:24:17 -070066 config1 &= ~NFC_4_8N_ECC;
67 else
68 config1 |= NFC_4_8N_ECC;
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020069 writew(config1, &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -070070#elif defined(MXC_NFC_V1)
Magnus Lilja4133f652009-06-13 20:50:01 +020071 /* unlocking RAM Buff */
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020072 writew(0x2, &nfc->config);
Magnus Lilja4133f652009-06-13 20:50:01 +020073
74 /* hardware ECC checking and correct */
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020075 writew(NFC_ECC_EN, &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -070076#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020077}
78
79static void nfc_nand_command(unsigned short command)
80{
81 writew(command, &nfc->flash_cmd);
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020082 writew(NFC_CMD, &nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +020083 nfc_wait_ready();
84}
85
86static void nfc_nand_page_address(unsigned int page_address)
87{
88 unsigned int page_count;
89
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020090 writew(0x00, &nfc->flash_addr);
91 writew(NFC_ADDR, &nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +020092 nfc_wait_ready();
93
John Rigby4c94c452010-01-26 19:24:17 -070094 /* code only for large page flash */
95 if (CONFIG_SYS_NAND_PAGE_SIZE > 512) {
Benoît Thébaudeau3f775192012-08-13 22:48:12 +020096 writew(0x00, &nfc->flash_addr);
97 writew(NFC_ADDR, &nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +020098 nfc_wait_ready();
99 }
100
101 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
102
103 if (page_address <= page_count) {
104 page_count--; /* transform 0x01000000 to 0x00ffffff */
105 do {
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200106 writew(page_address & 0xff, &nfc->flash_addr);
107 writew(NFC_ADDR, &nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +0200108 nfc_wait_ready();
109 page_address = page_address >> 8;
110 page_count = page_count >> 8;
111 } while (page_count);
112 }
John Rigby4c94c452010-01-26 19:24:17 -0700113
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200114 writew(0x00, &nfc->flash_addr);
115 writew(NFC_ADDR, &nfc->config2);
John Rigby4c94c452010-01-26 19:24:17 -0700116 nfc_wait_ready();
Magnus Lilja4133f652009-06-13 20:50:01 +0200117}
118
119static void nfc_nand_data_output(void)
120{
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200121 int config1 = readw(&nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700122#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200123 int i;
John Rigby4c94c452010-01-26 19:24:17 -0700124#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200125
John Rigby4c94c452010-01-26 19:24:17 -0700126 config1 |= NFC_ECC_EN | NFC_INT_MSK;
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200127 writew(config1, &nfc->config1);
128 writew(0, &nfc->buf_addr);
129 writew(NFC_OUTPUT, &nfc->config2);
John Rigby4c94c452010-01-26 19:24:17 -0700130 nfc_wait_ready();
131#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200132 /*
John Rigby4c94c452010-01-26 19:24:17 -0700133 * This NAND controller requires multiple input commands
134 * for pages larger than 512 bytes.
Magnus Lilja4133f652009-06-13 20:50:01 +0200135 */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200136 for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200137 config1 = readw(&nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700138 config1 |= NFC_ECC_EN | NFC_INT_MSK;
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200139 writew(config1, &nfc->config1);
140 writew(i, &nfc->buf_addr);
141 writew(NFC_OUTPUT, &nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +0200142 nfc_wait_ready();
143 }
John Rigby4c94c452010-01-26 19:24:17 -0700144#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200145}
146
147static int nfc_nand_check_ecc(void)
148{
149 return readw(&nfc->ecc_status_result);
150}
151
152static int nfc_read_page(unsigned int page_address, unsigned char *buf)
153{
154 int i;
155 u32 *src;
156 u32 *dst;
157
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200158 writew(0, &nfc->buf_addr); /* read in first 0 buffer */
Magnus Lilja4133f652009-06-13 20:50:01 +0200159 nfc_nand_command(NAND_CMD_READ0);
160 nfc_nand_page_address(page_address);
161
John Rigby4c94c452010-01-26 19:24:17 -0700162 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200163 nfc_nand_command(NAND_CMD_READSTART);
164
165 nfc_nand_data_output(); /* fill the main buffer 0 */
166
167 if (nfc_nand_check_ecc())
168 return -1;
169
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200170 src = (u32 *)&nfc->main_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200171 dst = (u32 *)buf;
172
173 /* main copy loop from NAND-buffer to SDRAM memory */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200174 for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200175 writel(readl(src), dst);
176 src++;
177 dst++;
178 }
179
180 return 0;
181}
182
183static int is_badblock(int pagenumber)
184{
185 int page = pagenumber;
186 u32 badblock;
187 u32 *src;
188
189 /* Check the first two pages for bad block markers */
190 for (page = pagenumber; page < pagenumber + 2; page++) {
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200191 writew(0, &nfc->buf_addr); /* read in first 0 buffer */
Magnus Lilja4133f652009-06-13 20:50:01 +0200192 nfc_nand_command(NAND_CMD_READ0);
193 nfc_nand_page_address(page);
194
John Rigby4c94c452010-01-26 19:24:17 -0700195 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200196 nfc_nand_command(NAND_CMD_READSTART);
197
198 nfc_nand_data_output(); /* fill the main buffer 0 */
199
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200200 src = (u32 *)&nfc->spare_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200201
202 /*
203 * IMPORTANT NOTE: The nand flash controller uses a non-
204 * standard layout for large page devices. This can
205 * affect the position of the bad block marker.
206 */
207 /* Get the bad block marker */
208 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
209 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
210 badblock &= 0xff;
211
212 /* bad block marker verify */
213 if (badblock != 0xff)
214 return 1; /* potential bad block */
215 }
216
217 return 0;
218}
219
220static int nand_load(unsigned int from, unsigned int size, unsigned char *buf)
221{
222 int i;
223 unsigned int page;
224 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
225 CONFIG_SYS_NAND_PAGE_SIZE;
226
Magnus Lilja4133f652009-06-13 20:50:01 +0200227 nfc_nand_init();
228
229 /* Convert to page number */
230 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
231 i = 0;
232
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200233 while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200234 if (nfc_read_page(page, buf) < 0)
235 return -1;
236
237 page++;
238 i++;
239 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
240
241 /*
242 * Check if we have crossed a block boundary, and if so
243 * check for bad block.
244 */
245 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
246 /*
247 * Yes, new block. See if this block is good. If not,
John Rigby4c94c452010-01-26 19:24:17 -0700248 * loop until we find a good block.
Magnus Lilja4133f652009-06-13 20:50:01 +0200249 */
250 while (is_badblock(page)) {
251 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
252 /* Check i we've reached the end of flash. */
253 if (page >= maxpages)
254 return -1;
255 }
256 }
257 }
258
259 return 0;
260}
261
Wolfgang Denk627857d2010-10-28 20:35:36 +0200262#if defined(CONFIG_ARM)
Heiko Schocheraeb29912010-09-17 13:10:39 +0200263void board_init_f (ulong bootflag)
264{
Wolfgang Denkfb2759c2010-10-18 23:43:37 +0200265 relocate_code (CONFIG_SYS_TEXT_BASE - TOTAL_MALLOC_LEN, NULL,
266 CONFIG_SYS_TEXT_BASE);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200267}
268#endif
269
Magnus Lilja4133f652009-06-13 20:50:01 +0200270/*
271 * The main entry for NAND booting. It's necessary that SDRAM is already
272 * configured and available since this code loads the main U-Boot image
273 * from NAND into SDRAM and starts it from there.
274 */
275void nand_boot(void)
276{
277 __attribute__((noreturn)) void (*uboot)(void);
278
Magnus Lilja4133f652009-06-13 20:50:01 +0200279 /*
280 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
281 * be aligned to full pages
282 */
283 if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
284 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
285 /* Copy from NAND successful, start U-boot */
286 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
287 uboot();
288 } else {
289 /* Unrecoverable error when copying from NAND */
290 hang();
291 }
292}
293
294/*
295 * Called in case of an exception.
296 */
297void hang(void)
298{
299 /* Loop forever */
300 while (1) ;
301}