1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2018 exceet electronics GmbH
4 * Copyright (c) 2018 Kontron Electronics GmbH
6 * Author: Frieder Schrempf <frieder.schrempf@kontron.de>
11 #include <linux/device.h>
12 #include <linux/kernel.h>
14 #include <linux/mtd/spinand.h>
16 #define SPINAND_MFR_TOSHIBA 0x98
17 #define TOSH_STATUS_ECC_HAS_BITFLIPS_T (3 << 4)
19 static SPINAND_OP_VARIANTS(read_cache_variants,
20 SPINAND_PAGE_READ_FROM_CACHE_X4_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));
25 static SPINAND_OP_VARIANTS(write_cache_variants,
26 SPINAND_PROG_LOAD(true, 0, NULL, 0));
28 static SPINAND_OP_VARIANTS(update_cache_variants,
29 SPINAND_PROG_LOAD(false, 0, NULL, 0));
31 static int tc58cxgxsx_ooblayout_ecc(struct mtd_info *mtd, int section,
32 struct mtd_oob_region *region)
37 region->offset = mtd->oobsize / 2;
38 region->length = mtd->oobsize / 2;
43 static int tc58cxgxsx_ooblayout_free(struct mtd_info *mtd, int section,
44 struct mtd_oob_region *region)
49 /* 2 bytes reserved for BBM */
51 region->length = (mtd->oobsize / 2) - 2;
56 static const struct mtd_ooblayout_ops tc58cxgxsx_ooblayout = {
57 .ecc = tc58cxgxsx_ooblayout_ecc,
58 .rfree = tc58cxgxsx_ooblayout_free,
61 static int tc58cxgxsx_ecc_get_status(struct spinand_device *spinand,
64 struct nand_device *nand = spinand_to_nand(spinand);
66 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, &mbf);
68 switch (status & STATUS_ECC_MASK) {
69 case STATUS_ECC_NO_BITFLIPS:
72 case STATUS_ECC_UNCOR_ERROR:
75 case STATUS_ECC_HAS_BITFLIPS:
76 case TOSH_STATUS_ECC_HAS_BITFLIPS_T:
78 * Let's try to retrieve the real maximum number of bitflips
79 * in order to avoid forcing the wear-leveling layer to move
80 * data around if it's not necessary.
82 if (spi_mem_exec_op(spinand->slave, &op))
83 return nand->eccreq.strength;
87 if (WARN_ON(mbf > nand->eccreq.strength || !mbf))
88 return nand->eccreq.strength;
99 static const struct spinand_info toshiba_spinand_table[] = {
101 SPINAND_INFO("TC58CVG0S3", 0xC2,
102 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
104 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
105 &write_cache_variants,
106 &update_cache_variants),
108 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
109 tc58cxgxsx_ecc_get_status)),
111 SPINAND_INFO("TC58CVG1S3", 0xCB,
112 NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
114 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
115 &write_cache_variants,
116 &update_cache_variants),
118 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
119 tc58cxgxsx_ecc_get_status)),
121 SPINAND_INFO("TC58CVG2S0", 0xCD,
122 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
124 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
125 &write_cache_variants,
126 &update_cache_variants),
128 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
129 tc58cxgxsx_ecc_get_status)),
131 SPINAND_INFO("TC58CVG2S0", 0xED,
132 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
134 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
135 &write_cache_variants,
136 &update_cache_variants),
138 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
139 tc58cxgxsx_ecc_get_status)),
141 SPINAND_INFO("TC58CYG0S3", 0xB2,
142 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
144 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
145 &write_cache_variants,
146 &update_cache_variants),
148 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
149 tc58cxgxsx_ecc_get_status)),
151 SPINAND_INFO("TC58CYG1S3", 0xBB,
152 NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
154 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
155 &write_cache_variants,
156 &update_cache_variants),
158 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
159 tc58cxgxsx_ecc_get_status)),
161 SPINAND_INFO("TC58CYG2S0", 0xBD,
162 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
164 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
165 &write_cache_variants,
166 &update_cache_variants),
168 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
169 tc58cxgxsx_ecc_get_status)),
172 static int toshiba_spinand_detect(struct spinand_device *spinand)
174 u8 *id = spinand->id.data;
178 * Toshiba SPI NAND read ID needs a dummy byte,
179 * so the first byte in id is garbage.
181 if (id[1] != SPINAND_MFR_TOSHIBA)
184 ret = spinand_match_and_init(spinand, toshiba_spinand_table,
185 ARRAY_SIZE(toshiba_spinand_table),
193 static const struct spinand_manufacturer_ops toshiba_spinand_manuf_ops = {
194 .detect = toshiba_spinand_detect,
197 const struct spinand_manufacturer toshiba_spinand_manufacturer = {
198 .id = SPINAND_MFR_TOSHIBA,
200 .ops = &toshiba_spinand_manuf_ops,