1 // SPDX-License-Identifier: GPL-2.0
6 #include <linux/device.h>
7 #include <linux/kernel.h>
8 #include <linux/mtd/spinand.h>
10 #define SPINAND_MFR_ALLIANCEMEMORY 0x52
12 #define AM_STATUS_ECC_BITMASK (3 << 4)
14 #define AM_STATUS_ECC_NONE_DETECTED (0 << 4)
15 #define AM_STATUS_ECC_CORRECTED (1 << 4)
16 #define AM_STATUS_ECC_ERRORED (2 << 4)
17 #define AM_STATUS_ECC_MAX_CORRECTED (3 << 4)
19 static SPINAND_OP_VARIANTS(read_cache_variants,
20 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 1, NULL, 0),
21 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
22 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0),
23 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
24 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
25 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
27 static SPINAND_OP_VARIANTS(write_cache_variants,
28 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
29 SPINAND_PROG_LOAD(true, 0, NULL, 0));
31 static SPINAND_OP_VARIANTS(update_cache_variants,
32 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
33 SPINAND_PROG_LOAD(false, 0, NULL, 0));
35 static int am_get_eccsize(struct mtd_info *mtd)
37 if (mtd->oobsize == 64)
39 else if (mtd->oobsize == 128)
41 else if (mtd->oobsize == 256)
47 static int am_ooblayout_ecc(struct mtd_info *mtd, int section,
48 struct mtd_oob_region *region)
52 ecc_bytes = am_get_eccsize(mtd);
56 region->offset = mtd->oobsize - ecc_bytes;
57 region->length = ecc_bytes;
62 static int am_ooblayout_free(struct mtd_info *mtd, int section,
63 struct mtd_oob_region *region)
70 ecc_bytes = am_get_eccsize(mtd);
75 * It is unclear how many bytes are used for the bad block marker. We
76 * reserve the common two bytes here.
78 * The free area in this kind of flash is divided into chunks where the
79 * first 4 bytes of each chunk are unprotected. The number of chunks
80 * depends on the specific model. The models with 4096+256 bytes pages
81 * have 8 chunks, the others 4 chunks.
85 region->length = mtd->oobsize - 2 - ecc_bytes;
90 static const struct mtd_ooblayout_ops am_ooblayout = {
91 .ecc = am_ooblayout_ecc,
92 .free = am_ooblayout_free,
95 static int am_ecc_get_status(struct spinand_device *spinand, u8 status)
97 switch (status & AM_STATUS_ECC_BITMASK) {
98 case AM_STATUS_ECC_NONE_DETECTED:
101 case AM_STATUS_ECC_CORRECTED:
103 * use oobsize to determine the flash model and the maximum of
104 * correctable errors and return maximum - 1 by convention
106 if (spinand->base.mtd.oobsize == 64)
111 case AM_STATUS_ECC_ERRORED:
114 case AM_STATUS_ECC_MAX_CORRECTED:
116 * use oobsize to determine the flash model and the maximum of
119 if (spinand->base.mtd.oobsize == 64)
131 static const struct spinand_info alliancememory_spinand_table[] = {
132 SPINAND_INFO("AS5F34G04SND",
133 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x2f),
134 NAND_MEMORG(1, 2048, 128, 64, 4096, 80, 1, 1, 1),
136 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
137 &write_cache_variants,
138 &update_cache_variants),
140 SPINAND_ECCINFO(&am_ooblayout,
144 static const struct spinand_manufacturer_ops alliancememory_spinand_manuf_ops = {
147 const struct spinand_manufacturer alliancememory_spinand_manufacturer = {
148 .id = SPINAND_MFR_ALLIANCEMEMORY,
149 .name = "AllianceMemory",
150 .chips = alliancememory_spinand_table,
151 .nchips = ARRAY_SIZE(alliancememory_spinand_table),
152 .ops = &alliancememory_spinand_manuf_ops,