xref: /linux/drivers/mtd/nand/spi/macronix.c (revision 5c61f59824b5e46516ea5d0543ad7a8871567416)
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, 0x03),
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, 0x03),
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, 0x03),
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, 0x03),
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("MX35LF2G24AD-Z4I8",
162 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x64, 0x03),
163 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 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("MX35LF4G24AD",
171 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x35, 0x03),
172 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 2, 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, NULL)),
179 	SPINAND_INFO("MX35LF4G24AD-Z4I8",
180 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x75, 0x03),
181 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
182 		     NAND_ECCREQ(8, 512),
183 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
184 					      &write_cache_variants,
185 					      &update_cache_variants),
186 		     SPINAND_HAS_QE_BIT,
187 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout, NULL)),
188 	SPINAND_INFO("MX31LF1GE4BC",
189 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x1e),
190 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
191 		     NAND_ECCREQ(8, 512),
192 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
193 					      &write_cache_variants,
194 					      &update_cache_variants),
195 		     SPINAND_HAS_QE_BIT,
196 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
197 				     mx35lf1ge4ab_ecc_get_status)),
198 	SPINAND_INFO("MX31UF1GE4BC",
199 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x9e),
200 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
201 		     NAND_ECCREQ(8, 512),
202 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
203 					      &write_cache_variants,
204 					      &update_cache_variants),
205 		     SPINAND_HAS_QE_BIT,
206 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
207 				     mx35lf1ge4ab_ecc_get_status)),
208 
209 	SPINAND_INFO("MX35LF2G14AC",
210 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x20),
211 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 2, 1, 1),
212 		     NAND_ECCREQ(4, 512),
213 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
214 					      &write_cache_variants,
215 					      &update_cache_variants),
216 		     SPINAND_HAS_QE_BIT,
217 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
218 				     mx35lf1ge4ab_ecc_get_status)),
219 	SPINAND_INFO("MX35UF4G24AD",
220 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xb5, 0x03),
221 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 2, 1, 1),
222 		     NAND_ECCREQ(8, 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(&mx35lfxge4ab_ooblayout,
228 				     mx35lf1ge4ab_ecc_get_status)),
229 	SPINAND_INFO("MX35UF4G24AD-Z4I8",
230 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xf5, 0x03),
231 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
232 		     NAND_ECCREQ(8, 512),
233 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
234 					      &write_cache_variants,
235 					      &update_cache_variants),
236 		     SPINAND_HAS_QE_BIT,
237 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
238 				     mx35lf1ge4ab_ecc_get_status)),
239 	SPINAND_INFO("MX35UF4GE4AD",
240 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xb7, 0x03),
241 		     NAND_MEMORG(1, 4096, 256, 64, 2048, 40, 1, 1, 1),
242 		     NAND_ECCREQ(8, 512),
243 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
244 					      &write_cache_variants,
245 					      &update_cache_variants),
246 		     SPINAND_HAS_QE_BIT,
247 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
248 				     mx35lf1ge4ab_ecc_get_status)),
249 	SPINAND_INFO("MX35UF2G14AC",
250 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa0),
251 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 2, 1, 1),
252 		     NAND_ECCREQ(4, 512),
253 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
254 					      &write_cache_variants,
255 					      &update_cache_variants),
256 		     SPINAND_HAS_QE_BIT,
257 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
258 				     mx35lf1ge4ab_ecc_get_status)),
259 	SPINAND_INFO("MX35UF2G24AD",
260 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa4, 0x03),
261 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 2, 1, 1),
262 		     NAND_ECCREQ(8, 512),
263 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
264 					      &write_cache_variants,
265 					      &update_cache_variants),
266 		     SPINAND_HAS_QE_BIT,
267 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
268 				     mx35lf1ge4ab_ecc_get_status)),
269 	SPINAND_INFO("MX35UF2G24AD-Z4I8",
270 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xe4, 0x03),
271 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
272 		     NAND_ECCREQ(8, 512),
273 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
274 					      &write_cache_variants,
275 					      &update_cache_variants),
276 		     SPINAND_HAS_QE_BIT,
277 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
278 				     mx35lf1ge4ab_ecc_get_status)),
279 	SPINAND_INFO("MX35UF2GE4AD",
280 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa6, 0x03),
281 		     NAND_MEMORG(1, 2048, 128, 64, 2048, 40, 1, 1, 1),
282 		     NAND_ECCREQ(8, 512),
283 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
284 					      &write_cache_variants,
285 					      &update_cache_variants),
286 		     SPINAND_HAS_QE_BIT,
287 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
288 				     mx35lf1ge4ab_ecc_get_status)),
289 	SPINAND_INFO("MX35UF2GE4AC",
290 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xa2, 0x01),
291 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
292 		     NAND_ECCREQ(4, 512),
293 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
294 					      &write_cache_variants,
295 					      &update_cache_variants),
296 		     SPINAND_HAS_QE_BIT,
297 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
298 				     mx35lf1ge4ab_ecc_get_status)),
299 	SPINAND_INFO("MX35UF1G14AC",
300 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x90),
301 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
302 		     NAND_ECCREQ(4, 512),
303 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
304 					      &write_cache_variants,
305 					      &update_cache_variants),
306 		     SPINAND_HAS_QE_BIT,
307 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
308 				     mx35lf1ge4ab_ecc_get_status)),
309 	SPINAND_INFO("MX35UF1G24AD",
310 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x94, 0x03),
311 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
312 		     NAND_ECCREQ(8, 512),
313 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
314 					      &write_cache_variants,
315 					      &update_cache_variants),
316 		     SPINAND_HAS_QE_BIT,
317 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
318 				     mx35lf1ge4ab_ecc_get_status)),
319 	SPINAND_INFO("MX35UF1GE4AD",
320 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x96, 0x03),
321 		     NAND_MEMORG(1, 2048, 128, 64, 1024, 20, 1, 1, 1),
322 		     NAND_ECCREQ(8, 512),
323 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
324 					      &write_cache_variants,
325 					      &update_cache_variants),
326 		     SPINAND_HAS_QE_BIT,
327 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
328 				     mx35lf1ge4ab_ecc_get_status)),
329 	SPINAND_INFO("MX35UF1GE4AC",
330 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x92, 0x01),
331 		     NAND_MEMORG(1, 2048, 64, 64, 1024, 20, 1, 1, 1),
332 		     NAND_ECCREQ(4, 512),
333 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
334 					      &write_cache_variants,
335 					      &update_cache_variants),
336 		     SPINAND_HAS_QE_BIT,
337 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
338 				     mx35lf1ge4ab_ecc_get_status)),
339 
340 	SPINAND_INFO("MX31LF2GE4BC",
341 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0x2e),
342 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
343 		     NAND_ECCREQ(8, 512),
344 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
345 					      &write_cache_variants,
346 					      &update_cache_variants),
347 		     SPINAND_HAS_QE_BIT,
348 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
349 				     mx35lf1ge4ab_ecc_get_status)),
350 	SPINAND_INFO("MX3UF2GE4BC",
351 		     SPINAND_ID(SPINAND_READID_METHOD_OPCODE_DUMMY, 0xae),
352 		     NAND_MEMORG(1, 2048, 64, 64, 2048, 40, 1, 1, 1),
353 		     NAND_ECCREQ(8, 512),
354 		     SPINAND_INFO_OP_VARIANTS(&read_cache_variants,
355 					      &write_cache_variants,
356 					      &update_cache_variants),
357 		     SPINAND_HAS_QE_BIT,
358 		     SPINAND_ECCINFO(&mx35lfxge4ab_ooblayout,
359 				     mx35lf1ge4ab_ecc_get_status)),
360 };
361 
362 static const struct spinand_manufacturer_ops macronix_spinand_manuf_ops = {
363 };
364 
365 const struct spinand_manufacturer macronix_spinand_manufacturer = {
366 	.id = SPINAND_MFR_MACRONIX,
367 	.name = "Macronix",
368 	.chips = macronix_spinand_table,
369 	.nchips = ARRAY_SIZE(macronix_spinand_table),
370 	.ops = &macronix_spinand_manuf_ops,
371 };
372