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 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 11 | * SPDX-License-Identifier: GPL-2.0+ |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 12 | */ |
| 13 | |
| 14 | #include <common.h> |
| 15 | #include <nand.h> |
Peter Tyser | 61f2b38 | 2010-04-12 22:28:07 -0500 | [diff] [blame] | 16 | #include <asm/arch/imx-regs.h> |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 17 | #include <asm/io.h> |
Benoît Thébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 18 | #include "mxc_nand.h" |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 19 | |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 20 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 21 | static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR; |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 22 | #elif defined(MXC_NFC_V3_2) |
Benoît Thébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 23 | static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR_AXI; |
| 24 | static struct mxc_nand_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR; |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 25 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 26 | |
| 27 | static void nfc_wait_ready(void) |
| 28 | { |
| 29 | uint32_t tmp; |
| 30 | |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 31 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 32 | while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT)) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 33 | ; |
| 34 | |
| 35 | /* Reset interrupt flag */ |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 36 | tmp = readnfc(&nfc->config2); |
| 37 | tmp &= ~NFC_V1_V2_CONFIG2_INT; |
| 38 | writenfc(tmp, &nfc->config2); |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 39 | #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 Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 48 | } |
| 49 | |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 50 | static void nfc_nand_init(void) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 51 | { |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 52 | #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ébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 58 | NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_OOBSIZE / 2) | |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 59 | 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ébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 71 | if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16) |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 72 | 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ébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 89 | int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512; |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 90 | int config1; |
| 91 | |
Benoît Thébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 92 | writenfc(CONFIG_SYS_NAND_OOBSIZE / 2, &nfc->spare_area_size); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 93 | |
| 94 | /* unlocking RAM Buff */ |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 95 | writenfc(0x2, &nfc->config); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 96 | |
| 97 | /* hardware ECC checking and correct */ |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 98 | 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 Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 101 | /* |
| 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ébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 105 | if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16) |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 106 | config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4; |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 107 | else |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 108 | config1 |= NFC_V2_CONFIG1_ECC_MODE_4; |
| 109 | writenfc(config1, &nfc->config1); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 110 | #elif defined(MXC_NFC_V1) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 111 | /* unlocking RAM Buff */ |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 112 | writenfc(0x2, &nfc->config); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 113 | |
| 114 | /* hardware ECC checking and correct */ |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 115 | writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK, |
| 116 | &nfc->config1); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 117 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void nfc_nand_command(unsigned short command) |
| 121 | { |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 122 | writenfc(command, &nfc->flash_cmd); |
| 123 | writenfc(NFC_CMD, &nfc->operation); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 124 | nfc_wait_ready(); |
| 125 | } |
| 126 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 127 | static void nfc_nand_address(unsigned short address) |
| 128 | { |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 129 | writenfc(address, &nfc->flash_addr); |
| 130 | writenfc(NFC_ADDR, &nfc->operation); |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 131 | nfc_wait_ready(); |
| 132 | } |
| 133 | |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 134 | static void nfc_nand_page_address(unsigned int page_address) |
| 135 | { |
| 136 | unsigned int page_count; |
| 137 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 138 | nfc_nand_address(0x00); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 139 | |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 140 | /* code only for large page flash */ |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 141 | if (CONFIG_SYS_NAND_PAGE_SIZE > 512) |
| 142 | nfc_nand_address(0x00); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 143 | |
| 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ébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 149 | nfc_nand_address(page_address & 0xff); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 150 | page_address = page_address >> 8; |
| 151 | page_count = page_count >> 8; |
| 152 | } while (page_count); |
| 153 | } |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 154 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 155 | nfc_nand_address(0x00); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | static void nfc_nand_data_output(void) |
| 159 | { |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 160 | #ifdef NAND_MXC_2K_MULTI_CYCLE |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 161 | int i; |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 162 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 163 | |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 164 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 165 | writenfc(0, &nfc->buf_addr); |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 166 | #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ébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 171 | writenfc(NFC_OUTPUT, &nfc->operation); |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 172 | nfc_wait_ready(); |
| 173 | #ifdef NAND_MXC_2K_MULTI_CYCLE |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 174 | /* |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 175 | * This NAND controller requires multiple input commands |
| 176 | * for pages larger than 512 bytes. |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 177 | */ |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 178 | for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) { |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 179 | writenfc(i, &nfc->buf_addr); |
| 180 | writenfc(NFC_OUTPUT, &nfc->operation); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 181 | nfc_wait_ready(); |
| 182 | } |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 183 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static int nfc_nand_check_ecc(void) |
| 187 | { |
Benoît Thébaudeau | c1db8dd | 2012-08-13 22:49:42 +0200 | [diff] [blame] | 188 | #if defined(MXC_NFC_V1) |
Benoît Thébaudeau | b8fea2b | 2012-08-13 22:49:53 +0200 | [diff] [blame] | 189 | u16 ecc_status = readw(&nfc->ecc_status_result); |
| 190 | return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2; |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 191 | #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2) |
Benoît Thébaudeau | b8fea2b | 2012-08-13 22:49:53 +0200 | [diff] [blame] | 192 | u32 ecc_status = readl(&nfc->ecc_status_result); |
| 193 | int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512; |
Benoît Thébaudeau | da962b7 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 194 | int err_limit = CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16 ? 8 : 4; |
Benoît Thébaudeau | b8fea2b | 2012-08-13 22:49:53 +0200 | [diff] [blame] | 195 | 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ébaudeau | c1db8dd | 2012-08-13 22:49:42 +0200 | [diff] [blame] | 204 | #endif |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 205 | } |
| 206 | |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 207 | static void nfc_nand_read_page(unsigned int page_address) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 208 | { |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 209 | /* read in first 0 buffer */ |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 210 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 2dc0aa0 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 211 | writenfc(0, &nfc->buf_addr); |
Benoît Thébaudeau | 35537bc | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 212 | #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 Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 217 | nfc_nand_command(NAND_CMD_READ0); |
| 218 | nfc_nand_page_address(page_address); |
| 219 | |
John Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 220 | if (CONFIG_SYS_NAND_PAGE_SIZE > 512) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 221 | nfc_nand_command(NAND_CMD_READSTART); |
| 222 | |
| 223 | nfc_nand_data_output(); /* fill the main buffer 0 */ |
Benoît Thébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | static 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 Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 233 | |
| 234 | if (nfc_nand_check_ecc()) |
| 235 | return -1; |
| 236 | |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 237 | src = (u32 *)&nfc->main_area[0][0]; |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 238 | dst = (u32 *)buf; |
| 239 | |
| 240 | /* main copy loop from NAND-buffer to SDRAM memory */ |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 241 | for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) { |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 242 | writel(readl(src), dst); |
| 243 | src++; |
| 244 | dst++; |
| 245 | } |
| 246 | |
| 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | static 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ébaudeau | 5d818a2 | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 258 | nfc_nand_read_page(page); |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 259 | |
Benoît Thébaudeau | 80c8ab7 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 260 | src = (u32 *)&nfc->spare_area[0][0]; |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 261 | |
| 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 Vasut | 5c651e8 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 280 | int nand_spl_load_image(uint32_t from, unsigned int size, void *buf) |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 281 | { |
| 282 | int i; |
| 283 | unsigned int page; |
| 284 | unsigned int maxpages = CONFIG_SYS_NAND_SIZE / |
| 285 | CONFIG_SYS_NAND_PAGE_SIZE; |
| 286 | |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 287 | nfc_nand_init(); |
| 288 | |
| 289 | /* Convert to page number */ |
| 290 | page = from / CONFIG_SYS_NAND_PAGE_SIZE; |
| 291 | i = 0; |
| 292 | |
Marek Vasut | 5c651e8 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 293 | size = roundup(size, CONFIG_SYS_NAND_PAGE_SIZE); |
Benoît Thébaudeau | 365b2c0 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 294 | while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) { |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 295 | 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 Rigby | f3bb63a | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 309 | * loop until we find a good block. |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 310 | */ |
| 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 Vasut | 5c651e8 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 323 | #ifndef CONFIG_SPL_FRAMEWORK |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 324 | /* |
| 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 | */ |
| 329 | void nand_boot(void) |
| 330 | { |
| 331 | __attribute__((noreturn)) void (*uboot)(void); |
| 332 | |
Magnus Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 333 | /* |
| 334 | * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must |
| 335 | * be aligned to full pages |
| 336 | */ |
Marek Vasut | 5c651e8 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 337 | 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 Lilja | 40c642b | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 340 | /* 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 Vasut | 5c651e8 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 348 | #endif |
Albert ARIBAUD | c1b43ac | 2013-05-11 04:29:50 +0000 | [diff] [blame] | 349 | |
| 350 | void nand_init(void) {} |
| 351 | void nand_deselect(void) {} |