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