blob: 6c03db8428f65d42fad0ec88cf879212c2da7033 [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
13#include <common.h>
14#include <nand.h>
Peter Tyser133c0fe2010-04-12 22:28:07 -050015#include <asm/arch/imx-regs.h>
Magnus Lilja4133f652009-06-13 20:50:01 +020016#include <asm/io.h>
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000017#include "mxc_nand.h"
Magnus Lilja4133f652009-06-13 20:50:01 +020018
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000019#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000020static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000021#elif defined(MXC_NFC_V3_2)
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000022static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR_AXI;
23static struct mxc_nand_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000024#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020025
26static void nfc_wait_ready(void)
27{
28 uint32_t tmp;
29
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000030#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000031 while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT))
Magnus Lilja4133f652009-06-13 20:50:01 +020032 ;
33
34 /* Reset interrupt flag */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000035 tmp = readnfc(&nfc->config2);
36 tmp &= ~NFC_V1_V2_CONFIG2_INT;
37 writenfc(tmp, &nfc->config2);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000038#elif defined(MXC_NFC_V3_2)
39 while (!(readnfc(&nfc_ip->ipc) & NFC_V3_IPC_INT))
40 ;
41
42 /* Reset interrupt flag */
43 tmp = readnfc(&nfc_ip->ipc);
44 tmp &= ~NFC_V3_IPC_INT;
45 writenfc(tmp, &nfc_ip->ipc);
46#endif
Magnus Lilja4133f652009-06-13 20:50:01 +020047}
48
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020049static void nfc_nand_init(void)
Magnus Lilja4133f652009-06-13 20:50:01 +020050{
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000051#if defined(MXC_NFC_V3_2)
52 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
53 int tmp;
54
55 tmp = (readnfc(&nfc_ip->config2) & ~(NFC_V3_CONFIG2_SPAS_MASK |
56 NFC_V3_CONFIG2_EDC_MASK | NFC_V3_CONFIG2_PS_MASK)) |
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000057 NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_OOBSIZE / 2) |
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000058 NFC_V3_CONFIG2_INT_MSK | NFC_V3_CONFIG2_ECC_EN |
59 NFC_V3_CONFIG2_ONE_CYCLE;
60 if (CONFIG_SYS_NAND_PAGE_SIZE == 4096)
61 tmp |= NFC_V3_CONFIG2_PS_4096;
62 else if (CONFIG_SYS_NAND_PAGE_SIZE == 2048)
63 tmp |= NFC_V3_CONFIG2_PS_2048;
64 else if (CONFIG_SYS_NAND_PAGE_SIZE == 512)
65 tmp |= NFC_V3_CONFIG2_PS_512;
66 /*
67 * if spare size is larger that 16 bytes per 512 byte hunk
68 * then use 8 symbol correction instead of 4
69 */
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000070 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
Benoît Thébaudeau8f265962013-04-11 09:35:37 +000071 tmp |= NFC_V3_CONFIG2_ECC_MODE_8;
72 else
73 tmp &= ~NFC_V3_CONFIG2_ECC_MODE_8;
74 writenfc(tmp, &nfc_ip->config2);
75
76 tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
77 NFC_V3_CONFIG3_NO_SDMA |
78 NFC_V3_CONFIG3_RBB_MODE |
79 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
80 NFC_V3_CONFIG3_ADD_OP(0);
81#ifndef CONFIG_SYS_NAND_BUSWIDTH_16
82 tmp |= NFC_V3_CONFIG3_FW8;
83#endif
84 writenfc(tmp, &nfc_ip->config3);
85
86 writenfc(0, &nfc_ip->delay_line);
87#elif defined(MXC_NFC_V2_1)
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +020088 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
John Rigby4c94c452010-01-26 19:24:17 -070089 int config1;
90
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +000091 writenfc(CONFIG_SYS_NAND_OOBSIZE / 2, &nfc->spare_area_size);
John Rigby4c94c452010-01-26 19:24:17 -070092
93 /* unlocking RAM Buff */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000094 writenfc(0x2, &nfc->config);
John Rigby4c94c452010-01-26 19:24:17 -070095
96 /* hardware ECC checking and correct */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +000097 config1 = readnfc(&nfc->config1) | NFC_V1_V2_CONFIG1_ECC_EN |
98 NFC_V1_V2_CONFIG1_INT_MSK | NFC_V2_CONFIG1_ONE_CYCLE |
99 NFC_V2_CONFIG1_FP_INT;
John Rigby4c94c452010-01-26 19:24:17 -0700100 /*
101 * if spare size is larger that 16 bytes per 512 byte hunk
102 * then use 8 symbol correction instead of 4
103 */
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +0000104 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000105 config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4;
John Rigby4c94c452010-01-26 19:24:17 -0700106 else
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000107 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
108 writenfc(config1, &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700109#elif defined(MXC_NFC_V1)
Magnus Lilja4133f652009-06-13 20:50:01 +0200110 /* unlocking RAM Buff */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000111 writenfc(0x2, &nfc->config);
Magnus Lilja4133f652009-06-13 20:50:01 +0200112
113 /* hardware ECC checking and correct */
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000114 writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK,
115 &nfc->config1);
John Rigby4c94c452010-01-26 19:24:17 -0700116#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200117}
118
119static void nfc_nand_command(unsigned short command)
120{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000121 writenfc(command, &nfc->flash_cmd);
122 writenfc(NFC_CMD, &nfc->operation);
Magnus Lilja4133f652009-06-13 20:50:01 +0200123 nfc_wait_ready();
124}
125
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200126static void nfc_nand_address(unsigned short address)
127{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000128 writenfc(address, &nfc->flash_addr);
129 writenfc(NFC_ADDR, &nfc->operation);
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200130 nfc_wait_ready();
131}
132
Magnus Lilja4133f652009-06-13 20:50:01 +0200133static void nfc_nand_page_address(unsigned int page_address)
134{
135 unsigned int page_count;
136
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200137 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200138
John Rigby4c94c452010-01-26 19:24:17 -0700139 /* code only for large page flash */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200140 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
141 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200142
143 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
144
145 if (page_address <= page_count) {
146 page_count--; /* transform 0x01000000 to 0x00ffffff */
147 do {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200148 nfc_nand_address(page_address & 0xff);
Magnus Lilja4133f652009-06-13 20:50:01 +0200149 page_address = page_address >> 8;
150 page_count = page_count >> 8;
151 } while (page_count);
152 }
John Rigby4c94c452010-01-26 19:24:17 -0700153
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200154 nfc_nand_address(0x00);
Magnus Lilja4133f652009-06-13 20:50:01 +0200155}
156
157static void nfc_nand_data_output(void)
158{
John Rigby4c94c452010-01-26 19:24:17 -0700159#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200160 int i;
John Rigby4c94c452010-01-26 19:24:17 -0700161#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200162
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000163#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000164 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000165#elif defined(MXC_NFC_V3_2)
166 int config1 = readnfc(&nfc->config1);
167 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
168 writenfc(config1, &nfc->config1);
169#endif
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000170 writenfc(NFC_OUTPUT, &nfc->operation);
John Rigby4c94c452010-01-26 19:24:17 -0700171 nfc_wait_ready();
172#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja4133f652009-06-13 20:50:01 +0200173 /*
John Rigby4c94c452010-01-26 19:24:17 -0700174 * This NAND controller requires multiple input commands
175 * for pages larger than 512 bytes.
Magnus Lilja4133f652009-06-13 20:50:01 +0200176 */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200177 for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000178 writenfc(i, &nfc->buf_addr);
179 writenfc(NFC_OUTPUT, &nfc->operation);
Magnus Lilja4133f652009-06-13 20:50:01 +0200180 nfc_wait_ready();
181 }
John Rigby4c94c452010-01-26 19:24:17 -0700182#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200183}
184
185static int nfc_nand_check_ecc(void)
186{
Benoît Thébaudeaud29aeba2012-08-13 22:49:42 +0200187#if defined(MXC_NFC_V1)
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200188 u16 ecc_status = readw(&nfc->ecc_status_result);
189 return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2;
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000190#elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200191 u32 ecc_status = readl(&nfc->ecc_status_result);
192 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
Benoît Thébaudeauefb7c002013-04-11 09:35:51 +0000193 int err_limit = CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16 ? 8 : 4;
Benoît Thébaudeau21fa48c2012-08-13 22:49:53 +0200194 int subpages = CONFIG_SYS_NAND_PAGE_SIZE / 512;
195
196 do {
197 if ((ecc_status & 0xf) > err_limit)
198 return 1;
199 ecc_status >>= 4;
200 } while (--subpages);
201
202 return 0;
Benoît Thébaudeaud29aeba2012-08-13 22:49:42 +0200203#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200204}
205
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200206static void nfc_nand_read_page(unsigned int page_address)
Magnus Lilja4133f652009-06-13 20:50:01 +0200207{
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000208 /* read in first 0 buffer */
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000209#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau555bba12013-04-11 09:35:36 +0000210 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau8f265962013-04-11 09:35:37 +0000211#elif defined(MXC_NFC_V3_2)
212 int config1 = readnfc(&nfc->config1);
213 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
214 writenfc(config1, &nfc->config1);
215#endif
Magnus Lilja4133f652009-06-13 20:50:01 +0200216 nfc_nand_command(NAND_CMD_READ0);
217 nfc_nand_page_address(page_address);
218
John Rigby4c94c452010-01-26 19:24:17 -0700219 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja4133f652009-06-13 20:50:01 +0200220 nfc_nand_command(NAND_CMD_READSTART);
221
222 nfc_nand_data_output(); /* fill the main buffer 0 */
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200223}
224
225static int nfc_read_page(unsigned int page_address, unsigned char *buf)
226{
227 int i;
228 u32 *src;
229 u32 *dst;
230
231 nfc_nand_read_page(page_address);
Magnus Lilja4133f652009-06-13 20:50:01 +0200232
233 if (nfc_nand_check_ecc())
Scott Wood52ab7ce2016-05-30 13:57:58 -0500234 return -EBADMSG;
Magnus Lilja4133f652009-06-13 20:50:01 +0200235
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200236 src = (u32 *)&nfc->main_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200237 dst = (u32 *)buf;
238
239 /* main copy loop from NAND-buffer to SDRAM memory */
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200240 for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200241 writel(readl(src), dst);
242 src++;
243 dst++;
244 }
245
246 return 0;
247}
248
249static int is_badblock(int pagenumber)
250{
251 int page = pagenumber;
252 u32 badblock;
253 u32 *src;
254
255 /* Check the first two pages for bad block markers */
256 for (page = pagenumber; page < pagenumber + 2; page++) {
Benoît Thébaudeau2174f3b2012-08-13 22:48:56 +0200257 nfc_nand_read_page(page);
Magnus Lilja4133f652009-06-13 20:50:01 +0200258
Benoît Thébaudeau3f775192012-08-13 22:48:12 +0200259 src = (u32 *)&nfc->spare_area[0][0];
Magnus Lilja4133f652009-06-13 20:50:01 +0200260
261 /*
262 * IMPORTANT NOTE: The nand flash controller uses a non-
263 * standard layout for large page devices. This can
264 * affect the position of the bad block marker.
265 */
266 /* Get the bad block marker */
267 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
268 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
269 badblock &= 0xff;
270
271 /* bad block marker verify */
272 if (badblock != 0xff)
273 return 1; /* potential bad block */
274 }
275
276 return 0;
277}
278
Marek Vasut24016692013-04-21 05:52:23 +0000279int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
Magnus Lilja4133f652009-06-13 20:50:01 +0200280{
281 int i;
282 unsigned int page;
283 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
284 CONFIG_SYS_NAND_PAGE_SIZE;
285
Magnus Lilja4133f652009-06-13 20:50:01 +0200286 nfc_nand_init();
287
288 /* Convert to page number */
289 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
290 i = 0;
291
Marek Vasut24016692013-04-21 05:52:23 +0000292 size = roundup(size, CONFIG_SYS_NAND_PAGE_SIZE);
Benoît Thébaudeau32ae5b42012-08-13 22:48:26 +0200293 while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
Magnus Lilja4133f652009-06-13 20:50:01 +0200294 if (nfc_read_page(page, buf) < 0)
295 return -1;
296
297 page++;
298 i++;
299 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
300
301 /*
302 * Check if we have crossed a block boundary, and if so
303 * check for bad block.
304 */
305 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
306 /*
307 * Yes, new block. See if this block is good. If not,
John Rigby4c94c452010-01-26 19:24:17 -0700308 * loop until we find a good block.
Magnus Lilja4133f652009-06-13 20:50:01 +0200309 */
310 while (is_badblock(page)) {
311 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
312 /* Check i we've reached the end of flash. */
313 if (page >= maxpages)
314 return -1;
315 }
316 }
317 }
318
319 return 0;
320}
321
Marek Vasut24016692013-04-21 05:52:23 +0000322#ifndef CONFIG_SPL_FRAMEWORK
Magnus Lilja4133f652009-06-13 20:50:01 +0200323/*
324 * The main entry for NAND booting. It's necessary that SDRAM is already
325 * configured and available since this code loads the main U-Boot image
326 * from NAND into SDRAM and starts it from there.
327 */
328void nand_boot(void)
329{
330 __attribute__((noreturn)) void (*uboot)(void);
331
Magnus Lilja4133f652009-06-13 20:50:01 +0200332 /*
333 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
334 * be aligned to full pages
335 */
Marek Vasut24016692013-04-21 05:52:23 +0000336 if (!nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
337 CONFIG_SYS_NAND_U_BOOT_SIZE,
338 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
Bin Meng75574052016-02-05 19:30:11 -0800339 /* Copy from NAND successful, start U-Boot */
Magnus Lilja4133f652009-06-13 20:50:01 +0200340 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
341 uboot();
342 } else {
343 /* Unrecoverable error when copying from NAND */
344 hang();
345 }
346}
Marek Vasut24016692013-04-21 05:52:23 +0000347#endif
Albert ARIBAUDaa018732013-05-11 04:29:50 +0000348
349void nand_init(void) {}
350void nand_deselect(void) {}