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