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>
9 #include <linux/device.h>
10 #include <linux/kernel.h>
11 #include <linux/mtd/spinand.h>
13 #define SPINAND_MFR_TOSHIBA 0x98
14 #define TOSH_STATUS_ECC_HAS_BITFLIPS_T (3 << 4)
16 static SPINAND_OP_VARIANTS(read_cache_variants,
17 SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
18 SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
19 SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
20 SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
22 static SPINAND_OP_VARIANTS(write_cache_variants,
23 SPINAND_PROG_LOAD(true, 0, NULL, 0));
25 static SPINAND_OP_VARIANTS(update_cache_variants,
26 SPINAND_PROG_LOAD(false, 0, NULL, 0));
28 static int tc58cxgxsx_ooblayout_ecc(struct mtd_info *mtd, int section,
29 struct mtd_oob_region *region)
34 region->offset = mtd->oobsize / 2;
35 region->length = mtd->oobsize / 2;
40 static int tc58cxgxsx_ooblayout_free(struct mtd_info *mtd, int section,
41 struct mtd_oob_region *region)
46 /* 2 bytes reserved for BBM */
48 region->length = (mtd->oobsize / 2) - 2;
53 static const struct mtd_ooblayout_ops tc58cxgxsx_ooblayout = {
54 .ecc = tc58cxgxsx_ooblayout_ecc,
55 .free = tc58cxgxsx_ooblayout_free,
58 static int tc58cxgxsx_ecc_get_status(struct spinand_device *spinand,
61 struct nand_device *nand = spinand_to_nand(spinand);
63 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(0x30, &mbf);
65 switch (status & STATUS_ECC_MASK) {
66 case STATUS_ECC_NO_BITFLIPS:
69 case STATUS_ECC_UNCOR_ERROR:
72 case STATUS_ECC_HAS_BITFLIPS:
73 case TOSH_STATUS_ECC_HAS_BITFLIPS_T:
75 * Let's try to retrieve the real maximum number of bitflips
76 * in order to avoid forcing the wear-leveling layer to move
77 * data around if it's not necessary.
79 if (spi_mem_exec_op(spinand->spimem, &op))
80 return nand->eccreq.strength;
84 if (WARN_ON(mbf > nand->eccreq.strength || !mbf))
85 return nand->eccreq.strength;
96 static const struct spinand_info toshiba_spinand_table[] = {
98 SPINAND_INFO("TC58CVG0S3", 0xC2,
99 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
101 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
102 &write_cache_variants,
103 &update_cache_variants),
105 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
106 tc58cxgxsx_ecc_get_status)),
108 SPINAND_INFO("TC58CVG1S3", 0xCB,
109 NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
111 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
112 &write_cache_variants,
113 &update_cache_variants),
115 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
116 tc58cxgxsx_ecc_get_status)),
118 SPINAND_INFO("TC58CVG2S0", 0xCD,
119 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
121 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
122 &write_cache_variants,
123 &update_cache_variants),
125 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
126 tc58cxgxsx_ecc_get_status)),
128 SPINAND_INFO("TC58CYG0S3", 0xB2,
129 NAND_MEMORG(1, 2048, 128, 64, 1024, 1, 1, 1),
131 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
132 &write_cache_variants,
133 &update_cache_variants),
135 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
136 tc58cxgxsx_ecc_get_status)),
138 SPINAND_INFO("TC58CYG1S3", 0xBB,
139 NAND_MEMORG(1, 2048, 128, 64, 2048, 1, 1, 1),
141 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
142 &write_cache_variants,
143 &update_cache_variants),
145 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
146 tc58cxgxsx_ecc_get_status)),
148 SPINAND_INFO("TC58CYG2S0", 0xBD,
149 NAND_MEMORG(1, 4096, 256, 64, 2048, 1, 1, 1),
151 SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
152 &write_cache_variants,
153 &update_cache_variants),
155 SPINAND_ECCINFO(&tc58cxgxsx_ooblayout,
156 tc58cxgxsx_ecc_get_status)),
159 static int toshiba_spinand_detect(struct spinand_device *spinand)
161 u8 *id = spinand->id.data;
165 * Toshiba SPI NAND read ID needs a dummy byte,
166 * so the first byte in id is garbage.
168 if (id[1] != SPINAND_MFR_TOSHIBA)
171 ret = spinand_match_and_init(spinand, toshiba_spinand_table,
172 ARRAY_SIZE(toshiba_spinand_table),
180 static const struct spinand_manufacturer_ops toshiba_spinand_manuf_ops = {
181 .detect = toshiba_spinand_detect,
184 const struct spinand_manufacturer toshiba_spinand_manufacturer = {
185 .id = SPINAND_MFR_TOSHIBA,
187 .ops = &toshiba_spinand_manuf_ops,