1 // SPDX-License-Identifier: GPL-2.0+
4 * Magnus Lilja <lilja.magnus@gmail.com>
7 * Maxim Artamonov, <scn1874 at yandex.ru>
9 * (C) Copyright 2006-2008
10 * Stefan Roese, DENX Software Engineering, sr at denx.de.
15 #include <asm/arch/imx-regs.h>
19 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
20 static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR;
21 #elif defined(MXC_NFC_V3_2)
22 static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR_AXI;
23 static struct mxc_nand_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR;
26 static void nfc_wait_ready(void)
30 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
31 while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT))
34 /* Reset interrupt flag */
35 tmp = readnfc(&nfc->config2);
36 tmp &= ~NFC_V1_V2_CONFIG2_INT;
37 writenfc(tmp, &nfc->config2);
38 #elif defined(MXC_NFC_V3_2)
39 while (!(readnfc(&nfc_ip->ipc) & NFC_V3_IPC_INT))
42 /* Reset interrupt flag */
43 tmp = readnfc(&nfc_ip->ipc);
44 tmp &= ~NFC_V3_IPC_INT;
45 writenfc(tmp, &nfc_ip->ipc);
49 static void nfc_nand_init(void)
51 #if defined(MXC_NFC_V3_2)
52 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
55 tmp = (readnfc(&nfc_ip->config2) & ~(NFC_V3_CONFIG2_SPAS_MASK |
56 NFC_V3_CONFIG2_EDC_MASK | NFC_V3_CONFIG2_PS_MASK)) |
57 NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_OOBSIZE / 2) |
58 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;
67 * if spare size is larger that 16 bytes per 512 byte hunk
68 * then use 8 symbol correction instead of 4
70 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
71 tmp |= NFC_V3_CONFIG2_ECC_MODE_8;
73 tmp &= ~NFC_V3_CONFIG2_ECC_MODE_8;
74 writenfc(tmp, &nfc_ip->config2);
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;
84 writenfc(tmp, &nfc_ip->config3);
86 writenfc(0, &nfc_ip->delay_line);
87 #elif defined(MXC_NFC_V2_1)
88 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
91 writenfc(CONFIG_SYS_NAND_OOBSIZE / 2, &nfc->spare_area_size);
93 /* unlocking RAM Buff */
94 writenfc(0x2, &nfc->config);
96 /* hardware ECC checking and correct */
97 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;
101 * if spare size is larger that 16 bytes per 512 byte hunk
102 * then use 8 symbol correction instead of 4
104 if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16)
105 config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4;
107 config1 |= NFC_V2_CONFIG1_ECC_MODE_4;
108 writenfc(config1, &nfc->config1);
109 #elif defined(MXC_NFC_V1)
110 /* unlocking RAM Buff */
111 writenfc(0x2, &nfc->config);
113 /* hardware ECC checking and correct */
114 writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK,
119 static void nfc_nand_command(unsigned short command)
121 writenfc(command, &nfc->flash_cmd);
122 writenfc(NFC_CMD, &nfc->operation);
126 static void nfc_nand_address(unsigned short address)
128 writenfc(address, &nfc->flash_addr);
129 writenfc(NFC_ADDR, &nfc->operation);
133 static void nfc_nand_page_address(unsigned int page_address)
135 unsigned int page_count;
137 nfc_nand_address(0x00);
139 /* code only for large page flash */
140 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
141 nfc_nand_address(0x00);
143 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
145 if (page_address <= page_count) {
146 page_count--; /* transform 0x01000000 to 0x00ffffff */
148 nfc_nand_address(page_address & 0xff);
149 page_address = page_address >> 8;
150 page_count = page_count >> 8;
151 } while (page_count);
154 nfc_nand_address(0x00);
157 static void nfc_nand_data_output(void)
159 #ifdef NAND_MXC_2K_MULTI_CYCLE
163 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
164 writenfc(0, &nfc->buf_addr);
165 #elif defined(MXC_NFC_V3_2)
166 int config1 = readnfc(&nfc->config1);
167 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
168 writenfc(config1, &nfc->config1);
170 writenfc(NFC_OUTPUT, &nfc->operation);
172 #ifdef NAND_MXC_2K_MULTI_CYCLE
174 * This NAND controller requires multiple input commands
175 * for pages larger than 512 bytes.
177 for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) {
178 writenfc(i, &nfc->buf_addr);
179 writenfc(NFC_OUTPUT, &nfc->operation);
185 static int nfc_nand_check_ecc(void)
187 #if defined(MXC_NFC_V1)
188 u16 ecc_status = readw(&nfc->ecc_status_result);
189 return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2;
190 #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2)
191 u32 ecc_status = readl(&nfc->ecc_status_result);
192 int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512;
193 int err_limit = CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16 ? 8 : 4;
194 int subpages = CONFIG_SYS_NAND_PAGE_SIZE / 512;
197 if ((ecc_status & 0xf) > err_limit)
200 } while (--subpages);
206 static void nfc_nand_read_page(unsigned int page_address)
208 /* read in first 0 buffer */
209 #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1)
210 writenfc(0, &nfc->buf_addr);
211 #elif defined(MXC_NFC_V3_2)
212 int config1 = readnfc(&nfc->config1);
213 config1 &= ~NFC_V3_CONFIG1_RBA_MASK;
214 writenfc(config1, &nfc->config1);
216 nfc_nand_command(NAND_CMD_READ0);
217 nfc_nand_page_address(page_address);
219 if (CONFIG_SYS_NAND_PAGE_SIZE > 512)
220 nfc_nand_command(NAND_CMD_READSTART);
222 nfc_nand_data_output(); /* fill the main buffer 0 */
225 static int nfc_read_page(unsigned int page_address, unsigned char *buf)
231 nfc_nand_read_page(page_address);
233 if (nfc_nand_check_ecc())
236 src = (u32 *)&nfc->main_area[0][0];
239 /* main copy loop from NAND-buffer to SDRAM memory */
240 for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) {
241 writel(readl(src), dst);
249 static int is_badblock(int pagenumber)
251 int page = pagenumber;
255 /* Check the first two pages for bad block markers */
256 for (page = pagenumber; page < pagenumber + 2; page++) {
257 nfc_nand_read_page(page);
259 src = (u32 *)&nfc->spare_area[0][0];
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.
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);
271 /* bad block marker verify */
272 if (badblock != 0xff)
273 return 1; /* potential bad block */
279 int nand_spl_load_image(uint32_t from, unsigned int size, void *buf)
283 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
284 CONFIG_SYS_NAND_PAGE_SIZE;
288 /* Convert to page number */
289 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
292 size = roundup(size, CONFIG_SYS_NAND_PAGE_SIZE);
293 while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) {
294 if (nfc_read_page(page, buf) < 0)
299 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
302 * Check if we have crossed a block boundary, and if so
303 * check for bad block.
305 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
307 * Yes, new block. See if this block is good. If not,
308 * loop until we find a good block.
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)
322 #ifndef CONFIG_SPL_FRAMEWORK
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.
330 __attribute__((noreturn)) void (*uboot)(void);
333 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
334 * be aligned to full pages
336 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)) {
339 /* Copy from NAND successful, start U-Boot */
340 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
343 /* Unrecoverable error when copying from NAND */
349 void nand_init(void) {}
350 void nand_deselect(void) {}