1 // SPDX-License-Identifier: GPL-2.0
4 * Chuanhong Guo <gch981213@gmail.com>
7 #include <linux/device.h>
8 #include <linux/kernel.h>
9 #include <linux/mtd/spinand.h>
11 #define SPINAND_MFR_GIGADEVICE 0xC8
13 #define GD5FXGQ4XA_STATUS_ECC_1_7_BITFLIPS (1 << 4)
14 #define GD5FXGQ4XA_STATUS_ECC_8_BITFLIPS (3 << 4)
16 #define GD5FXGQ5XE_STATUS_ECC_1_4_BITFLIPS (1 << 4)
17 #define GD5FXGQ5XE_STATUS_ECC_4_BITFLIPS (3 << 4)
19 #define GD5FXGQXXEXXG_REG_STATUS2 0xf0
21 #define GD5FXGQ4UXFXXG_STATUS_ECC_MASK (7 << 4)
22 #define GD5FXGQ4UXFXXG_STATUS_ECC_NO_BITFLIPS (0 << 4)
23 #define GD5FXGQ4UXFXXG_STATUS_ECC_1_3_BITFLIPS (1 << 4)
24 #define GD5FXGQ4UXFXXG_STATUS_ECC_UNCOR_ERROR (7 << 4)
26 static SPINAND_OP_VARIANTS(read_cache_variants,
27 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 1, NULL, 0),
28 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
29 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
30 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
31 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
32 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
34 static SPINAND_OP_VARIANTS(read_cache_variants_f,
35 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 1, NULL, 0),
36 SPINAND_PAGE_READ_FROM_CACHE_X4_OP_3A(0, 1, NULL, 0),
37 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
38 SPINAND_PAGE_READ_FROM_CACHE_X2_OP_3A(0, 1, NULL, 0),
39 SPINAND_PAGE_READ_FROM_CACHE_OP_3A(true, 0, 1, NULL, 0),
40 SPINAND_PAGE_READ_FROM_CACHE_OP_3A(false, 0, 0, NULL, 0));
42 static SPINAND_OP_VARIANTS(read_cache_variants_1gq5,
43 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0),
44 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
45 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
46 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
47 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
48 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
50 static SPINAND_OP_VARIANTS(read_cache_variants_2gq5,
51 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 4, NULL, 0),
52 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
53 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 2, NULL, 0),
54 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
55 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
56 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
58 static SPINAND_OP_VARIANTS(write_cache_variants,
59 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
60 SPINAND_PROG_LOAD(true, 0, NULL, 0));
62 static SPINAND_OP_VARIANTS(update_cache_variants,
63 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
64 SPINAND_PROG_LOAD(false, 0, NULL, 0));
66 static int gd5fxgq4xa_ooblayout_ecc(struct mtd_info *mtd, int section,
67 struct mtd_oob_region *region)
72 region->offset = (16 * section) + 8;
78 static int gd5fxgq4xa_ooblayout_free(struct mtd_info *mtd, int section,
79 struct mtd_oob_region *region)
85 region->offset = 16 * section;
88 /* section 0 has one byte reserved for bad block mark */
95 static const struct mtd_ooblayout_ops gd5fxgq4xa_ooblayout = {
96 .ecc = gd5fxgq4xa_ooblayout_ecc,
97 .free = gd5fxgq4xa_ooblayout_free,
100 static int gd5fxgq4xa_ecc_get_status(struct spinand_device *spinand,
103 switch (status & STATUS_ECC_MASK) {
104 case STATUS_ECC_NO_BITFLIPS:
107 case GD5FXGQ4XA_STATUS_ECC_1_7_BITFLIPS:
108 /* 1-7 bits are flipped. return the maximum. */
111 case GD5FXGQ4XA_STATUS_ECC_8_BITFLIPS:
114 case STATUS_ECC_UNCOR_ERROR:
124 static int gd5fxgqx_variant2_ooblayout_ecc(struct mtd_info *mtd, int section,
125 struct mtd_oob_region *region)
136 static int gd5fxgqx_variant2_ooblayout_free(struct mtd_info *mtd, int section,
137 struct mtd_oob_region *region)
142 /* Reserve 1 bytes for the BBM. */
149 /* Valid for Q4/Q5 and Q6 (untested) devices */
150 static const struct mtd_ooblayout_ops gd5fxgqx_variant2_ooblayout = {
151 .ecc = gd5fxgqx_variant2_ooblayout_ecc,
152 .free = gd5fxgqx_variant2_ooblayout_free,
155 static int gd5fxgq4xc_ooblayout_256_ecc(struct mtd_info *mtd, int section,
156 struct mtd_oob_region *oobregion)
161 oobregion->offset = 128;
162 oobregion->length = 128;
167 static int gd5fxgq4xc_ooblayout_256_free(struct mtd_info *mtd, int section,
168 struct mtd_oob_region *oobregion)
173 oobregion->offset = 1;
174 oobregion->length = 127;
179 static const struct mtd_ooblayout_ops gd5fxgq4xc_oob_256_ops = {
180 .ecc = gd5fxgq4xc_ooblayout_256_ecc,
181 .free = gd5fxgq4xc_ooblayout_256_free,
184 static int gd5fxgq4uexxg_ecc_get_status(struct spinand_device *spinand,
188 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(GD5FXGQXXEXXG_REG_STATUS2,
192 switch (status & STATUS_ECC_MASK) {
193 case STATUS_ECC_NO_BITFLIPS:
196 case GD5FXGQ4XA_STATUS_ECC_1_7_BITFLIPS:
198 * Read status2 register to determine a more fine grained
201 ret = spi_mem_exec_op(spinand->spimem, &op);
206 * 4 ... 7 bits are flipped (1..4 can't be detected, so
207 * report the maximum of 4 in this case
209 /* bits sorted this way (3...0): ECCS1,ECCS0,ECCSE1,ECCSE0 */
210 return ((status & STATUS_ECC_MASK) >> 2) |
211 ((status2 & STATUS_ECC_MASK) >> 4);
213 case GD5FXGQ4XA_STATUS_ECC_8_BITFLIPS:
216 case STATUS_ECC_UNCOR_ERROR:
226 static int gd5fxgq5xexxg_ecc_get_status(struct spinand_device *spinand,
230 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(GD5FXGQXXEXXG_REG_STATUS2,
234 switch (status & STATUS_ECC_MASK) {
235 case STATUS_ECC_NO_BITFLIPS:
238 case GD5FXGQ5XE_STATUS_ECC_1_4_BITFLIPS:
240 * Read status2 register to determine a more fine grained
243 ret = spi_mem_exec_op(spinand->spimem, &op);
248 * 1 ... 4 bits are flipped (and corrected)
250 /* bits sorted this way (1...0): ECCSE1, ECCSE0 */
251 return ((status2 & STATUS_ECC_MASK) >> 4) + 1;
253 case STATUS_ECC_UNCOR_ERROR:
263 static int gd5fxgq4ufxxg_ecc_get_status(struct spinand_device *spinand,
266 switch (status & GD5FXGQ4UXFXXG_STATUS_ECC_MASK) {
267 case GD5FXGQ4UXFXXG_STATUS_ECC_NO_BITFLIPS:
270 case GD5FXGQ4UXFXXG_STATUS_ECC_1_3_BITFLIPS:
273 case GD5FXGQ4UXFXXG_STATUS_ECC_UNCOR_ERROR:
276 default: /* (2 << 4) through (6 << 4) are 4-8 corrected errors */
277 return ((status & GD5FXGQ4UXFXXG_STATUS_ECC_MASK) >> 4) + 2;
283 static const struct spinand_info gigadevice_spinand_table[] = {
284 SPINAND_INFO("GD5F1GQ4xA",
285 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xf1),
286 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
288 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
289 &write_cache_variants,
290 &update_cache_variants),
292 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
293 gd5fxgq4xa_ecc_get_status)),
294 SPINAND_INFO("GD5F2GQ4xA",
295 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xf2),
296 NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
298 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
299 &write_cache_variants,
300 &update_cache_variants),
302 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
303 gd5fxgq4xa_ecc_get_status)),
304 SPINAND_INFO("GD5F4GQ4xA",
305 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xf4),
306 NAND_MEMORG(1, 2048, 64, 64, 4096, 80, 1, 1, 1),
308 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
309 &write_cache_variants,
310 &update_cache_variants),
312 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
313 gd5fxgq4xa_ecc_get_status)),
314 SPINAND_INFO("GD5F4GQ4RC",
315 SPINAND_ID(SPINAND_READID_METHOD_OPCODE, 0xa4, 0x68),
316 NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
318 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_f,
319 &write_cache_variants,
320 &update_cache_variants),
322 SPINAND_ECCINFO(&gd5fxgq4xc_oob_256_ops,
323 gd5fxgq4ufxxg_ecc_get_status)),
324 SPINAND_INFO("GD5F4GQ4UC",
325 SPINAND_ID(SPINAND_READID_METHOD_OPCODE, 0xb4, 0x68),
326 NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
328 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_f,
329 &write_cache_variants,
330 &update_cache_variants),
332 SPINAND_ECCINFO(&gd5fxgq4xc_oob_256_ops,
333 gd5fxgq4ufxxg_ecc_get_status)),
334 SPINAND_INFO("GD5F1GQ4UExxG",
335 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xd1),
336 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
338 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
339 &write_cache_variants,
340 &update_cache_variants),
342 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
343 gd5fxgq4uexxg_ecc_get_status)),
344 SPINAND_INFO("GD5F1GQ4RExxG",
345 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xc1),
346 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
348 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
349 &write_cache_variants,
350 &update_cache_variants),
352 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
353 gd5fxgq4uexxg_ecc_get_status)),
354 SPINAND_INFO("GD5F2GQ4UExxG",
355 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xd2),
356 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
358 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
359 &write_cache_variants,
360 &update_cache_variants),
362 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
363 gd5fxgq4uexxg_ecc_get_status)),
364 SPINAND_INFO("GD5F2GQ4RExxG",
365 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xc2),
366 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
368 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
369 &write_cache_variants,
370 &update_cache_variants),
372 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
373 gd5fxgq4uexxg_ecc_get_status)),
374 SPINAND_INFO("GD5F1GQ4UFxxG",
375 SPINAND_ID(SPINAND_READID_METHOD_OPCODE, 0xb1, 0x48),
376 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
378 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_f,
379 &write_cache_variants,
380 &update_cache_variants),
382 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
383 gd5fxgq4ufxxg_ecc_get_status)),
384 SPINAND_INFO("GD5F1GQ5UExxG",
385 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x51),
386 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
388 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
389 &write_cache_variants,
390 &update_cache_variants),
392 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
393 gd5fxgq5xexxg_ecc_get_status)),
394 SPINAND_INFO("GD5F1GQ5RExxG",
395 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x41),
396 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
398 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
399 &write_cache_variants,
400 &update_cache_variants),
402 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
403 gd5fxgq5xexxg_ecc_get_status)),
404 SPINAND_INFO("GD5F2GQ5UExxG",
405 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x52),
406 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
408 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_2gq5,
409 &write_cache_variants,
410 &update_cache_variants),
412 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
413 gd5fxgq5xexxg_ecc_get_status)),
414 SPINAND_INFO("GD5F2GQ5RExxG",
415 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x42),
416 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
418 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_2gq5,
419 &write_cache_variants,
420 &update_cache_variants),
422 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
423 gd5fxgq5xexxg_ecc_get_status)),
424 SPINAND_INFO("GD5F4GQ6UExxG",
425 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x55),
426 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 2, 1),
428 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_2gq5,
429 &write_cache_variants,
430 &update_cache_variants),
432 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
433 gd5fxgq5xexxg_ecc_get_status)),
434 SPINAND_INFO("GD5F4GQ6RExxG",
435 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x45),
436 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 2, 1),
438 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_2gq5,
439 &write_cache_variants,
440 &update_cache_variants),
442 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
443 gd5fxgq5xexxg_ecc_get_status)),
444 SPINAND_INFO("GD5F1GM7UExxG",
445 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x91),
446 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
448 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
449 &write_cache_variants,
450 &update_cache_variants),
452 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
453 gd5fxgq4uexxg_ecc_get_status)),
454 SPINAND_INFO("GD5F1GM7RExxG",
455 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x81),
456 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
458 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
459 &write_cache_variants,
460 &update_cache_variants),
462 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
463 gd5fxgq4uexxg_ecc_get_status)),
464 SPINAND_INFO("GD5F2GM7UExxG",
465 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x92),
466 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
468 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
469 &write_cache_variants,
470 &update_cache_variants),
472 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
473 gd5fxgq4uexxg_ecc_get_status)),
474 SPINAND_INFO("GD5F2GM7RExxG",
475 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x82),
476 NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
478 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
479 &write_cache_variants,
480 &update_cache_variants),
482 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
483 gd5fxgq4uexxg_ecc_get_status)),
484 SPINAND_INFO("GD5F4GM8UExxG",
485 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x95),
486 NAND_MEMORG(1, 2048, 128, 64, 4096, 80, 1, 1, 1),
488 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
489 &write_cache_variants,
490 &update_cache_variants),
492 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
493 gd5fxgq4uexxg_ecc_get_status)),
494 SPINAND_INFO("GD5F4GM8RExxG",
495 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x85),
496 NAND_MEMORG(1, 2048, 128, 64, 4096, 80, 1, 1, 1),
498 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_1gq5,
499 &write_cache_variants,
500 &update_cache_variants),
502 SPINAND_ECCINFO(&gd5fxgqx_variant2_ooblayout,
503 gd5fxgq4uexxg_ecc_get_status)),
506 static const struct spinand_manufacturer_ops gigadevice_spinand_manuf_ops = {
509 const struct spinand_manufacturer gigadevice_spinand_manufacturer = {
510 .id = SPINAND_MFR_GIGADEVICE,
511 .name = "GigaDevice",
512 .chips = gigadevice_spinand_table,
513 .nchips = ARRAY_SIZE(gigadevice_spinand_table),
514 .ops = &gigadevice_spinand_manuf_ops,