b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #include "qspi_host.h" |
| 2 | #include "spi_nand.h" |
| 3 | #include "Errors.h" |
| 4 | #include "misc.h" |
| 5 | #include "timer.h" |
| 6 | #include "PlatformConfig.h" |
| 7 | #if COPYIMAGESTOFLASH |
| 8 | #include "BootLoader.h" |
| 9 | #endif |
| 10 | |
| 11 | static void generic_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 12 | uint8_t status, uint32_t *corrected, uint32_t *ecc_error); |
| 13 | static void mxic_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 14 | uint8_t status, uint32_t *corrected, uint32_t *ecc_error); |
| 15 | static void mxic2_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 16 | uint8_t status, uint32_t *corrected, uint32_t *ecc_error); |
| 17 | static void micron_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 18 | uint8_t status, uint32_t *corrected, |
| 19 | uint32_t *ecc_error); |
| 20 | static void gd_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 21 | uint32_t *corrected, uint32_t *ecc_error); |
| 22 | static void gd_spi_nand_ecc_status2(struct spi_flash_chip *chip, uint8_t status, |
| 23 | uint32_t *corrected, uint32_t *ecc_error); |
| 24 | static void xtx_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 25 | uint32_t *corrected, uint32_t *ecc_error); |
| 26 | static void xtx2_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 27 | uint32_t *corrected, uint32_t *ecc_error); |
| 28 | static void yxsc_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 29 | uint32_t *corrected, uint32_t *ecc_error); |
| 30 | static void fm_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 31 | uint32_t *corrected, uint32_t *ecc_error); |
| 32 | static void wb_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 33 | uint32_t *corrected, uint32_t *ecc_error); |
| 34 | static void wb_spi_nand_ecc_status2(struct spi_flash_chip *chip, uint8_t status, |
| 35 | uint32_t *corrected, uint32_t *ecc_error); |
| 36 | |
| 37 | enum { |
| 38 | GET_FEATURE, |
| 39 | SET_FEATURE, |
| 40 | PAGE_READ, |
| 41 | READ_PAGE_CACHE_RDM, |
| 42 | READ_PAGE_CACHE_LAST, |
| 43 | READ_FROM_CACHE, |
| 44 | READ_FROM_CACHE_GD_C, |
| 45 | READ_FROM_CACHE_FAST, |
| 46 | READ_FROM_CACHE_X2, |
| 47 | READ_FROM_CACHE_DUAL, |
| 48 | READ_FROM_CACHE_X4, |
| 49 | READ_FROM_CACHE_QUAD, |
| 50 | READ_FROM_CACHE_DTR, |
| 51 | BLK_ERASE, |
| 52 | PROG_EXC, |
| 53 | PROG_LOAD, |
| 54 | PROG_LOAD_RDM_DATA, |
| 55 | PROG_LOAD_X4, |
| 56 | PROG_LOAD_RDM_DATA_X4, |
| 57 | WR_ENABLE, |
| 58 | WR_DISABLE, |
| 59 | READ_ID, |
| 60 | RESET, |
| 61 | READ_FROM_CACHE_QUAD_GD, |
| 62 | READ_FROM_CACHE_QUAD_GD2, |
| 63 | READ_ECCSR_MXIC, |
| 64 | MAX_CMD, |
| 65 | }; |
| 66 | |
| 67 | static struct spi_flash_chip nand_chip; |
| 68 | |
| 69 | static struct spi_nand_info spi_nand_table[] = { |
| 70 | /* Macronix */ |
| 71 | SPI_NAND_INFO("MX35UF1GE4AC", 0xC2, 0x92, 2048, 64, 64, 1024, |
| 72 | 1, 4, SPINAND_NEED_SET_BFT, 3, 0, |
| 73 | READ_FROM_CACHE_QUAD, mxic_spi_nand_ecc_status), |
| 74 | SPI_NAND_INFO("MX35UF2GE4AC", 0xC2, 0xA2, 2048, 64, 64, 2048, |
| 75 | 1, 4, SPINAND_NEED_SET_BFT, 3, 0, |
| 76 | READ_FROM_CACHE_QUAD, mxic_spi_nand_ecc_status), |
| 77 | SPI_NAND_INFO("MX35UF1GE4AD", 0xC2, 0x96, 2048, 128, 64, 1024, |
| 78 | 1, 8, SPINAND_NEED_SET_BFT, 6, 0, |
| 79 | READ_FROM_CACHE_QUAD, mxic2_spi_nand_ecc_status), |
| 80 | SPI_NAND_INFO("MX35UF2GE4AD", 0xC2, 0xA6, 2048, 128, 64, 2048, |
| 81 | 1, 8, SPINAND_NEED_SET_BFT, 6, 0, |
| 82 | READ_FROM_CACHE_QUAD, mxic2_spi_nand_ecc_status), |
| 83 | |
| 84 | /* GigaDeivce 1.8V */ |
| 85 | SPI_NAND_INFO("GD5F1GQ4RBxIG", 0xC8, 0xC1, 2048, 128, 64, 1024, |
| 86 | 1, 8, 0, 6, 0, |
| 87 | READ_FROM_CACHE_QUAD_GD, gd_spi_nand_ecc_status), |
| 88 | SPI_NAND_INFO("GD5F2GQ4RBxIG", 0xC8, 0xC2, 2048, 128, 64, 2048, |
| 89 | 1, 8, 0, 6, 0, |
| 90 | READ_FROM_CACHE_QUAD_GD, gd_spi_nand_ecc_status), |
| 91 | SPI_NAND_INFO("GD5F1GQ4RCxIG", 0xC8, 0xA148, 2048, 128, 64, 1024, |
| 92 | 1, 7, 0, 6, 0, |
| 93 | READ_FROM_CACHE_QUAD_GD, gd_spi_nand_ecc_status), |
| 94 | SPI_NAND_INFO("GD5F1GQ5RExxG", 0xC8, 0x41, 2048, 128, 64, 1024, |
| 95 | 1, 4, 0, 3, 0, |
| 96 | READ_FROM_CACHE_QUAD, gd_spi_nand_ecc_status2), |
| 97 | SPI_NAND_INFO("GD5F2GQ5RExxG", 0xC8, 0x42, 2048, 128, 64, 2048, |
| 98 | 1, 4, 0, 3, 0, |
| 99 | READ_FROM_CACHE_QUAD_GD2, gd_spi_nand_ecc_status2), |
| 100 | #if 1 |
| 101 | SPI_NAND_INFO_DTR("GD5F1GM7RE", 0xC8, 0x81, 2048, 128, 64, 1024, |
| 102 | 1, 8, SPINAND_SUPPORT_DTR, 6, 9, 2, 2, 80, |
| 103 | READ_FROM_CACHE_QUAD, READ_FROM_CACHE_DTR, |
| 104 | gd_spi_nand_ecc_status), |
| 105 | #else |
| 106 | SPI_NAND_INFO_TIMING("GD5F1GM7RE", 0xC8, 0x81, 2048, 128, 64, 1024, |
| 107 | 1, 8, 0, 6, 9, 2, 2, 0, |
| 108 | READ_FROM_CACHE_QUAD, gd_spi_nand_ecc_status), |
| 109 | #endif |
| 110 | SPI_NAND_INFO("GD5F2GM7RE", 0xC8, 0x82, 2048, 128, 64, 2048, |
| 111 | 1, 8, 0, 6, 0, |
| 112 | READ_FROM_CACHE_QUAD, gd_spi_nand_ecc_status), |
| 113 | SPI_NAND_INFO_TIMING("GD5F4GQ6RExxG", 0xC8, 0x45, 2048, 128, 64, 2048, |
| 114 | 2, 4, 0, 3, 11, 2, 2, 0, |
| 115 | READ_FROM_CACHE_QUAD_GD2, gd_spi_nand_ecc_status2), |
| 116 | SPI_NAND_INFO("GD5F4GM8RExxG", 0xC8, 0x85, 2048, 128, 64, 4096, |
| 117 | 1, 8, 0, 6, 0, |
| 118 | READ_FROM_CACHE_QUAD, gd_spi_nand_ecc_status), |
| 119 | |
| 120 | /* GigaDeivce 3.3V */ |
| 121 | SPI_NAND_INFO("GD5F1GQ4UExxG", 0xC8, 0xD1, 2048, 128, 64, 1024, |
| 122 | 1, 8, 0, 6, 0, |
| 123 | READ_FROM_CACHE_QUAD_GD, gd_spi_nand_ecc_status), |
| 124 | |
| 125 | /* Winbond */ |
| 126 | SPI_NAND_INFO("W25N512GWxxR/T", 0xEF, 0x20BA, 2048, 64, 64, 512, |
| 127 | 1, 4, 0, 4, 0, |
| 128 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 129 | SPI_NAND_INFO("W25N01GWxxIx", 0xEF, 0x21BA, 2048, 64, 64, 1024, |
| 130 | 1, 4, 0, 4, 0, |
| 131 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 132 | SPI_NAND_INFO("W25N01KW", 0xEF, 0x21BE, 2048, 64, 64, 1024, |
| 133 | 1, 4, 0, 3, 0, |
| 134 | READ_FROM_CACHE_QUAD, wb_spi_nand_ecc_status), |
| 135 | SPI_NAND_INFO("W25N02KW", 0xEF, 0x22BA, 2048, 64, 64, 2048, |
| 136 | 1, 8, 0, 6, 0, |
| 137 | READ_FROM_CACHE_QUAD, wb_spi_nand_ecc_status2), |
| 138 | SPI_NAND_INFO("W25M02GWxxIx", 0xEF, 0x21BB, 2048, 64, 64, 2048, |
| 139 | 1, 4, 0, 4, 0, |
| 140 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 141 | SPI_NAND_INFO("W25M01JW", 0xEF, 0x21BC, 2048, 64, 64, 1024, |
| 142 | 1, 4, 0, 4, 0, |
| 143 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 144 | |
| 145 | /* Dosilicon */ |
| 146 | SPI_NAND_INFO("DS35M1GAxx", 0xE5, 0x21, 2048, 64, 64, 1024, |
| 147 | 1, 4, 0, 4, 0, |
| 148 | READ_FROM_CACHE_X4, generic_spi_nand_ecc_status), |
| 149 | SPI_NAND_INFO("DS35M2GAxx", 0xE5, 0x22, 2048, 64, 64, 2048, |
| 150 | 1, 4, SPINAND_NEED_PLANE_SELECT, 4, 0, |
| 151 | READ_FROM_CACHE_X4, generic_spi_nand_ecc_status), |
| 152 | SPI_NAND_INFO("DS35M2GBxx", 0xE5, 0xA2, 2048, 128, 64, 2048, |
| 153 | 1, 8, SPINAND_NEED_PLANE_SELECT, 4, 83, |
| 154 | READ_FROM_CACHE_X4, micron_spi_nand_ecc_status), |
| 155 | SPI_NAND_INFO("DS35M4GMxx", 0xE5, 0xA4, 2048, 128, 64, 4096, |
| 156 | 1, 8, SPINAND_NEED_PLANE_SELECT, 4, 83, |
| 157 | READ_FROM_CACHE_X4, micron_spi_nand_ecc_status), |
| 158 | |
| 159 | /* ZettaDevice */ |
| 160 | SPI_NAND_INFO("ZD35M1GAxx", 0xBA, 0x21, 2048, 64, 64, 1024, |
| 161 | 1, 4, 0, 4, 0, |
| 162 | READ_FROM_CACHE_X4, generic_spi_nand_ecc_status), |
| 163 | |
| 164 | /* XTX */ |
| 165 | SPI_NAND_INFO("PN26Q01AWSIUG", 0xA1, 0xC1, 2048, 64, 64, 1024, |
| 166 | 1, 4, SPINAND_RDM_CMD_NEED_PAGE_READ | SPINAND_ECC_EN_ADDR_90H, |
| 167 | 8, 52, READ_FROM_CACHE_X4, xtx_spi_nand_ecc_status), |
| 168 | SPI_NAND_INFO("XT26Q01D-BE", 0x0B, 0x51, 2048, 128, 64, 1024, |
| 169 | 1, 8, 0, 5, 0, |
| 170 | READ_FROM_CACHE_QUAD_GD, xtx2_spi_nand_ecc_status), |
| 171 | SPI_NAND_INFO("XT26Q02D", 0x0B, 0x52, 2048, 128, 64, 2048, |
| 172 | 1, 8, 0, 5, 0, |
| 173 | READ_FROM_CACHE_QUAD_GD, xtx2_spi_nand_ecc_status), |
| 174 | SPI_NAND_INFO("XT26Q02E", 0x2C, 0x25, 2048, 128, 64, 2048, |
| 175 | 1, 8, SPINAND_NEED_PLANE_SELECT, 4, 52, |
| 176 | READ_FROM_CACHE_QUAD, micron_spi_nand_ecc_status), |
| 177 | |
| 178 | /* YXSC */ |
| 179 | SPI_NAND_INFO("TX25G01", 0xA1, 0xF1, 2048, 64, 64, 1024, |
| 180 | 1, 4, SPINAND_RDM_CMD_NEED_PAGE_READ | SPINAND_ECC_EN_ADDR_90H, |
| 181 | 2, 0, READ_FROM_CACHE_QUAD_GD, yxsc_spi_nand_ecc_status), |
| 182 | |
| 183 | /* ESMT */ |
| 184 | SPI_NAND_INFO("F50D1G41LB", 0xC8, 0x11, 2048, 128, 64, 1024, |
| 185 | 1, 7, 0, 4, 52, |
| 186 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 187 | |
| 188 | /* Fudan Microelectronics */ |
| 189 | SPI_NAND_INFO("FM25LS01", 0xA1, 0xA5, 2048, 128, 64, 1024, |
| 190 | 1, 4, 0, 4, 0, |
| 191 | READ_FROM_CACHE_X4, generic_spi_nand_ecc_status), |
| 192 | SPI_NAND_INFO("FM25LG02B", 0xA1, 0xB2, 2048, 128, 64, 2048, |
| 193 | 1, 8, 0, 4, 0, |
| 194 | READ_FROM_CACHE_X4, fm_spi_nand_ecc_status), |
| 195 | |
| 196 | /* Micron */ |
| 197 | SPI_NAND_INFO("MT29F1G01ABBFD", 0x2C, 0x15, 2048, 64, 64, 2048, |
| 198 | 1, 8, SPINAND_NEED_PLANE_SELECT, 4, 0, |
| 199 | READ_FROM_CACHE_X4, micron_spi_nand_ecc_status), |
| 200 | |
| 201 | /* SiliconGo 3.3V */ |
| 202 | SPI_NAND_INFO("SGM7000I-S24W1GH", 0xEA, 0xC1, 2048, 64, 64, 1024, |
| 203 | 1, 4, 0, 4, 0, |
| 204 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 205 | |
| 206 | SPI_NAND_INFO("SGM7000I-S25W2GH", 0xEA, 0xC2, 2048, 64, 64, 2048, |
| 207 | 1, 4, 0, 4, 0, |
| 208 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 209 | SPI_NAND_INFO("SGM7000I-S25W4GH", 0xEA, 0xC4, 2048, 64, 64, 4096, |
| 210 | 1, 4, 0, 4, 0, |
| 211 | READ_FROM_CACHE_QUAD, generic_spi_nand_ecc_status), |
| 212 | |
| 213 | {.name = NULL}, |
| 214 | }; |
| 215 | |
| 216 | /* Standard SPI-NAND flash commands */ |
| 217 | static struct spi_flash_cmd_cfg cmd_table[] = { |
| 218 | /*opcode addr_bytes addr_pins mode_bits mode_pins dummy_cycles |
| 219 | dummy_pins data_pins seq_id cmd_type */ |
| 220 | [GET_FEATURE] = SPI_CMD(0x0f, 1, 1, 0, 0, 0, 0, 1, 2, 1), |
| 221 | [SET_FEATURE] = SPI_CMD(0x1f, 1, 1, 0, 0, 0, 0, 1, -1, 2), |
| 222 | [PAGE_READ] = SPI_CMD(0x13, 3, 1, 0, 0, 0, 0, 0, 3, 0), |
| 223 | [READ_PAGE_CACHE_RDM] = SPI_CMD(0x30, 3, 1, 0, 0, 0, 0, 0, -1, 0), |
| 224 | [READ_PAGE_CACHE_LAST] = SPI_CMD(0x3f, 0, 0, 0, 0, 0, 0, 0, -1, 0), |
| 225 | [READ_FROM_CACHE] = SPI_CMD(0x03, 2, 1, 0, 0, 8, 1, 1, -1, 1), |
| 226 | [READ_FROM_CACHE_FAST] = SPI_CMD(0x0b, 2, 1, 0, 0, 8, 1, 1, -1, 1), |
| 227 | [READ_FROM_CACHE_X2] = SPI_CMD(0x3b, 2, 1, 0, 0, 8, 1, 2, -1, 1), |
| 228 | [READ_FROM_CACHE_DUAL] = SPI_CMD(0xbb, 2, 2, 0, 0, 4, 2, 2, -1, 1), |
| 229 | [READ_FROM_CACHE_X4] = SPI_CMD(0x6b, 2, 1, 0, 0, 8, 1, 4, 4, 1), |
| 230 | [READ_FROM_CACHE_QUAD] = SPI_CMD(0xeb, 2, 4, 0, 0, 4, 4, 4, 5, 1), |
| 231 | [BLK_ERASE] = SPI_CMD(0xd8, 3, 1, 0, 0, 0, 0, 0, -1, 0), |
| 232 | [PROG_EXC] = SPI_CMD(0x10, 3, 1, 0, 0, 0, 0, 0, 6, 0), |
| 233 | [PROG_LOAD] = SPI_CMD(0x02, 2, 1, 0, 0, 0, 0, 1, 7, 2), |
| 234 | [PROG_LOAD_RDM_DATA] = SPI_CMD(0x84, 2, 1, 0, 0, 0, 0, 1, -1, 2), |
| 235 | [PROG_LOAD_X4] = SPI_CMD(0x32, 2, 1, 0, 0, 0, 0, 4, 8, 2), |
| 236 | [PROG_LOAD_RDM_DATA_X4] = SPI_CMD(0x34, 2, 1, 0, 0, 0, 0, 4, -1, 2), |
| 237 | [WR_ENABLE] = SPI_CMD(0x06, 0, 0, 0, 0, 0, 0, 0, -1, 0), |
| 238 | [WR_DISABLE] = SPI_CMD(0x04, 0, 0, 0, 0, 0, 0, 0, -1, 0), |
| 239 | [READ_ID] = SPI_CMD(0x9f, 0, 0, 0, 0, 0, 0, 1, -1, 1), |
| 240 | [RESET] = SPI_CMD(0xff, 0, 0, 0, 0, 0, 0, 0, -1, 0), |
| 241 | |
| 242 | /* Vendor Specific command */ |
| 243 | [READ_FROM_CACHE_QUAD_GD] = SPI_CMD(0xeb, 2, 4, 0, 0, 2, 4, 4, 9, 1), |
| 244 | [READ_FROM_CACHE_QUAD_GD2] = SPI_CMD(0xeb, 2, 4, 0, 0, 8, 4, 4, 10, 1), |
| 245 | [READ_ECCSR_MXIC] = SPI_CMD(0x7c, 0, 0, 0, 0, 8, 1, 1, -1, 1), |
| 246 | [READ_FROM_CACHE_GD_C] = SPI_CMD(0x03, 3, 1, 0, 0, 0, 0, 1, -1, 1), |
| 247 | [READ_FROM_CACHE_DTR] = SPI_CMD_DTR(0xee, 0, 4, 4, 1, 0, 0, 0, 8, 4, 4, 1, 11, 1), |
| 248 | |
| 249 | /* END Mark */ |
| 250 | [MAX_CMD] = SPI_CMD(0x00, 0, 0, 0, 0, 0, 0, 0, -1, 0), |
| 251 | }; |
| 252 | |
| 253 | /** |
| 254 | * spi_nand_read_reg - send command 0Fh to read register |
| 255 | * @chip: SPI_FLASH device structure |
| 256 | * @reg; register to read |
| 257 | * @buf: buffer to store value |
| 258 | */ |
| 259 | static int spi_nand_read_reg(struct spi_flash_chip *chip, |
| 260 | uint8_t reg, uint8_t *buf) |
| 261 | { |
| 262 | struct spi_flash_cmd cmd; |
| 263 | int ret; |
| 264 | |
| 265 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 266 | cmd.cmd_cfg = chip->table + GET_FEATURE; |
| 267 | cmd.n_addr = 1; |
| 268 | cmd.addr[0] = reg; |
| 269 | cmd.n_rx = 1; |
| 270 | cmd.rx_buf = buf; |
| 271 | |
| 272 | ret = qspi_start_cmd(chip, &cmd); |
| 273 | if (ret < 0) |
| 274 | obm_printf("err: %d read register %d\r\n", ret, reg); |
| 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * spi_nand_write_reg - send command 1Fh to write register |
| 281 | * @chip: SPI-NAND device structure |
| 282 | * @reg; register to write |
| 283 | * @buf: buffer stored value |
| 284 | */ |
| 285 | static int spi_nand_write_reg(struct spi_flash_chip *chip, |
| 286 | uint8_t reg, uint8_t *buf) |
| 287 | { |
| 288 | struct spi_flash_cmd cmd; |
| 289 | int ret; |
| 290 | |
| 291 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 292 | cmd.cmd_cfg = chip->table + SET_FEATURE; |
| 293 | cmd.n_addr = 1; |
| 294 | cmd.addr[0] = reg; |
| 295 | cmd.n_tx = 1; |
| 296 | cmd.tx_buf = buf; |
| 297 | |
| 298 | ret = qspi_start_cmd(chip, &cmd); |
| 299 | if (ret < 0) |
| 300 | obm_printf("err: %d write register %d\r\n", ret, reg); |
| 301 | |
| 302 | return ret; |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * spi_nand_read_status - get status register value |
| 307 | * @chip: SPI-NAND device structure |
| 308 | * @status: buffer to store value |
| 309 | * Description: |
| 310 | * After read, write, or erase, the Nand device is expected to set the |
| 311 | * busy status. |
| 312 | * This function is to allow reading the status of the command: read, |
| 313 | * write, and erase. |
| 314 | * Once the status turns to be ready, the other status bits also are |
| 315 | * valid status bits. |
| 316 | */ |
| 317 | static int spi_nand_read_status(struct spi_flash_chip *chip, uint8_t *status) |
| 318 | { |
| 319 | return spi_nand_read_reg(chip, REG_STATUS, status); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * spi_nand_get_cfg - get configuration register value |
| 324 | * @chip: SPI-NAND device structure |
| 325 | * @cfg: buffer to store value |
| 326 | * Description: |
| 327 | * Configuration register includes OTP config, Lock Tight enable/disable |
| 328 | * and Internal ECC enable/disable. |
| 329 | */ |
| 330 | static int spi_nand_get_cfg(struct spi_flash_chip *chip, uint8_t *cfg) |
| 331 | { |
| 332 | return spi_nand_read_reg(chip, REG_CFG, cfg); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * spi_nand_set_cfg - set value to configuration register |
| 337 | * @chip: SPI-NAND device structure |
| 338 | * @cfg: buffer stored value |
| 339 | * Description: |
| 340 | * Configuration register includes OTP config, Lock Tight enable/disable |
| 341 | * and Internal ECC enable/disable. |
| 342 | */ |
| 343 | static int spi_nand_set_cfg(struct spi_flash_chip *chip, uint8_t *cfg) |
| 344 | { |
| 345 | return spi_nand_write_reg(chip, REG_CFG, cfg); |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * spi_nand_enable_ecc - enable internal ECC |
| 350 | * @chip: SPI-NAND device structure |
| 351 | * Description: |
| 352 | * There is one bit( bit 0x10 ) to set or to clear the internal ECC. |
| 353 | * Enable chip internal ECC, set the bit to 1 |
| 354 | * Disable chip internal ECC, clear the bit to 0 |
| 355 | */ |
| 356 | static int spi_nand_enable_ecc(struct spi_flash_chip *chip) |
| 357 | { |
| 358 | uint8_t cfg = 0; |
| 359 | |
| 360 | /* For XTX spi-nand, ECC_EN locate in 0x90 feature register */ |
| 361 | if (chip->options & SPINAND_ECC_EN_ADDR_90H) |
| 362 | spi_nand_read_reg(chip, 0x90, &cfg); |
| 363 | else |
| 364 | spi_nand_get_cfg(chip, &cfg); |
| 365 | |
| 366 | if ((cfg & CFG_ECC_MASK) == CFG_ECC_ENABLE) |
| 367 | return 0; |
| 368 | |
| 369 | cfg |= CFG_ECC_ENABLE; |
| 370 | |
| 371 | if (chip->options & SPINAND_ECC_EN_ADDR_90H) |
| 372 | return spi_nand_write_reg(chip, 0x90, &cfg); |
| 373 | else |
| 374 | return spi_nand_set_cfg(chip, &cfg); |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * spi_nand_disable_ecc - disable internal ECC |
| 379 | * @chip: SPI-NAND device structure |
| 380 | * Description: |
| 381 | * There is one bit( bit 0x10 ) to set or to clear the internal ECC. |
| 382 | * Enable chip internal ECC, set the bit to 1 |
| 383 | * Disable chip internal ECC, clear the bit to 0 |
| 384 | */ |
| 385 | static int spi_nand_disable_ecc(struct spi_flash_chip *chip) |
| 386 | { |
| 387 | uint8_t cfg = 0; |
| 388 | |
| 389 | /* For XTX spi-nand, ECC_EN locate in 0x90 feature register */ |
| 390 | if (chip->options & SPINAND_ECC_EN_ADDR_90H) |
| 391 | spi_nand_read_reg(chip, 0x90, &cfg); |
| 392 | else |
| 393 | spi_nand_get_cfg(chip, &cfg); |
| 394 | |
| 395 | if ((cfg & CFG_ECC_MASK) == CFG_ECC_ENABLE) { |
| 396 | cfg &= ~CFG_ECC_ENABLE; |
| 397 | if (chip->options & SPINAND_ECC_EN_ADDR_90H) |
| 398 | return spi_nand_write_reg(chip, 0x90, &cfg); |
| 399 | else |
| 400 | return spi_nand_set_cfg(chip, &cfg); |
| 401 | } |
| 402 | return 0; |
| 403 | } |
| 404 | |
| 405 | static int spi_nand_enable_quad(struct spi_flash_chip *chip) |
| 406 | { |
| 407 | uint8_t cfg = 0; |
| 408 | |
| 409 | spi_nand_get_cfg(chip, &cfg); |
| 410 | if ((cfg & CFG_QE_MASK) == CFG_QE_ENABLE) |
| 411 | return 0; |
| 412 | cfg |= CFG_QE_ENABLE; |
| 413 | return spi_nand_set_cfg(chip, &cfg); |
| 414 | } |
| 415 | |
| 416 | /** |
| 417 | * generic_spi_nand_ecc_status - decode status regisger to get ecc info |
| 418 | * @status: status register value to decode |
| 419 | * @corrected: bitflip count that ecc corrected |
| 420 | * @ecc_error: uncorrected bitflip happen or not |
| 421 | */ |
| 422 | static void generic_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 423 | uint8_t status, uint32_t *corrected, uint32_t *ecc_error) |
| 424 | { |
| 425 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 426 | |
| 427 | *ecc_error = 0; |
| 428 | switch (ecc_status) { |
| 429 | case 0x0: |
| 430 | /* No bit error detected */ |
| 431 | *corrected = 0; |
| 432 | break; |
| 433 | case 0x1: |
| 434 | /* 1~4 error bits detected and corrected */ |
| 435 | *corrected = 4; |
| 436 | break; |
| 437 | case 0x2: |
| 438 | /* Error detected and can not corrected */ |
| 439 | *ecc_error = 1; |
| 440 | break; |
| 441 | default: |
| 442 | obm_printf("%s: unexpected status: %d\r\n", |
| 443 | __func__, ecc_status); |
| 444 | *ecc_error = 1; |
| 445 | break; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | static int mxic_spi_nand_set_bft(struct spi_flash_chip *chip, uint8_t threshold) |
| 450 | { |
| 451 | uint8_t val, bft; |
| 452 | int ret; |
| 453 | |
| 454 | ret = spi_nand_read_reg(chip, 0x10, &val); |
| 455 | if (ret) |
| 456 | return ret; |
| 457 | |
| 458 | bft = (val & 0xf0) >> 4; |
| 459 | obm_printf("%s: read BFT=0x%x threshold=%d\n", __func__, val, threshold); |
| 460 | |
| 461 | if (bft != threshold) { |
| 462 | val = threshold << 4; |
| 463 | ret = spi_nand_write_reg(chip, 0x10, &val); |
| 464 | if (ret) |
| 465 | return ret; |
| 466 | |
| 467 | obm_printf("%s: update BFT=0x%x\n", __func__, val); |
| 468 | } |
| 469 | |
| 470 | return 0; |
| 471 | } |
| 472 | |
| 473 | static void mxic_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 474 | uint8_t status, uint32_t *corrected, uint32_t *ecc_error) |
| 475 | { |
| 476 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 477 | |
| 478 | *ecc_error = 0; |
| 479 | switch (ecc_status) { |
| 480 | case 0x0: |
| 481 | /* No bit error detected */ |
| 482 | *corrected = 0; |
| 483 | break; |
| 484 | case 0x1: |
| 485 | /* 1~4 error bits detected and corrected */ |
| 486 | *corrected = 4; |
| 487 | break; |
| 488 | case 0x2: |
| 489 | /* Error detected and can not corrected */ |
| 490 | *ecc_error = 1; |
| 491 | break; |
| 492 | default: |
| 493 | *corrected = 4; |
| 494 | break; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | static int mxic_spi_nand_read_eccsr(struct spi_flash_chip *chip, uint8_t *eccsr) |
| 499 | { |
| 500 | struct spi_flash_cmd cmd; |
| 501 | int ret; |
| 502 | |
| 503 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 504 | cmd.cmd_cfg = chip->table + READ_ECCSR_MXIC; |
| 505 | cmd.n_rx = 1; |
| 506 | cmd.rx_buf = eccsr; |
| 507 | |
| 508 | ret = qspi_start_cmd(chip, &cmd); |
| 509 | if (ret < 0) |
| 510 | obm_printf("err: %d read eccsr register\r\n", ret); |
| 511 | |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static void mxic2_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 516 | uint8_t status, uint32_t *corrected, uint32_t *ecc_error) |
| 517 | { |
| 518 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 519 | uint8_t eccsr; |
| 520 | |
| 521 | *ecc_error = 0; |
| 522 | switch (ecc_status) { |
| 523 | case 0x0: |
| 524 | /* No bit error detected */ |
| 525 | *corrected = 0; |
| 526 | break; |
| 527 | case 0x2: |
| 528 | /* Error detected and can not corrected */ |
| 529 | *ecc_error = 1; |
| 530 | break; |
| 531 | case 0x1: |
| 532 | /* 1~8 error bits detected and corrected */ |
| 533 | *corrected = 4; |
| 534 | default: |
| 535 | mxic_spi_nand_read_eccsr(chip, &eccsr); |
| 536 | eccsr &= 0xf; |
| 537 | if (eccsr == 0xf) |
| 538 | *ecc_error = 1; |
| 539 | else |
| 540 | *corrected = eccsr; |
| 541 | break; |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | static void micron_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 546 | uint8_t status, uint32_t *corrected, |
| 547 | uint32_t *ecc_error) |
| 548 | { |
| 549 | uint8_t ecc_status = (status & 0x70) >> SPI_NAND_ECC_SHIFT; |
| 550 | |
| 551 | *ecc_error = 0; |
| 552 | switch (ecc_status) { |
| 553 | case 0x0: |
| 554 | /* No bit error detected */ |
| 555 | *corrected = 0; |
| 556 | break; |
| 557 | case 0x1: |
| 558 | /* 1~3 error bits detected and corrected */ |
| 559 | *corrected = 3; |
| 560 | break; |
| 561 | case 0x2: |
| 562 | /* Error detected and can not corrected */ |
| 563 | *ecc_error = 1; |
| 564 | break; |
| 565 | case 0x3: |
| 566 | /* 4~6 error bits detected and corrected */ |
| 567 | *corrected = 6; |
| 568 | break; |
| 569 | case 0x5: |
| 570 | /* 7~8 error bits detected and corrected */ |
| 571 | *corrected = 8; |
| 572 | break; |
| 573 | default: |
| 574 | obm_printf("%s: unexpected status: %d\r\n", |
| 575 | __func__, ecc_status); |
| 576 | *ecc_error = 1; |
| 577 | break; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | static void fm_spi_nand_ecc_status(struct spi_flash_chip *chip, |
| 582 | uint8_t status, uint32_t *corrected, |
| 583 | uint32_t *ecc_error) |
| 584 | { |
| 585 | uint8_t ecc_status = (status & 0x70) >> SPI_NAND_ECC_SHIFT; |
| 586 | |
| 587 | *ecc_error = 0; |
| 588 | switch (ecc_status) { |
| 589 | case 0x0: |
| 590 | /* No bit error detected */ |
| 591 | *corrected = 0; |
| 592 | break; |
| 593 | case 0x1: |
| 594 | *corrected = 3; |
| 595 | break; |
| 596 | case 0x2: |
| 597 | *corrected = 4; |
| 598 | break; |
| 599 | case 0x3: |
| 600 | *corrected = 5; |
| 601 | break; |
| 602 | case 0x4: |
| 603 | *corrected = 6; |
| 604 | break; |
| 605 | case 0x5: |
| 606 | *corrected = 7; |
| 607 | break; |
| 608 | case 0x6: |
| 609 | *corrected = 8; |
| 610 | break; |
| 611 | case 0x7: |
| 612 | /* Error detected and can not corrected */ |
| 613 | *ecc_error = 1; |
| 614 | break; |
| 615 | default: |
| 616 | obm_printf("%s: unexpected status: %d\r\n", |
| 617 | __func__, ecc_status); |
| 618 | *ecc_error = 1; |
| 619 | break; |
| 620 | } |
| 621 | } |
| 622 | |
| 623 | static void xtx_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 624 | uint32_t *corrected, uint32_t *ecc_error) |
| 625 | { |
| 626 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 627 | |
| 628 | *ecc_error = 0; |
| 629 | switch (ecc_status) { |
| 630 | case 0x0: |
| 631 | /* No bit error detected */ |
| 632 | *corrected = 0; |
| 633 | break; |
| 634 | case 0x1: |
| 635 | /* 1~7 error bits detected and corrected */ |
| 636 | *corrected = 7; |
| 637 | break; |
| 638 | case 0x2: |
| 639 | /* Error detected and can not corrected */ |
| 640 | *ecc_error = 1; |
| 641 | break; |
| 642 | case 0x3: |
| 643 | /* 8 error bits detected and corrected */ |
| 644 | *corrected = 8; |
| 645 | break; |
| 646 | } |
| 647 | } |
| 648 | |
| 649 | static void xtx2_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 650 | uint32_t *corrected, uint32_t *ecc_error) |
| 651 | { |
| 652 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 653 | uint8_t ecc_status2 = (status & 0xf0) >> SPI_NAND_ECC_SHIFT; |
| 654 | |
| 655 | *ecc_error = 0; |
| 656 | switch (ecc_status) { |
| 657 | case 0x0: |
| 658 | /* No bit error detected */ |
| 659 | *corrected = 0; |
| 660 | break; |
| 661 | case 0x1: |
| 662 | switch (ecc_status2) { |
| 663 | case 1: |
| 664 | *corrected = 4; |
| 665 | break; |
| 666 | case 5: |
| 667 | *corrected = 5; |
| 668 | break; |
| 669 | case 9: |
| 670 | *corrected = 6; |
| 671 | break; |
| 672 | case 0xd: |
| 673 | *corrected = 7; |
| 674 | break; |
| 675 | } |
| 676 | break; |
| 677 | case 0x2: |
| 678 | /* Error detected and can not corrected */ |
| 679 | *ecc_error = 1; |
| 680 | break; |
| 681 | case 0x3: |
| 682 | /* 8 error bits detected and corrected */ |
| 683 | *corrected = 8; |
| 684 | break; |
| 685 | } |
| 686 | } |
| 687 | |
| 688 | static void yxsc_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 689 | uint32_t *corrected, uint32_t *ecc_error) |
| 690 | { |
| 691 | uint8_t ecc_status = (status & 0x70) >> SPI_NAND_ECC_SHIFT; |
| 692 | |
| 693 | *ecc_error = 0; |
| 694 | switch (ecc_status) { |
| 695 | case 0x0: |
| 696 | /* No bit error detected */ |
| 697 | *corrected = 0; |
| 698 | break; |
| 699 | case 0x1: |
| 700 | /* 1~7 error bits detected and corrected */ |
| 701 | *corrected = 1; |
| 702 | break; |
| 703 | case 0x2: |
| 704 | /* 2 error bits detected and corrected */ |
| 705 | *ecc_error = 2; |
| 706 | break; |
| 707 | case 0x3: |
| 708 | /* 3 error bits detected and corrected */ |
| 709 | *corrected = 3; |
| 710 | break; |
| 711 | case 0x4: |
| 712 | /* 4 error bits detected and corrected */ |
| 713 | *corrected = 4; |
| 714 | break; |
| 715 | case 0x7: |
| 716 | /* Error detected and can not corrected */ |
| 717 | *ecc_error = 1; |
| 718 | break; |
| 719 | default: |
| 720 | obm_printf("%s: unexpected status: %d\r\n", |
| 721 | __func__, ecc_status); |
| 722 | *ecc_error = 1; |
| 723 | break; |
| 724 | } |
| 725 | } |
| 726 | |
| 727 | static void gd_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 728 | uint32_t *corrected, uint32_t *ecc_error) |
| 729 | { |
| 730 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 731 | uint8_t ext_status; |
| 732 | int ret; |
| 733 | |
| 734 | *ecc_error = 0; |
| 735 | *corrected = 0; |
| 736 | if (ecc_status == 0x1) { |
| 737 | ret = spi_nand_read_reg(chip, 0xf0, &ext_status); |
| 738 | if (ret) { |
| 739 | obm_printf("gd_spi_nand_ecc_status failed\n"); |
| 740 | return; |
| 741 | } |
| 742 | |
| 743 | ext_status = (ext_status & SPI_NAND_ECC_MASK) >> |
| 744 | SPI_NAND_ECC_SHIFT; |
| 745 | switch (ext_status) { |
| 746 | case 0x0: |
| 747 | /* 1~4 error bits detected and corrected */ |
| 748 | *corrected = 4; |
| 749 | break; |
| 750 | case 0x1: |
| 751 | /* 5 error bits detected and corrected */ |
| 752 | *corrected = 5; |
| 753 | break; |
| 754 | case 0x2: |
| 755 | /* 6 error bits detected and corrected */ |
| 756 | *corrected = 6; |
| 757 | break; |
| 758 | case 0x3: |
| 759 | /* 7 error bits detected and corrected */ |
| 760 | *corrected = 7; |
| 761 | break; |
| 762 | } |
| 763 | } else if (ecc_status == 0x2) { |
| 764 | /* Error detected and can not corrected */ |
| 765 | *ecc_error = 1; |
| 766 | } else if (ecc_status == 0x3) { |
| 767 | /* 8 error bits detected and corrected */ |
| 768 | *corrected = 8; |
| 769 | } else if (ecc_status == 0x0) { |
| 770 | /* No bit error detected */ |
| 771 | *corrected = 0; |
| 772 | } |
| 773 | } |
| 774 | |
| 775 | static void gd_spi_nand_ecc_status2(struct spi_flash_chip *chip, uint8_t status, |
| 776 | uint32_t *corrected, uint32_t *ecc_error) |
| 777 | { |
| 778 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 779 | uint8_t ext_status; |
| 780 | int ret; |
| 781 | |
| 782 | *ecc_error = 0; |
| 783 | *corrected = 0; |
| 784 | if (ecc_status == 0x0) { |
| 785 | /* No bit error detected */ |
| 786 | *corrected = 0; |
| 787 | } else if (ecc_status == 0x1) { |
| 788 | ret = spi_nand_read_reg(chip, 0xf0, &ext_status); |
| 789 | if (ret) { |
| 790 | obm_printf("gd_spi_nand_ecc_status failed\n"); |
| 791 | return; |
| 792 | } |
| 793 | |
| 794 | ext_status = (ext_status & SPI_NAND_ECC_MASK) >> |
| 795 | SPI_NAND_ECC_SHIFT; |
| 796 | |
| 797 | switch (ext_status) { |
| 798 | case 0x0: |
| 799 | *corrected = 1; |
| 800 | break; |
| 801 | case 0x1: |
| 802 | *corrected = 2; |
| 803 | break; |
| 804 | case 0x2: |
| 805 | *corrected = 3; |
| 806 | break; |
| 807 | case 0x3: |
| 808 | *corrected = 4; |
| 809 | break; |
| 810 | } |
| 811 | } else if (ecc_status == 0x2) { |
| 812 | /* Error bit > 4 detected and can not corrected */ |
| 813 | *ecc_error = 1; |
| 814 | } else { |
| 815 | obm_printf("%s: unexpected status: %d\r\n", |
| 816 | __func__, ecc_status); |
| 817 | *ecc_error = 1; |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | static void wb_spi_nand_ecc_status(struct spi_flash_chip *chip, uint8_t status, |
| 822 | uint32_t *corrected, uint32_t *ecc_error) |
| 823 | { |
| 824 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 825 | uint8_t ext_status; |
| 826 | int ret; |
| 827 | |
| 828 | *ecc_error = 0; |
| 829 | *corrected = 0; |
| 830 | if (ecc_status == 0x1) { |
| 831 | ret = spi_nand_read_reg(chip, 0x30, &ext_status); |
| 832 | if (ret) { |
| 833 | obm_printf("wb_spi_nand_ecc_status failed\n"); |
| 834 | return; |
| 835 | } |
| 836 | |
| 837 | ext_status = (ext_status & 0x70) >> 4; |
| 838 | if (ext_status == 7) |
| 839 | *ecc_error = 1; |
| 840 | else |
| 841 | *corrected = ext_status; |
| 842 | } else if (ecc_status == 0x2) { |
| 843 | /* Error detected and can not corrected */ |
| 844 | *ecc_error = 1; |
| 845 | } else if (ecc_status == 0x3) { |
| 846 | /* 4 error bits detected and corrected */ |
| 847 | *corrected = 4; |
| 848 | } else if (ecc_status == 0x0) { |
| 849 | /* No bit error detected */ |
| 850 | *corrected = 0; |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | static void wb_spi_nand_ecc_status2(struct spi_flash_chip *chip, uint8_t status, |
| 855 | uint32_t *corrected, uint32_t *ecc_error) |
| 856 | { |
| 857 | uint8_t ecc_status = (status & SPI_NAND_ECC_MASK) >> SPI_NAND_ECC_SHIFT; |
| 858 | uint8_t ext_status; |
| 859 | int ret; |
| 860 | |
| 861 | *ecc_error = 0; |
| 862 | *corrected = 0; |
| 863 | if (ecc_status == 0x1) { |
| 864 | ret = spi_nand_read_reg(chip, 0x30, &ext_status); |
| 865 | if (ret) { |
| 866 | obm_printf("wb_spi_nand_ecc_status2 failed\n"); |
| 867 | return; |
| 868 | } |
| 869 | |
| 870 | ext_status = (ext_status & 0xf0) >> 4; |
| 871 | if (ext_status == 0xf) |
| 872 | *ecc_error = 1; |
| 873 | else |
| 874 | *corrected = ext_status; |
| 875 | } else if (ecc_status == 0x2) { |
| 876 | /* Error detected and can not corrected */ |
| 877 | *ecc_error = 1; |
| 878 | } else if (ecc_status == 0x3) { |
| 879 | /* 4 error bits detected and corrected */ |
| 880 | *corrected = 4; |
| 881 | } else if (ecc_status == 0x0) { |
| 882 | /* No bit error detected */ |
| 883 | *corrected = 0; |
| 884 | } |
| 885 | } |
| 886 | |
| 887 | /** |
| 888 | * spi_nand_write_enable - send command 06h to enable write or erase the |
| 889 | * Nand cells |
| 890 | * @chip: SPI-NAND device structure |
| 891 | * Description: |
| 892 | * Before write and erase the Nand cells, the write enable has to be set. |
| 893 | * After the write or erase, the write enable bit is automatically |
| 894 | * cleared (status register bit 2) |
| 895 | * Set the bit 2 of the status register has the same effect |
| 896 | */ |
| 897 | static int spi_nand_write_enable(struct spi_flash_chip *chip) |
| 898 | { |
| 899 | struct spi_flash_cmd cmd; |
| 900 | |
| 901 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 902 | cmd.cmd_cfg = chip->table + WR_ENABLE; |
| 903 | |
| 904 | return qspi_start_cmd(chip, &cmd); |
| 905 | } |
| 906 | |
| 907 | /** |
| 908 | * spi_nand_read_page_to_cache - send command 13h to read data from Nand to cache |
| 909 | * @chip: SPI-NAND device structure |
| 910 | * @page_addr: page to read |
| 911 | */ |
| 912 | static int spi_nand_read_page_to_cache(struct spi_flash_chip *chip, |
| 913 | uint32_t page_addr) |
| 914 | { |
| 915 | struct spi_flash_cmd cmd; |
| 916 | |
| 917 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 918 | cmd.cmd_cfg = chip->table + PAGE_READ; |
| 919 | cmd.n_addr = 3; |
| 920 | cmd.addr[0] = (uint8_t)(page_addr >> 16); |
| 921 | cmd.addr[1] = (uint8_t)(page_addr >> 8); |
| 922 | cmd.addr[2] = (uint8_t)page_addr; |
| 923 | cmd.flag = RST_AHB_DOMAIN | AHB_MAP_SIZE_PAGE; |
| 924 | |
| 925 | return qspi_start_cmd(chip, &cmd); |
| 926 | } |
| 927 | |
| 928 | /** |
| 929 | * spi_nand_read_from_cache - read data out from cache register |
| 930 | * @chip: SPI-NAND device structure |
| 931 | * @page_addr: page to read |
| 932 | * @column: the location to read from the cache |
| 933 | * @len: number of bytes to read |
| 934 | * @rbuf: buffer held @len bytes |
| 935 | * Description: |
| 936 | * Command can be 03h, 0Bh, 3Bh, 6Bh, BBh, EBh |
| 937 | * The read can specify 1 to (page size + spare size) bytes of data read at |
| 938 | * the corresponding locations. |
| 939 | * No tRd delay. |
| 940 | */ |
| 941 | static int spi_nand_read_from_cache(struct spi_flash_chip *chip, |
| 942 | uint32_t page_addr, uint32_t column, |
| 943 | uint32_t len, uint8_t *rbuf) |
| 944 | { |
| 945 | struct spi_flash_cmd cmd; |
| 946 | |
| 947 | if (chip->options & SPINAND_NEED_PLANE_SELECT) { |
| 948 | column |= (((page_addr >> |
| 949 | (chip->block_shift - chip->page_shift)) & 0x1) << 12); |
| 950 | } |
| 951 | |
| 952 | if (chip->host->xip_read) { |
| 953 | uint32_t addr; |
| 954 | |
| 955 | addr = column; |
| 956 | addr += chip->host->cs_addr[chip->cs]; |
| 957 | memcpy(rbuf, (void *)addr, len); |
| 958 | return 0; |
| 959 | } |
| 960 | |
| 961 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 962 | cmd.cmd_cfg = chip->table + chip->read_cache_op; |
| 963 | cmd.n_addr = 2; |
| 964 | cmd.addr[0] = (uint8_t)(column >> 8); |
| 965 | cmd.addr[1] = (uint8_t)column; |
| 966 | cmd.n_rx = len; |
| 967 | cmd.rx_buf = rbuf; |
| 968 | |
| 969 | return qspi_start_cmd(chip, &cmd); |
| 970 | } |
| 971 | |
| 972 | /** |
| 973 | * spi_nand_program_data_to_cache - write data to cache register |
| 974 | * @chip: SPI-NAND device structure |
| 975 | * @page_addr: page to write |
| 976 | * @column: the location to write to the cache |
| 977 | * @len: number of bytes to write |
| 978 | * @wrbuf: buffer held @len bytes |
| 979 | * @clr_cache: clear cache register or not |
| 980 | * Description: |
| 981 | * Command can be 02h, 32h, 84h, 34h |
| 982 | * 02h and 32h will clear the cache with 0xff value first |
| 983 | * Since it is writing the data to cache, there is no tPROG time. |
| 984 | */ |
| 985 | static int spi_nand_program_data_to_cache(struct spi_flash_chip *chip, |
| 986 | uint32_t page_addr, uint32_t column, uint32_t len, |
| 987 | const uint8_t *wbuf, uint8_t clr_cache) |
| 988 | { |
| 989 | struct spi_flash_cmd cmd; |
| 990 | |
| 991 | if (chip->options & SPINAND_NEED_PLANE_SELECT) { |
| 992 | column |= (((page_addr >> |
| 993 | (chip->block_shift - chip->page_shift)) & 0x1) << 12); |
| 994 | } |
| 995 | |
| 996 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 997 | if (clr_cache) |
| 998 | cmd.cmd_cfg = chip->table + chip->write_cache_op; |
| 999 | else |
| 1000 | cmd.cmd_cfg = chip->table + chip->write_cache_rdm_op; |
| 1001 | cmd.n_addr = 2; |
| 1002 | cmd.addr[0] = (uint8_t)(column >> 8); |
| 1003 | cmd.addr[1] = (uint8_t)column; |
| 1004 | cmd.n_tx = len; |
| 1005 | cmd.tx_buf = wbuf; |
| 1006 | cmd.flag = RST_AHB_DOMAIN; |
| 1007 | |
| 1008 | return qspi_start_cmd(chip, &cmd); |
| 1009 | } |
| 1010 | |
| 1011 | /** |
| 1012 | * spi_nand_program_execute - send command 10h to write a page from |
| 1013 | * cache to the Nand array |
| 1014 | * @chip: SPI-NAND device structure |
| 1015 | * @page_addr: the physical page location to write the page. |
| 1016 | * Description: |
| 1017 | * Need to wait for tPROG time to finish the transaction. |
| 1018 | */ |
| 1019 | static int spi_nand_program_execute(struct spi_flash_chip *chip, uint32_t page_addr) |
| 1020 | { |
| 1021 | struct spi_flash_cmd cmd; |
| 1022 | |
| 1023 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 1024 | cmd.cmd_cfg = chip->table + PROG_EXC; |
| 1025 | cmd.n_addr = 3; |
| 1026 | cmd.addr[0] = (uint8_t)(page_addr >> 16); |
| 1027 | cmd.addr[1] = (uint8_t)(page_addr >> 8); |
| 1028 | cmd.addr[2] = (uint8_t)page_addr; |
| 1029 | |
| 1030 | return qspi_start_cmd(chip, &cmd); |
| 1031 | } |
| 1032 | |
| 1033 | /** |
| 1034 | * spi_nand_erase_block_erase - send command D8h to erase a block |
| 1035 | * @chip: SPI-NAND device structure |
| 1036 | * @page_addr: the page to erase. |
| 1037 | * Description: |
| 1038 | * Need to wait for tERS. |
| 1039 | */ |
| 1040 | static int spi_nand_erase_block(struct spi_flash_chip *chip, |
| 1041 | uint32_t page_addr) |
| 1042 | { |
| 1043 | struct spi_flash_cmd cmd; |
| 1044 | |
| 1045 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 1046 | cmd.cmd_cfg = chip->table + BLK_ERASE; |
| 1047 | cmd.n_addr = 3; |
| 1048 | cmd.addr[0] = (uint8_t)(page_addr >> 16); |
| 1049 | cmd.addr[1] = (uint8_t)(page_addr >> 8); |
| 1050 | cmd.addr[2] = (uint8_t)page_addr; |
| 1051 | cmd.flag = RST_AHB_DOMAIN | AHB_MAP_SIZE_PAGE; |
| 1052 | |
| 1053 | return qspi_start_cmd(chip, &cmd); |
| 1054 | } |
| 1055 | |
| 1056 | /** |
| 1057 | * spi_nand_wait - wait until the command is done |
| 1058 | * @chip: SPI-NAND device structure |
| 1059 | * @s: buffer to store status register(can be NULL) |
| 1060 | */ |
| 1061 | static int spi_nand_wait(struct spi_flash_chip *chip, uint8_t *s) |
| 1062 | { |
| 1063 | uint8_t status; |
| 1064 | unsigned long ret = -ETIMEDOUT; |
| 1065 | |
| 1066 | while (1) { |
| 1067 | spi_nand_read_status(chip, &status); |
| 1068 | if ((status & STATUS_OIP_MASK) == STATUS_READY) { |
| 1069 | ret = 0; |
| 1070 | goto out; |
| 1071 | } |
| 1072 | } |
| 1073 | out: |
| 1074 | if (s) |
| 1075 | *s = status; |
| 1076 | |
| 1077 | return ret; |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * spi_nand_read_id - send 9Fh command to get ID |
| 1082 | * @chip: SPI_FLASH device structure |
| 1083 | * @buf: buffer to store id |
| 1084 | */ |
| 1085 | static int spi_nand_read_id(struct spi_flash_chip *chip, uint8_t *buf) |
| 1086 | { |
| 1087 | struct spi_flash_cmd cmd; |
| 1088 | |
| 1089 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 1090 | cmd.cmd_cfg = chip->table + READ_ID; |
| 1091 | cmd.n_rx = 4; |
| 1092 | cmd.rx_buf = buf; |
| 1093 | |
| 1094 | return qspi_start_cmd(chip, &cmd); |
| 1095 | } |
| 1096 | |
| 1097 | /** |
| 1098 | * spi_nand_reset - send command FFh to reset chip. |
| 1099 | * @chip: SPI_FLASH device structure |
| 1100 | */ |
| 1101 | static int spi_nand_reset(struct spi_flash_chip *chip) |
| 1102 | { |
| 1103 | struct spi_flash_cmd cmd; |
| 1104 | |
| 1105 | memset(&cmd, 0, sizeof(struct spi_flash_cmd)); |
| 1106 | cmd.cmd_cfg = chip->table + RESET; |
| 1107 | |
| 1108 | if (qspi_start_cmd(chip, &cmd)) |
| 1109 | obm_printf("spi_nand reset failed!\r\n"); |
| 1110 | |
| 1111 | /* elapse 2ms before issuing any other command */ |
| 1112 | Delay(2000); |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | /** |
| 1117 | * spi_nand_lock_block - [Interface] write block lock register to |
| 1118 | * lock/unlock device |
| 1119 | * @spi: spi device structure |
| 1120 | * @lock: value to set to block lock register |
| 1121 | * Description: |
| 1122 | * After power up, all the Nand blocks are locked. This function allows |
| 1123 | * one to unlock the blocks, and so it can be written or erased. |
| 1124 | * |
| 1125 | * Register Bit Address |
| 1126 | * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | |
| 1127 | * |---------------------------------------------------------| |
| 1128 | * | BRWD2 | BP3 | BP2 | BP1 | BP0 | TB | WP | Reversed | |
| 1129 | * |
| 1130 | * Block Lock Register Block Protection Bits |
| 1131 | * |
| 1132 | * TB BP3 BP2 BP1 BP0 Protected Portion |
| 1133 | * 0 0 0 0 0 All unlocked |
| 1134 | * 0 0 0 0 1 Upper 1/1024 locked |
| 1135 | * 0 0 0 1 0 Upper 1/512 locked |
| 1136 | * 0 0 0 1 1 Upper 1/256 locked |
| 1137 | * 0 0 1 0 0 Upper 1/128 locked |
| 1138 | * 0 0 1 0 1 Upper 1/64 locked |
| 1139 | * 0 0 1 1 0 Upper 1/32 locked |
| 1140 | * 0 0 1 1 1 Upper 1/16 locked |
| 1141 | * 0 1 0 0 0 Upper 1/8 locked |
| 1142 | * 0 1 0 0 1 Upper 1/4 locked |
| 1143 | * 0 1 0 1 0 Upper 1/2 locked |
| 1144 | * 0 1 0 1 1 All Locked |
| 1145 | * 0 1 1 0 0 All Locked |
| 1146 | * 0 1 1 0 1 All Locked |
| 1147 | * 0 1 1 1 0 All Locked |
| 1148 | * 0 1 1 1 1 All Locked |
| 1149 | * 1 0 0 0 0 All unlocked |
| 1150 | * 1 0 0 0 1 Lower 1/1024 locked |
| 1151 | * 1 0 0 1 0 Lower 1/512 locked |
| 1152 | * 1 0 0 1 1 Lower 1/256 locked |
| 1153 | * 1 0 1 0 0 Lower 1/128 locked |
| 1154 | * 1 0 1 0 1 Lower 1/64 locked |
| 1155 | * 1 0 1 1 0 Lower 1/32 locked |
| 1156 | * 1 0 1 1 1 Lower 1/16 locked |
| 1157 | * 1 1 0 0 0 Lower 1/8 locked |
| 1158 | * 1 1 0 0 1 Lower 1/4 locked |
| 1159 | * 1 1 0 1 0 Lower 1/2 locked |
| 1160 | * 1 1 0 1 1 All Locked |
| 1161 | * 1 1 1 0 0 All Locked |
| 1162 | * 1 1 1 0 1 All Locked |
| 1163 | * 1 1 1 1 0 All Locked |
| 1164 | * 1 1 1 1 1 All Locked |
| 1165 | |
| 1166 | */ |
| 1167 | int spi_nand_lock_block(struct spi_flash_chip *chip, uint8_t lock) |
| 1168 | { |
| 1169 | return spi_nand_write_reg(chip, REG_BLOCK_LOCK, &lock); |
| 1170 | } |
| 1171 | |
| 1172 | /** |
| 1173 | * spi_nand_change_mode - switch chip to OTP/OTP protect/Normal mode |
| 1174 | * @chip: SPI-NAND device structure |
| 1175 | * @mode: mode to enter |
| 1176 | */ |
| 1177 | static int spi_nand_change_mode(struct spi_flash_chip *chip, uint8_t mode) |
| 1178 | { |
| 1179 | uint8_t cfg; |
| 1180 | |
| 1181 | spi_nand_get_cfg(chip, &cfg); |
| 1182 | switch (mode) { |
| 1183 | case OTP_MODE: |
| 1184 | cfg = (cfg & ~CFG_OTP_MASK) | CFG_OTP_ENTER; |
| 1185 | break; |
| 1186 | case OTP_PROTECT_MODE: |
| 1187 | cfg = (cfg & ~CFG_OTP_MASK) | CFG_OTP_PROTECT; |
| 1188 | break; |
| 1189 | case SNOR_READ_ENABLE_MODE: |
| 1190 | cfg = (cfg & ~CFG_OTP_MASK) | CFG_SNOR_ENABLE; |
| 1191 | break; |
| 1192 | case NORMAL_MODE: |
| 1193 | cfg = (cfg & ~CFG_OTP_MASK) | CFG_OTP_EXIT; |
| 1194 | break; |
| 1195 | } |
| 1196 | spi_nand_set_cfg(chip, &cfg); |
| 1197 | |
| 1198 | return 0; |
| 1199 | } |
| 1200 | |
| 1201 | /** |
| 1202 | * spi_nand_scan_id_table - scan chip info in id table |
| 1203 | * @chip: SPI-NAND device structure |
| 1204 | * @id: point to manufacture id and device id |
| 1205 | * Description: |
| 1206 | * If found in id table, config chip with table information. |
| 1207 | */ |
| 1208 | static int spi_nand_scan_id_table(struct spi_flash_chip *chip) |
| 1209 | { |
| 1210 | struct spi_nand_info *type = spi_nand_table; |
| 1211 | int id; |
| 1212 | |
| 1213 | for (; type->name; type++) { |
| 1214 | /* ignore high byte if not used */ |
| 1215 | if (!(type->dev_id >> 8)) |
| 1216 | id = (chip->dev_id & 0xff); |
| 1217 | else |
| 1218 | id = chip->dev_id; |
| 1219 | if (chip->mfr_id == type->mfr_id && id == type->dev_id) { |
| 1220 | chip->name = type->name; |
| 1221 | chip->size = type->page_size * type->pages_per_blk |
| 1222 | * type->blks_per_lun * type->luns_per_chip; |
| 1223 | chip->block_size = type->page_size |
| 1224 | * type->pages_per_blk; |
| 1225 | chip->page_size = type->page_size; |
| 1226 | chip->oob_size = type->oob_size; |
| 1227 | chip->lun_shift = |
| 1228 | ilog2(chip->block_size * type->blks_per_lun); |
| 1229 | chip->ecc_strength = type->ecc_strength; |
| 1230 | chip->options = type->options; |
| 1231 | chip->refresh_threshold = type->bitflip_threshold; |
| 1232 | chip->get_ecc_status = type->get_ecc_status; |
| 1233 | chip->max_mhz = type->max_mhz; |
| 1234 | chip->flash_info = type; |
| 1235 | |
| 1236 | obm_printf("SPI-NAND: %s is found in table\r\n", |
| 1237 | type->name); |
| 1238 | return true; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | return false; |
| 1243 | } |
| 1244 | |
| 1245 | static uint16_t onfi_crc16(uint16_t crc, uint8_t const *p, uint32_t len) |
| 1246 | { |
| 1247 | int i; |
| 1248 | |
| 1249 | while (len--) { |
| 1250 | crc ^= *p++ << 8; |
| 1251 | for (i = 0; i < 8; i++) |
| 1252 | crc = (crc << 1) ^ ((crc & 0x8000) ? 0x8005 : 0); |
| 1253 | } |
| 1254 | |
| 1255 | return crc; |
| 1256 | } |
| 1257 | |
| 1258 | /* Sanitize ONFI strings so we can safely print them */ |
| 1259 | static void sanitize_string(char *s, uint32_t len) |
| 1260 | { |
| 1261 | int i = len - 1; |
| 1262 | int j = 0; |
| 1263 | |
| 1264 | /* Null terminate */ |
| 1265 | s[i--] = 0; |
| 1266 | |
| 1267 | /* Remove unnecessary space */ |
| 1268 | while (i >= 0 && (s[i] <= ' ' || s[i] > 127)) { |
| 1269 | s[i--] = 0; |
| 1270 | } |
| 1271 | /* Remove non printable chars */ |
| 1272 | for (j = 0; j <= i; j++) { |
| 1273 | if (s[j] < ' ' || s[j] > 127) |
| 1274 | s[j] = '?'; |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | /** |
| 1279 | * spi_nand_detect_onfi - config chip with parameter page |
| 1280 | * @chip: SPI-NAND device structure |
| 1281 | * Description: |
| 1282 | * This function is called when we can not get info from id table. |
| 1283 | */ |
| 1284 | static int spi_nand_detect_onfi(struct spi_flash_chip *chip) |
| 1285 | { |
| 1286 | struct spi_nand_onfi_params *p; |
| 1287 | uint8_t *buffer = NULL; |
| 1288 | int read_cache_op; |
| 1289 | int ret = true; |
| 1290 | int i; |
| 1291 | |
| 1292 | buffer = malloc(chip->block_size); |
| 1293 | if(buffer == NULL) { |
| 1294 | return HeapExhaustedError; |
| 1295 | } |
| 1296 | |
| 1297 | obm_printf("spi_nand_detect_onfi: buffer=0x%x\r\n", buffer); |
| 1298 | memset(buffer, 0x0, 256*3); |
| 1299 | |
| 1300 | spi_nand_change_mode(chip, OTP_MODE); |
| 1301 | spi_nand_read_page_to_cache(chip, 0x01); |
| 1302 | spi_nand_wait(chip, NULL); |
| 1303 | /* |
| 1304 | * read parameter page can only ues 1-1-1 mode |
| 1305 | */ |
| 1306 | read_cache_op = chip->read_cache_op; |
| 1307 | chip->read_cache_op = READ_FROM_CACHE; |
| 1308 | spi_nand_read_from_cache(chip, 0x01, 0, 256 * 3, buffer); |
| 1309 | chip->read_cache_op = read_cache_op; |
| 1310 | spi_nand_change_mode(chip, NORMAL_MODE); |
| 1311 | |
| 1312 | p = (struct spi_nand_onfi_params *)buffer; |
| 1313 | for (i = 0; i < 3; i++, p++) { |
| 1314 | if (p->sig[0] != 'O' || p->sig[1] != 'N' || |
| 1315 | p->sig[2] != 'F' || p->sig[3] != 'I') |
| 1316 | continue; |
| 1317 | //if (onfi_crc16(ONFI_CRC_BASE, (uint8_t *)p, 254) == |
| 1318 | // (p->crc)) |
| 1319 | break; |
| 1320 | } |
| 1321 | |
| 1322 | if (i == 3) { |
| 1323 | obm_printf("Could not find valid ONFI, use default settings\r\n"); |
| 1324 | ret = false; |
| 1325 | goto out; |
| 1326 | } |
| 1327 | |
| 1328 | memcpy(&chip->onfi_params, p, sizeof(*p)); |
| 1329 | p = &chip->onfi_params; |
| 1330 | sanitize_string(p->manufacturer, sizeof(p->manufacturer)); |
| 1331 | sanitize_string(p->model, sizeof(p->model)); |
| 1332 | |
| 1333 | chip->name = p->model; |
| 1334 | chip->size = (p->byte_per_page) * |
| 1335 | (p->pages_per_block) * |
| 1336 | (p->blocks_per_lun) * p->lun_count; |
| 1337 | chip->block_size = (p->byte_per_page) * |
| 1338 | (p->pages_per_block); |
| 1339 | chip->page_size = (p->byte_per_page); |
| 1340 | chip->oob_size = (p->spare_bytes_per_page); |
| 1341 | chip->lun_shift = ilog2(chip->block_size * (p->blocks_per_lun)); |
| 1342 | |
| 1343 | // if (chip->mfr_id == SPIFLASH_MFR_MICRON) { |
| 1344 | // if (p->vendor.micron_sepcific.two_plane_page_read) |
| 1345 | // chip->options |= SPINAND_NEED_PLANE_SELECT; |
| 1346 | // if (p->vendor.micron_sepcific.die_selection) |
| 1347 | // chip->options |= SPINAND_NEED_DIE_SELECT; |
| 1348 | // } |
| 1349 | chip->ecc_strength = p->vendor.micron_sepcific.ecc_ability; |
| 1350 | out: |
| 1351 | free(buffer); |
| 1352 | return ret; |
| 1353 | } |
| 1354 | |
| 1355 | #define SPI_NAND_MAX_RETRY 3 |
| 1356 | /** |
| 1357 | * spi_nand_do_read_page - read page from flash to buffer |
| 1358 | * @chip: spi nand chip structure |
| 1359 | * @page_addr: page address/raw address |
| 1360 | * @column: column address |
| 1361 | * @ecc_off: without ecc or not |
| 1362 | * @corrected: how many bit error corrected |
| 1363 | * @buf: data buffer |
| 1364 | * @len: data length to read |
| 1365 | * Description: |
| 1366 | * Return -EBADMSG when internal ecc can not correct bitflips. |
| 1367 | * The command sequence to transfer data from NAND array to output is |
| 1368 | * follows: |
| 1369 | * 13h (PAGE READ to cache register) |
| 1370 | * 0Fh (GET FEATURES command to read the status) |
| 1371 | * 0Bh/03h/3Bh/6Bh (Read from Cache Xn); or BBh/EBh (Read From |
| 1372 | * Cache Dual/Quad IO) |
| 1373 | */ |
| 1374 | static int __spi_nand_do_read_page(struct spi_flash_chip *chip, uint32_t page_addr, |
| 1375 | uint32_t column, uint8_t ecc_off, |
| 1376 | uint32_t *corrected, uint8_t *buf, uint32_t len) |
| 1377 | { |
| 1378 | uint32_t ecc_error; |
| 1379 | uint8_t status; |
| 1380 | int ret; |
| 1381 | int rx_len, i; |
| 1382 | |
| 1383 | spi_nand_read_page_to_cache(chip, page_addr); |
| 1384 | ret = spi_nand_wait(chip, &status); |
| 1385 | if (ret < 0) { |
| 1386 | obm_printf("error %d waiting page 0x%x to cache\r\n", |
| 1387 | ret, page_addr); |
| 1388 | return ret; |
| 1389 | } |
| 1390 | |
| 1391 | if (!ecc_off && chip->get_ecc_status) { |
| 1392 | chip->get_ecc_status(chip, status, corrected, &ecc_error); |
| 1393 | if (ecc_error) { |
| 1394 | obm_printf("internal ECC error reading page 0x%x" |
| 1395 | " status=0x%x\r\n", page_addr, status); |
| 1396 | ret = -EBADMSG; |
| 1397 | return ret; |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | rx_len = chip->rx_max_len ? chip->rx_max_len : len; |
| 1402 | do { |
| 1403 | int real_len; |
| 1404 | |
| 1405 | i = 0; |
| 1406 | retry: |
| 1407 | real_len = min(len, rx_len); |
| 1408 | ret = spi_nand_read_from_cache(chip, page_addr, column, |
| 1409 | real_len, buf); |
| 1410 | if (ret == -EAGAIN && ++i <= SPI_NAND_MAX_RETRY) { |
| 1411 | rx_len = rx_len / 2; |
| 1412 | goto retry; |
| 1413 | } else if (ret) { |
| 1414 | return -EIO; |
| 1415 | } else if (i) { |
| 1416 | obm_printf("Pass after the %dth retry\r\n", i); |
| 1417 | } |
| 1418 | |
| 1419 | column += real_len; |
| 1420 | buf += real_len; |
| 1421 | len -= real_len; |
| 1422 | } while (len); |
| 1423 | |
| 1424 | return ret; |
| 1425 | } |
| 1426 | |
| 1427 | static int spi_nand_do_read_page(struct spi_flash_chip *chip, uint32_t page_addr, |
| 1428 | uint32_t column, uint8_t ecc_off, |
| 1429 | uint32_t *corrected, uint8_t *buf, uint32_t len) |
| 1430 | { |
| 1431 | uint8_t *tmp_buf = NULL; |
| 1432 | int read_len; |
| 1433 | int ret; |
| 1434 | |
| 1435 | if (upload_nand_spare == TRUE) |
| 1436 | len += chip->oob_size; |
| 1437 | |
| 1438 | if (!column && len == chip->page_size) |
| 1439 | return __spi_nand_do_read_page(chip, page_addr, column, ecc_off, |
| 1440 | corrected, buf, len); |
| 1441 | |
| 1442 | tmp_buf = malloc(chip->page_size + chip->oob_size); |
| 1443 | if(tmp_buf == NULL) |
| 1444 | return HeapExhaustedError; |
| 1445 | |
| 1446 | read_len = chip->page_size; |
| 1447 | if (column + len > chip->page_size) |
| 1448 | read_len += chip->oob_size; |
| 1449 | |
| 1450 | ret = __spi_nand_do_read_page(chip, page_addr, 0, ecc_off, |
| 1451 | corrected, tmp_buf, read_len); |
| 1452 | if (!ret) |
| 1453 | memcpy(buf, tmp_buf + column, len); |
| 1454 | |
| 1455 | free(tmp_buf); |
| 1456 | return ret; |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * spi_nand_do_write_page - write data from buffer to flash |
| 1461 | * @chip: spi nand chip structure |
| 1462 | * @page_addr: page address/raw address |
| 1463 | * @column: column address |
| 1464 | * @buf: data buffer |
| 1465 | * @len: data length to write |
| 1466 | * @clr_cache: clear cache register with 0xFF or not |
| 1467 | * Description: |
| 1468 | * Page program sequence is as follows: |
| 1469 | * 06h (WRITE ENABLE) |
| 1470 | * 02h/32h/84h/34h (PROGRAM LOAD (RAMDOM_DATA) Xn) |
| 1471 | * 10h (PROGRAM EXECUTE) |
| 1472 | * 0Fh (GET FEATURE command to read the status) |
| 1473 | * PROGRAM LOAD Xn instruction will reset the cache resigter with 0xFF, |
| 1474 | * while PROGRAM LOAD RANDOM DATA Xn instruction will only update the |
| 1475 | * data bytes that are specified by the command input sequence and the rest |
| 1476 | * of data in the cache buffer will remain unchanged. |
| 1477 | */ |
| 1478 | static int spi_nand_do_write_page(struct spi_flash_chip *chip, uint32_t page_addr, |
| 1479 | uint32_t column, const uint8_t *buf, |
| 1480 | uint32_t len, uint8_t clr_cache) |
| 1481 | { |
| 1482 | uint32_t column_save, len_save; |
| 1483 | uint8_t *buf_save; |
| 1484 | uint8_t status; |
| 1485 | uint8_t p_fail = false; |
| 1486 | int tx_len, i; |
| 1487 | int ret = 0; |
| 1488 | int cache_done = 0; |
| 1489 | |
| 1490 | buf_save = buf; |
| 1491 | column_save = column; |
| 1492 | len_save = len; |
| 1493 | |
| 1494 | tx_len = chip->tx_max_len; |
| 1495 | load_cache: |
| 1496 | if (tx_len < len && (chip->options & SPINAND_RDM_CMD_NEED_PAGE_READ)) { |
| 1497 | /* |
| 1498 | * XTX spi-nand PROGRAM LOAD RANDOM DATA cmd only used for |
| 1499 | * Internal Data Move, need to send 13H (PAGE READ TO CACHE) |
| 1500 | * before use this command. |
| 1501 | * |
| 1502 | * If spi-nand from new vendor have same limitation as XTX, must |
| 1503 | * set SPINAND_RDM_CMD_NEED_PAGE_READ in options of spi_nand_table. |
| 1504 | */ |
| 1505 | spi_nand_read_page_to_cache(chip, page_addr); |
| 1506 | ret = spi_nand_wait(chip, &status); |
| 1507 | if (ret < 0) { |
| 1508 | obm_printf("error %d waiting page 0x%x to cache\r\n", |
| 1509 | ret, page_addr); |
| 1510 | return ret; |
| 1511 | } |
| 1512 | |
| 1513 | cache_done = 1; |
| 1514 | clr_cache = 0; |
| 1515 | } |
| 1516 | |
| 1517 | spi_nand_write_enable(chip); |
| 1518 | do { |
| 1519 | int real_len; |
| 1520 | |
| 1521 | i = 0; |
| 1522 | retry: |
| 1523 | real_len = min(len, tx_len); |
| 1524 | ret = spi_nand_program_data_to_cache(chip, page_addr, column, |
| 1525 | real_len, buf, clr_cache); |
| 1526 | if (ret == -EAGAIN && ++i <= SPI_NAND_MAX_RETRY) { |
| 1527 | tx_len = tx_len / 2; |
| 1528 | if (!cache_done && |
| 1529 | (chip->options & SPINAND_RDM_CMD_NEED_PAGE_READ)) { |
| 1530 | buf = buf_save; |
| 1531 | column = column_save; |
| 1532 | len = len_save; |
| 1533 | goto load_cache; |
| 1534 | } |
| 1535 | goto retry; |
| 1536 | } else if (ret) { |
| 1537 | return -EIO; |
| 1538 | } else if (i) { |
| 1539 | obm_printf("Write after the %dth retry\r\n", i); |
| 1540 | } |
| 1541 | |
| 1542 | clr_cache = 0; |
| 1543 | column += real_len; |
| 1544 | buf += real_len; |
| 1545 | len -= real_len; |
| 1546 | } while (len); |
| 1547 | |
| 1548 | spi_nand_program_execute(chip, page_addr); |
| 1549 | ret = spi_nand_wait(chip, &status); |
| 1550 | if (ret < 0) { |
| 1551 | obm_printf("error %d reading page 0x%x from cache\r\n", |
| 1552 | ret, page_addr); |
| 1553 | return ret; |
| 1554 | } |
| 1555 | |
| 1556 | if ((status & STATUS_P_FAIL_MASK) == STATUS_P_FAIL) { |
| 1557 | obm_printf("program page 0x%x failed\r\n", page_addr); |
| 1558 | p_fail = true; |
| 1559 | } |
| 1560 | |
| 1561 | if (p_fail) |
| 1562 | ret = -EIO; |
| 1563 | |
| 1564 | return ret; |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * spi_nand_read_pages - read data from flash to buffer |
| 1569 | * @chip: spi nand chip structure |
| 1570 | * @from: offset to read from |
| 1571 | * @ops: oob operations description structure |
| 1572 | * Description: |
| 1573 | * Normal read function, read one page to buffer before issue |
| 1574 | * another. Return -EUCLEAN when bitflip is over threshold. |
| 1575 | * Return -EBADMSG when internal ecc can not correct bitflips. |
| 1576 | */ |
| 1577 | static int spi_nand_read_pages(struct spi_flash_chip *chip, uint32_t from, |
| 1578 | uint32_t len, uint32_t *retlen, uint8_t *buf, |
| 1579 | uint8_t ecc_off) |
| 1580 | { |
| 1581 | int page_addr, page_offset, size; |
| 1582 | int ret; |
| 1583 | uint32_t corrected = 0; |
| 1584 | uint32_t max_bitflip = 0; |
| 1585 | int readlen = len; |
| 1586 | uint32_t failed = 0; |
| 1587 | uint32_t _retlen; |
| 1588 | |
| 1589 | page_addr = from >> chip->page_shift; |
| 1590 | page_offset = from & chip->page_mask; |
| 1591 | _retlen = 0; |
| 1592 | |
| 1593 | while (1) { |
| 1594 | size = min(readlen, chip->page_size - page_offset); |
| 1595 | ret = spi_nand_do_read_page(chip, page_addr, page_offset, |
| 1596 | ecc_off, &corrected, buf + _retlen, size); |
| 1597 | if (ret == -EBADMSG) { |
| 1598 | failed++; |
| 1599 | } else if (ret) { |
| 1600 | obm_printf("error %d reading page 0x%x\r\n", |
| 1601 | ret, page_addr); |
| 1602 | goto out; |
| 1603 | } |
| 1604 | max_bitflip = max(corrected, max_bitflip); |
| 1605 | |
| 1606 | _retlen += size; |
| 1607 | if (upload_nand_spare == TRUE) |
| 1608 | _retlen += chip->oob_size; |
| 1609 | readlen -= size; |
| 1610 | page_offset = 0; |
| 1611 | |
| 1612 | if (!readlen) |
| 1613 | break; |
| 1614 | page_addr++; |
| 1615 | } |
| 1616 | out: |
| 1617 | *retlen = _retlen; |
| 1618 | |
| 1619 | #ifdef BITFLIPS_SCRUBBING |
| 1620 | if (chip->refresh_threshold && |
| 1621 | max_bitflip >= chip->refresh_threshold) |
| 1622 | ret = ReadDisturbError; |
| 1623 | #endif |
| 1624 | if (failed) |
| 1625 | ret = -EBADMSG; |
| 1626 | |
| 1627 | return ret; |
| 1628 | } |
| 1629 | |
| 1630 | /** |
| 1631 | * spi_nand_read - [Interface] SPI-NAND read |
| 1632 | * @chip: spi nand device structure |
| 1633 | * @from: offset to read from |
| 1634 | * @len: number of bytes to read |
| 1635 | * @retlen: pointer to variable to store the number of read bytes |
| 1636 | * @buf: the databuffer to put data |
| 1637 | */ |
| 1638 | static int __spi_nand_read(struct spi_flash_chip *chip, uint32_t from, |
| 1639 | uint32_t len, uint32_t *retlen, uint8_t *buf, |
| 1640 | uint8_t ecc_off) |
| 1641 | { |
| 1642 | int ret; |
| 1643 | |
| 1644 | /* Do not allow reads past end of device */ |
| 1645 | if (from >= chip->size) { |
| 1646 | obm_printf("%s: attempt to read beyond end of device\r\n", |
| 1647 | __func__); |
| 1648 | return -EINVAL; |
| 1649 | } |
| 1650 | |
| 1651 | if (ecc_off) |
| 1652 | spi_nand_disable_ecc(chip); |
| 1653 | |
| 1654 | ret = spi_nand_read_pages(chip, from, len, retlen, buf, ecc_off); |
| 1655 | |
| 1656 | if (ecc_off) |
| 1657 | spi_nand_enable_ecc(chip); |
| 1658 | |
| 1659 | return ret; |
| 1660 | } |
| 1661 | |
| 1662 | static int spi_nand_read(struct spi_flash_chip *chip, |
| 1663 | int addr, int size, uint8_t *rbuf) |
| 1664 | { |
| 1665 | uint32_t retlen; |
| 1666 | |
| 1667 | return __spi_nand_read(chip, addr, size, &retlen, rbuf, |
| 1668 | upload_disable_ecc); |
| 1669 | } |
| 1670 | |
| 1671 | /** |
| 1672 | * spi_nand_write - [Interface] SPI-NAND write |
| 1673 | * @chip: spi nand device structure |
| 1674 | * @to: offset to write to |
| 1675 | * @len: number of bytes to write |
| 1676 | * @retlen: pointer to variable to store the number of written bytes |
| 1677 | * @buf: the data to write |
| 1678 | */ |
| 1679 | static int __spi_nand_write(struct spi_flash_chip *chip, uint32_t to, |
| 1680 | uint32_t len, uint32_t *retlen, const uint8_t *buf, |
| 1681 | uint8_t ecc_off) |
| 1682 | { |
| 1683 | int page_addr, page_offset, size; |
| 1684 | int writelen = len; |
| 1685 | int ret = 0; |
| 1686 | uint8_t clr_cache = true; |
| 1687 | uint32_t _retlen; |
| 1688 | |
| 1689 | /* Do not allow reads past end of device */ |
| 1690 | if (to >= chip->size) { |
| 1691 | obm_printf("%s: attempt to write beyond end of device\r\n", |
| 1692 | __func__); |
| 1693 | return -EINVAL; |
| 1694 | } |
| 1695 | |
| 1696 | page_addr = to >> chip->page_shift; |
| 1697 | page_offset = to & chip->page_mask; |
| 1698 | _retlen = 0; |
| 1699 | |
| 1700 | if (ecc_off) |
| 1701 | spi_nand_disable_ecc(chip); |
| 1702 | |
| 1703 | while (1) { |
| 1704 | size = min(writelen, chip->page_size - page_offset); |
| 1705 | ret = spi_nand_do_write_page(chip, page_addr, page_offset, |
| 1706 | buf + _retlen, size, clr_cache); |
| 1707 | if (ret) { |
| 1708 | obm_printf("error %d writing page 0x%x\r\n", |
| 1709 | ret, page_addr); |
| 1710 | goto out; |
| 1711 | } |
| 1712 | |
| 1713 | _retlen += size; |
| 1714 | writelen -= size; |
| 1715 | if (!writelen) |
| 1716 | break; |
| 1717 | |
| 1718 | page_offset = 0; |
| 1719 | page_addr++; |
| 1720 | } |
| 1721 | out: |
| 1722 | *retlen = _retlen; |
| 1723 | if (ecc_off) |
| 1724 | spi_nand_enable_ecc(chip); |
| 1725 | |
| 1726 | return ret; |
| 1727 | } |
| 1728 | |
| 1729 | static int spi_nand_write(struct spi_flash_chip *chip, |
| 1730 | int addr, int size, uint8_t *wbuf) |
| 1731 | { |
| 1732 | uint32_t retlen; |
| 1733 | |
| 1734 | return __spi_nand_write(chip, addr, size, &retlen, wbuf, 0); |
| 1735 | } |
| 1736 | |
| 1737 | /** |
| 1738 | * spi_nand_erase - [Interface] erase block(s) |
| 1739 | * @chip: spi nand device structure |
| 1740 | * @addr: address that erase start with, should be blocksize aligned |
| 1741 | * @len: length that want to be erased, should be blocksize aligned |
| 1742 | * Description: |
| 1743 | * Erase one ore more blocks |
| 1744 | * The command sequence for the BLOCK ERASE operation is as follows: |
| 1745 | * 06h (WRITE ENBALE command) |
| 1746 | * D8h (BLOCK ERASE command) |
| 1747 | * 0Fh (GET FEATURES command to read the status register) |
| 1748 | */ |
| 1749 | int spi_nand_erase(struct spi_flash_chip *chip, uint32_t addr, uint32_t len) |
| 1750 | { |
| 1751 | int page_addr, pages_per_block; |
| 1752 | uint8_t status; |
| 1753 | int ret = 0; |
| 1754 | |
| 1755 | //obm_printf("%s: %d: addr=0x%x size=%d\r\n", |
| 1756 | // __func__, __LINE__, addr, len); |
| 1757 | |
| 1758 | /* check address align on block boundary */ |
| 1759 | if (addr & (chip->block_size - 1)) { |
| 1760 | obm_printf("%s: Unaligned address\r\n", __func__); |
| 1761 | return -EINVAL; |
| 1762 | } |
| 1763 | |
| 1764 | if (len & (chip->block_size - 1)) { |
| 1765 | obm_printf("%s: Length not block aligned\r\n", __func__); |
| 1766 | return -EINVAL; |
| 1767 | } |
| 1768 | |
| 1769 | /* Do not allow erase past end of device */ |
| 1770 | if ((len + addr) > chip->size) { |
| 1771 | obm_printf("%s: Erase past end of device len=%d" |
| 1772 | " addr=0x%x size=0x%x\r\n", |
| 1773 | __func__, len, addr, chip->size); |
| 1774 | return -EINVAL; |
| 1775 | } |
| 1776 | |
| 1777 | pages_per_block = 1 << (chip->block_shift - chip->page_shift); |
| 1778 | page_addr = addr >> chip->page_shift; |
| 1779 | |
| 1780 | while (len) { |
| 1781 | /* Check if we have a bad block, we do not erase bad blocks! */ |
| 1782 | // if (spi_nand_block_isbad(chip, ((uint32_t) page_addr) << |
| 1783 | // chip->page_shift)) { |
| 1784 | // obm_printf("%s: attempt to erase a bad block at 0x%x\r\n", |
| 1785 | // __func__, ((uint32_t) page_addr) << chip->page_shift); |
| 1786 | // goto erase_exit; |
| 1787 | // } |
| 1788 | spi_nand_write_enable(chip); |
| 1789 | spi_nand_erase_block(chip, page_addr); |
| 1790 | ret = spi_nand_wait(chip, &status); |
| 1791 | if (ret < 0) { |
| 1792 | obm_printf("block erase command wait failed\r\n"); |
| 1793 | goto erase_exit; |
| 1794 | } |
| 1795 | if ((status & STATUS_E_FAIL_MASK) == STATUS_E_FAIL) { |
| 1796 | obm_printf("erase block 0x%x failed\r\n", |
| 1797 | ((uint32_t) page_addr) << chip->page_shift); |
| 1798 | ret = -EIO; |
| 1799 | goto erase_exit; |
| 1800 | } |
| 1801 | |
| 1802 | /* Increment page address and decrement length */ |
| 1803 | len -= chip->block_size; |
| 1804 | page_addr += pages_per_block; |
| 1805 | } |
| 1806 | |
| 1807 | erase_exit: |
| 1808 | return ret; |
| 1809 | } |
| 1810 | |
| 1811 | /** |
| 1812 | * spi_nand_set_rd_wr_op - Chose the best read write command |
| 1813 | * @chip: SPI-NAND device structure |
| 1814 | * Description: |
| 1815 | * Chose the fastest r/w command according to spi controller's ability. |
| 1816 | * Note: |
| 1817 | * If 03h/0Bh follows SPI NAND protocol, there is no difference, |
| 1818 | * while if follows SPI NOR protocol, 03h command is working under |
| 1819 | * <=20Mhz@3.3V,<=5MHz@1.8V; 0Bh command is working under |
| 1820 | * 133Mhz@3.3v, 83Mhz@1.8V. |
| 1821 | */ |
| 1822 | static void spi_nand_set_rd_wr_op(struct spi_flash_chip *chip, |
| 1823 | uint32_t op_mode_rx, uint32_t op_mode_tx) |
| 1824 | { |
| 1825 | struct spi_nand_info *type = (struct spi_nand_info *)chip->flash_info; |
| 1826 | struct spi_flash_cmd_cfg *read_cmd; |
| 1827 | |
| 1828 | if (op_mode_rx & SPI_OPM_RX_QUAD) { |
| 1829 | if (type) { |
| 1830 | if (chip->options & SPINAND_SUPPORT_DTR) |
| 1831 | chip->read_cache_op = type->quad_cmd_dtr_index; |
| 1832 | else |
| 1833 | chip->read_cache_op = type->quad_cmd_index; |
| 1834 | } else { |
| 1835 | chip->read_cache_op = READ_FROM_CACHE_X4; |
| 1836 | } |
| 1837 | } else if (op_mode_rx & SPI_OPM_RX_DUAL) { |
| 1838 | chip->read_cache_op = READ_FROM_CACHE_DUAL; |
| 1839 | } else { |
| 1840 | if (chip->gd_ver_c) |
| 1841 | chip->read_cache_op = READ_FROM_CACHE_GD_C; |
| 1842 | else |
| 1843 | chip->read_cache_op = READ_FROM_CACHE_FAST; |
| 1844 | } |
| 1845 | |
| 1846 | if (op_mode_tx & SPI_OPM_TX_QUAD) { |
| 1847 | chip->write_cache_op = PROG_LOAD_X4; |
| 1848 | chip->write_cache_rdm_op = PROG_LOAD_RDM_DATA_X4; |
| 1849 | } else { |
| 1850 | chip->write_cache_op = PROG_LOAD; |
| 1851 | chip->write_cache_rdm_op = PROG_LOAD_RDM_DATA; |
| 1852 | } |
| 1853 | |
| 1854 | read_cmd = chip->table + chip->read_cache_op; |
| 1855 | obm_printf("Set rx_pins: %d, tx_pins: %d, Read_CMD:0x%x\r\n", |
| 1856 | op_mode_rx, op_mode_tx, read_cmd->opcode); |
| 1857 | } |
| 1858 | |
| 1859 | static int spi_nand_do_reset(FlashBootType_T fbt) |
| 1860 | { |
| 1861 | struct spi_flash_chip *chip = &nand_chip; |
| 1862 | (void) fbt; |
| 1863 | |
| 1864 | return spi_nand_reset(chip); |
| 1865 | } |
| 1866 | |
| 1867 | static int spi_nand_do_read(UINT_T FlashOffset, UINT_T buffer, UINT_T size, |
| 1868 | FlashBootType_T fbt) |
| 1869 | { |
| 1870 | struct spi_flash_chip *chip = &nand_chip; |
| 1871 | int ret; |
| 1872 | (void) fbt; |
| 1873 | |
| 1874 | ret = spi_nand_read(chip, FlashOffset, size, (UINT8_T *)buffer); |
| 1875 | if (ret == -EINVAL) { |
| 1876 | return InvalidAddressRangeError; |
| 1877 | } else if (ret == -EBADMSG) { |
| 1878 | return FlashReadEccError; |
| 1879 | } else if (ret < 0) { |
| 1880 | return ReadError; |
| 1881 | } |
| 1882 | |
| 1883 | return ret; |
| 1884 | } |
| 1885 | |
| 1886 | static int spi_nand_do_write(UINT_T FlashOffset, UINT_T buffer, UINT_T size, |
| 1887 | FlashBootType_T fbt) |
| 1888 | { |
| 1889 | struct spi_flash_chip *chip = &nand_chip; |
| 1890 | int ret; |
| 1891 | (void) fbt; |
| 1892 | |
| 1893 | ret = spi_nand_write(chip, FlashOffset, size, (UINT8_T *)buffer); |
| 1894 | if (ret == -EINVAL) { |
| 1895 | return InvalidAddressRangeError; |
| 1896 | } else if (ret < 0) { |
| 1897 | return WriteError; |
| 1898 | } |
| 1899 | |
| 1900 | return ret; |
| 1901 | } |
| 1902 | |
| 1903 | static int spi_nand_do_erase(UINT_T FlashOffset, UINT_T size, |
| 1904 | FlashBootType_T fbt) |
| 1905 | { |
| 1906 | struct spi_flash_chip *chip = &nand_chip; |
| 1907 | int ret; |
| 1908 | (void) fbt; |
| 1909 | |
| 1910 | ret = spi_nand_erase(chip, FlashOffset, size); |
| 1911 | if (ret == -EINVAL) { |
| 1912 | return InvalidAddressRangeError; |
| 1913 | } else if (ret < 0) { |
| 1914 | return EraseError; |
| 1915 | } |
| 1916 | |
| 1917 | return ret; |
| 1918 | } |
| 1919 | |
| 1920 | static int spi_nand_gen_fbbt(UINT16 *badblocklist) |
| 1921 | { |
| 1922 | struct spi_flash_chip *chip = &nand_chip; |
| 1923 | P_FlashProperties_T pFlashP = GetFlashProperties(BOOT_FLASH); |
| 1924 | unsigned int i, max, bad_blks = 0; |
| 1925 | uint8_t *buf = NULL; |
| 1926 | uint32_t corrected = 0; |
| 1927 | int page_addr, ret; |
| 1928 | |
| 1929 | buf = malloc(chip->block_size); |
| 1930 | if(buf == NULL) |
| 1931 | return HeapExhaustedError; |
| 1932 | |
| 1933 | /* Max number of relocation is 2% of device */ |
| 1934 | max = (pFlashP->NumBlocks * LEGACY_BBM_RELOC_PERCENTAGE + 99) / 100; |
| 1935 | |
| 1936 | /* Scan from block 1, block 0 is for TIM, should be good */ |
| 1937 | for (i = 1; i < pFlashP->NumBlocks; i++) { |
| 1938 | page_addr = i << (chip->block_shift - chip->page_shift); |
| 1939 | ret = spi_nand_do_read_page(chip, page_addr, chip->page_size, |
| 1940 | 1, &corrected, buf, chip->oob_size); |
| 1941 | if (ret) { |
| 1942 | obm_printf("error %d reading page 0x%x\r\n", |
| 1943 | ret, page_addr); |
| 1944 | break; |
| 1945 | } |
| 1946 | |
| 1947 | if ((buf[0] & 0xFF) != 0xFF) { |
| 1948 | badblocklist[bad_blks++] = i; |
| 1949 | if(bad_blks >= max) |
| 1950 | break; |
| 1951 | } |
| 1952 | } |
| 1953 | |
| 1954 | if (bad_blks) { |
| 1955 | obm_printf("Scan factory bad blocks:\r\n"); |
| 1956 | for (i = 0; i < bad_blks;) { |
| 1957 | obm_printf("%-4d ", badblocklist[i]); |
| 1958 | if (++i % 8 == 0) |
| 1959 | obm_printf("\r\n"); |
| 1960 | } |
| 1961 | obm_printf("\r\n"); |
| 1962 | } |
| 1963 | |
| 1964 | free(buf); |
| 1965 | return bad_blks; |
| 1966 | } |
| 1967 | |
| 1968 | static int spi_nand_exit() |
| 1969 | { |
| 1970 | struct spi_flash_chip *chip = &nand_chip; |
| 1971 | |
| 1972 | #ifdef QSPI_SUPPORT_DQS |
| 1973 | qspi_config_disable_dqs(chip->host); |
| 1974 | #endif |
| 1975 | return NoError; |
| 1976 | } |
| 1977 | |
| 1978 | /** |
| 1979 | * spi_nand_init - [Interface] Init SPI-NAND device driver |
| 1980 | * @spi: spi device structure |
| 1981 | * @chip_ptr: pointer point to spi nand device structure pointer |
| 1982 | */ |
| 1983 | struct spi_flash_chip *spi_nand_init(struct qspi_host *host, int cs, |
| 1984 | int rx_mode, int tx_mode) |
| 1985 | { |
| 1986 | uint8_t id[SPINAND_MAX_ID_LEN] = {0}; |
| 1987 | struct spi_flash_chip *chip = &nand_chip; |
| 1988 | int dev_in_tbl = 0; |
| 1989 | |
| 1990 | memset(chip, 0, sizeof(struct spi_flash_chip)); |
| 1991 | chip->cs = cs >= QSPI_CS_MAX ? QSPI_CS_A1 : cs; |
| 1992 | chip->host = host; |
| 1993 | chip->table = cmd_table; |
| 1994 | chip->page_size = 2048; |
| 1995 | chip->block_size = 64*2048; |
| 1996 | chip->size = 1024*64*2048; |
| 1997 | |
| 1998 | spi_nand_reset(chip); |
| 1999 | spi_nand_read_id(chip, id); |
| 2000 | |
| 2001 | /* First check if this is a GD version-C spi-nand */ |
| 2002 | if (id[0] == SPIFLASH_MFR_GIGADEVICE) { |
| 2003 | chip->mfr_id = id[0]; |
| 2004 | chip->dev_id = id[1] << 8 | id[2]; |
| 2005 | chip->gd_ver_c = 1; |
| 2006 | obm_printf("GigaDeivce version-C spi-nand\r\n"); |
| 2007 | /* For debug, change to 1-bit read/write command*/ |
| 2008 | rx_mode = SPI_OPM_RX; |
| 2009 | tx_mode = SPI_OPM_TX; |
| 2010 | chip->max_mhz = 13; |
| 2011 | } else { |
| 2012 | chip->mfr_id = id[1]; |
| 2013 | chip->dev_id = id[2] | id[3] << 8; |
| 2014 | chip->gd_ver_c = 0; |
| 2015 | } |
| 2016 | |
| 2017 | if (spi_nand_scan_id_table(chip)) |
| 2018 | dev_in_tbl = 1; |
| 2019 | |
| 2020 | obm_printf("SPI-NAND type mfr_id: %x, dev_id: %x\r\n", |
| 2021 | chip->mfr_id, chip->dev_id); |
| 2022 | |
| 2023 | if (!chip->host->has_dtr) |
| 2024 | chip->options &= ~SPINAND_SUPPORT_DTR; |
| 2025 | |
| 2026 | spi_nand_set_rd_wr_op(chip, rx_mode, tx_mode); |
| 2027 | |
| 2028 | /* Not every vendor show QE bit in CFG register */ |
| 2029 | if (chip->mfr_id != SPIFLASH_MFR_MICRON && |
| 2030 | chip->mfr_id != SPIFLASH_MFR_WINBOND) |
| 2031 | spi_nand_enable_quad(chip); |
| 2032 | /* |
| 2033 | * TODO: |
| 2034 | * Maybe change due to different vendor |
| 2035 | */ |
| 2036 | qspi_enable_xip(chip, chip->table + chip->read_cache_op); |
| 2037 | if (qspi_preinit_lookup_tbl(chip) < 0) { |
| 2038 | obm_printf("preinit_lookup_tbl failed, check cmd table\r\n"); |
| 2039 | return NULL; |
| 2040 | } |
| 2041 | |
| 2042 | if (chip->options & SPINAND_NEED_SET_BFT) |
| 2043 | mxic_spi_nand_set_bft(chip, chip->refresh_threshold); |
| 2044 | |
| 2045 | spi_nand_disable_ecc(chip); |
| 2046 | if(!dev_in_tbl) { |
| 2047 | /* |
| 2048 | * Since Program Load Random Data cmd only valid for some vendor's |
| 2049 | * device, set below flag for new device unknown in table for safe. |
| 2050 | */ |
| 2051 | chip->options |= SPINAND_RDM_CMD_NEED_PAGE_READ; |
| 2052 | obm_printf("unknown device, set SPINAND_RDM_CMD_NEED_PAGE_READ\r\n"); |
| 2053 | if (spi_nand_detect_onfi(chip)) |
| 2054 | obm_printf("nand support onfi\r\n"); |
| 2055 | |
| 2056 | chip->get_ecc_status = generic_spi_nand_ecc_status; |
| 2057 | } |
| 2058 | |
| 2059 | spi_nand_lock_block(chip, BL_ALL_UNLOCKED); |
| 2060 | spi_nand_enable_ecc(chip); |
| 2061 | |
| 2062 | chip->block_shift = ilog2(chip->block_size); |
| 2063 | chip->page_shift = ilog2(chip->page_size); |
| 2064 | chip->page_mask = chip->page_size - 1; |
| 2065 | chip->lun = 0; |
| 2066 | chip->page_mask = chip->page_size - 1; |
| 2067 | if (!chip->enable_ecc) |
| 2068 | chip->enable_ecc = spi_nand_enable_ecc; |
| 2069 | if (!chip->disable_ecc) |
| 2070 | chip->disable_ecc = spi_nand_disable_ecc; |
| 2071 | |
| 2072 | obm_printf("block_size=0x%x page_size=0x%x bitflip_threshold=%d\r\n", |
| 2073 | chip->block_size, chip->page_size, chip->refresh_threshold); |
| 2074 | return chip; |
| 2075 | } |
| 2076 | |
| 2077 | #ifdef SPI_FLASH_DQS_SCAN_WIN |
| 2078 | static void spi_nand_scan_dqs_window(struct spi_flash_chip *chip, int freq) |
| 2079 | { |
| 2080 | struct qspi_host *host = chip->host; |
| 2081 | uint8_t *buffer = NULL; |
| 2082 | uint8_t pattern = 0x5a; |
| 2083 | int flash_offset = 0x0; |
| 2084 | int min, max; |
| 2085 | int len = 0, avg, i; |
| 2086 | int fail; |
| 2087 | |
| 2088 | if (chip->options & SPINAND_SUPPORT_DTR) { |
| 2089 | obm_printf("==> DQS not support DTR mode\r\n"); |
| 2090 | return; |
| 2091 | } |
| 2092 | |
| 2093 | buffer = malloc(chip->page_size); |
| 2094 | memset(buffer, pattern, chip->page_size); |
| 2095 | |
| 2096 | /* switch qspi bus to 13M to write pattern data */ |
| 2097 | qspi_set_func_clk(host, 13, 0, 0, 0, 0); |
| 2098 | |
| 2099 | spi_nand_do_erase(flash_offset, chip->block_size, BOOT_FLASH); |
| 2100 | spi_nand_do_write(flash_offset, buffer, chip->page_size, BOOT_FLASH); |
| 2101 | |
| 2102 | /* switch qspi bus back to high frequency to scan valid window */ |
| 2103 | qspi_set_func_clk(host, freq, 0, 0, 0, 0); |
| 2104 | |
| 2105 | min = 0; |
| 2106 | len = 0; |
| 2107 | do { |
| 2108 | while (min <= 0xff) { |
| 2109 | memset(buffer, 0x0, chip->page_size); |
| 2110 | qspi_config_dqs_delay(chip->host, min); |
| 2111 | spi_nand_do_read(flash_offset, buffer, chip->page_size, BOOT_FLASH); |
| 2112 | |
| 2113 | fail = 0; |
| 2114 | for (i = 0; i < chip->page_size; i++) { |
| 2115 | if (buffer[i] != pattern) { |
| 2116 | fail = 1; |
| 2117 | break; |
| 2118 | } |
| 2119 | } |
| 2120 | |
| 2121 | if (!fail) |
| 2122 | break; |
| 2123 | |
| 2124 | min++; |
| 2125 | } |
| 2126 | |
| 2127 | if (min > 0xff) |
| 2128 | break; |
| 2129 | |
| 2130 | max = min + 1; |
| 2131 | while (max <= 0xff) { |
| 2132 | memset(buffer, 0x0, chip->page_size); |
| 2133 | qspi_config_dqs_delay(chip->host, max); |
| 2134 | spi_nand_do_read(flash_offset, buffer, chip->page_size, BOOT_FLASH); |
| 2135 | |
| 2136 | fail = 0; |
| 2137 | for (i = 0; i < chip->page_size; i++) { |
| 2138 | if (buffer[i] != pattern) { |
| 2139 | fail = 1; |
| 2140 | break; |
| 2141 | } |
| 2142 | } |
| 2143 | |
| 2144 | if (fail) |
| 2145 | break; |
| 2146 | |
| 2147 | max++; |
| 2148 | } |
| 2149 | |
| 2150 | if (max - min > len) { |
| 2151 | len = max - min; |
| 2152 | avg = (min + max - 1) / 2; |
| 2153 | } |
| 2154 | |
| 2155 | obm_printf("pass window [%d : %d], len = %d\r\n", |
| 2156 | min, max - 1, max - min); |
| 2157 | min = max + 1; |
| 2158 | } while (min <= 0xff); |
| 2159 | |
| 2160 | obm_printf("max window len = %d, at %d\r\n", len, avg); |
| 2161 | qspi_config_dqs_delay(chip->host, avg); |
| 2162 | |
| 2163 | free(buffer); |
| 2164 | } |
| 2165 | #endif |
| 2166 | |
| 2167 | /*********************************************************** |
| 2168 | * InitializeQSPINAND() |
| 2169 | * Initializes the SSP port on the platform and issues |
| 2170 | * a Read ID command to see if a device is connected |
| 2171 | * returns: |
| 2172 | * NoError - on a successful read ID |
| 2173 | * NotFoundError - when read ID value is bogus (0xFFFF or 0x0000) |
| 2174 | ************************************************************/ |
| 2175 | UINT_T InitializeQSPINAND(UINT8_T FlashNum, FlashBootType_T FlashBootType, |
| 2176 | UINT8_T* P_DefaultPartitionNum) |
| 2177 | { |
| 2178 | P_FlashProperties_T pFlashP = GetFlashProperties(FlashBootType); |
| 2179 | struct spi_flash_chip *chip; |
| 2180 | struct qspi_host *host; |
| 2181 | struct spi_nand_info *type; |
| 2182 | int tclqv, tset, thold; |
| 2183 | #if KSTR |
| 2184 | int freq = 52; |
| 2185 | #else |
| 2186 | int freq = 78; |
| 2187 | #endif |
| 2188 | |
| 2189 | /* |
| 2190 | * Use low frequency during init and increase later, since some |
| 2191 | * device max may not support such large frequency |
| 2192 | */ |
| 2193 | host = qspi_host_init(0, 13, 1); |
| 2194 | chip = spi_nand_init(host, 0, SPI_OPM_RX_QUAD, SPI_OPM_TX_QUAD); |
| 2195 | if (!chip) |
| 2196 | return NotFoundError; |
| 2197 | |
| 2198 | if (chip->max_mhz && freq > chip->max_mhz) { |
| 2199 | obm_printf("warn: device max frequency is %d MHz!!!\r\n", |
| 2200 | chip->max_mhz); |
| 2201 | freq = chip->max_mhz; |
| 2202 | } |
| 2203 | |
| 2204 | /* flash_info may be inited in spi_nand_init */ |
| 2205 | type = (struct spi_nand_info *)chip->flash_info; |
| 2206 | if (type) { |
| 2207 | tclqv = type->tclqv; |
| 2208 | tset = type->tset; |
| 2209 | thold = type->thold; |
| 2210 | } else { |
| 2211 | tclqv = 0; |
| 2212 | tset = 0; |
| 2213 | thold = 0; |
| 2214 | } |
| 2215 | |
| 2216 | if (!PlatformIsFPGA()) |
| 2217 | qspi_set_func_clk(host, freq, |
| 2218 | (chip->options & SPINAND_SUPPORT_DTR), |
| 2219 | tclqv, tset, thold); |
| 2220 | |
| 2221 | //define functions |
| 2222 | pFlashP->ReadFromFlash = spi_nand_do_read; |
| 2223 | pFlashP->WriteToFlash = spi_nand_do_write; |
| 2224 | pFlashP->EraseFlash = spi_nand_do_erase; |
| 2225 | pFlashP->FinalizeFlash = spi_nand_exit; |
| 2226 | pFlashP->ResetFlash = spi_nand_do_reset; |
| 2227 | pFlashP->GenerateFBBT = spi_nand_gen_fbbt; |
| 2228 | pFlashP->FlashSettings.UseBBM = 1; |
| 2229 | pFlashP->FlashSettings.UseSpareArea = 0; |
| 2230 | pFlashP->PageSize = chip->page_size; |
| 2231 | pFlashP->BlockSize = chip->block_size; |
| 2232 | pFlashP->FlashSettings.SASize = chip->oob_size; |
| 2233 | pFlashP->FlashSettings.UseHwEcc = 0; |
| 2234 | pFlashP->StreamingFlash = FALSE; |
| 2235 | pFlashP->FlashType = SPI_NAND; |
| 2236 | pFlashP->FinalizeFlash = NULL; |
| 2237 | pFlashP->TimFlashAddress = 0; |
| 2238 | pFlashP->NumBlocks = chip->size >> chip->block_shift; |
| 2239 | |
| 2240 | if (!host->en_tx_dma) |
| 2241 | chip->tx_max_len = QSPI_TX_BUFF_MAX << 2; |
| 2242 | else |
| 2243 | chip->tx_max_len = chip->page_size; |
| 2244 | |
| 2245 | if (chip->host->xip_read) |
| 2246 | chip->rx_max_len = chip->page_size; |
| 2247 | else |
| 2248 | chip->rx_max_len = QSPI_RX_BUFF_MAX << 2; |
| 2249 | |
| 2250 | *P_DefaultPartitionNum = 0; |
| 2251 | obm_printf("pFlashP->NumBlocks: %d\r\n", pFlashP->NumBlocks); |
| 2252 | |
| 2253 | #ifdef SPI_FLASH_DQS_SCAN_WIN |
| 2254 | spi_nand_scan_dqs_window(chip, freq); |
| 2255 | #endif |
| 2256 | return NoError; |
| 2257 | } |