blob: 69b736a848dbca6b3e0c8a4365ace5e317e7d53f [file] [log] [blame]
Magnus Lilja40c642b2009-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 *
Wolfgang Denk1a459662013-07-08 09:37:19 +020011 * SPDX-License-Identifier: GPL-2.0+
Magnus Lilja40c642b2009-06-13 20:50:01 +020012 */
13
14#include <common.h>
15#include <nand.h>
Peter Tyser61f2b382010-04-12 22:28:07 -050016#include <asm/arch/imx-regs.h>
Magnus Lilja40c642b2009-06-13 20:50:01 +020017#include <asm/io.h>
Benoît Thébaudeauda962b72013-04-11 09:35:51 +000018#include "mxc_nand.h"
Magnus Lilja40c642b2009-06-13 20:50:01 +020019
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000020#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeauda962b72013-04-11 09:35:51 +000021static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR;
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000022#elif defined(MXC_NFC_V3_2)
Benoît Thébaudeauda962b72013-04-11 09:35:51 +000023static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR_AXI;
24static struct mxc_nand_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR;
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000025#endif
Magnus Lilja40c642b2009-06-13 20:50:01 +020026
27static void nfc_wait_ready(void)
28{
29 uint32_t tmp;
30
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000031#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +000032 while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT))
Magnus Lilja40c642b2009-06-13 20:50:01 +020033 ;
34
35 /* Reset interrupt flag */
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +000036 tmp = readnfc(&nfc->config2);
37 tmp &= ~NFC_V1_V2_CONFIG2_INT;
38 writenfc(tmp, &nfc->config2);
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000039#elif defined(MXC_NFC_V3_2)
40 while (!(readnfc(&nfc_ip->ipc) & NFC_V3_IPC_INT))
41 ;
42
43 /* Reset interrupt flag */
44 tmp = readnfc(&nfc_ip->ipc);
45 tmp &= ~NFC_V3_IPC_INT;
46 writenfc(tmp, &nfc_ip->ipc);
47#endif
Magnus Lilja40c642b2009-06-13 20:50:01 +020048}
49
Benoît Thébaudeau365b2c02012-08-13 22:48:26 +020050static void nfc_nand_init(void)
Magnus Lilja40c642b2009-06-13 20:50:01 +020051{
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000052#if defined(MXC_NFC_V3_2)
53 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
54 int tmp;
55
56 tmp = (readnfc(&nfc_ip->config2) & ~(NFC_V3_CONFIG2_SPAS_MASK |
57 NFC_V3_CONFIG2_EDC_MASK | NFC_V3_CONFIG2_PS_MASK)) |
Benoît Thébaudeauda962b72013-04-11 09:35:51 +000058 NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_OOBSIZE / 2) |
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000059 NFC_V3_CONFIG2_INT_MSK | NFC_V3_CONFIG2_ECC_EN |
60 NFC_V3_CONFIG2_ONE_CYCLE;
61 if (CONFIG_SYS_NAND_PAGE_SIZE == 4096)
62 tmp |= NFC_V3_CONFIG2_PS_4096;
63 else if (CONFIG_SYS_NAND_PAGE_SIZE == 2048)
64 tmp |= NFC_V3_CONFIG2_PS_2048;
65 else if (CONFIG_SYS_NAND_PAGE_SIZE == 512)
66 tmp |= NFC_V3_CONFIG2_PS_512;
67 /*
68 * if spare size is larger that 16 bytes per 512 byte hunk
69 * then use 8 symbol correction instead of 4
70 */
Benoît Thébaudeauda962b72013-04-11 09:35:51 +000071 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +000072 tmp |= NFC_V3_CONFIG2_ECC_MODE_8;
73 else
74 tmp &= ~NFC_V3_CONFIG2_ECC_MODE_8;
75 writenfc(tmp, &nfc_ip->config2);
76
77 tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) |
78 NFC_V3_CONFIG3_NO_SDMA |
79 NFC_V3_CONFIG3_RBB_MODE |
80 NFC_V3_CONFIG3_SBB(6) | /* Reset default */
81 NFC_V3_CONFIG3_ADD_OP(0);
82#ifndef CONFIG_SYS_NAND_BUSWIDTH_16
83 tmp |= NFC_V3_CONFIG3_FW8;
84#endif
85 writenfc(tmp, &nfc_ip->config3);
86
87 writenfc(0, &nfc_ip->delay_line);
88#elif defined(MXC_NFC_V2_1)
Benoît Thébaudeau365b2c02012-08-13 22:48:26 +020089 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
John Rigbyf3bb63a2010-01-26 19:24:17 -070090 int config1;
91
Benoît Thébaudeauda962b72013-04-11 09:35:51 +000092 writenfc(CONFIG_SYS_NAND_OOBSIZE / 2, &nfc->spare_area_size);
John Rigbyf3bb63a2010-01-26 19:24:17 -070093
94 /* unlocking RAM Buff */
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +000095 writenfc(0x2, &nfc->config);
John Rigbyf3bb63a2010-01-26 19:24:17 -070096
97 /* hardware ECC checking and correct */
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +000098 config1 = readnfc(&nfc->config1) | NFC_V1_V2_CONFIG1_ECC_EN |
99 NFC_V1_V2_CONFIG1_INT_MSK | NFC_V2_CONFIG1_ONE_CYCLE |
100 NFC_V2_CONFIG1_FP_INT;
John Rigbyf3bb63a2010-01-26 19:24:17 -0700101 /*
102 * if spare size is larger that 16 bytes per 512 byte hunk
103 * then use 8 symbol correction instead of 4
104 */
Benoît Thébaudeauda962b72013-04-11 09:35:51 +0000105 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000106 config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4;
John Rigbyf3bb63a2010-01-26 19:24:17 -0700107 else
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000108 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
109 writenfc(config1, &nfc->config1);
John Rigbyf3bb63a2010-01-26 19:24:17 -0700110#elif defined(MXC_NFC_V1)
Magnus Lilja40c642b2009-06-13 20:50:01 +0200111 /* unlocking RAM Buff */
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000112 writenfc(0x2, &nfc->config);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200113
114 /* hardware ECC checking and correct */
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000115 writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK,
116 &nfc->config1);
John Rigbyf3bb63a2010-01-26 19:24:17 -0700117#endif
Magnus Lilja40c642b2009-06-13 20:50:01 +0200118}
119
120static void nfc_nand_command(unsigned short command)
121{
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000122 writenfc(command, &nfc->flash_cmd);
123 writenfc(NFC_CMD, &nfc->operation);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200124 nfc_wait_ready();
125}
126
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200127static void nfc_nand_address(unsigned short address)
128{
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000129 writenfc(address, &nfc->flash_addr);
130 writenfc(NFC_ADDR, &nfc->operation);
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200131 nfc_wait_ready();
132}
133
Magnus Lilja40c642b2009-06-13 20:50:01 +0200134static void nfc_nand_page_address(unsigned int page_address)
135{
136 unsigned int page_count;
137
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200138 nfc_nand_address(0x00);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200139
John Rigbyf3bb63a2010-01-26 19:24:17 -0700140 /* code only for large page flash */
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200141 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
142 nfc_nand_address(0x00);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200143
144 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
145
146 if (page_address <= page_count) {
147 page_count--; /* transform 0x01000000 to 0x00ffffff */
148 do {
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200149 nfc_nand_address(page_address & 0xff);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200150 page_address = page_address >> 8;
151 page_count = page_count >> 8;
152 } while (page_count);
153 }
John Rigbyf3bb63a2010-01-26 19:24:17 -0700154
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200155 nfc_nand_address(0x00);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200156}
157
158static void nfc_nand_data_output(void)
159{
John Rigbyf3bb63a2010-01-26 19:24:17 -0700160#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja40c642b2009-06-13 20:50:01 +0200161 int i;
John Rigbyf3bb63a2010-01-26 19:24:17 -0700162#endif
Magnus Lilja40c642b2009-06-13 20:50:01 +0200163
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +0000164#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000165 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +0000166#elif defined(MXC_NFC_V3_2)
167 int config1 = readnfc(&nfc->config1);
168 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
169 writenfc(config1, &nfc->config1);
170#endif
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000171 writenfc(NFC_OUTPUT, &nfc->operation);
John Rigbyf3bb63a2010-01-26 19:24:17 -0700172 nfc_wait_ready();
173#ifdef NAND_MXC_2K_MULTI_CYCLE
Magnus Lilja40c642b2009-06-13 20:50:01 +0200174 /*
John Rigbyf3bb63a2010-01-26 19:24:17 -0700175 * This NAND controller requires multiple input commands
176 * for pages larger than 512 bytes.
Magnus Lilja40c642b2009-06-13 20:50:01 +0200177 */
Benoît Thébaudeau365b2c02012-08-13 22:48:26 +0200178 for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000179 writenfc(i, &nfc->buf_addr);
180 writenfc(NFC_OUTPUT, &nfc->operation);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200181 nfc_wait_ready();
182 }
John Rigbyf3bb63a2010-01-26 19:24:17 -0700183#endif
Magnus Lilja40c642b2009-06-13 20:50:01 +0200184}
185
186static int nfc_nand_check_ecc(void)
187{
Benoît Thébaudeauc1db8dd2012-08-13 22:49:42 +0200188#if defined(MXC_NFC_V1)
Benoît Thébaudeaub8fea2b2012-08-13 22:49:53 +0200189 u16 ecc_status = readw(&nfc->ecc_status_result);
190 return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2;
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +0000191#elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
Benoît Thébaudeaub8fea2b2012-08-13 22:49:53 +0200192 u32 ecc_status = readl(&nfc->ecc_status_result);
193 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
Benoît Thébaudeauda962b72013-04-11 09:35:51 +0000194 int err_limit = CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16 ? 8 : 4;
Benoît Thébaudeaub8fea2b2012-08-13 22:49:53 +0200195 int subpages = CONFIG_SYS_NAND_PAGE_SIZE / 512;
196
197 do {
198 if ((ecc_status & 0xf) > err_limit)
199 return 1;
200 ecc_status >>= 4;
201 } while (--subpages);
202
203 return 0;
Benoît Thébaudeauc1db8dd2012-08-13 22:49:42 +0200204#endif
Magnus Lilja40c642b2009-06-13 20:50:01 +0200205}
206
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200207static void nfc_nand_read_page(unsigned int page_address)
Magnus Lilja40c642b2009-06-13 20:50:01 +0200208{
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000209 /* read in first 0 buffer */
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +0000210#if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
Benoît Thébaudeau2dc0aa02013-04-11 09:35:36 +0000211 writenfc(0, &nfc->buf_addr);
Benoît Thébaudeau35537bc2013-04-11 09:35:37 +0000212#elif defined(MXC_NFC_V3_2)
213 int config1 = readnfc(&nfc->config1);
214 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
215 writenfc(config1, &nfc->config1);
216#endif
Magnus Lilja40c642b2009-06-13 20:50:01 +0200217 nfc_nand_command(NAND_CMD_READ0);
218 nfc_nand_page_address(page_address);
219
John Rigbyf3bb63a2010-01-26 19:24:17 -0700220 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
Magnus Lilja40c642b2009-06-13 20:50:01 +0200221 nfc_nand_command(NAND_CMD_READSTART);
222
223 nfc_nand_data_output(); /* fill the main buffer 0 */
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200224}
225
226static int nfc_read_page(unsigned int page_address, unsigned char *buf)
227{
228 int i;
229 u32 *src;
230 u32 *dst;
231
232 nfc_nand_read_page(page_address);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200233
234 if (nfc_nand_check_ecc())
235 return -1;
236
Benoît Thébaudeau80c8ab72012-08-13 22:48:12 +0200237 src = (u32 *)&nfc->main_area[0][0];
Magnus Lilja40c642b2009-06-13 20:50:01 +0200238 dst = (u32 *)buf;
239
240 /* main copy loop from NAND-buffer to SDRAM memory */
Benoît Thébaudeau365b2c02012-08-13 22:48:26 +0200241 for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
Magnus Lilja40c642b2009-06-13 20:50:01 +0200242 writel(readl(src), dst);
243 src++;
244 dst++;
245 }
246
247 return 0;
248}
249
250static int is_badblock(int pagenumber)
251{
252 int page = pagenumber;
253 u32 badblock;
254 u32 *src;
255
256 /* Check the first two pages for bad block markers */
257 for (page = pagenumber; page < pagenumber + 2; page++) {
Benoît Thébaudeau5d818a22012-08-13 22:48:56 +0200258 nfc_nand_read_page(page);
Magnus Lilja40c642b2009-06-13 20:50:01 +0200259
Benoît Thébaudeau80c8ab72012-08-13 22:48:12 +0200260 src = (u32 *)&nfc->spare_area[0][0];
Magnus Lilja40c642b2009-06-13 20:50:01 +0200261
262 /*
263 * IMPORTANT NOTE: The nand flash controller uses a non-
264 * standard layout for large page devices. This can
265 * affect the position of the bad block marker.
266 */
267 /* Get the bad block marker */
268 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
269 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
270 badblock &= 0xff;
271
272 /* bad block marker verify */
273 if (badblock != 0xff)
274 return 1; /* potential bad block */
275 }
276
277 return 0;
278}
279
Marek Vasut5c651e82013-04-21 05:52:23 +0000280int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
Magnus Lilja40c642b2009-06-13 20:50:01 +0200281{
282 int i;
283 unsigned int page;
284 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
285 CONFIG_SYS_NAND_PAGE_SIZE;
286
Magnus Lilja40c642b2009-06-13 20:50:01 +0200287 nfc_nand_init();
288
289 /* Convert to page number */
290 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
291 i = 0;
292
Marek Vasut5c651e82013-04-21 05:52:23 +0000293 size = roundup(size, CONFIG_SYS_NAND_PAGE_SIZE);
Benoît Thébaudeau365b2c02012-08-13 22:48:26 +0200294 while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
Magnus Lilja40c642b2009-06-13 20:50:01 +0200295 if (nfc_read_page(page, buf) < 0)
296 return -1;
297
298 page++;
299 i++;
300 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
301
302 /*
303 * Check if we have crossed a block boundary, and if so
304 * check for bad block.
305 */
306 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
307 /*
308 * Yes, new block. See if this block is good. If not,
John Rigbyf3bb63a2010-01-26 19:24:17 -0700309 * loop until we find a good block.
Magnus Lilja40c642b2009-06-13 20:50:01 +0200310 */
311 while (is_badblock(page)) {
312 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
313 /* Check i we've reached the end of flash. */
314 if (page >= maxpages)
315 return -1;
316 }
317 }
318 }
319
320 return 0;
321}
322
Marek Vasut5c651e82013-04-21 05:52:23 +0000323#ifndef CONFIG_SPL_FRAMEWORK
Magnus Lilja40c642b2009-06-13 20:50:01 +0200324/*
325 * The main entry for NAND booting. It's necessary that SDRAM is already
326 * configured and available since this code loads the main U-Boot image
327 * from NAND into SDRAM and starts it from there.
328 */
329void nand_boot(void)
330{
331 __attribute__((noreturn)) void (*uboot)(void);
332
Magnus Lilja40c642b2009-06-13 20:50:01 +0200333 /*
334 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
335 * be aligned to full pages
336 */
Marek Vasut5c651e82013-04-21 05:52:23 +0000337 if (!nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS,
338 CONFIG_SYS_NAND_U_BOOT_SIZE,
339 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
Magnus Lilja40c642b2009-06-13 20:50:01 +0200340 /* Copy from NAND successful, start U-boot */
341 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
342 uboot();
343 } else {
344 /* Unrecoverable error when copying from NAND */
345 hang();
346 }
347}
Marek Vasut5c651e82013-04-21 05:52:23 +0000348#endif
Albert ARIBAUDc1b43ac2013-05-11 04:29:50 +0000349
350void nand_init(void) {}
351void nand_deselect(void) {}