blob: 1096727aed30ae33d5fabd0b6cfe8b3b0734af7a [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
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000033#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Heiko Schocher26cc10a2010-09-17 13:10:38 +020034static struct fsl_nfc_regs *const nfc = (void *)NFC_BASE_ADDR;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000035#elif defined(MXC_NFC_V3_2)
36static struct fsl_nfc_regs *const nfc = (void *)NFC_BASE_ADDR_AXI;
37static struct fsl_nfc_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR;
38#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020039
40static void nfc_wait_ready(void)
41{
42 uint32_t tmp;
43
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000044#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000045 while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT))
Magnus Lilja4133f652009-06-13 20:50:01 +020046 ;
47
48 /* Reset interrupt flag */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000049 tmp = readnfc(&nfc->config2);
50 tmp &= ~NFC_V1_V2_CONFIG2_INT;
51 writenfc(tmp, &nfc->config2);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000052#elif defined(MXC_NFC_V3_2)
53 while (!(readnfc(&nfc_ip->ipc) & NFC_V3_IPC_INT))
54 ;
55
56 /* Reset interrupt flag */
57 tmp = readnfc(&nfc_ip->ipc);
58 tmp &= ~NFC_V3_IPC_INT;
59 writenfc(tmp, &nfc_ip->ipc);
60#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020061}
62
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020063static void nfc_nand_init(void)
Magnus Lilja4133f652009-06-13 20:50:01 +020064{
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000065#if defined(MXC_NFC_V3_2)
66 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
67 int tmp;
68
69 tmp = (readnfc(&nfc_ip->config2) & ~(NFC_V3_CONFIG2_SPAS_MASK |
70 NFC_V3_CONFIG2_EDC_MASK | NFC_V3_CONFIG2_PS_MASK)) |
71 NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_SPARE_SIZE / 2) |
72 NFC_V3_CONFIG2_INT_MSK | NFC_V3_CONFIG2_ECC_EN |
73 NFC_V3_CONFIG2_ONE_CYCLE;
74 if (CONFIG_SYS_NAND_PAGE_SIZE == 4096)
75 tmp |= NFC_V3_CONFIG2_PS_4096;
76 else if (CONFIG_SYS_NAND_PAGE_SIZE == 2048)
77 tmp |= NFC_V3_CONFIG2_PS_2048;
78 else if (CONFIG_SYS_NAND_PAGE_SIZE == 512)
79 tmp |= NFC_V3_CONFIG2_PS_512;
80 /*
81 * if spare size is larger that 16 bytes per 512 byte hunk
82 * then use 8 symbol correction instead of 4
83 */
84 if (CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page > 16)
85 tmp |= NFC_V3_CONFIG2_ECC_MODE_8;
86 else
87 tmp &= ~NFC_V3_CONFIG2_ECC_MODE_8;
88 writenfc(tmp, &nfc_ip->config2);
89
90 tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
91 NFC_V3_CONFIG3_NO_SDMA |
92 NFC_V3_CONFIG3_RBB_MODE |
93 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
94 NFC_V3_CONFIG3_ADD_OP(0);
95#ifndef CONFIG_SYS_NAND_BUSWIDTH_16
96 tmp |= NFC_V3_CONFIG3_FW8;
97#endif
98 writenfc(tmp, &nfc_ip->config3);
99
100 writenfc(0, &nfc_ip->delay_line);
101#elif defined(MXC_NFC_V2_1)
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200102 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
John Rigby4c94c452010-01-26 19:24:17 -0700103 int config1;
104
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000105 writenfc(CONFIG_SYS_NAND_SPARE_SIZE / 2, &nfc->spare_area_size);
John Rigby4c94c452010-01-26 19:24:17 -0700106
107 /* unlocking RAM Buff */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000108 writenfc(0x2, &nfc->config);
John Rigby4c94c452010-01-26 19:24:17 -0700109
110 /* hardware ECC checking and correct */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000111 config1 = readnfc(&nfc->config1) | NFC_V1_V2_CONFIG1_ECC_EN |
112 NFC_V1_V2_CONFIG1_INT_MSK | NFC_V2_CONFIG1_ONE_CYCLE |
113 NFC_V2_CONFIG1_FP_INT;
John Rigby4c94c452010-01-26 19:24:17 -0700114 /*
115 * if spare size is larger that 16 bytes per 512 byte hunk
116 * then use 8 symbol correction instead of 4
117 */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200118 if (CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page > 16)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000119 config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4;
John Rigby4c94c452010-01-26 19:24:17 -0700120 else
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000121 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
122 writenfc(config1, &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700123#elif defined(MXC_NFC_V1)
Magnus Lilja4133f652009-06-13 20:50:01 +0200124 /* unlocking RAM Buff */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000125 writenfc(0x2, &nfc->config);
Magnus Lilja4133f652009-06-13 20:50:01 +0200126
127 /* hardware ECC checking and correct */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000128 writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK,
129 &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700130#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200131}
132
133static void nfc_nand_command(unsigned short command)
134{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000135 writenfc(command, &nfc->flash_cmd);
136 writenfc(NFC_CMD, &nfc->operation);
Magnus Lilja4133f652009-06-13 20:50:01 +0200137 nfc_wait_ready();
138}
139
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200140static void nfc_nand_address(unsigned short address)
141{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000142 writenfc(address, &nfc->flash_addr);
143 writenfc(NFC_ADDR, &nfc->operation);
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200144 nfc_wait_ready();
145}
146
Magnus Lilja4133f652009-06-13 20:50:01 +0200147static void nfc_nand_page_address(unsigned int page_address)
148{
149 unsigned int page_count;
150
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200151 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200152
John Rigby4c94c452010-01-26 19:24:17 -0700153 /* code only for large page flash */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200154 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
155 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200156
157 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
158
159 if (page_address <= page_count) {
160 page_count--; /* transform 0x01000000 to 0x00ffffff */
161 do {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200162 nfc_nand_address(page_address & 0xff);
Magnus Lilja4133f652009-06-13 20:50:01 +0200163 page_address = page_address >> 8;
164 page_count = page_count >> 8;
165 } while (page_count);
166 }
John Rigby4c94c452010-01-26 19:24:17 -0700167
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200168 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200169}
170
171static void nfc_nand_data_output(void)
172{
John Rigby4c94c452010-01-26 19:24:17 -0700173#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200174 int i;
John Rigby4c94c452010-01-26 19:24:17 -0700175#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200176
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000177#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000178 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000179#elif defined(MXC_NFC_V3_2)
180 int config1 = readnfc(&nfc->config1);
181 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
182 writenfc(config1, &nfc->config1);
183#endif
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000184 writenfc(NFC_OUTPUT, &nfc->operation);
John Rigby4c94c452010-01-26 19:24:17 -0700185 nfc_wait_ready();
186#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200187 /*
John Rigby4c94c452010-01-26 19:24:17 -0700188 * This NAND controller requires multiple input commands
189 * for pages larger than 512 bytes.
Magnus Lilja4133f652009-06-13 20:50:01 +0200190 */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200191 for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000192 writenfc(i, &nfc->buf_addr);
193 writenfc(NFC_OUTPUT, &nfc->operation);
Magnus Lilja4133f652009-06-13 20:50:01 +0200194 nfc_wait_ready();
195 }
John Rigby4c94c452010-01-26 19:24:17 -0700196#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200197}
198
199static int nfc_nand_check_ecc(void)
200{
Benoît Thébaudeaud29aeba2012-08-13 22:49:42 +0200201#if defined(MXC_NFC_V1)
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200202 u16 ecc_status = readw(&nfc->ecc_status_result);
203 return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000204#elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200205 u32 ecc_status = readl(&nfc->ecc_status_result);
206 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
207 int err_limit = CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page > 16 ? 8 : 4;
208 int subpages = CONFIG_SYS_NAND_PAGE_SIZE / 512;
209
210 do {
211 if ((ecc_status & 0xf) > err_limit)
212 return 1;
213 ecc_status >>= 4;
214 } while (--subpages);
215
216 return 0;
Benoît Thébaudeaud29aeba2012-08-13 22:49:42 +0200217#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200218}
219
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200220static void nfc_nand_read_page(unsigned int page_address)
Magnus Lilja4133f652009-06-13 20:50:01 +0200221{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000222 /* read in first 0 buffer */
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000223#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000224 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000225#elif defined(MXC_NFC_V3_2)
226 int config1 = readnfc(&nfc->config1);
227 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
228 writenfc(config1, &nfc->config1);
229#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200230 nfc_nand_command(NAND_CMD_READ0);
231 nfc_nand_page_address(page_address);
232
John Rigby4c94c452010-01-26 19:24:17 -0700233 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200234 nfc_nand_command(NAND_CMD_READSTART);
235
236 nfc_nand_data_output(); /* fill the main buffer 0 */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200237}
238
239static int nfc_read_page(unsigned int page_address, unsigned char *buf)
240{
241 int i;
242 u32 *src;
243 u32 *dst;
244
245 nfc_nand_read_page(page_address);
Magnus Lilja4133f652009-06-13 20:50:01 +0200246
247 if (nfc_nand_check_ecc())
248 return -1;
249
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200250 src = (u32 *)&nfc->main_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200251 dst = (u32 *)buf;
252
253 /* main copy loop from NAND-buffer to SDRAM memory */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200254 for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200255 writel(readl(src), dst);
256 src++;
257 dst++;
258 }
259
260 return 0;
261}
262
263static int is_badblock(int pagenumber)
264{
265 int page = pagenumber;
266 u32 badblock;
267 u32 *src;
268
269 /* Check the first two pages for bad block markers */
270 for (page = pagenumber; page < pagenumber + 2; page++) {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200271 nfc_nand_read_page(page);
Magnus Lilja4133f652009-06-13 20:50:01 +0200272
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200273 src = (u32 *)&nfc->spare_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200274
275 /*
276 * IMPORTANT NOTE: The nand flash controller uses a non-
277 * standard layout for large page devices. This can
278 * affect the position of the bad block marker.
279 */
280 /* Get the bad block marker */
281 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
282 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
283 badblock &= 0xff;
284
285 /* bad block marker verify */
286 if (badblock != 0xff)
287 return 1; /* potential bad block */
288 }
289
290 return 0;
291}
292
293static int nand_load(unsigned int from, unsigned int size, unsigned char *buf)
294{
295 int i;
296 unsigned int page;
297 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
298 CONFIG_SYS_NAND_PAGE_SIZE;
299
Magnus Lilja4133f652009-06-13 20:50:01 +0200300 nfc_nand_init();
301
302 /* Convert to page number */
303 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
304 i = 0;
305
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200306 while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200307 if (nfc_read_page(page, buf) < 0)
308 return -1;
309
310 page++;
311 i++;
312 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
313
314 /*
315 * Check if we have crossed a block boundary, and if so
316 * check for bad block.
317 */
318 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
319 /*
320 * Yes, new block. See if this block is good. If not,
John Rigby4c94c452010-01-26 19:24:17 -0700321 * loop until we find a good block.
Magnus Lilja4133f652009-06-13 20:50:01 +0200322 */
323 while (is_badblock(page)) {
324 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
325 /* Check i we've reached the end of flash. */
326 if (page >= maxpages)
327 return -1;
328 }
329 }
330 }
331
332 return 0;
333}
334
Wolfgang Denk627857d2010-10-28 20:35:36 +0200335#if defined(CONFIG_ARM)
Heiko Schocheraeb29912010-09-17 13:10:39 +0200336void board_init_f (ulong bootflag)
337{
Wolfgang Denkfb2759c2010-10-18 23:43:37 +0200338 relocate_code (CONFIG_SYS_TEXT_BASE - TOTAL_MALLOC_LEN, NULL,
339 CONFIG_SYS_TEXT_BASE);
Heiko Schocheraeb29912010-09-17 13:10:39 +0200340}
341#endif
342
Magnus Lilja4133f652009-06-13 20:50:01 +0200343/*
344 * The main entry for NAND booting. It's necessary that SDRAM is already
345 * configured and available since this code loads the main U-Boot image
346 * from NAND into SDRAM and starts it from there.
347 */
348void nand_boot(void)
349{
350 __attribute__((noreturn)) void (*uboot)(void);
351
Magnus Lilja4133f652009-06-13 20:50:01 +0200352 /*
353 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
354 * be aligned to full pages
355 */
356 if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
357 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
358 /* Copy from NAND successful, start U-boot */
359 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
360 uboot();
361 } else {
362 /* Unrecoverable error when copying from NAND */
363 hang();
364 }
365}
366
367/*
368 * Called in case of an exception.
369 */
370void hang(void)
371{
372 /* Loop forever */
373 while (1) ;
374}