Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 1 | /* |
| 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 | * |
| 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. |
| 15 | * |
| 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. |
| 20 | * |
| 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, |
| 24 | * MA 02111-1307 USA |
| 25 | */ |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <nand.h> |
Peter Tyser | 61f2b38 | 2010-04-12 22:28:07 -0500 | [diff] [blame] | 29 | #include <asm/arch/imx-regs.h> |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 30 | #include <asm/io.h> |
| 31 | #include <fsl_nfc.h> |
| 32 | |
Heiko Schocher | 4fff329 | 2010-09-17 13:10:38 +0200 | [diff] [blame] | 33 | static struct fsl_nfc_regs *const nfc = (void *)NFC_BASE_ADDR; |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 34 | |
| 35 | static void nfc_wait_ready(void) |
| 36 | { |
| 37 | uint32_t tmp; |
| 38 | |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 39 | while (!(readw(&nfc->config2) & NFC_INT)) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 40 | ; |
| 41 | |
| 42 | /* Reset interrupt flag */ |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 43 | tmp = readw(&nfc->config2); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 44 | tmp &= ~NFC_INT; |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 45 | writew(tmp, &nfc->config2); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 46 | } |
| 47 | |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 48 | static void nfc_nand_init(void) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 49 | { |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 50 | #if defined(MXC_NFC_V1_1) |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 51 | int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512; |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 52 | int config1; |
| 53 | |
| 54 | writew(CONFIG_SYS_NAND_SPARE_SIZE / 2, &nfc->spare_area_size); |
| 55 | |
| 56 | /* unlocking RAM Buff */ |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 57 | writew(0x2, &nfc->config); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 58 | |
| 59 | /* hardware ECC checking and correct */ |
Benoît Thébaudeau | 0e55ad7 | 2012-08-13 22:49:31 +0200 | [diff] [blame] | 60 | config1 = readw(&nfc->config1) | NFC_ECC_EN | NFC_INT_MSK | |
| 61 | NFC_ONE_CYCLE | NFC_FP_INT; |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 62 | /* |
| 63 | * if spare size is larger that 16 bytes per 512 byte hunk |
| 64 | * then use 8 symbol correction instead of 4 |
| 65 | */ |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 66 | if (CONFIG_SYS_NAND_SPARE_SIZE / ecc_per_page > 16) |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 67 | config1 &= ~NFC_4_8N_ECC; |
| 68 | else |
| 69 | config1 |= NFC_4_8N_ECC; |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 70 | writew(config1, &nfc->config1); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 71 | #elif defined(MXC_NFC_V1) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 72 | /* unlocking RAM Buff */ |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 73 | writew(0x2, &nfc->config); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 74 | |
| 75 | /* hardware ECC checking and correct */ |
Benoît Thébaudeau | 0eee20f | 2012-08-13 22:49:15 +0200 | [diff] [blame] | 76 | writew(NFC_ECC_EN | NFC_INT_MSK, &nfc->config1); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 77 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | static void nfc_nand_command(unsigned short command) |
| 81 | { |
| 82 | writew(command, &nfc->flash_cmd); |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 83 | writew(NFC_CMD, &nfc->config2); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 84 | nfc_wait_ready(); |
| 85 | } |
| 86 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 87 | static void nfc_nand_address(unsigned short address) |
| 88 | { |
| 89 | writew(address, &nfc->flash_addr); |
| 90 | writew(NFC_ADDR, &nfc->config2); |
| 91 | nfc_wait_ready(); |
| 92 | } |
| 93 | |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 94 | static void nfc_nand_page_address(unsigned int page_address) |
| 95 | { |
| 96 | unsigned int page_count; |
| 97 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 98 | nfc_nand_address(0x00); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 99 | |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 100 | /* code only for large page flash */ |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 101 | if (CONFIG_SYS_NAND_PAGE_SIZE > 512) |
| 102 | nfc_nand_address(0x00); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 103 | |
| 104 | page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE; |
| 105 | |
| 106 | if (page_address <= page_count) { |
| 107 | page_count--; /* transform 0x01000000 to 0x00ffffff */ |
| 108 | do { |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 109 | nfc_nand_address(page_address & 0xff); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 110 | page_address = page_address >> 8; |
| 111 | page_count = page_count >> 8; |
| 112 | } while (page_count); |
| 113 | } |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 114 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 115 | nfc_nand_address(0x00); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void nfc_nand_data_output(void) |
| 119 | { |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 120 | #ifdef NAND_MXC_2K_MULTI_CYCLE |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 121 | int i; |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 122 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 123 | |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 124 | writew(0, &nfc->buf_addr); |
| 125 | writew(NFC_OUTPUT, &nfc->config2); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 126 | nfc_wait_ready(); |
| 127 | #ifdef NAND_MXC_2K_MULTI_CYCLE |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 128 | /* |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 129 | * This NAND controller requires multiple input commands |
| 130 | * for pages larger than 512 bytes. |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 131 | */ |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 132 | for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) { |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 133 | writew(i, &nfc->buf_addr); |
| 134 | writew(NFC_OUTPUT, &nfc->config2); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 135 | nfc_wait_ready(); |
| 136 | } |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 137 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | static int nfc_nand_check_ecc(void) |
| 141 | { |
Benoît Thébaudeau | c1db8dd | 2012-08-13 22:49:42 +0200 | [diff] [blame^] | 142 | #if defined(MXC_NFC_V1) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 143 | return readw(&nfc->ecc_status_result); |
Benoît Thébaudeau | c1db8dd | 2012-08-13 22:49:42 +0200 | [diff] [blame^] | 144 | #elif defined(MXC_NFC_V1_1) |
| 145 | return readl(&nfc->ecc_status_result); |
| 146 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 147 | } |
| 148 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 149 | static void nfc_nand_read_page(unsigned int page_address) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 150 | { |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 151 | writew(0, &nfc->buf_addr); /* read in first 0 buffer */ |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 152 | nfc_nand_command(NAND_CMD_READ0); |
| 153 | nfc_nand_page_address(page_address); |
| 154 | |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 155 | if (CONFIG_SYS_NAND_PAGE_SIZE > 512) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 156 | nfc_nand_command(NAND_CMD_READSTART); |
| 157 | |
| 158 | nfc_nand_data_output(); /* fill the main buffer 0 */ |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | static int nfc_read_page(unsigned int page_address, unsigned char *buf) |
| 162 | { |
| 163 | int i; |
| 164 | u32 *src; |
| 165 | u32 *dst; |
| 166 | |
| 167 | nfc_nand_read_page(page_address); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 168 | |
| 169 | if (nfc_nand_check_ecc()) |
| 170 | return -1; |
| 171 | |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 172 | src = (u32 *)&nfc->main_area[0][0]; |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 173 | dst = (u32 *)buf; |
| 174 | |
| 175 | /* main copy loop from NAND-buffer to SDRAM memory */ |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 176 | for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) { |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 177 | writel(readl(src), dst); |
| 178 | src++; |
| 179 | dst++; |
| 180 | } |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
| 185 | static int is_badblock(int pagenumber) |
| 186 | { |
| 187 | int page = pagenumber; |
| 188 | u32 badblock; |
| 189 | u32 *src; |
| 190 | |
| 191 | /* Check the first two pages for bad block markers */ |
| 192 | for (page = pagenumber; page < pagenumber + 2; page++) { |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 193 | nfc_nand_read_page(page); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 194 | |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 195 | src = (u32 *)&nfc->spare_area[0][0]; |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 196 | |
| 197 | /* |
| 198 | * IMPORTANT NOTE: The nand flash controller uses a non- |
| 199 | * standard layout for large page devices. This can |
| 200 | * affect the position of the bad block marker. |
| 201 | */ |
| 202 | /* Get the bad block marker */ |
| 203 | badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]); |
| 204 | badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4); |
| 205 | badblock &= 0xff; |
| 206 | |
| 207 | /* bad block marker verify */ |
| 208 | if (badblock != 0xff) |
| 209 | return 1; /* potential bad block */ |
| 210 | } |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | static int nand_load(unsigned int from, unsigned int size, unsigned char *buf) |
| 216 | { |
| 217 | int i; |
| 218 | unsigned int page; |
| 219 | unsigned int maxpages = CONFIG_SYS_NAND_SIZE / |
| 220 | CONFIG_SYS_NAND_PAGE_SIZE; |
| 221 | |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 222 | nfc_nand_init(); |
| 223 | |
| 224 | /* Convert to page number */ |
| 225 | page = from / CONFIG_SYS_NAND_PAGE_SIZE; |
| 226 | i = 0; |
| 227 | |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 228 | while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) { |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 229 | if (nfc_read_page(page, buf) < 0) |
| 230 | return -1; |
| 231 | |
| 232 | page++; |
| 233 | i++; |
| 234 | buf = buf + CONFIG_SYS_NAND_PAGE_SIZE; |
| 235 | |
| 236 | /* |
| 237 | * Check if we have crossed a block boundary, and if so |
| 238 | * check for bad block. |
| 239 | */ |
| 240 | if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) { |
| 241 | /* |
| 242 | * Yes, new block. See if this block is good. If not, |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 243 | * loop until we find a good block. |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 244 | */ |
| 245 | while (is_badblock(page)) { |
| 246 | page = page + CONFIG_SYS_NAND_PAGE_COUNT; |
| 247 | /* Check i we've reached the end of flash. */ |
| 248 | if (page >= maxpages) |
| 249 | return -1; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return 0; |
| 255 | } |
| 256 | |
Wolfgang Denk | a9aa392 | 2010-10-28 20:35:36 +0200 | [diff] [blame] | 257 | #if defined(CONFIG_ARM) |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 258 | void board_init_f (ulong bootflag) |
| 259 | { |
Wolfgang Denk | c8d76ea | 2010-10-18 23:43:37 +0200 | [diff] [blame] | 260 | relocate_code (CONFIG_SYS_TEXT_BASE - TOTAL_MALLOC_LEN, NULL, |
| 261 | CONFIG_SYS_TEXT_BASE); |
Heiko Schocher | f1d2b31 | 2010-09-17 13:10:39 +0200 | [diff] [blame] | 262 | } |
| 263 | #endif |
| 264 | |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 265 | /* |
| 266 | * The main entry for NAND booting. It's necessary that SDRAM is already |
| 267 | * configured and available since this code loads the main U-Boot image |
| 268 | * from NAND into SDRAM and starts it from there. |
| 269 | */ |
| 270 | void nand_boot(void) |
| 271 | { |
| 272 | __attribute__((noreturn)) void (*uboot)(void); |
| 273 | |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 274 | /* |
| 275 | * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must |
| 276 | * be aligned to full pages |
| 277 | */ |
| 278 | if (!nand_load(CONFIG_SYS_NAND_U_BOOT_OFFS, CONFIG_SYS_NAND_U_BOOT_SIZE, |
| 279 | (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) { |
| 280 | /* Copy from NAND successful, start U-boot */ |
| 281 | uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START; |
| 282 | uboot(); |
| 283 | } else { |
| 284 | /* Unrecoverable error when copying from NAND */ |
| 285 | hang(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Called in case of an exception. |
| 291 | */ |
| 292 | void hang(void) |
| 293 | { |
| 294 | /* Loop forever */ |
| 295 | while (1) ; |
| 296 | } |