3 * Magnus Lilja <lilja.magnus@gmail.com>
6 * Maxim Artamonov, <scn1874 at yandex.ru>
8 * (C) Copyright 2006-2008
9 * Stefan Roese, DENX Software Engineering, sr at denx.de.
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.
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.
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,
29 #include <asm-arm/arch/mx31-regs.h>
33 static struct fsl_nfc_regs *nfc;
35 static void nfc_wait_ready(void)
39 while (!(readw(&nfc->nand_flash_config2) & NFC_INT))
42 /* Reset interrupt flag */
43 tmp = readw(&nfc->nand_flash_config2);
45 writew(tmp, &nfc->nand_flash_config2);
48 static void nfc_nand_init(void)
50 /* unlocking RAM Buff */
51 writew(0x2, &nfc->configuration);
53 /* hardware ECC checking and correct */
54 writew(NFC_ECC_EN, &nfc->nand_flash_config1);
57 static void nfc_nand_command(unsigned short command)
59 writew(command, &nfc->flash_cmd);
60 writew(NFC_CMD, &nfc->nand_flash_config2);
64 static void nfc_nand_page_address(unsigned int page_address)
66 unsigned int page_count;
68 writew(0x00, &nfc->flash_cmd);
69 writew(NFC_ADDR, &nfc->nand_flash_config2);
72 /* code only for 2kb flash */
73 if (CONFIG_SYS_NAND_PAGE_SIZE == 0x800) {
74 writew(0x00, &nfc->flash_add);
75 writew(NFC_ADDR, &nfc->nand_flash_config2);
79 page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE;
81 if (page_address <= page_count) {
82 page_count--; /* transform 0x01000000 to 0x00ffffff */
84 writew(page_address & 0xff, &nfc->flash_add);
85 writew(NFC_ADDR, &nfc->nand_flash_config2);
87 page_address = page_address >> 8;
88 page_count = page_count >> 8;
93 static void nfc_nand_data_output(void)
98 * The NAND controller requires four output commands for
101 for (i = 0; i < (CONFIG_SYS_NAND_PAGE_SIZE / 512); i++) {
102 writew(NFC_ECC_EN, &nfc->nand_flash_config1);
103 writew(i, &nfc->buffer_address); /* read in i:th buffer */
104 writew(NFC_OUTPUT, &nfc->nand_flash_config2);
109 static int nfc_nand_check_ecc(void)
111 return readw(&nfc->ecc_status_result);
114 static int nfc_read_page(unsigned int page_address, unsigned char *buf)
120 writew(0, &nfc->buffer_address); /* read in first 0 buffer */
121 nfc_nand_command(NAND_CMD_READ0);
122 nfc_nand_page_address(page_address);
124 if (CONFIG_SYS_NAND_PAGE_SIZE == 0x800)
125 nfc_nand_command(NAND_CMD_READSTART);
127 nfc_nand_data_output(); /* fill the main buffer 0 */
129 if (nfc_nand_check_ecc())
132 src = &nfc->main_area0[0];
135 /* main copy loop from NAND-buffer to SDRAM memory */
136 for (i = 0; i < (CONFIG_SYS_NAND_PAGE_SIZE / 4); i++) {
137 writel(readl(src), dst);
145 static int is_badblock(int pagenumber)
147 int page = pagenumber;
151 /* Check the first two pages for bad block markers */
152 for (page = pagenumber; page < pagenumber + 2; page++) {
153 writew(0, &nfc->buffer_address); /* read in first 0 buffer */
154 nfc_nand_command(NAND_CMD_READ0);
155 nfc_nand_page_address(page);
157 if (CONFIG_SYS_NAND_PAGE_SIZE == 0x800)
158 nfc_nand_command(NAND_CMD_READSTART);
160 nfc_nand_data_output(); /* fill the main buffer 0 */
162 src = &nfc->spare_area0[0];
165 * IMPORTANT NOTE: The nand flash controller uses a non-
166 * standard layout for large page devices. This can
167 * affect the position of the bad block marker.
169 /* Get the bad block marker */
170 badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]);
171 badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4);
174 /* bad block marker verify */
175 if (badblock != 0xff)
176 return 1; /* potential bad block */
182 static int nand_load(unsigned int from, unsigned int size, unsigned char *buf)
186 unsigned int maxpages = CONFIG_SYS_NAND_SIZE /
187 CONFIG_SYS_NAND_PAGE_SIZE;
189 nfc = (void *)NFC_BASE_ADDR;
193 /* Convert to page number */
194 page = from / CONFIG_SYS_NAND_PAGE_SIZE;
197 while (i < (size / CONFIG_SYS_NAND_PAGE_SIZE)) {
198 if (nfc_read_page(page, buf) < 0)
203 buf = buf + CONFIG_SYS_NAND_PAGE_SIZE;
206 * Check if we have crossed a block boundary, and if so
207 * check for bad block.
209 if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) {
211 * Yes, new block. See if this block is good. If not,
212 * loop until we find i good block.
214 while (is_badblock(page)) {
215 page = page + CONFIG_SYS_NAND_PAGE_COUNT;
216 /* Check i we've reached the end of flash. */
217 if (page >= maxpages)
227 * The main entry for NAND booting. It's necessary that SDRAM is already
228 * configured and available since this code loads the main U-Boot image
229 * from NAND into SDRAM and starts it from there.
233 __attribute__((noreturn)) void (*uboot)(void);
235 nfc = (void *)NFC_BASE_ADDR;
238 * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must
239 * be aligned to full pages
241 if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE,
242 (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) {
243 /* Copy from NAND successful, start U-boot */
244 uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START;
247 /* Unrecoverable error when copying from NAND */
253 * Called in case of an exception.