xref: /linux/drivers/mtd/nand/spi/core.c (revision 3f75bfff44be0646580fe4efda45d646f9c1693b)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2016-2017 Micron Technology, Inc.
4  *
5  * Authors:
6  *	Peter Pan <peterpandong@micron.com>
7  *	Boris Brezillon <boris.brezillon@bootlin.com>
8  */
9 
10 #define pr_fmt(fmt)	"spi-nand: " fmt
11 
12 #include <linux/device.h>
13 #include <linux/jiffies.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/mtd/spinand.h>
17 #include <linux/of.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi-mem.h>
22 
spinand_read_reg_op(struct spinand_device * spinand,u8 reg,u8 * val)23 static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
24 {
25 	struct spi_mem_op op = SPINAND_GET_FEATURE_1S_1S_1S_OP(reg,
26 						      spinand->scratchbuf);
27 	int ret;
28 
29 	ret = spi_mem_exec_op(spinand->spimem, &op);
30 	if (ret)
31 		return ret;
32 
33 	*val = *spinand->scratchbuf;
34 	return 0;
35 }
36 
spinand_write_reg_op(struct spinand_device * spinand,u8 reg,u8 val)37 int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val)
38 {
39 	struct spi_mem_op op = SPINAND_SET_FEATURE_1S_1S_1S_OP(reg,
40 						      spinand->scratchbuf);
41 
42 	*spinand->scratchbuf = val;
43 	return spi_mem_exec_op(spinand->spimem, &op);
44 }
45 
spinand_read_status(struct spinand_device * spinand,u8 * status)46 static int spinand_read_status(struct spinand_device *spinand, u8 *status)
47 {
48 	return spinand_read_reg_op(spinand, REG_STATUS, status);
49 }
50 
spinand_get_cfg(struct spinand_device * spinand,u8 * cfg)51 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg)
52 {
53 	struct nand_device *nand = spinand_to_nand(spinand);
54 
55 	if (WARN_ON(spinand->cur_target < 0 ||
56 		    spinand->cur_target >= nand->memorg.ntargets))
57 		return -EINVAL;
58 
59 	*cfg = spinand->cfg_cache[spinand->cur_target];
60 	return 0;
61 }
62 
spinand_set_cfg(struct spinand_device * spinand,u8 cfg)63 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg)
64 {
65 	struct nand_device *nand = spinand_to_nand(spinand);
66 	int ret;
67 
68 	if (WARN_ON(spinand->cur_target < 0 ||
69 		    spinand->cur_target >= nand->memorg.ntargets))
70 		return -EINVAL;
71 
72 	if (spinand->cfg_cache[spinand->cur_target] == cfg)
73 		return 0;
74 
75 	ret = spinand_write_reg_op(spinand, REG_CFG, cfg);
76 	if (ret)
77 		return ret;
78 
79 	spinand->cfg_cache[spinand->cur_target] = cfg;
80 	return 0;
81 }
82 
83 /**
84  * spinand_upd_cfg() - Update the configuration register
85  * @spinand: the spinand device
86  * @mask: the mask encoding the bits to update in the config reg
87  * @val: the new value to apply
88  *
89  * Update the configuration register.
90  *
91  * Return: 0 on success, a negative error code otherwise.
92  */
spinand_upd_cfg(struct spinand_device * spinand,u8 mask,u8 val)93 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
94 {
95 	int ret;
96 	u8 cfg;
97 
98 	ret = spinand_get_cfg(spinand, &cfg);
99 	if (ret)
100 		return ret;
101 
102 	cfg &= ~mask;
103 	cfg |= val;
104 
105 	return spinand_set_cfg(spinand, cfg);
106 }
107 
108 /**
109  * spinand_select_target() - Select a specific NAND target/die
110  * @spinand: the spinand device
111  * @target: the target/die to select
112  *
113  * Select a new target/die. If chip only has one die, this function is a NOOP.
114  *
115  * Return: 0 on success, a negative error code otherwise.
116  */
spinand_select_target(struct spinand_device * spinand,unsigned int target)117 int spinand_select_target(struct spinand_device *spinand, unsigned int target)
118 {
119 	struct nand_device *nand = spinand_to_nand(spinand);
120 	int ret;
121 
122 	if (WARN_ON(target >= nand->memorg.ntargets))
123 		return -EINVAL;
124 
125 	if (spinand->cur_target == target)
126 		return 0;
127 
128 	if (nand->memorg.ntargets == 1) {
129 		spinand->cur_target = target;
130 		return 0;
131 	}
132 
133 	ret = spinand->select_target(spinand, target);
134 	if (ret)
135 		return ret;
136 
137 	spinand->cur_target = target;
138 	return 0;
139 }
140 
spinand_read_cfg(struct spinand_device * spinand)141 static int spinand_read_cfg(struct spinand_device *spinand)
142 {
143 	struct nand_device *nand = spinand_to_nand(spinand);
144 	unsigned int target;
145 	int ret;
146 
147 	for (target = 0; target < nand->memorg.ntargets; target++) {
148 		ret = spinand_select_target(spinand, target);
149 		if (ret)
150 			return ret;
151 
152 		/*
153 		 * We use spinand_read_reg_op() instead of spinand_get_cfg()
154 		 * here to bypass the config cache.
155 		 */
156 		ret = spinand_read_reg_op(spinand, REG_CFG,
157 					  &spinand->cfg_cache[target]);
158 		if (ret)
159 			return ret;
160 	}
161 
162 	return 0;
163 }
164 
spinand_init_cfg_cache(struct spinand_device * spinand)165 static int spinand_init_cfg_cache(struct spinand_device *spinand)
166 {
167 	struct nand_device *nand = spinand_to_nand(spinand);
168 	struct device *dev = &spinand->spimem->spi->dev;
169 
170 	spinand->cfg_cache = devm_kcalloc(dev,
171 					  nand->memorg.ntargets,
172 					  sizeof(*spinand->cfg_cache),
173 					  GFP_KERNEL);
174 	if (!spinand->cfg_cache)
175 		return -ENOMEM;
176 
177 	return 0;
178 }
179 
spinand_init_quad_enable(struct spinand_device * spinand)180 static int spinand_init_quad_enable(struct spinand_device *spinand)
181 {
182 	bool enable = false;
183 
184 	if (!(spinand->flags & SPINAND_HAS_QE_BIT))
185 		return 0;
186 
187 	if (spinand->op_templates.read_cache->data.buswidth == 4 ||
188 	    spinand->op_templates.write_cache->data.buswidth == 4 ||
189 	    spinand->op_templates.update_cache->data.buswidth == 4)
190 		enable = true;
191 
192 	return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE,
193 			       enable ? CFG_QUAD_ENABLE : 0);
194 }
195 
spinand_ecc_enable(struct spinand_device * spinand,bool enable)196 static int spinand_ecc_enable(struct spinand_device *spinand,
197 			      bool enable)
198 {
199 	return spinand_upd_cfg(spinand, CFG_ECC_ENABLE,
200 			       enable ? CFG_ECC_ENABLE : 0);
201 }
202 
spinand_cont_read_enable(struct spinand_device * spinand,bool enable)203 static int spinand_cont_read_enable(struct spinand_device *spinand,
204 				    bool enable)
205 {
206 	return spinand->set_cont_read(spinand, enable);
207 }
208 
spinand_check_ecc_status(struct spinand_device * spinand,u8 status)209 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
210 {
211 	struct nand_device *nand = spinand_to_nand(spinand);
212 
213 	if (spinand->eccinfo.get_status)
214 		return spinand->eccinfo.get_status(spinand, status);
215 
216 	switch (status & STATUS_ECC_MASK) {
217 	case STATUS_ECC_NO_BITFLIPS:
218 		return 0;
219 
220 	case STATUS_ECC_HAS_BITFLIPS:
221 		/*
222 		 * We have no way to know exactly how many bitflips have been
223 		 * fixed, so let's return the maximum possible value so that
224 		 * wear-leveling layers move the data immediately.
225 		 */
226 		return nanddev_get_ecc_conf(nand)->strength;
227 
228 	case STATUS_ECC_UNCOR_ERROR:
229 		return -EBADMSG;
230 
231 	default:
232 		break;
233 	}
234 
235 	return -EINVAL;
236 }
237 
spinand_noecc_ooblayout_ecc(struct mtd_info * mtd,int section,struct mtd_oob_region * region)238 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
239 				       struct mtd_oob_region *region)
240 {
241 	return -ERANGE;
242 }
243 
spinand_noecc_ooblayout_free(struct mtd_info * mtd,int section,struct mtd_oob_region * region)244 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
245 					struct mtd_oob_region *region)
246 {
247 	if (section)
248 		return -ERANGE;
249 
250 	/* Reserve 2 bytes for the BBM. */
251 	region->offset = 2;
252 	region->length = 62;
253 
254 	return 0;
255 }
256 
257 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
258 	.ecc = spinand_noecc_ooblayout_ecc,
259 	.free = spinand_noecc_ooblayout_free,
260 };
261 
spinand_ondie_ecc_init_ctx(struct nand_device * nand)262 static int spinand_ondie_ecc_init_ctx(struct nand_device *nand)
263 {
264 	struct spinand_device *spinand = nand_to_spinand(nand);
265 	struct mtd_info *mtd = nanddev_to_mtd(nand);
266 	struct spinand_ondie_ecc_conf *engine_conf;
267 
268 	nand->ecc.ctx.conf.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE;
269 	nand->ecc.ctx.conf.step_size = nand->ecc.requirements.step_size;
270 	nand->ecc.ctx.conf.strength = nand->ecc.requirements.strength;
271 
272 	engine_conf = kzalloc(sizeof(*engine_conf), GFP_KERNEL);
273 	if (!engine_conf)
274 		return -ENOMEM;
275 
276 	nand->ecc.ctx.priv = engine_conf;
277 
278 	if (spinand->eccinfo.ooblayout)
279 		mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
280 	else
281 		mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
282 
283 	return 0;
284 }
285 
spinand_ondie_ecc_cleanup_ctx(struct nand_device * nand)286 static void spinand_ondie_ecc_cleanup_ctx(struct nand_device *nand)
287 {
288 	kfree(nand->ecc.ctx.priv);
289 }
290 
spinand_ondie_ecc_prepare_io_req(struct nand_device * nand,struct nand_page_io_req * req)291 static int spinand_ondie_ecc_prepare_io_req(struct nand_device *nand,
292 					    struct nand_page_io_req *req)
293 {
294 	struct spinand_device *spinand = nand_to_spinand(nand);
295 	bool enable = (req->mode != MTD_OPS_RAW);
296 
297 	if (!enable && spinand->flags & SPINAND_NO_RAW_ACCESS)
298 		return -EOPNOTSUPP;
299 
300 	memset(spinand->oobbuf, 0xff, nanddev_per_page_oobsize(nand));
301 
302 	/* Only enable or disable the engine */
303 	return spinand_ecc_enable(spinand, enable);
304 }
305 
spinand_ondie_ecc_finish_io_req(struct nand_device * nand,struct nand_page_io_req * req)306 static int spinand_ondie_ecc_finish_io_req(struct nand_device *nand,
307 					   struct nand_page_io_req *req)
308 {
309 	struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv;
310 	struct spinand_device *spinand = nand_to_spinand(nand);
311 	struct mtd_info *mtd = spinand_to_mtd(spinand);
312 	int ret;
313 
314 	if (req->mode == MTD_OPS_RAW)
315 		return 0;
316 
317 	/* Nothing to do when finishing a page write */
318 	if (req->type == NAND_PAGE_WRITE)
319 		return 0;
320 
321 	/* Finish a page read: check the status, report errors/bitflips */
322 	ret = spinand_check_ecc_status(spinand, engine_conf->status);
323 	if (ret == -EBADMSG) {
324 		mtd->ecc_stats.failed++;
325 	} else if (ret > 0) {
326 		unsigned int pages;
327 
328 		/*
329 		 * Continuous reads don't allow us to get the detail,
330 		 * so we may exagerate the actual number of corrected bitflips.
331 		 */
332 		if (!req->continuous)
333 			pages = 1;
334 		else
335 			pages = req->datalen / nanddev_page_size(nand);
336 
337 		mtd->ecc_stats.corrected += ret * pages;
338 	}
339 
340 	return ret;
341 }
342 
343 static const struct nand_ecc_engine_ops spinand_ondie_ecc_engine_ops = {
344 	.init_ctx = spinand_ondie_ecc_init_ctx,
345 	.cleanup_ctx = spinand_ondie_ecc_cleanup_ctx,
346 	.prepare_io_req = spinand_ondie_ecc_prepare_io_req,
347 	.finish_io_req = spinand_ondie_ecc_finish_io_req,
348 };
349 
350 static struct nand_ecc_engine spinand_ondie_ecc_engine = {
351 	.ops = &spinand_ondie_ecc_engine_ops,
352 };
353 
spinand_ondie_ecc_save_status(struct nand_device * nand,u8 status)354 static void spinand_ondie_ecc_save_status(struct nand_device *nand, u8 status)
355 {
356 	struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv;
357 
358 	if (nand->ecc.ctx.conf.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE &&
359 	    engine_conf)
360 		engine_conf->status = status;
361 }
362 
spinand_write_enable_op(struct spinand_device * spinand)363 static int spinand_write_enable_op(struct spinand_device *spinand)
364 {
365 	struct spi_mem_op op = SPINAND_WR_EN_DIS_1S_0_0_OP(true);
366 
367 	return spi_mem_exec_op(spinand->spimem, &op);
368 }
369 
spinand_load_page_op(struct spinand_device * spinand,const struct nand_page_io_req * req)370 static int spinand_load_page_op(struct spinand_device *spinand,
371 				const struct nand_page_io_req *req)
372 {
373 	struct nand_device *nand = spinand_to_nand(spinand);
374 	unsigned int row = nanddev_pos_to_row(nand, &req->pos);
375 	struct spi_mem_op op = SPINAND_PAGE_READ_1S_1S_0_OP(row);
376 
377 	return spi_mem_exec_op(spinand->spimem, &op);
378 }
379 
spinand_read_from_cache_op(struct spinand_device * spinand,const struct nand_page_io_req * req)380 static int spinand_read_from_cache_op(struct spinand_device *spinand,
381 				      const struct nand_page_io_req *req)
382 {
383 	struct nand_device *nand = spinand_to_nand(spinand);
384 	struct mtd_info *mtd = spinand_to_mtd(spinand);
385 	struct spi_mem_dirmap_desc *rdesc;
386 	unsigned int nbytes = 0;
387 	void *buf = NULL;
388 	u16 column = 0;
389 	ssize_t ret;
390 
391 	if (req->datalen) {
392 		buf = spinand->databuf;
393 		if (!req->continuous)
394 			nbytes = nanddev_page_size(nand);
395 		else
396 			nbytes = round_up(req->dataoffs + req->datalen,
397 					  nanddev_page_size(nand));
398 		column = 0;
399 	}
400 
401 	if (req->ooblen) {
402 		nbytes += nanddev_per_page_oobsize(nand);
403 		if (!buf) {
404 			buf = spinand->oobbuf;
405 			column = nanddev_page_size(nand);
406 		}
407 	}
408 
409 	if (req->mode == MTD_OPS_RAW)
410 		rdesc = spinand->dirmaps[req->pos.plane].rdesc;
411 	else
412 		rdesc = spinand->dirmaps[req->pos.plane].rdesc_ecc;
413 
414 	if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT)
415 		column |= req->pos.plane << fls(nanddev_page_size(nand));
416 
417 	while (nbytes) {
418 		ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf);
419 		if (ret < 0)
420 			return ret;
421 
422 		if (!ret || ret > nbytes)
423 			return -EIO;
424 
425 		nbytes -= ret;
426 		column += ret;
427 		buf += ret;
428 
429 		/*
430 		 * Dirmap accesses are allowed to toggle the CS.
431 		 * Toggling the CS during a continuous read is forbidden.
432 		 */
433 		if (nbytes && req->continuous)
434 			return -EIO;
435 	}
436 
437 	if (req->datalen)
438 		memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
439 		       req->datalen);
440 
441 	if (req->ooblen) {
442 		if (req->mode == MTD_OPS_AUTO_OOB)
443 			mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
444 						    spinand->oobbuf,
445 						    req->ooboffs,
446 						    req->ooblen);
447 		else
448 			memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
449 			       req->ooblen);
450 	}
451 
452 	return 0;
453 }
454 
spinand_write_to_cache_op(struct spinand_device * spinand,const struct nand_page_io_req * req)455 static int spinand_write_to_cache_op(struct spinand_device *spinand,
456 				     const struct nand_page_io_req *req)
457 {
458 	struct nand_device *nand = spinand_to_nand(spinand);
459 	struct mtd_info *mtd = spinand_to_mtd(spinand);
460 	struct spi_mem_dirmap_desc *wdesc;
461 	unsigned int nbytes, column = 0;
462 	void *buf = spinand->databuf;
463 	ssize_t ret;
464 
465 	/*
466 	 * Looks like PROGRAM LOAD (AKA write cache) does not necessarily reset
467 	 * the cache content to 0xFF (depends on vendor implementation), so we
468 	 * must fill the page cache entirely even if we only want to program
469 	 * the data portion of the page, otherwise we might corrupt the BBM or
470 	 * user data previously programmed in OOB area.
471 	 *
472 	 * Only reset the data buffer manually, the OOB buffer is prepared by
473 	 * ECC engines ->prepare_io_req() callback.
474 	 */
475 	nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
476 	memset(spinand->databuf, 0xff, nanddev_page_size(nand));
477 
478 	if (req->datalen)
479 		memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
480 		       req->datalen);
481 
482 	if (req->ooblen) {
483 		if (req->mode == MTD_OPS_AUTO_OOB)
484 			mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
485 						    spinand->oobbuf,
486 						    req->ooboffs,
487 						    req->ooblen);
488 		else
489 			memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
490 			       req->ooblen);
491 	}
492 
493 	if (req->mode == MTD_OPS_RAW)
494 		wdesc = spinand->dirmaps[req->pos.plane].wdesc;
495 	else
496 		wdesc = spinand->dirmaps[req->pos.plane].wdesc_ecc;
497 
498 	if (spinand->flags & SPINAND_HAS_PROG_PLANE_SELECT_BIT)
499 		column |= req->pos.plane << fls(nanddev_page_size(nand));
500 
501 	while (nbytes) {
502 		ret = spi_mem_dirmap_write(wdesc, column, nbytes, buf);
503 		if (ret < 0)
504 			return ret;
505 
506 		if (!ret || ret > nbytes)
507 			return -EIO;
508 
509 		nbytes -= ret;
510 		column += ret;
511 		buf += ret;
512 	}
513 
514 	return 0;
515 }
516 
spinand_program_op(struct spinand_device * spinand,const struct nand_page_io_req * req)517 static int spinand_program_op(struct spinand_device *spinand,
518 			      const struct nand_page_io_req *req)
519 {
520 	struct nand_device *nand = spinand_to_nand(spinand);
521 	unsigned int row = nanddev_pos_to_row(nand, &req->pos);
522 	struct spi_mem_op op = SPINAND_PROG_EXEC_1S_1S_0_OP(row);
523 
524 	return spi_mem_exec_op(spinand->spimem, &op);
525 }
526 
spinand_erase_op(struct spinand_device * spinand,const struct nand_pos * pos)527 static int spinand_erase_op(struct spinand_device *spinand,
528 			    const struct nand_pos *pos)
529 {
530 	struct nand_device *nand = spinand_to_nand(spinand);
531 	unsigned int row = nanddev_pos_to_row(nand, pos);
532 	struct spi_mem_op op = SPINAND_BLK_ERASE_1S_1S_0_OP(row);
533 
534 	return spi_mem_exec_op(spinand->spimem, &op);
535 }
536 
537 /**
538  * spinand_wait() - Poll memory device status
539  * @spinand: the spinand device
540  * @initial_delay_us: delay in us before starting to poll
541  * @poll_delay_us: time to sleep between reads in us
542  * @s: the pointer to variable to store the value of REG_STATUS
543  *
544  * This function polls a status register (REG_STATUS) and returns when
545  * the STATUS_READY bit is 0 or when the timeout has expired.
546  *
547  * Return: 0 on success, a negative error code otherwise.
548  */
spinand_wait(struct spinand_device * spinand,unsigned long initial_delay_us,unsigned long poll_delay_us,u8 * s)549 int spinand_wait(struct spinand_device *spinand, unsigned long initial_delay_us,
550 		 unsigned long poll_delay_us, u8 *s)
551 {
552 	struct spi_mem_op op = SPINAND_GET_FEATURE_1S_1S_1S_OP(REG_STATUS,
553 							       spinand->scratchbuf);
554 	u8 status;
555 	int ret;
556 
557 	ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0,
558 				  initial_delay_us,
559 				  poll_delay_us,
560 				  SPINAND_WAITRDY_TIMEOUT_MS);
561 	if (ret)
562 		return ret;
563 
564 	status = *spinand->scratchbuf;
565 	if (!(status & STATUS_BUSY))
566 		goto out;
567 
568 	/*
569 	 * Extra read, just in case the STATUS_READY bit has changed
570 	 * since our last check
571 	 */
572 	ret = spinand_read_status(spinand, &status);
573 	if (ret)
574 		return ret;
575 
576 out:
577 	if (s)
578 		*s = status;
579 
580 	return status & STATUS_BUSY ? -ETIMEDOUT : 0;
581 }
582 
spinand_read_id_op(struct spinand_device * spinand,u8 naddr,u8 ndummy,u8 * buf)583 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr,
584 			      u8 ndummy, u8 *buf)
585 {
586 	struct spi_mem_op op = SPINAND_READID_1S_1S_1S_OP(
587 		naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
588 	int ret;
589 
590 	ret = spi_mem_exec_op(spinand->spimem, &op);
591 	if (!ret)
592 		memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
593 
594 	return ret;
595 }
596 
spinand_reset_op(struct spinand_device * spinand)597 static int spinand_reset_op(struct spinand_device *spinand)
598 {
599 	struct spi_mem_op op = SPINAND_RESET_1S_0_0_OP;
600 	int ret;
601 
602 	ret = spi_mem_exec_op(spinand->spimem, &op);
603 	if (ret)
604 		return ret;
605 
606 	return spinand_wait(spinand,
607 			    SPINAND_RESET_INITIAL_DELAY_US,
608 			    SPINAND_RESET_POLL_DELAY_US,
609 			    NULL);
610 }
611 
spinand_lock_block(struct spinand_device * spinand,u8 lock)612 static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
613 {
614 	return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
615 }
616 
617 /**
618  * spinand_read_page() - Read a page
619  * @spinand: the spinand device
620  * @req: the I/O request
621  *
622  * Return: 0 or a positive number of bitflips corrected on success.
623  * A negative error code otherwise.
624  */
spinand_read_page(struct spinand_device * spinand,const struct nand_page_io_req * req)625 int spinand_read_page(struct spinand_device *spinand,
626 		      const struct nand_page_io_req *req)
627 {
628 	struct nand_device *nand = spinand_to_nand(spinand);
629 	u8 status;
630 	int ret;
631 
632 	ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
633 	if (ret)
634 		return ret;
635 
636 	ret = spinand_load_page_op(spinand, req);
637 	if (ret)
638 		return ret;
639 
640 	ret = spinand_wait(spinand,
641 			   SPINAND_READ_INITIAL_DELAY_US,
642 			   SPINAND_READ_POLL_DELAY_US,
643 			   &status);
644 	if (ret < 0)
645 		return ret;
646 
647 	spinand_ondie_ecc_save_status(nand, status);
648 
649 	ret = spinand_read_from_cache_op(spinand, req);
650 	if (ret)
651 		return ret;
652 
653 	return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
654 }
655 
656 /**
657  * spinand_write_page() - Write a page
658  * @spinand: the spinand device
659  * @req: the I/O request
660  *
661  * Return: 0 or a positive number of bitflips corrected on success.
662  * A negative error code otherwise.
663  */
spinand_write_page(struct spinand_device * spinand,const struct nand_page_io_req * req)664 int spinand_write_page(struct spinand_device *spinand,
665 		       const struct nand_page_io_req *req)
666 {
667 	struct nand_device *nand = spinand_to_nand(spinand);
668 	u8 status;
669 	int ret;
670 
671 	ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
672 	if (ret)
673 		return ret;
674 
675 	ret = spinand_write_enable_op(spinand);
676 	if (ret)
677 		return ret;
678 
679 	ret = spinand_write_to_cache_op(spinand, req);
680 	if (ret)
681 		return ret;
682 
683 	ret = spinand_program_op(spinand, req);
684 	if (ret)
685 		return ret;
686 
687 	ret = spinand_wait(spinand,
688 			   SPINAND_WRITE_INITIAL_DELAY_US,
689 			   SPINAND_WRITE_POLL_DELAY_US,
690 			   &status);
691 	if (!ret && (status & STATUS_PROG_FAILED))
692 		return -EIO;
693 
694 	return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
695 }
696 
spinand_mtd_regular_page_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops,unsigned int * max_bitflips)697 static int spinand_mtd_regular_page_read(struct mtd_info *mtd, loff_t from,
698 					 struct mtd_oob_ops *ops,
699 					 unsigned int *max_bitflips)
700 {
701 	struct spinand_device *spinand = mtd_to_spinand(mtd);
702 	struct nand_device *nand = mtd_to_nanddev(mtd);
703 	struct mtd_ecc_stats old_stats;
704 	struct nand_io_iter iter;
705 	bool disable_ecc = false;
706 	bool ecc_failed = false;
707 	unsigned int retry_mode = 0;
708 	int ret;
709 
710 	old_stats = mtd->ecc_stats;
711 
712 	if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
713 		disable_ecc = true;
714 
715 	nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) {
716 		if (disable_ecc)
717 			iter.req.mode = MTD_OPS_RAW;
718 
719 		ret = spinand_select_target(spinand, iter.req.pos.target);
720 		if (ret)
721 			break;
722 
723 read_retry:
724 		ret = spinand_read_page(spinand, &iter.req);
725 		if (ret < 0 && ret != -EBADMSG)
726 			break;
727 
728 		if (ret == -EBADMSG && spinand->set_read_retry) {
729 			if (spinand->read_retries && (++retry_mode <= spinand->read_retries)) {
730 				ret = spinand->set_read_retry(spinand, retry_mode);
731 				if (ret < 0) {
732 					spinand->set_read_retry(spinand, 0);
733 					return ret;
734 				}
735 
736 				/* Reset ecc_stats; retry */
737 				mtd->ecc_stats = old_stats;
738 				goto read_retry;
739 			} else {
740 				/* No more retry modes; real failure */
741 				ecc_failed = true;
742 			}
743 		} else if (ret == -EBADMSG) {
744 			ecc_failed = true;
745 		} else {
746 			*max_bitflips = max_t(unsigned int, *max_bitflips, ret);
747 		}
748 
749 		ret = 0;
750 		ops->retlen += iter.req.datalen;
751 		ops->oobretlen += iter.req.ooblen;
752 
753 		/* Reset to retry mode 0 */
754 		if (retry_mode) {
755 			retry_mode = 0;
756 			ret = spinand->set_read_retry(spinand, retry_mode);
757 			if (ret < 0)
758 				return ret;
759 		}
760 	}
761 
762 	if (ecc_failed && !ret)
763 		ret = -EBADMSG;
764 
765 	return ret;
766 }
767 
spinand_mtd_continuous_page_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops,unsigned int * max_bitflips)768 static int spinand_mtd_continuous_page_read(struct mtd_info *mtd, loff_t from,
769 					    struct mtd_oob_ops *ops,
770 					    unsigned int *max_bitflips)
771 {
772 	struct spinand_device *spinand = mtd_to_spinand(mtd);
773 	struct nand_device *nand = mtd_to_nanddev(mtd);
774 	struct nand_io_iter iter;
775 	u8 status;
776 	int ret;
777 
778 	ret = spinand_cont_read_enable(spinand, true);
779 	if (ret)
780 		return ret;
781 
782 	/*
783 	 * The cache is divided into two halves. While one half of the cache has
784 	 * the requested data, the other half is loaded with the next chunk of data.
785 	 * Therefore, the host can read out the data continuously from page to page.
786 	 * Each data read must be a multiple of 4-bytes and full pages should be read;
787 	 * otherwise, the data output might get out of sequence from one read command
788 	 * to another.
789 	 */
790 	nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) {
791 		ret = spinand_select_target(spinand, iter.req.pos.target);
792 		if (ret)
793 			goto end_cont_read;
794 
795 		ret = nand_ecc_prepare_io_req(nand, &iter.req);
796 		if (ret)
797 			goto end_cont_read;
798 
799 		ret = spinand_load_page_op(spinand, &iter.req);
800 		if (ret)
801 			goto end_cont_read;
802 
803 		ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US,
804 				   SPINAND_READ_POLL_DELAY_US, NULL);
805 		if (ret < 0)
806 			goto end_cont_read;
807 
808 		ret = spinand_read_from_cache_op(spinand, &iter.req);
809 		if (ret)
810 			goto end_cont_read;
811 
812 		ops->retlen += iter.req.datalen;
813 
814 		ret = spinand_read_status(spinand, &status);
815 		if (ret)
816 			goto end_cont_read;
817 
818 		spinand_ondie_ecc_save_status(nand, status);
819 
820 		ret = nand_ecc_finish_io_req(nand, &iter.req);
821 		if (ret < 0)
822 			goto end_cont_read;
823 
824 		*max_bitflips = max_t(unsigned int, *max_bitflips, ret);
825 		ret = 0;
826 	}
827 
828 end_cont_read:
829 	/*
830 	 * Once all the data has been read out, the host can either pull CS#
831 	 * high and wait for tRST or manually clear the bit in the configuration
832 	 * register to terminate the continuous read operation. We have no
833 	 * guarantee the SPI controller drivers will effectively deassert the CS
834 	 * when we expect them to, so take the register based approach.
835 	 */
836 	spinand_cont_read_enable(spinand, false);
837 
838 	return ret;
839 }
840 
spinand_cont_read_init(struct spinand_device * spinand)841 static void spinand_cont_read_init(struct spinand_device *spinand)
842 {
843 	struct nand_device *nand = spinand_to_nand(spinand);
844 	enum nand_ecc_engine_type engine_type = nand->ecc.ctx.conf.engine_type;
845 
846 	/* OOBs cannot be retrieved so external/on-host ECC engine won't work */
847 	if (spinand->set_cont_read &&
848 	    (engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE ||
849 	     engine_type == NAND_ECC_ENGINE_TYPE_NONE)) {
850 		spinand->cont_read_possible = true;
851 	}
852 }
853 
spinand_use_cont_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)854 static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from,
855 				  struct mtd_oob_ops *ops)
856 {
857 	struct nand_device *nand = mtd_to_nanddev(mtd);
858 	struct spinand_device *spinand = nand_to_spinand(nand);
859 	struct nand_pos start_pos, end_pos;
860 
861 	if (!spinand->cont_read_possible)
862 		return false;
863 
864 	/* OOBs won't be retrieved */
865 	if (ops->ooblen || ops->oobbuf)
866 		return false;
867 
868 	nanddev_offs_to_pos(nand, from, &start_pos);
869 	nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos);
870 
871 	/*
872 	 * Continuous reads never cross LUN boundaries. Some devices don't
873 	 * support crossing planes boundaries. Some devices don't even support
874 	 * crossing blocks boundaries. The common case being to read through UBI,
875 	 * we will very rarely read two consequent blocks or more, so it is safer
876 	 * and easier (can be improved) to only enable continuous reads when
877 	 * reading within the same erase block.
878 	 */
879 	if (start_pos.target != end_pos.target ||
880 	    start_pos.plane != end_pos.plane ||
881 	    start_pos.eraseblock != end_pos.eraseblock)
882 		return false;
883 
884 	return start_pos.page < end_pos.page;
885 }
886 
spinand_mtd_read(struct mtd_info * mtd,loff_t from,struct mtd_oob_ops * ops)887 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
888 			    struct mtd_oob_ops *ops)
889 {
890 	struct spinand_device *spinand = mtd_to_spinand(mtd);
891 	struct mtd_ecc_stats old_stats;
892 	unsigned int max_bitflips = 0;
893 	int ret;
894 
895 	mutex_lock(&spinand->lock);
896 
897 	old_stats = mtd->ecc_stats;
898 
899 	if (spinand_use_cont_read(mtd, from, ops))
900 		ret = spinand_mtd_continuous_page_read(mtd, from, ops, &max_bitflips);
901 	else
902 		ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips);
903 
904 	if (ops->stats) {
905 		ops->stats->uncorrectable_errors +=
906 			mtd->ecc_stats.failed - old_stats.failed;
907 		ops->stats->corrected_bitflips +=
908 			mtd->ecc_stats.corrected - old_stats.corrected;
909 	}
910 
911 	mutex_unlock(&spinand->lock);
912 
913 	return ret ? ret : max_bitflips;
914 }
915 
spinand_mtd_write(struct mtd_info * mtd,loff_t to,struct mtd_oob_ops * ops)916 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
917 			     struct mtd_oob_ops *ops)
918 {
919 	struct spinand_device *spinand = mtd_to_spinand(mtd);
920 	struct nand_device *nand = mtd_to_nanddev(mtd);
921 	struct nand_io_iter iter;
922 	bool disable_ecc = false;
923 	int ret = 0;
924 
925 	if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
926 		disable_ecc = true;
927 
928 	mutex_lock(&spinand->lock);
929 
930 	nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) {
931 		if (disable_ecc)
932 			iter.req.mode = MTD_OPS_RAW;
933 
934 		ret = spinand_select_target(spinand, iter.req.pos.target);
935 		if (ret)
936 			break;
937 
938 		ret = spinand_write_page(spinand, &iter.req);
939 		if (ret)
940 			break;
941 
942 		ops->retlen += iter.req.datalen;
943 		ops->oobretlen += iter.req.ooblen;
944 	}
945 
946 	mutex_unlock(&spinand->lock);
947 
948 	return ret;
949 }
950 
spinand_isbad(struct nand_device * nand,const struct nand_pos * pos)951 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
952 {
953 	struct spinand_device *spinand = nand_to_spinand(nand);
954 	u8 marker[2] = { };
955 	struct nand_page_io_req req = {
956 		.pos = *pos,
957 		.ooblen = sizeof(marker),
958 		.ooboffs = 0,
959 		.oobbuf.in = marker,
960 		.mode = MTD_OPS_RAW,
961 	};
962 	int ret;
963 
964 	spinand_select_target(spinand, pos->target);
965 
966 	ret = spinand_read_page(spinand, &req);
967 	if (ret == -EOPNOTSUPP) {
968 		/* Retry with ECC in case raw access is not supported */
969 		req.mode = MTD_OPS_PLACE_OOB;
970 		spinand_read_page(spinand, &req);
971 	}
972 
973 	if (marker[0] != 0xff || marker[1] != 0xff)
974 		return true;
975 
976 	return false;
977 }
978 
spinand_mtd_block_isbad(struct mtd_info * mtd,loff_t offs)979 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
980 {
981 	struct nand_device *nand = mtd_to_nanddev(mtd);
982 	struct spinand_device *spinand = nand_to_spinand(nand);
983 	struct nand_pos pos;
984 	int ret;
985 
986 	nanddev_offs_to_pos(nand, offs, &pos);
987 	mutex_lock(&spinand->lock);
988 	ret = nanddev_isbad(nand, &pos);
989 	mutex_unlock(&spinand->lock);
990 
991 	return ret;
992 }
993 
spinand_markbad(struct nand_device * nand,const struct nand_pos * pos)994 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
995 {
996 	struct spinand_device *spinand = nand_to_spinand(nand);
997 	u8 marker[2] = { };
998 	struct nand_page_io_req req = {
999 		.pos = *pos,
1000 		.ooboffs = 0,
1001 		.ooblen = sizeof(marker),
1002 		.oobbuf.out = marker,
1003 		.mode = MTD_OPS_RAW,
1004 	};
1005 	int ret;
1006 
1007 	ret = spinand_select_target(spinand, pos->target);
1008 	if (ret)
1009 		return ret;
1010 
1011 	ret = spinand_write_page(spinand, &req);
1012 	if (ret == -EOPNOTSUPP) {
1013 		/* Retry with ECC in case raw access is not supported */
1014 		req.mode = MTD_OPS_PLACE_OOB;
1015 		ret = spinand_write_page(spinand, &req);
1016 	}
1017 
1018 	return ret;
1019 }
1020 
spinand_mtd_block_markbad(struct mtd_info * mtd,loff_t offs)1021 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
1022 {
1023 	struct nand_device *nand = mtd_to_nanddev(mtd);
1024 	struct spinand_device *spinand = nand_to_spinand(nand);
1025 	struct nand_pos pos;
1026 	int ret;
1027 
1028 	nanddev_offs_to_pos(nand, offs, &pos);
1029 	mutex_lock(&spinand->lock);
1030 	ret = nanddev_markbad(nand, &pos);
1031 	mutex_unlock(&spinand->lock);
1032 
1033 	return ret;
1034 }
1035 
spinand_erase(struct nand_device * nand,const struct nand_pos * pos)1036 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
1037 {
1038 	struct spinand_device *spinand = nand_to_spinand(nand);
1039 	u8 status;
1040 	int ret;
1041 
1042 	ret = spinand_select_target(spinand, pos->target);
1043 	if (ret)
1044 		return ret;
1045 
1046 	ret = spinand_write_enable_op(spinand);
1047 	if (ret)
1048 		return ret;
1049 
1050 	ret = spinand_erase_op(spinand, pos);
1051 	if (ret)
1052 		return ret;
1053 
1054 	ret = spinand_wait(spinand,
1055 			   SPINAND_ERASE_INITIAL_DELAY_US,
1056 			   SPINAND_ERASE_POLL_DELAY_US,
1057 			   &status);
1058 
1059 	if (!ret && (status & STATUS_ERASE_FAILED))
1060 		ret = -EIO;
1061 
1062 	return ret;
1063 }
1064 
spinand_mtd_erase(struct mtd_info * mtd,struct erase_info * einfo)1065 static int spinand_mtd_erase(struct mtd_info *mtd,
1066 			     struct erase_info *einfo)
1067 {
1068 	struct spinand_device *spinand = mtd_to_spinand(mtd);
1069 	int ret;
1070 
1071 	mutex_lock(&spinand->lock);
1072 	ret = nanddev_mtd_erase(mtd, einfo);
1073 	mutex_unlock(&spinand->lock);
1074 
1075 	return ret;
1076 }
1077 
spinand_mtd_block_isreserved(struct mtd_info * mtd,loff_t offs)1078 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
1079 {
1080 	struct spinand_device *spinand = mtd_to_spinand(mtd);
1081 	struct nand_device *nand = mtd_to_nanddev(mtd);
1082 	struct nand_pos pos;
1083 	int ret;
1084 
1085 	nanddev_offs_to_pos(nand, offs, &pos);
1086 	mutex_lock(&spinand->lock);
1087 	ret = nanddev_isreserved(nand, &pos);
1088 	mutex_unlock(&spinand->lock);
1089 
1090 	return ret;
1091 }
1092 
spinand_create_dirmap(struct spinand_device * spinand,unsigned int plane)1093 static int spinand_create_dirmap(struct spinand_device *spinand,
1094 				 unsigned int plane)
1095 {
1096 	struct nand_device *nand = spinand_to_nand(spinand);
1097 	struct spi_mem_dirmap_info info = {
1098 		.length = nanddev_page_size(nand) +
1099 			  nanddev_per_page_oobsize(nand),
1100 	};
1101 	struct spi_mem_dirmap_desc *desc;
1102 
1103 	if (spinand->cont_read_possible)
1104 		info.length = nanddev_eraseblock_size(nand);
1105 
1106 	/* The plane number is passed in MSB just above the column address */
1107 	info.offset = plane << fls(nand->memorg.pagesize);
1108 
1109 	info.op_tmpl = *spinand->op_templates.update_cache;
1110 	desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1111 					  spinand->spimem, &info);
1112 	if (IS_ERR(desc))
1113 		return PTR_ERR(desc);
1114 
1115 	spinand->dirmaps[plane].wdesc = desc;
1116 
1117 	info.op_tmpl = *spinand->op_templates.read_cache;
1118 	desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1119 					  spinand->spimem, &info);
1120 	if (IS_ERR(desc))
1121 		return PTR_ERR(desc);
1122 
1123 	spinand->dirmaps[plane].rdesc = desc;
1124 
1125 	if (nand->ecc.engine->integration != NAND_ECC_ENGINE_INTEGRATION_PIPELINED) {
1126 		spinand->dirmaps[plane].wdesc_ecc = spinand->dirmaps[plane].wdesc;
1127 		spinand->dirmaps[plane].rdesc_ecc = spinand->dirmaps[plane].rdesc;
1128 
1129 		return 0;
1130 	}
1131 
1132 	info.op_tmpl = *spinand->op_templates.update_cache;
1133 	info.op_tmpl.data.ecc = true;
1134 	desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1135 					  spinand->spimem, &info);
1136 	if (IS_ERR(desc))
1137 		return PTR_ERR(desc);
1138 
1139 	spinand->dirmaps[plane].wdesc_ecc = desc;
1140 
1141 	info.op_tmpl = *spinand->op_templates.read_cache;
1142 	info.op_tmpl.data.ecc = true;
1143 	desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1144 					  spinand->spimem, &info);
1145 	if (IS_ERR(desc))
1146 		return PTR_ERR(desc);
1147 
1148 	spinand->dirmaps[plane].rdesc_ecc = desc;
1149 
1150 	return 0;
1151 }
1152 
spinand_create_dirmaps(struct spinand_device * spinand)1153 static int spinand_create_dirmaps(struct spinand_device *spinand)
1154 {
1155 	struct nand_device *nand = spinand_to_nand(spinand);
1156 	int i, ret;
1157 
1158 	spinand->dirmaps = devm_kzalloc(&spinand->spimem->spi->dev,
1159 					sizeof(*spinand->dirmaps) *
1160 					nand->memorg.planes_per_lun,
1161 					GFP_KERNEL);
1162 	if (!spinand->dirmaps)
1163 		return -ENOMEM;
1164 
1165 	for (i = 0; i < nand->memorg.planes_per_lun; i++) {
1166 		ret = spinand_create_dirmap(spinand, i);
1167 		if (ret)
1168 			return ret;
1169 	}
1170 
1171 	return 0;
1172 }
1173 
1174 static const struct nand_ops spinand_ops = {
1175 	.erase = spinand_erase,
1176 	.markbad = spinand_markbad,
1177 	.isbad = spinand_isbad,
1178 };
1179 
1180 static const struct spinand_manufacturer *spinand_manufacturers[] = {
1181 	&alliancememory_spinand_manufacturer,
1182 	&ato_spinand_manufacturer,
1183 	&esmt_c8_spinand_manufacturer,
1184 	&foresee_spinand_manufacturer,
1185 	&gigadevice_spinand_manufacturer,
1186 	&macronix_spinand_manufacturer,
1187 	&micron_spinand_manufacturer,
1188 	&paragon_spinand_manufacturer,
1189 	&skyhigh_spinand_manufacturer,
1190 	&toshiba_spinand_manufacturer,
1191 	&winbond_spinand_manufacturer,
1192 	&xtx_spinand_manufacturer,
1193 };
1194 
spinand_manufacturer_match(struct spinand_device * spinand,enum spinand_readid_method rdid_method)1195 static int spinand_manufacturer_match(struct spinand_device *spinand,
1196 				      enum spinand_readid_method rdid_method)
1197 {
1198 	u8 *id = spinand->id.data;
1199 	unsigned int i;
1200 	int ret;
1201 
1202 	for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
1203 		const struct spinand_manufacturer *manufacturer =
1204 			spinand_manufacturers[i];
1205 
1206 		if (id[0] != manufacturer->id)
1207 			continue;
1208 
1209 		ret = spinand_match_and_init(spinand,
1210 					     manufacturer->chips,
1211 					     manufacturer->nchips,
1212 					     rdid_method);
1213 		if (ret < 0)
1214 			continue;
1215 
1216 		spinand->manufacturer = manufacturer;
1217 		return 0;
1218 	}
1219 	return -EOPNOTSUPP;
1220 }
1221 
spinand_id_detect(struct spinand_device * spinand)1222 static int spinand_id_detect(struct spinand_device *spinand)
1223 {
1224 	u8 *id = spinand->id.data;
1225 	int ret;
1226 
1227 	ret = spinand_read_id_op(spinand, 0, 0, id);
1228 	if (ret)
1229 		return ret;
1230 	ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE);
1231 	if (!ret)
1232 		return 0;
1233 
1234 	ret = spinand_read_id_op(spinand, 1, 0, id);
1235 	if (ret)
1236 		return ret;
1237 	ret = spinand_manufacturer_match(spinand,
1238 					 SPINAND_READID_METHOD_OPCODE_ADDR);
1239 	if (!ret)
1240 		return 0;
1241 
1242 	ret = spinand_read_id_op(spinand, 0, 1, id);
1243 	if (ret)
1244 		return ret;
1245 	ret = spinand_manufacturer_match(spinand,
1246 					 SPINAND_READID_METHOD_OPCODE_DUMMY);
1247 
1248 	return ret;
1249 }
1250 
spinand_manufacturer_init(struct spinand_device * spinand)1251 static int spinand_manufacturer_init(struct spinand_device *spinand)
1252 {
1253 	if (spinand->manufacturer->ops->init)
1254 		return spinand->manufacturer->ops->init(spinand);
1255 
1256 	return 0;
1257 }
1258 
spinand_manufacturer_cleanup(struct spinand_device * spinand)1259 static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
1260 {
1261 	/* Release manufacturer private data */
1262 	if (spinand->manufacturer->ops->cleanup)
1263 		return spinand->manufacturer->ops->cleanup(spinand);
1264 }
1265 
1266 static const struct spi_mem_op *
spinand_select_op_variant(struct spinand_device * spinand,const struct spinand_op_variants * variants)1267 spinand_select_op_variant(struct spinand_device *spinand,
1268 			  const struct spinand_op_variants *variants)
1269 {
1270 	struct nand_device *nand = spinand_to_nand(spinand);
1271 	const struct spi_mem_op *best_variant = NULL;
1272 	u64 best_op_duration_ns = ULLONG_MAX;
1273 	unsigned int i;
1274 
1275 	for (i = 0; i < variants->nops; i++) {
1276 		struct spi_mem_op op = variants->ops[i];
1277 		u64 op_duration_ns = 0;
1278 		unsigned int nbytes;
1279 		int ret;
1280 
1281 		nbytes = nanddev_per_page_oobsize(nand) +
1282 			 nanddev_page_size(nand);
1283 
1284 		while (nbytes) {
1285 			op.data.nbytes = nbytes;
1286 			ret = spi_mem_adjust_op_size(spinand->spimem, &op);
1287 			if (ret)
1288 				break;
1289 
1290 			spi_mem_adjust_op_freq(spinand->spimem, &op);
1291 
1292 			if (!spi_mem_supports_op(spinand->spimem, &op))
1293 				break;
1294 
1295 			nbytes -= op.data.nbytes;
1296 
1297 			op_duration_ns += spi_mem_calc_op_duration(&op);
1298 		}
1299 
1300 		if (!nbytes && op_duration_ns < best_op_duration_ns) {
1301 			best_op_duration_ns = op_duration_ns;
1302 			best_variant = &variants->ops[i];
1303 		}
1304 	}
1305 
1306 	return best_variant;
1307 }
1308 
1309 /**
1310  * spinand_match_and_init() - Try to find a match between a device ID and an
1311  *			      entry in a spinand_info table
1312  * @spinand: SPI NAND object
1313  * @table: SPI NAND device description table
1314  * @table_size: size of the device description table
1315  * @rdid_method: read id method to match
1316  *
1317  * Match between a device ID retrieved through the READ_ID command and an
1318  * entry in the SPI NAND description table. If a match is found, the spinand
1319  * object will be initialized with information provided by the matching
1320  * spinand_info entry.
1321  *
1322  * Return: 0 on success, a negative error code otherwise.
1323  */
spinand_match_and_init(struct spinand_device * spinand,const struct spinand_info * table,unsigned int table_size,enum spinand_readid_method rdid_method)1324 int spinand_match_and_init(struct spinand_device *spinand,
1325 			   const struct spinand_info *table,
1326 			   unsigned int table_size,
1327 			   enum spinand_readid_method rdid_method)
1328 {
1329 	u8 *id = spinand->id.data;
1330 	struct nand_device *nand = spinand_to_nand(spinand);
1331 	unsigned int i;
1332 
1333 	for (i = 0; i < table_size; i++) {
1334 		const struct spinand_info *info = &table[i];
1335 		const struct spi_mem_op *op;
1336 
1337 		if (rdid_method != info->devid.method)
1338 			continue;
1339 
1340 		if (memcmp(id + 1, info->devid.id, info->devid.len))
1341 			continue;
1342 
1343 		nand->memorg = table[i].memorg;
1344 		nanddev_set_ecc_requirements(nand, &table[i].eccreq);
1345 		spinand->eccinfo = table[i].eccinfo;
1346 		spinand->flags = table[i].flags;
1347 		spinand->id.len = 1 + table[i].devid.len;
1348 		spinand->select_target = table[i].select_target;
1349 		spinand->set_cont_read = table[i].set_cont_read;
1350 		spinand->fact_otp = &table[i].fact_otp;
1351 		spinand->user_otp = &table[i].user_otp;
1352 		spinand->read_retries = table[i].read_retries;
1353 		spinand->set_read_retry = table[i].set_read_retry;
1354 
1355 		op = spinand_select_op_variant(spinand,
1356 					       info->op_variants.read_cache);
1357 		if (!op)
1358 			return -ENOTSUPP;
1359 
1360 		spinand->op_templates.read_cache = op;
1361 
1362 		op = spinand_select_op_variant(spinand,
1363 					       info->op_variants.write_cache);
1364 		if (!op)
1365 			return -ENOTSUPP;
1366 
1367 		spinand->op_templates.write_cache = op;
1368 
1369 		op = spinand_select_op_variant(spinand,
1370 					       info->op_variants.update_cache);
1371 		spinand->op_templates.update_cache = op;
1372 
1373 		return 0;
1374 	}
1375 
1376 	return -ENOTSUPP;
1377 }
1378 
spinand_detect(struct spinand_device * spinand)1379 static int spinand_detect(struct spinand_device *spinand)
1380 {
1381 	struct device *dev = &spinand->spimem->spi->dev;
1382 	struct nand_device *nand = spinand_to_nand(spinand);
1383 	int ret;
1384 
1385 	ret = spinand_reset_op(spinand);
1386 	if (ret)
1387 		return ret;
1388 
1389 	ret = spinand_id_detect(spinand);
1390 	if (ret) {
1391 		dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
1392 			spinand->id.data);
1393 		return ret;
1394 	}
1395 
1396 	if (nand->memorg.ntargets > 1 && !spinand->select_target) {
1397 		dev_err(dev,
1398 			"SPI NANDs with more than one die must implement ->select_target()\n");
1399 		return -EINVAL;
1400 	}
1401 
1402 	dev_info(&spinand->spimem->spi->dev,
1403 		 "%s SPI NAND was found.\n", spinand->manufacturer->name);
1404 	dev_info(&spinand->spimem->spi->dev,
1405 		 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
1406 		 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
1407 		 nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
1408 
1409 	return 0;
1410 }
1411 
spinand_init_flash(struct spinand_device * spinand)1412 static int spinand_init_flash(struct spinand_device *spinand)
1413 {
1414 	struct device *dev = &spinand->spimem->spi->dev;
1415 	struct nand_device *nand = spinand_to_nand(spinand);
1416 	int ret, i;
1417 
1418 	ret = spinand_read_cfg(spinand);
1419 	if (ret)
1420 		return ret;
1421 
1422 	ret = spinand_init_quad_enable(spinand);
1423 	if (ret)
1424 		return ret;
1425 
1426 	ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
1427 	if (ret)
1428 		return ret;
1429 
1430 	ret = spinand_manufacturer_init(spinand);
1431 	if (ret) {
1432 		dev_err(dev,
1433 		"Failed to initialize the SPI NAND chip (err = %d)\n",
1434 		ret);
1435 		return ret;
1436 	}
1437 
1438 	/* After power up, all blocks are locked, so unlock them here. */
1439 	for (i = 0; i < nand->memorg.ntargets; i++) {
1440 		ret = spinand_select_target(spinand, i);
1441 		if (ret)
1442 			break;
1443 
1444 		ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
1445 		if (ret)
1446 			break;
1447 	}
1448 
1449 	if (ret)
1450 		spinand_manufacturer_cleanup(spinand);
1451 
1452 	return ret;
1453 }
1454 
spinand_mtd_resume(struct mtd_info * mtd)1455 static void spinand_mtd_resume(struct mtd_info *mtd)
1456 {
1457 	struct spinand_device *spinand = mtd_to_spinand(mtd);
1458 	int ret;
1459 
1460 	ret = spinand_reset_op(spinand);
1461 	if (ret)
1462 		return;
1463 
1464 	ret = spinand_init_flash(spinand);
1465 	if (ret)
1466 		return;
1467 
1468 	spinand_ecc_enable(spinand, false);
1469 }
1470 
spinand_init(struct spinand_device * spinand)1471 static int spinand_init(struct spinand_device *spinand)
1472 {
1473 	struct device *dev = &spinand->spimem->spi->dev;
1474 	struct mtd_info *mtd = spinand_to_mtd(spinand);
1475 	struct nand_device *nand = mtd_to_nanddev(mtd);
1476 	int ret;
1477 
1478 	/*
1479 	 * We need a scratch buffer because the spi_mem interface requires that
1480 	 * buf passed in spi_mem_op->data.buf be DMA-able.
1481 	 */
1482 	spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
1483 	if (!spinand->scratchbuf)
1484 		return -ENOMEM;
1485 
1486 	ret = spinand_detect(spinand);
1487 	if (ret)
1488 		goto err_free_bufs;
1489 
1490 	/*
1491 	 * Use kzalloc() instead of devm_kzalloc() here, because some drivers
1492 	 * may use this buffer for DMA access.
1493 	 * Memory allocated by devm_ does not guarantee DMA-safe alignment.
1494 	 */
1495 	spinand->databuf = kzalloc(nanddev_eraseblock_size(nand),
1496 				   GFP_KERNEL);
1497 	if (!spinand->databuf) {
1498 		ret = -ENOMEM;
1499 		goto err_free_bufs;
1500 	}
1501 
1502 	spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
1503 
1504 	ret = spinand_init_cfg_cache(spinand);
1505 	if (ret)
1506 		goto err_free_bufs;
1507 
1508 	ret = spinand_init_flash(spinand);
1509 	if (ret)
1510 		goto err_free_bufs;
1511 
1512 	ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
1513 	if (ret)
1514 		goto err_manuf_cleanup;
1515 
1516 	/* SPI-NAND default ECC engine is on-die */
1517 	nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE;
1518 	nand->ecc.ondie_engine = &spinand_ondie_ecc_engine;
1519 
1520 	spinand_ecc_enable(spinand, false);
1521 	ret = nanddev_ecc_engine_init(nand);
1522 	if (ret)
1523 		goto err_cleanup_nanddev;
1524 
1525 	/*
1526 	 * Continuous read can only be enabled with an on-die ECC engine, so the
1527 	 * ECC initialization must have happened previously.
1528 	 */
1529 	spinand_cont_read_init(spinand);
1530 
1531 	mtd->_read_oob = spinand_mtd_read;
1532 	mtd->_write_oob = spinand_mtd_write;
1533 	mtd->_block_isbad = spinand_mtd_block_isbad;
1534 	mtd->_block_markbad = spinand_mtd_block_markbad;
1535 	mtd->_block_isreserved = spinand_mtd_block_isreserved;
1536 	mtd->_erase = spinand_mtd_erase;
1537 	mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
1538 	mtd->_resume = spinand_mtd_resume;
1539 
1540 	if (spinand_user_otp_size(spinand) || spinand_fact_otp_size(spinand)) {
1541 		ret = spinand_set_mtd_otp_ops(spinand);
1542 		if (ret)
1543 			goto err_cleanup_ecc_engine;
1544 	}
1545 
1546 	if (nand->ecc.engine) {
1547 		ret = mtd_ooblayout_count_freebytes(mtd);
1548 		if (ret < 0)
1549 			goto err_cleanup_ecc_engine;
1550 	}
1551 
1552 	mtd->oobavail = ret;
1553 
1554 	/* Propagate ECC information to mtd_info */
1555 	mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength;
1556 	mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size;
1557 	mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
1558 
1559 	ret = spinand_create_dirmaps(spinand);
1560 	if (ret) {
1561 		dev_err(dev,
1562 			"Failed to create direct mappings for read/write operations (err = %d)\n",
1563 			ret);
1564 		goto err_cleanup_ecc_engine;
1565 	}
1566 
1567 	return 0;
1568 
1569 err_cleanup_ecc_engine:
1570 	nanddev_ecc_engine_cleanup(nand);
1571 
1572 err_cleanup_nanddev:
1573 	nanddev_cleanup(nand);
1574 
1575 err_manuf_cleanup:
1576 	spinand_manufacturer_cleanup(spinand);
1577 
1578 err_free_bufs:
1579 	kfree(spinand->databuf);
1580 	kfree(spinand->scratchbuf);
1581 	return ret;
1582 }
1583 
spinand_cleanup(struct spinand_device * spinand)1584 static void spinand_cleanup(struct spinand_device *spinand)
1585 {
1586 	struct nand_device *nand = spinand_to_nand(spinand);
1587 
1588 	nanddev_ecc_engine_cleanup(nand);
1589 	nanddev_cleanup(nand);
1590 	spinand_manufacturer_cleanup(spinand);
1591 	kfree(spinand->databuf);
1592 	kfree(spinand->scratchbuf);
1593 }
1594 
spinand_probe(struct spi_mem * mem)1595 static int spinand_probe(struct spi_mem *mem)
1596 {
1597 	struct spinand_device *spinand;
1598 	struct mtd_info *mtd;
1599 	int ret;
1600 
1601 	spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
1602 			       GFP_KERNEL);
1603 	if (!spinand)
1604 		return -ENOMEM;
1605 
1606 	spinand->spimem = mem;
1607 	spi_mem_set_drvdata(mem, spinand);
1608 	spinand_set_of_node(spinand, mem->spi->dev.of_node);
1609 	mutex_init(&spinand->lock);
1610 	mtd = spinand_to_mtd(spinand);
1611 	mtd->dev.parent = &mem->spi->dev;
1612 
1613 	ret = spinand_init(spinand);
1614 	if (ret)
1615 		return ret;
1616 
1617 	ret = mtd_device_register(mtd, NULL, 0);
1618 	if (ret)
1619 		goto err_spinand_cleanup;
1620 
1621 	return 0;
1622 
1623 err_spinand_cleanup:
1624 	spinand_cleanup(spinand);
1625 
1626 	return ret;
1627 }
1628 
spinand_remove(struct spi_mem * mem)1629 static int spinand_remove(struct spi_mem *mem)
1630 {
1631 	struct spinand_device *spinand;
1632 	struct mtd_info *mtd;
1633 	int ret;
1634 
1635 	spinand = spi_mem_get_drvdata(mem);
1636 	mtd = spinand_to_mtd(spinand);
1637 
1638 	ret = mtd_device_unregister(mtd);
1639 	if (ret)
1640 		return ret;
1641 
1642 	spinand_cleanup(spinand);
1643 
1644 	return 0;
1645 }
1646 
1647 static const struct spi_device_id spinand_ids[] = {
1648 	{ .name = "spi-nand" },
1649 	{ /* sentinel */ },
1650 };
1651 MODULE_DEVICE_TABLE(spi, spinand_ids);
1652 
1653 #ifdef CONFIG_OF
1654 static const struct of_device_id spinand_of_ids[] = {
1655 	{ .compatible = "spi-nand" },
1656 	{ /* sentinel */ },
1657 };
1658 MODULE_DEVICE_TABLE(of, spinand_of_ids);
1659 #endif
1660 
1661 static struct spi_mem_driver spinand_drv = {
1662 	.spidrv = {
1663 		.id_table = spinand_ids,
1664 		.driver = {
1665 			.name = "spi-nand",
1666 			.of_match_table = of_match_ptr(spinand_of_ids),
1667 		},
1668 	},
1669 	.probe = spinand_probe,
1670 	.remove = spinand_remove,
1671 };
1672 module_spi_mem_driver(spinand_drv);
1673 
1674 MODULE_DESCRIPTION("SPI NAND framework");
1675 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
1676 MODULE_LICENSE("GPL v2");
1677