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 GD5FXGQ4UEXXG_REG_STATUS2 0xf0
18 #define GD5FXGQ4UXFXXG_STATUS_ECC_MASK (7 << 4)
19 #define GD5FXGQ4UXFXXG_STATUS_ECC_NO_BITFLIPS (0 << 4)
20 #define GD5FXGQ4UXFXXG_STATUS_ECC_1_3_BITFLIPS (1 << 4)
21 #define GD5FXGQ4UXFXXG_STATUS_ECC_UNCOR_ERROR (7 << 4)
23 static SPINAND_OP_VARIANTS(read_cache_variants,
24 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 1, NULL, 0),
25 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
26 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
27 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
28 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
29 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
31 static SPINAND_OP_VARIANTS(read_cache_variants_f,
32 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 1, NULL, 0),
33 SPINAND_PAGE_READ_FROM_CACHE_X4_OP_3A(0, 1, NULL, 0),
34 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
35 SPINAND_PAGE_READ_FROM_CACHE_X2_OP_3A(0, 1, NULL, 0),
36 SPINAND_PAGE_READ_FROM_CACHE_OP_3A(true, 0, 1, NULL, 0),
37 SPINAND_PAGE_READ_FROM_CACHE_OP_3A(false, 0, 0, NULL, 0));
39 static SPINAND_OP_VARIANTS(write_cache_variants,
40 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
41 SPINAND_PROG_LOAD(true, 0, NULL, 0));
43 static SPINAND_OP_VARIANTS(update_cache_variants,
44 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
45 SPINAND_PROG_LOAD(false, 0, NULL, 0));
47 static int gd5fxgq4xa_ooblayout_ecc(struct mtd_info *mtd, int section,
48 struct mtd_oob_region *region)
53 region->offset = (16 * section) + 8;
59 static int gd5fxgq4xa_ooblayout_free(struct mtd_info *mtd, int section,
60 struct mtd_oob_region *region)
66 region->offset = 16 * section;
69 /* section 0 has one byte reserved for bad block mark */
76 static const struct mtd_ooblayout_ops gd5fxgq4xa_ooblayout = {
77 .ecc = gd5fxgq4xa_ooblayout_ecc,
78 .free = gd5fxgq4xa_ooblayout_free,
81 static int gd5fxgq4xa_ecc_get_status(struct spinand_device *spinand,
84 switch (status & STATUS_ECC_MASK) {
85 case STATUS_ECC_NO_BITFLIPS:
88 case GD5FXGQ4XA_STATUS_ECC_1_7_BITFLIPS:
89 /* 1-7 bits are flipped. return the maximum. */
92 case GD5FXGQ4XA_STATUS_ECC_8_BITFLIPS:
95 case STATUS_ECC_UNCOR_ERROR:
105 static int gd5fxgq4_variant2_ooblayout_ecc(struct mtd_info *mtd, int section,
106 struct mtd_oob_region *region)
117 static int gd5fxgq4_variant2_ooblayout_free(struct mtd_info *mtd, int section,
118 struct mtd_oob_region *region)
123 /* Reserve 1 bytes for the BBM. */
130 static const struct mtd_ooblayout_ops gd5fxgq4_variant2_ooblayout = {
131 .ecc = gd5fxgq4_variant2_ooblayout_ecc,
132 .free = gd5fxgq4_variant2_ooblayout_free,
135 static int gd5fxgq4xc_ooblayout_256_ecc(struct mtd_info *mtd, int section,
136 struct mtd_oob_region *oobregion)
141 oobregion->offset = 128;
142 oobregion->length = 128;
147 static int gd5fxgq4xc_ooblayout_256_free(struct mtd_info *mtd, int section,
148 struct mtd_oob_region *oobregion)
153 oobregion->offset = 1;
154 oobregion->length = 127;
159 static const struct mtd_ooblayout_ops gd5fxgq4xc_oob_256_ops = {
160 .ecc = gd5fxgq4xc_ooblayout_256_ecc,
161 .free = gd5fxgq4xc_ooblayout_256_free,
164 static int gd5fxgq4uexxg_ecc_get_status(struct spinand_device *spinand,
168 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(GD5FXGQ4UEXXG_REG_STATUS2,
172 switch (status & STATUS_ECC_MASK) {
173 case STATUS_ECC_NO_BITFLIPS:
176 case GD5FXGQ4XA_STATUS_ECC_1_7_BITFLIPS:
178 * Read status2 register to determine a more fine grained
181 ret = spi_mem_exec_op(spinand->spimem, &op);
186 * 4 ... 7 bits are flipped (1..4 can't be detected, so
187 * report the maximum of 4 in this case
189 /* bits sorted this way (3...0): ECCS1,ECCS0,ECCSE1,ECCSE0 */
190 return ((status & STATUS_ECC_MASK) >> 2) |
191 ((status2 & STATUS_ECC_MASK) >> 4);
193 case GD5FXGQ4XA_STATUS_ECC_8_BITFLIPS:
196 case STATUS_ECC_UNCOR_ERROR:
206 static int gd5fxgq4ufxxg_ecc_get_status(struct spinand_device *spinand,
209 switch (status & GD5FXGQ4UXFXXG_STATUS_ECC_MASK) {
210 case GD5FXGQ4UXFXXG_STATUS_ECC_NO_BITFLIPS:
213 case GD5FXGQ4UXFXXG_STATUS_ECC_1_3_BITFLIPS:
216 case GD5FXGQ4UXFXXG_STATUS_ECC_UNCOR_ERROR:
219 default: /* (2 << 4) through (6 << 4) are 4-8 corrected errors */
220 return ((status & GD5FXGQ4UXFXXG_STATUS_ECC_MASK) >> 4) + 2;
226 static const struct spinand_info gigadevice_spinand_table[] = {
227 SPINAND_INFO("GD5F1GQ4xA",
228 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xf1),
229 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
231 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
232 &write_cache_variants,
233 &update_cache_variants),
235 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
236 gd5fxgq4xa_ecc_get_status)),
237 SPINAND_INFO("GD5F2GQ4xA",
238 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xf2),
239 NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
241 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
242 &write_cache_variants,
243 &update_cache_variants),
245 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
246 gd5fxgq4xa_ecc_get_status)),
247 SPINAND_INFO("GD5F4GQ4xA",
248 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xf4),
249 NAND_MEMORG(1, 2048, 64, 64, 4096, 80, 1, 1, 1),
251 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
252 &write_cache_variants,
253 &update_cache_variants),
255 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
256 gd5fxgq4xa_ecc_get_status)),
257 SPINAND_INFO("GD5F4GQ4RC",
258 SPINAND_ID(SPINAND_READID_METHOD_OPCODE, 0xa4, 0x68),
259 NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
261 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_f,
262 &write_cache_variants,
263 &update_cache_variants),
265 SPINAND_ECCINFO(&gd5fxgq4xc_oob_256_ops,
266 gd5fxgq4ufxxg_ecc_get_status)),
267 SPINAND_INFO("GD5F4GQ4UC",
268 SPINAND_ID(SPINAND_READID_METHOD_OPCODE, 0xb4, 0x68),
269 NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
271 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_f,
272 &write_cache_variants,
273 &update_cache_variants),
275 SPINAND_ECCINFO(&gd5fxgq4xc_oob_256_ops,
276 gd5fxgq4ufxxg_ecc_get_status)),
277 SPINAND_INFO("GD5F1GQ4UExxG",
278 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_ADDR, 0xd1),
279 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
281 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
282 &write_cache_variants,
283 &update_cache_variants),
285 SPINAND_ECCINFO(&gd5fxgq4_variant2_ooblayout,
286 gd5fxgq4uexxg_ecc_get_status)),
287 SPINAND_INFO("GD5F1GQ4UFxxG",
288 SPINAND_ID(SPINAND_READID_METHOD_OPCODE, 0xb1, 0x48),
289 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
291 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_f,
292 &write_cache_variants,
293 &update_cache_variants),
295 SPINAND_ECCINFO(&gd5fxgq4_variant2_ooblayout,
296 gd5fxgq4ufxxg_ecc_get_status)),
299 static const struct spinand_manufacturer_ops gigadevice_spinand_manuf_ops = {
302 const struct spinand_manufacturer gigadevice_spinand_manufacturer = {
303 .id = SPINAND_MFR_GIGADEVICE,
304 .name = "GigaDevice",
305 .chips = gigadevice_spinand_table,
306 .nchips = ARRAY_SIZE(gigadevice_spinand_table),
307 .ops = &gigadevice_spinand_manuf_ops,