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, 2, 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, 2, 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 gd5fxgq4uexxg_ecc_get_status(struct spinand_device *spinand,
139 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(GD5FXGQ4UEXXG_REG_STATUS2,
143 switch (status & STATUS_ECC_MASK) {
144 case STATUS_ECC_NO_BITFLIPS:
147 case GD5FXGQ4XA_STATUS_ECC_1_7_BITFLIPS:
149 * Read status2 register to determine a more fine grained
152 ret = spi_mem_exec_op(spinand->spimem, &op);
157 * 4 ... 7 bits are flipped (1..4 can't be detected, so
158 * report the maximum of 4 in this case
160 /* bits sorted this way (3...0): ECCS1,ECCS0,ECCSE1,ECCSE0 */
161 return ((status & STATUS_ECC_MASK) >> 2) |
162 ((status2 & STATUS_ECC_MASK) >> 4);
164 case GD5FXGQ4XA_STATUS_ECC_8_BITFLIPS:
167 case STATUS_ECC_UNCOR_ERROR:
177 static int gd5fxgq4ufxxg_ecc_get_status(struct spinand_device *spinand,
180 switch (status & GD5FXGQ4UXFXXG_STATUS_ECC_MASK) {
181 case GD5FXGQ4UXFXXG_STATUS_ECC_NO_BITFLIPS:
184 case GD5FXGQ4UXFXXG_STATUS_ECC_1_3_BITFLIPS:
187 case GD5FXGQ4UXFXXG_STATUS_ECC_UNCOR_ERROR:
190 default: /* (2 << 4) through (6 << 4) are 4-8 corrected errors */
191 return ((status & GD5FXGQ4UXFXXG_STATUS_ECC_MASK) >> 4) + 2;
197 static const struct spinand_info gigadevice_spinand_table[] = {
198 SPINAND_INFO("GD5F1GQ4xA", 0xF1,
199 NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
201 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
202 &write_cache_variants,
203 &update_cache_variants),
205 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
206 gd5fxgq4xa_ecc_get_status)),
207 SPINAND_INFO("GD5F2GQ4xA", 0xF2,
208 NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
210 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
211 &write_cache_variants,
212 &update_cache_variants),
214 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
215 gd5fxgq4xa_ecc_get_status)),
216 SPINAND_INFO("GD5F4GQ4xA", 0xF4,
217 NAND_MEMORG(1, 2048, 64, 64, 4096, 80, 1, 1, 1),
219 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
220 &write_cache_variants,
221 &update_cache_variants),
223 SPINAND_ECCINFO(&gd5fxgq4xa_ooblayout,
224 gd5fxgq4xa_ecc_get_status)),
225 SPINAND_INFO("GD5F1GQ4UExxG", 0xd1,
226 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
228 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
229 &write_cache_variants,
230 &update_cache_variants),
232 SPINAND_ECCINFO(&gd5fxgq4_variant2_ooblayout,
233 gd5fxgq4uexxg_ecc_get_status)),
234 SPINAND_INFO("GD5F1GQ4UFxxG", 0xb148,
235 NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
237 SPINAND_INFO_OP_VARIANTS(&read_cache_variants_f,
238 &write_cache_variants,
239 &update_cache_variants),
241 SPINAND_ECCINFO(&gd5fxgq4_variant2_ooblayout,
242 gd5fxgq4ufxxg_ecc_get_status)),
245 static int gigadevice_spinand_detect(struct spinand_device *spinand)
247 u8 *id = spinand->id.data;
252 * Earlier GDF5-series devices (A,E) return [0][MID][DID]
253 * Later (F) devices return [MID][DID1][DID2]
256 if (id[0] == SPINAND_MFR_GIGADEVICE)
257 did = (id[1] << 8) + id[2];
258 else if (id[0] == 0 && id[1] == SPINAND_MFR_GIGADEVICE)
263 ret = spinand_match_and_init(spinand, gigadevice_spinand_table,
264 ARRAY_SIZE(gigadevice_spinand_table),
272 static const struct spinand_manufacturer_ops gigadevice_spinand_manuf_ops = {
273 .detect = gigadevice_spinand_detect,
276 const struct spinand_manufacturer gigadevice_spinand_manufacturer = {
277 .id = SPINAND_MFR_GIGADEVICE,
278 .name = "GigaDevice",
279 .ops = &gigadevice_spinand_manuf_ops,