xref: /openbmc/linux/drivers/mtd/nand/spi/macronix.c (revision 2d99a7ec)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2018 Macronix
4  *
5  * Author: Boris Brezillon <boris.brezillon@bootlin.com>
6  */
7 
8 #include <linux/device.h>
9 #include <linux/kernel.h>
10 #include <linux/mtd/spinand.h>
11 
12 #define SPINAND_MFR_MACRONIX		0xC2
13 #define MACRONIX_ECCSR_MASK		0x0F
14 
15 static SPINAND_OP_VARIANTS(read_cache_variants,
16 		SPINAND_PAGE_READ_FROM_CACHE_X4_OP(0, 1, NULL, 0),
17 		SPINAND_PAGE_READ_FROM_CACHE_X2_OP(0, 1, NULL, 0),
18 		SPINAND_PAGE_READ_FROM_CACHE_OP(true, 0, 1, NULL, 0),
19 		SPINAND_PAGE_READ_FROM_CACHE_OP(false, 0, 1, NULL, 0));
20 
21 static SPINAND_OP_VARIANTS(write_cache_variants,
22 		SPINAND_PROG_LOAD_X4(true, 0, NULL, 0),
23 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
24 
25 static SPINAND_OP_VARIANTS(update_cache_variants,
26 		SPINAND_PROG_LOAD_X4(false, 0, NULL, 0),
27 		SPINAND_PROG_LOAD(false, 0, NULL, 0));
28 
29 static int mx35lfxge4ab_ooblayout_ecc(struct mtd_info *mtd, int section,
30 				      struct mtd_oob_region *region)
31 {
32 	return -ERANGE;
33 }
34 
35 static int mx35lfxge4ab_ooblayout_free(struct mtd_info *mtd, int section,
36 				       struct mtd_oob_region *region)
37 {
38 	if (section)
39 		return -ERANGE;
40 
41 	region->offset = 2;
42 	region->length = mtd->oobsize - 2;
43 
44 	return 0;
45 }
46 
47 static const struct mtd_ooblayout_ops mx35lfxge4ab_ooblayout = {
48 	.ecc = mx35lfxge4ab_ooblayout_ecc,
49 	.free = mx35lfxge4ab_ooblayout_free,
50 };
51 
52 static int mx35lf1ge4ab_get_eccsr(struct spinand_device *spinand, u8 *eccsr)
53 {
54 	struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(0x7c, 1),
55 					  SPI_MEM_OP_NO_ADDR,
56 					  SPI_MEM_OP_DUMMY(1, 1),
57 					  SPI_MEM_OP_DATA_IN(1, eccsr, 1));
58 
59 	int ret = spi_mem_exec_op(spinand->spimem, &op);
60 	if (ret)
61 		return ret;
62 
63 	*eccsr &= MACRONIX_ECCSR_MASK;
64 	return 0;
65 }
66 
67 static int mx35lf1ge4ab_ecc_get_status(struct spinand_device *spinand,
68 				       u8 status)
69 {
70 	struct nand_device *nand = spinand_to_nand(spinand);
71 	u8 eccsr;
72 
73 	switch (status & STATUS_ECC_MASK) {
74 	case STATUS_ECC_NO_BITFLIPS:
75 		return 0;
76 
77 	case STATUS_ECC_UNCOR_ERROR:
78 		return -EBADMSG;
79 
80 	case STATUS_ECC_HAS_BITFLIPS:
81 		/*
82 		 * Let's try to retrieve the real maximum number of bitflips
83 		 * in order to avoid forcing the wear-leveling layer to move
84 		 * data around if it's not necessary.
85 		 */
86 		if (mx35lf1ge4ab_get_eccsr(spinand, spinand->scratchbuf))
87 			return nanddev_get_ecc_conf(nand)->strength;
88 
89 		eccsr = *spinand->scratchbuf;
90 		if (WARN_ON(eccsr > nanddev_get_ecc_conf(nand)->strength ||
91 			    !eccsr))
92 			return nanddev_get_ecc_conf(nand)->strength;
93 
94 		return eccsr;
95 
96 	default:
97 		break;
98 	}
99 
100 	return -EINVAL;
101 }
102 
103 static const struct spinand_info macronix_spinand_table[] = {
104 	SPINAND_INFO("MX35LF1GE4AB",
105 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x12),
106 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
107 		     NAND_ECCREQ(4, 512),
108 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
109 					      &write_cache_variants,
110 					      &update_cache_variants),
111 		     SPINAND_HAS_QE_BIT,
112 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
113 				     mx35lf1ge4ab_ecc_get_status)),
114 	SPINAND_INFO("MX35LF2GE4AB",
115 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x22),
116 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 2, 1, 1),
117 		     NAND_ECCREQ(4, 512),
118 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
119 					      &write_cache_variants,
120 					      &update_cache_variants),
121 		     SPINAND_HAS_QE_BIT,
122 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
123 	SPINAND_INFO("MX35LF2GE4AD",
124 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x26),
125 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
126 		     NAND_ECCREQ(8, 512),
127 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
128 					      &write_cache_variants,
129 					      &update_cache_variants),
130 		     SPINAND_HAS_QE_BIT,
131 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
132 				     mx35lf1ge4ab_ecc_get_status)),
133 	SPINAND_INFO("MX35LF4GE4AD",
134 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x37),
135 		     NAND_MEMORG(1, 4096, 128, 64, 2048, 40, 1, 1, 1),
136 		     NAND_ECCREQ(8, 512),
137 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
138 					      &write_cache_variants,
139 					      &update_cache_variants),
140 		     SPINAND_HAS_QE_BIT,
141 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
142 				     mx35lf1ge4ab_ecc_get_status)),
143 	SPINAND_INFO("MX35LF1G24AD",
144 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x14),
145 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
146 		     NAND_ECCREQ(8, 512),
147 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
148 					      &write_cache_variants,
149 					      &update_cache_variants),
150 		     SPINAND_HAS_QE_BIT,
151 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
152 	SPINAND_INFO("MX35LF2G24AD",
153 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x24),
154 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 2, 1, 1),
155 		     NAND_ECCREQ(8, 512),
156 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
157 					      &write_cache_variants,
158 					      &update_cache_variants),
159 		     SPINAND_HAS_QE_BIT,
160 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
161 	SPINAND_INFO("MX35LF4G24AD",
162 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x35),
163 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 2, 1, 1),
164 		     NAND_ECCREQ(8, 512),
165 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
166 					      &write_cache_variants,
167 					      &update_cache_variants),
168 		     SPINAND_HAS_QE_BIT,
169 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
170 	SPINAND_INFO("MX31LF1GE4BC",
171 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x1e),
172 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
173 		     NAND_ECCREQ(8, 512),
174 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
175 					      &write_cache_variants,
176 					      &update_cache_variants),
177 		     SPINAND_HAS_QE_BIT,
178 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
179 				     mx35lf1ge4ab_ecc_get_status)),
180 	SPINAND_INFO("MX31UF1GE4BC",
181 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x9e),
182 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
183 		     NAND_ECCREQ(8, 512),
184 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
185 					      &write_cache_variants,
186 					      &update_cache_variants),
187 		     SPINAND_HAS_QE_BIT,
188 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
189 				     mx35lf1ge4ab_ecc_get_status)),
190 
191 	SPINAND_INFO("MX35LF2G14AC",
192 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x20),
193 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 2, 1, 1),
194 		     NAND_ECCREQ(4, 512),
195 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
196 					      &write_cache_variants,
197 					      &update_cache_variants),
198 		     SPINAND_HAS_QE_BIT,
199 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
200 				     mx35lf1ge4ab_ecc_get_status)),
201 	SPINAND_INFO("MX35UF4G24AD",
202 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xb5),
203 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 2, 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(&mx35lfxge4ab_ooblayout,
210 				     mx35lf1ge4ab_ecc_get_status)),
211 	SPINAND_INFO("MX35UF4GE4AD",
212 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xb7),
213 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
214 		     NAND_ECCREQ(8, 512),
215 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
216 					      &write_cache_variants,
217 					      &update_cache_variants),
218 		     SPINAND_HAS_QE_BIT,
219 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
220 				     mx35lf1ge4ab_ecc_get_status)),
221 	SPINAND_INFO("MX35UF2G14AC",
222 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa0),
223 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 2, 1, 1),
224 		     NAND_ECCREQ(4, 512),
225 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
226 					      &write_cache_variants,
227 					      &update_cache_variants),
228 		     SPINAND_HAS_QE_BIT,
229 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
230 				     mx35lf1ge4ab_ecc_get_status)),
231 	SPINAND_INFO("MX35UF2G24AD",
232 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa4),
233 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 2, 1, 1),
234 		     NAND_ECCREQ(8, 512),
235 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
236 					      &write_cache_variants,
237 					      &update_cache_variants),
238 		     SPINAND_HAS_QE_BIT,
239 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
240 				     mx35lf1ge4ab_ecc_get_status)),
241 	SPINAND_INFO("MX35UF2GE4AD",
242 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa6),
243 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
244 		     NAND_ECCREQ(8, 512),
245 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
246 					      &write_cache_variants,
247 					      &update_cache_variants),
248 		     SPINAND_HAS_QE_BIT,
249 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
250 				     mx35lf1ge4ab_ecc_get_status)),
251 	SPINAND_INFO("MX35UF2GE4AC",
252 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa2),
253 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
254 		     NAND_ECCREQ(4, 512),
255 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
256 					      &write_cache_variants,
257 					      &update_cache_variants),
258 		     SPINAND_HAS_QE_BIT,
259 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
260 				     mx35lf1ge4ab_ecc_get_status)),
261 	SPINAND_INFO("MX35UF1G14AC",
262 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x90),
263 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
264 		     NAND_ECCREQ(4, 512),
265 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
266 					      &write_cache_variants,
267 					      &update_cache_variants),
268 		     SPINAND_HAS_QE_BIT,
269 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
270 				     mx35lf1ge4ab_ecc_get_status)),
271 	SPINAND_INFO("MX35UF1G24AD",
272 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x94),
273 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
274 		     NAND_ECCREQ(8, 512),
275 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
276 					      &write_cache_variants,
277 					      &update_cache_variants),
278 		     SPINAND_HAS_QE_BIT,
279 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
280 				     mx35lf1ge4ab_ecc_get_status)),
281 	SPINAND_INFO("MX35UF1GE4AD",
282 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x96),
283 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
284 		     NAND_ECCREQ(8, 512),
285 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
286 					      &write_cache_variants,
287 					      &update_cache_variants),
288 		     SPINAND_HAS_QE_BIT,
289 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
290 				     mx35lf1ge4ab_ecc_get_status)),
291 	SPINAND_INFO("MX35UF1GE4AC",
292 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x92),
293 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
294 		     NAND_ECCREQ(4, 512),
295 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
296 					      &write_cache_variants,
297 					      &update_cache_variants),
298 		     SPINAND_HAS_QE_BIT,
299 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
300 				     mx35lf1ge4ab_ecc_get_status)),
301 
302 };
303 
304 static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
305 };
306 
307 const struct spinand_manufacturer macronix_spinand_manufacturer = {
308 	.id = SPINAND_MFR_MACRONIX,
309 	.name = "Macronix",
310 	.chips = macronix_spinand_table,
311 	.nchips = ARRAY_SIZE(macronix_spinand_table),
312 	.ops = &macronix_spinand_manuf_ops,
313 };
314