Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 |
| 4 | * Magnus Lilja <lilja.magnus@gmail.com> |
| 5 | * |
| 6 | * (C) Copyright 2008 |
| 7 | * Maxim Artamonov, <scn1874 at yandex.ru> |
| 8 | * |
| 9 | * (C) Copyright 2006-2008 |
| 10 | * Stefan Roese, DENX Software Engineering, sr at denx.de. |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <common.h> |
Simon Glass | f11478f | 2019-12-28 10:45:07 -0700 | [diff] [blame] | 14 | #include <hang.h> |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 15 | #include <nand.h> |
Tom Rini | 3bde7e2 | 2021-09-22 14:50:35 -0400 | [diff] [blame] | 16 | #include <linux/mtd/rawnand.h> |
Peter Tyser | 133c0fe | 2010-04-12 22:28:07 -0500 | [diff] [blame] | 17 | #include <asm/arch/imx-regs.h> |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 18 | #include <asm/io.h> |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 19 | #include "mxc_nand.h" |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 20 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 21 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 22 | static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 23 | #elif defined(MXC_NFC_V3_2) |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 24 | static struct mxc_nand_regs *const nfc = (void *)NFC_BASE_ADDR_AXI; |
| 25 | static struct mxc_nand_ip_regs *const nfc_ip = (void *)NFC_BASE_ADDR; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 26 | #endif |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 27 | |
| 28 | static void nfc_wait_ready(void) |
| 29 | { |
| 30 | uint32_t tmp; |
| 31 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 32 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 33 | while (!(readnfc(&nfc->config2) & NFC_V1_V2_CONFIG2_INT)) |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 34 | ; |
| 35 | |
| 36 | /* Reset interrupt flag */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 37 | tmp = readnfc(&nfc->config2); |
| 38 | tmp &= ~NFC_V1_V2_CONFIG2_INT; |
| 39 | writenfc(tmp, &nfc->config2); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 40 | #elif defined(MXC_NFC_V3_2) |
| 41 | while (!(readnfc(&nfc_ip->ipc) & NFC_V3_IPC_INT)) |
| 42 | ; |
| 43 | |
| 44 | /* Reset interrupt flag */ |
| 45 | tmp = readnfc(&nfc_ip->ipc); |
| 46 | tmp &= ~NFC_V3_IPC_INT; |
| 47 | writenfc(tmp, &nfc_ip->ipc); |
| 48 | #endif |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 49 | } |
| 50 | |
Benoît Thébaudeau | 32ae5b4 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 51 | static void nfc_nand_init(void) |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 52 | { |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 53 | #if defined(MXC_NFC_V3_2) |
| 54 | int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512; |
| 55 | int tmp; |
| 56 | |
| 57 | tmp = (readnfc(&nfc_ip->config2) & ~(NFC_V3_CONFIG2_SPAS_MASK | |
| 58 | NFC_V3_CONFIG2_EDC_MASK | NFC_V3_CONFIG2_PS_MASK)) | |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 59 | NFC_V3_CONFIG2_SPAS(CONFIG_SYS_NAND_OOBSIZE / 2) | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 60 | NFC_V3_CONFIG2_INT_MSK | NFC_V3_CONFIG2_ECC_EN | |
| 61 | NFC_V3_CONFIG2_ONE_CYCLE; |
| 62 | if (CONFIG_SYS_NAND_PAGE_SIZE == 4096) |
| 63 | tmp |= NFC_V3_CONFIG2_PS_4096; |
| 64 | else if (CONFIG_SYS_NAND_PAGE_SIZE == 2048) |
| 65 | tmp |= NFC_V3_CONFIG2_PS_2048; |
| 66 | else if (CONFIG_SYS_NAND_PAGE_SIZE == 512) |
| 67 | tmp |= NFC_V3_CONFIG2_PS_512; |
| 68 | /* |
| 69 | * if spare size is larger that 16 bytes per 512 byte hunk |
| 70 | * then use 8 symbol correction instead of 4 |
| 71 | */ |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 72 | if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16) |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 73 | tmp |= NFC_V3_CONFIG2_ECC_MODE_8; |
| 74 | else |
| 75 | tmp &= ~NFC_V3_CONFIG2_ECC_MODE_8; |
| 76 | writenfc(tmp, &nfc_ip->config2); |
| 77 | |
| 78 | tmp = NFC_V3_CONFIG3_NUM_OF_DEVS(0) | |
| 79 | NFC_V3_CONFIG3_NO_SDMA | |
| 80 | NFC_V3_CONFIG3_RBB_MODE | |
| 81 | NFC_V3_CONFIG3_SBB(6) | /* Reset default */ |
| 82 | NFC_V3_CONFIG3_ADD_OP(0); |
| 83 | #ifndef CONFIG_SYS_NAND_BUSWIDTH_16 |
| 84 | tmp |= NFC_V3_CONFIG3_FW8; |
| 85 | #endif |
| 86 | writenfc(tmp, &nfc_ip->config3); |
| 87 | |
| 88 | writenfc(0, &nfc_ip->delay_line); |
| 89 | #elif defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 32ae5b4 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 90 | int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512; |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 91 | int config1; |
| 92 | |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 93 | writenfc(CONFIG_SYS_NAND_OOBSIZE / 2, &nfc->spare_area_size); |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 94 | |
| 95 | /* unlocking RAM Buff */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 96 | writenfc(0x2, &nfc->config); |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 97 | |
| 98 | /* hardware ECC checking and correct */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 99 | config1 = readnfc(&nfc->config1) | NFC_V1_V2_CONFIG1_ECC_EN | |
| 100 | NFC_V1_V2_CONFIG1_INT_MSK | NFC_V2_CONFIG1_ONE_CYCLE | |
| 101 | NFC_V2_CONFIG1_FP_INT; |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 102 | /* |
| 103 | * if spare size is larger that 16 bytes per 512 byte hunk |
| 104 | * then use 8 symbol correction instead of 4 |
| 105 | */ |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 106 | if (CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 107 | config1 &= ~NFC_V2_CONFIG1_ECC_MODE_4; |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 108 | else |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 109 | config1 |= NFC_V2_CONFIG1_ECC_MODE_4; |
| 110 | writenfc(config1, &nfc->config1); |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 111 | #elif defined(MXC_NFC_V1) |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 112 | /* unlocking RAM Buff */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 113 | writenfc(0x2, &nfc->config); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 114 | |
| 115 | /* hardware ECC checking and correct */ |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 116 | writenfc(NFC_V1_V2_CONFIG1_ECC_EN | NFC_V1_V2_CONFIG1_INT_MSK, |
| 117 | &nfc->config1); |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 118 | #endif |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static void nfc_nand_command(unsigned short command) |
| 122 | { |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 123 | writenfc(command, &nfc->flash_cmd); |
| 124 | writenfc(NFC_CMD, &nfc->operation); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 125 | nfc_wait_ready(); |
| 126 | } |
| 127 | |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 128 | static void nfc_nand_address(unsigned short address) |
| 129 | { |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 130 | writenfc(address, &nfc->flash_addr); |
| 131 | writenfc(NFC_ADDR, &nfc->operation); |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 132 | nfc_wait_ready(); |
| 133 | } |
| 134 | |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 135 | static void nfc_nand_page_address(unsigned int page_address) |
| 136 | { |
| 137 | unsigned int page_count; |
| 138 | |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 139 | nfc_nand_address(0x00); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 140 | |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 141 | /* code only for large page flash */ |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 142 | if (CONFIG_SYS_NAND_PAGE_SIZE > 512) |
| 143 | nfc_nand_address(0x00); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 144 | |
| 145 | page_count = CONFIG_SYS_NAND_SIZE / CONFIG_SYS_NAND_PAGE_SIZE; |
| 146 | |
| 147 | if (page_address <= page_count) { |
| 148 | page_count--; /* transform 0x01000000 to 0x00ffffff */ |
| 149 | do { |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 150 | nfc_nand_address(page_address & 0xff); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 151 | page_address = page_address >> 8; |
| 152 | page_count = page_count >> 8; |
| 153 | } while (page_count); |
| 154 | } |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 155 | |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 156 | nfc_nand_address(0x00); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void nfc_nand_data_output(void) |
| 160 | { |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 161 | #ifdef NAND_MXC_2K_MULTI_CYCLE |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 162 | int i; |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 163 | #endif |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 164 | |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 165 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 166 | writenfc(0, &nfc->buf_addr); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 167 | #elif defined(MXC_NFC_V3_2) |
| 168 | int config1 = readnfc(&nfc->config1); |
| 169 | config1 &= ~NFC_V3_CONFIG1_RBA_MASK; |
| 170 | writenfc(config1, &nfc->config1); |
| 171 | #endif |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 172 | writenfc(NFC_OUTPUT, &nfc->operation); |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 173 | nfc_wait_ready(); |
| 174 | #ifdef NAND_MXC_2K_MULTI_CYCLE |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 175 | /* |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 176 | * This NAND controller requires multiple input commands |
| 177 | * for pages larger than 512 bytes. |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 178 | */ |
Benoît Thébaudeau | 32ae5b4 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 179 | for (i = 1; i < CONFIG_SYS_NAND_PAGE_SIZE / 512; i++) { |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 180 | writenfc(i, &nfc->buf_addr); |
| 181 | writenfc(NFC_OUTPUT, &nfc->operation); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 182 | nfc_wait_ready(); |
| 183 | } |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 184 | #endif |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | static int nfc_nand_check_ecc(void) |
| 188 | { |
Benoît Thébaudeau | d29aeba | 2012-08-13 22:49:42 +0200 | [diff] [blame] | 189 | #if defined(MXC_NFC_V1) |
Benoît Thébaudeau | 21fa48c | 2012-08-13 22:49:53 +0200 | [diff] [blame] | 190 | u16 ecc_status = readw(&nfc->ecc_status_result); |
| 191 | return (ecc_status & 0x3) == 2 || (ecc_status >> 2) == 2; |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 192 | #elif defined(MXC_NFC_V2_1) || defined(MXC_NFC_V3_2) |
Benoît Thébaudeau | 21fa48c | 2012-08-13 22:49:53 +0200 | [diff] [blame] | 193 | u32 ecc_status = readl(&nfc->ecc_status_result); |
| 194 | int ecc_per_page = CONFIG_SYS_NAND_PAGE_SIZE / 512; |
Benoît Thébaudeau | efb7c00 | 2013-04-11 09:35:51 +0000 | [diff] [blame] | 195 | int err_limit = CONFIG_SYS_NAND_OOBSIZE / ecc_per_page > 16 ? 8 : 4; |
Benoît Thébaudeau | 21fa48c | 2012-08-13 22:49:53 +0200 | [diff] [blame] | 196 | int subpages = CONFIG_SYS_NAND_PAGE_SIZE / 512; |
| 197 | |
| 198 | do { |
| 199 | if ((ecc_status & 0xf) > err_limit) |
| 200 | return 1; |
| 201 | ecc_status >>= 4; |
| 202 | } while (--subpages); |
| 203 | |
| 204 | return 0; |
Benoît Thébaudeau | d29aeba | 2012-08-13 22:49:42 +0200 | [diff] [blame] | 205 | #endif |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 206 | } |
| 207 | |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 208 | static void nfc_nand_read_page(unsigned int page_address) |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 209 | { |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 210 | /* read in first 0 buffer */ |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 211 | #if defined(MXC_NFC_V1) || defined(MXC_NFC_V2_1) |
Benoît Thébaudeau | 555bba1 | 2013-04-11 09:35:36 +0000 | [diff] [blame] | 212 | writenfc(0, &nfc->buf_addr); |
Benoît Thébaudeau | 8f26596 | 2013-04-11 09:35:37 +0000 | [diff] [blame] | 213 | #elif defined(MXC_NFC_V3_2) |
| 214 | int config1 = readnfc(&nfc->config1); |
| 215 | config1 &= ~NFC_V3_CONFIG1_RBA_MASK; |
| 216 | writenfc(config1, &nfc->config1); |
| 217 | #endif |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 218 | nfc_nand_command(NAND_CMD_READ0); |
| 219 | nfc_nand_page_address(page_address); |
| 220 | |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 221 | if (CONFIG_SYS_NAND_PAGE_SIZE > 512) |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 222 | nfc_nand_command(NAND_CMD_READSTART); |
| 223 | |
| 224 | nfc_nand_data_output(); /* fill the main buffer 0 */ |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static int nfc_read_page(unsigned int page_address, unsigned char *buf) |
| 228 | { |
| 229 | int i; |
| 230 | u32 *src; |
| 231 | u32 *dst; |
| 232 | |
| 233 | nfc_nand_read_page(page_address); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 234 | |
| 235 | if (nfc_nand_check_ecc()) |
Scott Wood | 52ab7ce | 2016-05-30 13:57:58 -0500 | [diff] [blame] | 236 | return -EBADMSG; |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 237 | |
Benoît Thébaudeau | 3f77519 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 238 | src = (u32 *)&nfc->main_area[0][0]; |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 239 | dst = (u32 *)buf; |
| 240 | |
| 241 | /* main copy loop from NAND-buffer to SDRAM memory */ |
Benoît Thébaudeau | 32ae5b4 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 242 | for (i = 0; i < CONFIG_SYS_NAND_PAGE_SIZE / 4; i++) { |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 243 | writel(readl(src), dst); |
| 244 | src++; |
| 245 | dst++; |
| 246 | } |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | static int is_badblock(int pagenumber) |
| 252 | { |
| 253 | int page = pagenumber; |
| 254 | u32 badblock; |
| 255 | u32 *src; |
| 256 | |
| 257 | /* Check the first two pages for bad block markers */ |
| 258 | for (page = pagenumber; page < pagenumber + 2; page++) { |
Benoît Thébaudeau | 2174f3b | 2012-08-13 22:48:56 +0200 | [diff] [blame] | 259 | nfc_nand_read_page(page); |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 260 | |
Benoît Thébaudeau | 3f77519 | 2012-08-13 22:48:12 +0200 | [diff] [blame] | 261 | src = (u32 *)&nfc->spare_area[0][0]; |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 262 | |
| 263 | /* |
| 264 | * IMPORTANT NOTE: The nand flash controller uses a non- |
| 265 | * standard layout for large page devices. This can |
| 266 | * affect the position of the bad block marker. |
| 267 | */ |
| 268 | /* Get the bad block marker */ |
| 269 | badblock = readl(&src[CONFIG_SYS_NAND_BAD_BLOCK_POS / 4]); |
| 270 | badblock >>= 8 * (CONFIG_SYS_NAND_BAD_BLOCK_POS % 4); |
| 271 | badblock &= 0xff; |
| 272 | |
| 273 | /* bad block marker verify */ |
| 274 | if (badblock != 0xff) |
| 275 | return 1; /* potential bad block */ |
| 276 | } |
| 277 | |
| 278 | return 0; |
| 279 | } |
| 280 | |
Marek Vasut | 2401669 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 281 | int nand_spl_load_image(uint32_t from, unsigned int size, void *buf) |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 282 | { |
| 283 | int i; |
| 284 | unsigned int page; |
| 285 | unsigned int maxpages = CONFIG_SYS_NAND_SIZE / |
| 286 | CONFIG_SYS_NAND_PAGE_SIZE; |
| 287 | |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 288 | nfc_nand_init(); |
| 289 | |
| 290 | /* Convert to page number */ |
| 291 | page = from / CONFIG_SYS_NAND_PAGE_SIZE; |
| 292 | i = 0; |
| 293 | |
Marek Vasut | 2401669 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 294 | size = roundup(size, CONFIG_SYS_NAND_PAGE_SIZE); |
Benoît Thébaudeau | 32ae5b4 | 2012-08-13 22:48:26 +0200 | [diff] [blame] | 295 | while (i < size / CONFIG_SYS_NAND_PAGE_SIZE) { |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 296 | if (nfc_read_page(page, buf) < 0) |
| 297 | return -1; |
| 298 | |
| 299 | page++; |
| 300 | i++; |
| 301 | buf = buf + CONFIG_SYS_NAND_PAGE_SIZE; |
| 302 | |
| 303 | /* |
| 304 | * Check if we have crossed a block boundary, and if so |
| 305 | * check for bad block. |
| 306 | */ |
| 307 | if (!(page % CONFIG_SYS_NAND_PAGE_COUNT)) { |
| 308 | /* |
| 309 | * Yes, new block. See if this block is good. If not, |
John Rigby | 4c94c45 | 2010-01-26 19:24:17 -0700 | [diff] [blame] | 310 | * loop until we find a good block. |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 311 | */ |
| 312 | while (is_badblock(page)) { |
| 313 | page = page + CONFIG_SYS_NAND_PAGE_COUNT; |
| 314 | /* Check i we've reached the end of flash. */ |
| 315 | if (page >= maxpages) |
| 316 | return -1; |
| 317 | } |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | return 0; |
| 322 | } |
| 323 | |
Marek Vasut | 2401669 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 324 | #ifndef CONFIG_SPL_FRAMEWORK |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 325 | /* |
| 326 | * The main entry for NAND booting. It's necessary that SDRAM is already |
| 327 | * configured and available since this code loads the main U-Boot image |
| 328 | * from NAND into SDRAM and starts it from there. |
| 329 | */ |
Marek Behún | 4c0237c | 2021-05-20 13:24:13 +0200 | [diff] [blame] | 330 | __used void nand_boot(void) |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 331 | { |
| 332 | __attribute__((noreturn)) void (*uboot)(void); |
| 333 | |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 334 | /* |
| 335 | * CONFIG_SYS_NAND_U_BOOT_OFFS and CONFIG_SYS_NAND_U_BOOT_SIZE must |
| 336 | * be aligned to full pages |
| 337 | */ |
Marek Vasut | 2401669 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 338 | if (!nand_spl_load_image(CONFIG_SYS_NAND_U_BOOT_OFFS, |
| 339 | CONFIG_SYS_NAND_U_BOOT_SIZE, |
| 340 | (uchar *)CONFIG_SYS_NAND_U_BOOT_DST)) { |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 341 | /* Copy from NAND successful, start U-Boot */ |
Magnus Lilja | 4133f65 | 2009-06-13 20:50:01 +0200 | [diff] [blame] | 342 | uboot = (void *)CONFIG_SYS_NAND_U_BOOT_START; |
| 343 | uboot(); |
| 344 | } else { |
| 345 | /* Unrecoverable error when copying from NAND */ |
| 346 | hang(); |
| 347 | } |
| 348 | } |
Marek Vasut | 2401669 | 2013-04-21 05:52:23 +0000 | [diff] [blame] | 349 | #endif |
Albert ARIBAUD | aa01873 | 2013-05-11 04:29:50 +0000 | [diff] [blame] | 350 | |
| 351 | void nand_init(void) {} |
| 352 | void nand_deselect(void) {} |