blob: 059969ba9a5e5aeeacce9a93506dbfd2d00720ea [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ébaudeau326a74b2012-08-13 22:49:15 +020060 config1 = readw(&nfc->config1) | NFC_ECC_EN | NFC_INT_MSK | 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ébaudeau326a74b2012-08-13 22:49:15 +020075 writew(NFC_ECC_EN | NFC_INT_MSK, &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
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +020086static void nfc_nand_address(unsigned short address)
87{
88 writew(address, &nfc->flash_addr);
89 writew(NFC_ADDR, &nfc->config2);
90 nfc_wait_ready();
91}
92
Magnus Lilja4133f652009-06-13 20:50:01 +020093static void nfc_nand_page_address(unsigned int page_address)
94{
95 unsigned int page_count;
96
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +020097 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +020098
John Rigby4c94c452010-01-26 19:24:17 -070099 /* code only for large page flash */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200100 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
101 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200102
103 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
104
105 if (page_address <= page_count) {
106 page_count--; /* transform 0x01000000 to 0x00ffffff */
107 do {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200108 nfc_nand_address(page_address & 0xff);
Magnus Lilja4133f652009-06-13 20:50:01 +0200109 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ébaudeau2174f3b2012-08-13 22:48:56 +0200114 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200115}
116
117static void nfc_nand_data_output(void)
118{
John Rigby4c94c452010-01-26 19:24:17 -0700119#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200120 int i;
John Rigby4c94c452010-01-26 19:24:17 -0700121#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200122
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200123 writew(0, &nfc->buf_addr);
124 writew(NFC_OUTPUT, &nfc->config2);
John Rigby4c94c452010-01-26 19:24:17 -0700125 nfc_wait_ready();
126#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200127 /*
John Rigby4c94c452010-01-26 19:24:17 -0700128 * This NAND controller requires multiple input commands
129 * for pages larger than 512 bytes.
Magnus Lilja4133f652009-06-13 20:50:01 +0200130 */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200131 for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200132 writew(i, &nfc->buf_addr);
133 writew(NFC_OUTPUT, &nfc->config2);
Magnus Lilja4133f652009-06-13 20:50:01 +0200134 nfc_wait_ready();
135 }
John Rigby4c94c452010-01-26 19:24:17 -0700136#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200137}
138
139static int nfc_nand_check_ecc(void)
140{
141 return readw(&nfc->ecc_status_result);
142}
143
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200144static void nfc_nand_read_page(unsigned int page_address)
Magnus Lilja4133f652009-06-13 20:50:01 +0200145{
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200146 writew(0, &nfc->buf_addr); /* read in first 0 buffer */
Magnus Lilja4133f652009-06-13 20:50:01 +0200147 nfc_nand_command(NAND_CMD_READ0);
148 nfc_nand_page_address(page_address);
149
John Rigby4c94c452010-01-26 19:24:17 -0700150 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200151 nfc_nand_command(NAND_CMD_READSTART);
152
153 nfc_nand_data_output(); /* fill the main buffer 0 */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200154}
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 nfc_nand_read_page(page_address);
Magnus Lilja4133f652009-06-13 20:50:01 +0200163
164 if (nfc_nand_check_ecc())
165 return -1;
166
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200167 src = (u32 *)&nfc->main_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200168 dst = (u32 *)buf;
169
170 /* main copy loop from NAND-buffer to SDRAM memory */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200171 for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200172 writel(readl(src), dst);
173 src++;
174 dst++;
175 }
176
177 return 0;
178}
179
180static int is_badblock(int pagenumber)
181{
182 int page = pagenumber;
183 u32 badblock;
184 u32 *src;
185
186 /* Check the first two pages for bad block markers */
187 for (page = pagenumber; page < pagenumber + 2; page++) {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200188 nfc_nand_read_page(page);
Magnus Lilja4133f652009-06-13 20:50:01 +0200189
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200190 src = (u32 *)&nfc->spare_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200191
192 /*
193 * IMPORTANT NOTE: The nand flash controller uses a non-
194 * standard layout for large page devices. This can
195 * affect the position of the bad block marker.
196 */
197 /* Get the bad block marker */
198 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
199 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
200 badblock &= 0xff;
201
202 /* bad block marker verify */
203 if (badblock != 0xff)
204 return 1; /* potential bad block */
205 }
206
207 return 0;
208}
209
210static int nand_load(unsigned int from, unsigned int size, unsigned char *buf)
211{
212 int i;
213 unsigned int page;
214 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
215 CONFIG_SYS_NAND_PAGE_SIZE;
216
Magnus Lilja4133f652009-06-13 20:50:01 +0200217 nfc_nand_init();
218
219 /* Convert to page number */
220 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
221 i = 0;
222
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200223 while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200224 if (nfc_read_page(page, buf) < 0)
225 return -1;
226
227 page++;
228 i++;
229 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
230
231 /*
232 * Check if we have crossed a block boundary, and if so
233 * check for bad block.
234 */
235 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
236 /*
237 * Yes, new block. See if this block is good. If not,
John Rigby4c94c452010-01-26 19:24:17 -0700238 * loop until we find a good block.
Magnus Lilja4133f652009-06-13 20:50:01 +0200239 */
240 while (is_badblock(page)) {
241 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
242 /* Check i we've reached the end of flash. */
243 if (page >= maxpages)
244 return -1;
245 }
246 }
247 }
248
249 return 0;
250}
251
Wolfgang Denk627857d2010-10-28 20:35:36 +0200252#if defined(CONFIG_ARM)
Heiko Schocheraeb29912010-09-17 13:10:39 +0200253void board_init_f (ulong bootflag)
254{
Wolfgang Denkfb2759c2010-10-18 23:43:37 +0200255 relocate_code (CONFIG_SYS_TEXT_BASE - TOTAL_MALLOC_LEN, NULL,
256 CONFIG_SYS_TEXT_BASE);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200257}
258#endif
259
Magnus Lilja4133f652009-06-13 20:50:01 +0200260/*
261 * The main entry for NAND booting. It's necessary that SDRAM is already
262 * configured and available since this code loads the main U-Boot image
263 * from NAND into SDRAM and starts it from there.
264 */
265void nand_boot(void)
266{
267 __attribute__((noreturn)) void (*uboot)(void);
268
Magnus Lilja4133f652009-06-13 20:50:01 +0200269 /*
270 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
271 * be aligned to full pages
272 */
273 if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
274 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
275 /* Copy from NAND successful, start U-boot */
276 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
277 uboot();
278 } else {
279 /* Unrecoverable error when copying from NAND */
280 hang();
281 }
282}
283
284/*
285 * Called in case of an exception.
286 */
287void hang(void)
288{
289 /* Loop forever */
290 while (1) ;
291}