blob: a855c9987f803e452b1c3469c2eeeb8e703c5a02 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Magnus Lilja4133f652009-06-13 20:50:01 +02002/*
3 * (C) Copyright 2009
4 * Magnus Lilja <lilja.magnus@gmail.com>
5 *
6 * (C) Copyright 2008
7 * Maxim Artamonov, <scn1874 at yandex.ru>
8 *
9 * (C) Copyright 2006-2008
10 * Stefan Roese, DENX Software Engineering, sr at denx.de.
Magnus Lilja4133f652009-06-13 20:50:01 +020011 */
12
Tom Riniabb9a042024-05-18 20:20:43 -060013#include <common.h>
Simon Glassf11478f2019-12-28 10:45:07 -070014#include <hang.h>
Magnus Lilja4133f652009-06-13 20:50:01 +020015#include <nand.h>
Sean Anderson11a4c702023-11-04 16:37:41 -040016#include <system-constants.h>
Tom Rini3bde7e22021-09-22 14:50:35 -040017#include <linux/mtd/rawnand.h>
Peter Tyser133c0fe2010-04-12 22:28:07 -050018#include <asm/arch/imx-regs.h>
Magnus Lilja4133f652009-06-13 20:50:01 +020019#include <asm/io.h>
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000020#include "mxc_nand.h"
Magnus Lilja4133f652009-06-13 20:50:01 +020021
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000022#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000023static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000024#elif defined(MXC_NFC_V3_2)
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000025static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR_AXI;
26static struct mxc_nand_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000027#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020028
29static void nfc_wait_ready(void)
30{
31 uint32_t tmp;
32
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000033#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000034 while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT))
Magnus Lilja4133f652009-06-13 20:50:01 +020035 ;
36
37 /* Reset interrupt flag */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000038 tmp = readnfc(&nfc->config2);
39 tmp &= ~NFC_V1_V2_CONFIG2_INT;
40 writenfc(tmp, &nfc->config2);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000041#elif defined(MXC_NFC_V3_2)
42 while (!(readnfc(&nfc_ip->ipc) & NFC_V3_IPC_INT))
43 ;
44
45 /* Reset interrupt flag */
46 tmp = readnfc(&nfc_ip->ipc);
47 tmp &= ~NFC_V3_IPC_INT;
48 writenfc(tmp, &nfc_ip->ipc);
49#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020050}
51
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020052static void nfc_nand_init(void)
Magnus Lilja4133f652009-06-13 20:50:01 +020053{
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000054#if defined(MXC_NFC_V3_2)
55 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
56 int tmp;
57
58 tmp = (readnfc(&nfc_ip->config2) & ~(NFC_V3_CONFIG2_SPAS_MASK |
59 NFC_V3_CONFIG2_EDC_MASK | NFC_V3_CONFIG2_PS_MASK)) |
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000060 NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_OOBSIZE / 2) |
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000061 NFC_V3_CONFIG2_INT_MSK | NFC_V3_CONFIG2_ECC_EN |
62 NFC_V3_CONFIG2_ONE_CYCLE;
63 if (CONFIG_SYS_NAND_PAGE_SIZE == 4096)
64 tmp |= NFC_V3_CONFIG2_PS_4096;
65 else if (CONFIG_SYS_NAND_PAGE_SIZE == 2048)
66 tmp |= NFC_V3_CONFIG2_PS_2048;
67 else if (CONFIG_SYS_NAND_PAGE_SIZE == 512)
68 tmp |= NFC_V3_CONFIG2_PS_512;
69 /*
70 * if spare size is larger that 16 bytes per 512 byte hunk
71 * then use 8 symbol correction instead of 4
72 */
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000073 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000074 tmp |= NFC_V3_CONFIG2_ECC_MODE_8;
75 else
76 tmp &= ~NFC_V3_CONFIG2_ECC_MODE_8;
77 writenfc(tmp, &nfc_ip->config2);
78
79 tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
80 NFC_V3_CONFIG3_NO_SDMA |
81 NFC_V3_CONFIG3_RBB_MODE |
82 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
83 NFC_V3_CONFIG3_ADD_OP(0);
84#ifndef CONFIG_SYS_NAND_BUSWIDTH_16
85 tmp |= NFC_V3_CONFIG3_FW8;
86#endif
87 writenfc(tmp, &nfc_ip->config3);
88
89 writenfc(0, &nfc_ip->delay_line);
90#elif defined(MXC_NFC_V2_1)
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020091 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
John Rigby4c94c452010-01-26 19:24:17 -070092 int config1;
93
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000094 writenfc(CONFIG_SYS_NAND_OOBSIZE / 2, &nfc->spare_area_size);
John Rigby4c94c452010-01-26 19:24:17 -070095
96 /* unlocking RAM Buff */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000097 writenfc(0x2, &nfc->config);
John Rigby4c94c452010-01-26 19:24:17 -070098
99 /* hardware ECC checking and correct */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000100 config1 = readnfc(&nfc->config1) | NFC_V1_V2_CONFIG1_ECC_EN |
101 NFC_V1_V2_CONFIG1_INT_MSK | NFC_V2_CONFIG1_ONE_CYCLE |
102 NFC_V2_CONFIG1_FP_INT;
John Rigby4c94c452010-01-26 19:24:17 -0700103 /*
104 * if spare size is larger that 16 bytes per 512 byte hunk
105 * then use 8 symbol correction instead of 4
106 */
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +0000107 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000108 config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4;
John Rigby4c94c452010-01-26 19:24:17 -0700109 else
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000110 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
111 writenfc(config1, &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700112#elif defined(MXC_NFC_V1)
Magnus Lilja4133f652009-06-13 20:50:01 +0200113 /* unlocking RAM Buff */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000114 writenfc(0x2, &nfc->config);
Magnus Lilja4133f652009-06-13 20:50:01 +0200115
116 /* hardware ECC checking and correct */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000117 writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK,
118 &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700119#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200120}
121
122static void nfc_nand_command(unsigned short command)
123{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000124 writenfc(command, &nfc->flash_cmd);
125 writenfc(NFC_CMD, &nfc->operation);
Magnus Lilja4133f652009-06-13 20:50:01 +0200126 nfc_wait_ready();
127}
128
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200129static void nfc_nand_address(unsigned short address)
130{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000131 writenfc(address, &nfc->flash_addr);
132 writenfc(NFC_ADDR, &nfc->operation);
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200133 nfc_wait_ready();
134}
135
Magnus Lilja4133f652009-06-13 20:50:01 +0200136static void nfc_nand_page_address(unsigned int page_address)
137{
138 unsigned int page_count;
139
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200140 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200141
John Rigby4c94c452010-01-26 19:24:17 -0700142 /* code only for large page flash */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200143 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
144 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200145
146 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
147
148 if (page_address <= page_count) {
149 page_count--; /* transform 0x01000000 to 0x00ffffff */
150 do {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200151 nfc_nand_address(page_address & 0xff);
Magnus Lilja4133f652009-06-13 20:50:01 +0200152 page_address = page_address >> 8;
153 page_count = page_count >> 8;
154 } while (page_count);
155 }
John Rigby4c94c452010-01-26 19:24:17 -0700156
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200157 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200158}
159
160static void nfc_nand_data_output(void)
161{
John Rigby4c94c452010-01-26 19:24:17 -0700162#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200163 int i;
John Rigby4c94c452010-01-26 19:24:17 -0700164#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200165
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000166#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000167 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000168#elif defined(MXC_NFC_V3_2)
169 int config1 = readnfc(&nfc->config1);
170 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
171 writenfc(config1, &nfc->config1);
172#endif
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000173 writenfc(NFC_OUTPUT, &nfc->operation);
John Rigby4c94c452010-01-26 19:24:17 -0700174 nfc_wait_ready();
175#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200176 /*
John Rigby4c94c452010-01-26 19:24:17 -0700177 * This NAND controller requires multiple input commands
178 * for pages larger than 512 bytes.
Magnus Lilja4133f652009-06-13 20:50:01 +0200179 */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200180 for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000181 writenfc(i, &nfc->buf_addr);
182 writenfc(NFC_OUTPUT, &nfc->operation);
Magnus Lilja4133f652009-06-13 20:50:01 +0200183 nfc_wait_ready();
184 }
John Rigby4c94c452010-01-26 19:24:17 -0700185#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200186}
187
188static int nfc_nand_check_ecc(void)
189{
Benoît Thébaudeaud29aeba2012-08-13 22:49:42 +0200190#if defined(MXC_NFC_V1)
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200191 u16 ecc_status = readw(&nfc->ecc_status_result);
192 return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000193#elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200194 u32 ecc_status = readl(&nfc->ecc_status_result);
195 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +0000196 int err_limit = CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16 ? 8 : 4;
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200197 int subpages = CONFIG_SYS_NAND_PAGE_SIZE / 512;
198
199 do {
200 if ((ecc_status & 0xf) > err_limit)
201 return 1;
202 ecc_status >>= 4;
203 } while (--subpages);
204
205 return 0;
Benoît Thébaudeaud29aeba2012-08-13 22:49:42 +0200206#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200207}
208
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200209static void nfc_nand_read_page(unsigned int page_address)
Magnus Lilja4133f652009-06-13 20:50:01 +0200210{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000211 /* read in first 0 buffer */
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000212#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000213 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000214#elif defined(MXC_NFC_V3_2)
215 int config1 = readnfc(&nfc->config1);
216 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
217 writenfc(config1, &nfc->config1);
218#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200219 nfc_nand_command(NAND_CMD_READ0);
220 nfc_nand_page_address(page_address);
221
John Rigby4c94c452010-01-26 19:24:17 -0700222 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200223 nfc_nand_command(NAND_CMD_READSTART);
224
225 nfc_nand_data_output(); /* fill the main buffer 0 */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200226}
227
228static int nfc_read_page(unsigned int page_address, unsigned char *buf)
229{
230 int i;
231 u32 *src;
232 u32 *dst;
233
234 nfc_nand_read_page(page_address);
Magnus Lilja4133f652009-06-13 20:50:01 +0200235
236 if (nfc_nand_check_ecc())
Scott Wood52ab7ce2016-05-30 13:57:58 -0500237 return -EBADMSG;
Magnus Lilja4133f652009-06-13 20:50:01 +0200238
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200239 src = (u32 *)&nfc->main_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200240 dst = (u32 *)buf;
241
242 /* main copy loop from NAND-buffer to SDRAM memory */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200243 for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200244 writel(readl(src), dst);
245 src++;
246 dst++;
247 }
248
249 return 0;
250}
251
252static int is_badblock(int pagenumber)
253{
254 int page = pagenumber;
255 u32 badblock;
256 u32 *src;
257
258 /* Check the first two pages for bad block markers */
259 for (page = pagenumber; page < pagenumber + 2; page++) {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200260 nfc_nand_read_page(page);
Magnus Lilja4133f652009-06-13 20:50:01 +0200261
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200262 src = (u32 *)&nfc->spare_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200263
264 /*
265 * IMPORTANT NOTE: The nand flash controller uses a non-
266 * standard layout for large page devices. This can
267 * affect the position of the bad block marker.
268 */
269 /* Get the bad block marker */
270 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
271 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
272 badblock &= 0xff;
273
274 /* bad block marker verify */
275 if (badblock != 0xff)
276 return 1; /* potential bad block */
277 }
278
279 return 0;
280}
281
Marek Vasut24016692013-04-21 05:52:23 +0000282int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
Magnus Lilja4133f652009-06-13 20:50:01 +0200283{
284 int i;
285 unsigned int page;
286 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
287 CONFIG_SYS_NAND_PAGE_SIZE;
288
Magnus Lilja4133f652009-06-13 20:50:01 +0200289 nfc_nand_init();
290
291 /* Convert to page number */
292 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
293 i = 0;
294
Marek Vasut24016692013-04-21 05:52:23 +0000295 size = roundup(size, CONFIG_SYS_NAND_PAGE_SIZE);
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200296 while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200297 if (nfc_read_page(page, buf) < 0)
298 return -1;
299
300 page++;
301 i++;
302 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
303
304 /*
305 * Check if we have crossed a block boundary, and if so
306 * check for bad block.
307 */
Sean Anderson11a4c702023-11-04 16:37:41 -0400308 if (!(page % SYS_NAND_BLOCK_PAGES)) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200309 /*
310 * Yes, new block. See if this block is good. If not,
John Rigby4c94c452010-01-26 19:24:17 -0700311 * loop until we find a good block.
Magnus Lilja4133f652009-06-13 20:50:01 +0200312 */
313 while (is_badblock(page)) {
Sean Anderson11a4c702023-11-04 16:37:41 -0400314 page = page + SYS_NAND_BLOCK_PAGES;
Magnus Lilja4133f652009-06-13 20:50:01 +0200315 /* Check i we've reached the end of flash. */
316 if (page >= maxpages)
317 return -1;
318 }
319 }
320 }
321
322 return 0;
323}
324
Marek Vasut24016692013-04-21 05:52:23 +0000325#ifndef CONFIG_SPL_FRAMEWORK
Magnus Lilja4133f652009-06-13 20:50:01 +0200326/*
327 * The main entry for NAND booting. It's necessary that SDRAM is already
328 * configured and available since this code loads the main U-Boot image
329 * from NAND into SDRAM and starts it from there.
330 */
Marek Behún4c0237c2021-05-20 13:24:13 +0200331__used void nand_boot(void)
Magnus Lilja4133f652009-06-13 20:50:01 +0200332{
333 __attribute__((noreturn)) void (*uboot)(void);
334
Magnus Lilja4133f652009-06-13 20:50:01 +0200335 /*
Tom Rinib4213492022-11-12 17:36:51 -0500336 * CONFIG_SYS_NAND_U_BOOT_OFFS and CFG_SYS_NAND_U_BOOT_SIZE must
Magnus Lilja4133f652009-06-13 20:50:01 +0200337 * be aligned to full pages
338 */
Marek Vasut24016692013-04-21 05:52:23 +0000339 if (!nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
Tom Rinib4213492022-11-12 17:36:51 -0500340 CFG_SYS_NAND_U_BOOT_SIZE,
341 (uchar *)CFG_SYS_NAND_U_BOOT_DST)) {
Bin Meng75574052016-02-05 19:30:11 -0800342 /* Copy from NAND successful, start U-Boot */
Tom Rinib4213492022-11-12 17:36:51 -0500343 uboot = (void *)CFG_SYS_NAND_U_BOOT_START;
Magnus Lilja4133f652009-06-13 20:50:01 +0200344 uboot();
345 } else {
346 /* Unrecoverable error when copying from NAND */
347 hang();
348 }
349}
Marek Vasut24016692013-04-21 05:52:23 +0000350#endif
Albert ARIBAUDaa018732013-05-11 04:29:50 +0000351
352void nand_init(void) {}
353void nand_deselect(void) {}
Sean Anderson8805f9d2023-11-04 16:37:44 -0400354
355unsigned int nand_page_size(void)
356{
357 return CONFIG_SYS_NAND_PAGE_SIZE;
358}