1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2020-2021 Rockchip Electronics Co., Ltd 4 * 5 * Authors: 6 * Dingqiang Lin <jon.lin@rock-chips.com> 7 */ 8 9 #ifndef __UBOOT__ 10 #include <linux/device.h> 11 #include <linux/kernel.h> 12 #endif 13 #include <linux/mtd/spinand.h> 14 15 #define SPINAND_MFR_FMSH 0xA1 16 17 static SPINAND_OP_VARIANTS(read_cache_variants, 18 SPINAND_PAGE_READ_FROM_CACHE_QUADIO_OP(0, 2, NULL, 0), 19 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0), 20 SPINAND_PAGE_READ_FROM_CACHE_DUALIO_OP(0, 1, NULL, 0), 21 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0), 22 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0), 23 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0)); 24 25 static SPINAND_OP_VARIANTS(write_cache_variants, 26 SPINAND_PROG_LOAD_X4(true, 0, NULL, 0), 27 SPINAND_PROG_LOAD(true, 0, NULL, 0)); 28 29 static SPINAND_OP_VARIANTS(update_cache_variants, 30 SPINAND_PROG_LOAD_X4(false, 0, NULL, 0), 31 SPINAND_PROG_LOAD(false, 0, NULL, 0)); 32 33 static int fm25s01a_ooblayout_ecc(struct mtd_info *mtd, int section, 34 struct mtd_oob_region *region) 35 { 36 return -ERANGE; 37 } 38 39 static int fm25s01a_ooblayout_free(struct mtd_info *mtd, int section, 40 struct mtd_oob_region *region) 41 { 42 if (section) 43 return -ERANGE; 44 45 region->offset = 2; 46 region->length = 62; 47 48 return 0; 49 } 50 51 static const struct mtd_ooblayout_ops fm25s01a_ooblayout = { 52 .ecc = fm25s01a_ooblayout_ecc, 53 .rfree = fm25s01a_ooblayout_free, 54 }; 55 56 static int fm25s01_ooblayout_ecc(struct mtd_info *mtd, int section, 57 struct mtd_oob_region *region) 58 { 59 if (section) 60 return -ERANGE; 61 62 region->offset = 64; 63 region->length = 64; 64 65 return 0; 66 } 67 68 static int fm25s01_ooblayout_free(struct mtd_info *mtd, int section, 69 struct mtd_oob_region *region) 70 { 71 if (section) 72 return -ERANGE; 73 74 region->offset = 2; 75 region->length = 62; 76 77 return 0; 78 } 79 80 static const struct mtd_ooblayout_ops fm25s01_ooblayout = { 81 .ecc = fm25s01_ooblayout_ecc, 82 .rfree = fm25s01_ooblayout_free, 83 }; 84 85 /* 86 * ecc bits: 0xC0[4,6] 87 * [0b000], No bit errors were detected; 88 * [0b001] and [0b011], 1~6 Bit errors were detected and corrected. Not 89 * reach Flipping Bits; 90 * [0b101], Bit error count equals the bit flip 91 * detection threshold 92 * [0b010], Multiple bit errors were detected and 93 * not corrected. 94 * others, Reserved. 95 */ 96 static int fm25s01bi3_ecc_ecc_get_status(struct spinand_device *spinand, 97 u8 status) 98 { 99 struct nand_device *nand = spinand_to_nand(spinand); 100 u8 eccsr = (status & GENMASK(6, 4)) >> 4; 101 102 if (eccsr <= 1 || eccsr == 3) 103 return eccsr; 104 else if (eccsr == 5) 105 return nand->eccreq.strength; 106 else 107 return -EBADMSG; 108 } 109 110 static int fm25g0xd_ooblayout_ecc(struct mtd_info *mtd, int section, 111 struct mtd_oob_region *region) 112 { 113 if (section > 3) 114 return -ERANGE; 115 116 region->offset = (16 * section) + 8; 117 region->length = 8; 118 119 return 0; 120 } 121 122 static int fm25g0xd_ooblayout_free(struct mtd_info *mtd, int section, 123 struct mtd_oob_region *region) 124 { 125 if (section > 3) 126 return -ERANGE; 127 128 region->offset = (16 * section) + 2; 129 region->length = 6; 130 131 return 0; 132 } 133 134 static const struct mtd_ooblayout_ops fm25g0xd_ooblayout = { 135 .ecc = fm25g0xd_ooblayout_ecc, 136 .rfree = fm25g0xd_ooblayout_free, 137 }; 138 139 /* 140 * ecc bits: 0xC0[4,6] 141 * [0x0], No bit errors were detected; 142 * [0x001, 0x011], Bit errors were detected and corrected. Not 143 * reach Flipping Bits; 144 * [0x100], Bit error count equals the bit flip 145 * detectionthreshold 146 * [0x101, 0x110], Reserved; 147 * [0x111], Multiple bit errors were detected and 148 * not corrected. 149 */ 150 static int fm25g0xd_ecc_get_status(struct spinand_device *spinand, 151 u8 status) 152 { 153 struct nand_device *nand = spinand_to_nand(spinand); 154 u8 eccsr = (status & GENMASK(6, 4)) >> 4; 155 156 if (eccsr <= 3) 157 return 0; 158 else if (eccsr == 4) 159 return nand->eccreq.strength; 160 else 161 return -EBADMSG; 162 } 163 164 static const struct spinand_info fmsh_spinand_table[] = { 165 SPINAND_INFO("FM25S01A", 166 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xE4), 167 NAND_MEMORG(1, 2048, 64, 64, 1024, 1, 1, 1), 168 NAND_ECCREQ(1, 512), 169 SPINAND_INFO_OP_VARIANTS(&read_cache_variants, 170 &write_cache_variants, 171 &update_cache_variants), 172 0, 173 SPINAND_ECCINFO(&fm25s01a_ooblayout, NULL)), 174 SPINAND_INFO("FM25S02A", 175 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xE5), 176 NAND_MEMORG(1, 2048, 64, 64, 2048, 2, 1, 1), 177 NAND_ECCREQ(1, 512), 178 SPINAND_INFO_OP_VARIANTS(&read_cache_variants, 179 &write_cache_variants, 180 &update_cache_variants), 181 SPINAND_HAS_QE_BIT, 182 SPINAND_ECCINFO(&fm25s01a_ooblayout, NULL)), 183 SPINAND_INFO("FM25S01", 184 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xA1), 185 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1), 186 NAND_ECCREQ(1, 512), 187 SPINAND_INFO_OP_VARIANTS(&read_cache_variants, 188 &write_cache_variants, 189 &update_cache_variants), 190 0, 191 SPINAND_ECCINFO(&fm25s01_ooblayout, NULL)), 192 SPINAND_INFO("FM25LS01", 193 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xA5), 194 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1), 195 NAND_ECCREQ(1, 512), 196 SPINAND_INFO_OP_VARIANTS(&read_cache_variants, 197 &write_cache_variants, 198 &update_cache_variants), 199 0, 200 SPINAND_ECCINFO(&fm25s01_ooblayout, NULL)), 201 SPINAND_INFO("FM25S01BI3", 202 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xD4), 203 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1), 204 NAND_ECCREQ(8, 512), 205 SPINAND_INFO_OP_VARIANTS(&read_cache_variants, 206 &write_cache_variants, 207 &update_cache_variants), 208 SPINAND_HAS_QE_BIT, 209 SPINAND_ECCINFO(&fm25s01_ooblayout, fm25s01bi3_ecc_ecc_get_status)), 210 SPINAND_INFO("FM25S02BI3-DND-A-G3", 211 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xD6), 212 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1), 213 NAND_ECCREQ(8, 512), 214 SPINAND_INFO_OP_VARIANTS(&read_cache_variants, 215 &write_cache_variants, 216 &update_cache_variants), 217 SPINAND_HAS_QE_BIT, 218 SPINAND_ECCINFO(&fm25s01_ooblayout, fm25s01bi3_ecc_ecc_get_status)), 219 SPINAND_INFO("FM25G02D", 220 SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xF2), 221 NAND_MEMORG(1, 2048, 64, 64, 2048, 1, 1, 1), 222 NAND_ECCREQ(4, 512), 223 SPINAND_INFO_OP_VARIANTS(&read_cache_variants, 224 &write_cache_variants, 225 &update_cache_variants), 226 SPINAND_HAS_QE_BIT, 227 SPINAND_ECCINFO(&fm25g0xd_ooblayout, fm25g0xd_ecc_get_status)), 228 }; 229 230 static const struct spinand_manufacturer_ops fmsh_spinand_manuf_ops = { 231 }; 232 233 const struct spinand_manufacturer fmsh_spinand_manufacturer = { 234 .id = SPINAND_MFR_FMSH, 235 .name = "FMSH", 236 .chips = fmsh_spinand_table, 237 .nchips = ARRAY_SIZE(fmsh_spinand_table), 238 .ops = &fmsh_spinand_manuf_ops, 239 }; 240