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