xref: /rk3399_rockchip-uboot/drivers/mtd/nand/spi/core.c (revision d1e9de8ba042f8a38d7b6ee718aee94982eee73b)
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 #ifndef __UBOOT__
13 #include <linux/device.h>
14 #include <linux/jiffies.h>
15 #include <linux/kernel.h>
16 #include <linux/module.h>
17 #include <linux/mtd/spinand.h>
18 #include <linux/of.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi-mem.h>
22 #else
23 #include <common.h>
24 #include <errno.h>
25 #include <spi.h>
26 #include <spi-mem.h>
27 #include <linux/mtd/spinand.h>
28 #endif
29 
30 /* SPI NAND index visible in MTD names */
31 static int spi_nand_idx;
32 
33 static void spinand_cache_op_adjust_colum(struct spinand_device *spinand,
34 					  const struct nand_page_io_req *req,
35 					  u16 *column)
36 {
37 	struct nand_device *nand = spinand_to_nand(spinand);
38 	unsigned int shift;
39 
40 	if (nand->memorg.planes_per_lun < 2)
41 		return;
42 
43 	/* The plane number is passed in MSB just above the column address */
44 	shift = fls(nand->memorg.pagesize);
45 	*column |= req->pos.plane << shift;
46 }
47 
48 static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
49 {
50 	struct spi_mem_op op = SPINAND_GET_FEATURE_OP(reg,
51 						      spinand->scratchbuf);
52 	int ret;
53 
54 	ret = spi_mem_exec_op(spinand->slave, &op);
55 	if (ret)
56 		return ret;
57 
58 	*val = *spinand->scratchbuf;
59 	return 0;
60 }
61 
62 static int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val)
63 {
64 	struct spi_mem_op op = SPINAND_SET_FEATURE_OP(reg,
65 						      spinand->scratchbuf);
66 
67 	*spinand->scratchbuf = val;
68 	return spi_mem_exec_op(spinand->slave, &op);
69 }
70 
71 static int spinand_read_status(struct spinand_device *spinand, u8 *status)
72 {
73 	return spinand_read_reg_op(spinand, REG_STATUS, status);
74 }
75 
76 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg)
77 {
78 	struct nand_device *nand = spinand_to_nand(spinand);
79 
80 	if (WARN_ON(spinand->cur_target < 0 ||
81 		    spinand->cur_target >= nand->memorg.ntargets))
82 		return -EINVAL;
83 
84 	*cfg = spinand->cfg_cache[spinand->cur_target];
85 	return 0;
86 }
87 
88 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg)
89 {
90 	struct nand_device *nand = spinand_to_nand(spinand);
91 	int ret;
92 
93 	if (WARN_ON(spinand->cur_target < 0 ||
94 		    spinand->cur_target >= nand->memorg.ntargets))
95 		return -EINVAL;
96 
97 	if (spinand->cfg_cache[spinand->cur_target] == cfg)
98 		return 0;
99 
100 	ret = spinand_write_reg_op(spinand, REG_CFG, cfg);
101 	if (ret)
102 		return ret;
103 
104 	spinand->cfg_cache[spinand->cur_target] = cfg;
105 	return 0;
106 }
107 
108 /**
109  * spinand_upd_cfg() - Update the configuration register
110  * @spinand: the spinand device
111  * @mask: the mask encoding the bits to update in the config reg
112  * @val: the new value to apply
113  *
114  * Update the configuration register.
115  *
116  * Return: 0 on success, a negative error code otherwise.
117  */
118 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
119 {
120 	int ret;
121 	u8 cfg;
122 
123 	ret = spinand_get_cfg(spinand, &cfg);
124 	if (ret)
125 		return ret;
126 
127 	cfg &= ~mask;
128 	cfg |= val;
129 
130 	return spinand_set_cfg(spinand, cfg);
131 }
132 
133 /**
134  * spinand_select_target() - Select a specific NAND target/die
135  * @spinand: the spinand device
136  * @target: the target/die to select
137  *
138  * Select a new target/die. If chip only has one die, this function is a NOOP.
139  *
140  * Return: 0 on success, a negative error code otherwise.
141  */
142 int spinand_select_target(struct spinand_device *spinand, unsigned int target)
143 {
144 	struct nand_device *nand = spinand_to_nand(spinand);
145 	int ret;
146 
147 	if (WARN_ON(target >= nand->memorg.ntargets))
148 		return -EINVAL;
149 
150 	if (spinand->cur_target == target)
151 		return 0;
152 
153 	if (nand->memorg.ntargets == 1) {
154 		spinand->cur_target = target;
155 		return 0;
156 	}
157 
158 	ret = spinand->select_target(spinand, target);
159 	if (ret)
160 		return ret;
161 
162 	spinand->cur_target = target;
163 	return 0;
164 }
165 
166 static int spinand_init_cfg_cache(struct spinand_device *spinand)
167 {
168 	struct nand_device *nand = spinand_to_nand(spinand);
169 	struct udevice *dev = spinand->slave->dev;
170 	unsigned int target;
171 	int ret;
172 
173 	spinand->cfg_cache = devm_kzalloc(dev,
174 					  sizeof(*spinand->cfg_cache) *
175 					  nand->memorg.ntargets,
176 					  GFP_KERNEL);
177 	if (!spinand->cfg_cache)
178 		return -ENOMEM;
179 
180 	for (target = 0; target < nand->memorg.ntargets; target++) {
181 		ret = spinand_select_target(spinand, target);
182 		if (ret)
183 			return ret;
184 
185 		/*
186 		 * We use spinand_read_reg_op() instead of spinand_get_cfg()
187 		 * here to bypass the config cache.
188 		 */
189 		ret = spinand_read_reg_op(spinand, REG_CFG,
190 					  &spinand->cfg_cache[target]);
191 		if (ret)
192 			return ret;
193 	}
194 
195 	return 0;
196 }
197 
198 static int spinand_init_quad_enable(struct spinand_device *spinand)
199 {
200 	bool enable = false;
201 
202 	if (!(spinand->flags & SPINAND_HAS_QE_BIT))
203 		return 0;
204 
205 	if (spinand->op_templates.read_cache->data.buswidth == 4 ||
206 	    spinand->op_templates.write_cache->data.buswidth == 4 ||
207 	    spinand->op_templates.update_cache->data.buswidth == 4)
208 		enable = true;
209 
210 	return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE,
211 			       enable ? CFG_QUAD_ENABLE : 0);
212 }
213 
214 static int spinand_ecc_enable(struct spinand_device *spinand,
215 			      bool enable)
216 {
217 	return spinand_upd_cfg(spinand, CFG_ECC_ENABLE,
218 			       enable ? CFG_ECC_ENABLE : 0);
219 }
220 
221 static int spinand_write_enable_op(struct spinand_device *spinand)
222 {
223 	struct spi_mem_op op = SPINAND_WR_EN_DIS_OP(true);
224 
225 	return spi_mem_exec_op(spinand->slave, &op);
226 }
227 
228 static int spinand_load_page_op(struct spinand_device *spinand,
229 				const struct nand_page_io_req *req)
230 {
231 	struct nand_device *nand = spinand_to_nand(spinand);
232 	unsigned int row = nanddev_pos_to_row(nand, &req->pos);
233 	struct spi_mem_op op = SPINAND_PAGE_READ_OP(row);
234 
235 	return spi_mem_exec_op(spinand->slave, &op);
236 }
237 
238 static int spinand_read_from_cache_op(struct spinand_device *spinand,
239 				      const struct nand_page_io_req *req)
240 {
241 	struct spi_mem_op op = *spinand->op_templates.read_cache;
242 	struct nand_device *nand = spinand_to_nand(spinand);
243 	struct mtd_info *mtd = nanddev_to_mtd(nand);
244 	struct nand_page_io_req adjreq = *req;
245 	unsigned int nbytes = 0;
246 	void *buf = NULL;
247 	u16 column = 0;
248 	int ret;
249 
250 	if (req->datalen) {
251 		adjreq.datalen = nanddev_page_size(nand);
252 		adjreq.dataoffs = 0;
253 		adjreq.databuf.in = spinand->databuf;
254 		buf = spinand->databuf;
255 		nbytes = adjreq.datalen;
256 	}
257 
258 	if (req->ooblen) {
259 		adjreq.ooblen = nanddev_per_page_oobsize(nand);
260 		adjreq.ooboffs = 0;
261 		adjreq.oobbuf.in = spinand->oobbuf;
262 		nbytes += nanddev_per_page_oobsize(nand);
263 		if (!buf) {
264 			buf = spinand->oobbuf;
265 			column = nanddev_page_size(nand);
266 		}
267 	}
268 
269 	spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
270 	op.addr.val = column;
271 
272 	/*
273 	 * Some controllers are limited in term of max RX data size. In this
274 	 * case, just repeat the READ_CACHE operation after updating the
275 	 * column.
276 	 */
277 	while (nbytes) {
278 		op.data.buf.in = buf;
279 		op.data.nbytes = nbytes;
280 		ret = spi_mem_adjust_op_size(spinand->slave, &op);
281 		if (ret)
282 			return ret;
283 
284 		ret = spi_mem_exec_op(spinand->slave, &op);
285 		if (ret)
286 			return ret;
287 
288 		buf += op.data.nbytes;
289 		nbytes -= op.data.nbytes;
290 		op.addr.val += op.data.nbytes;
291 	}
292 
293 	if (req->datalen)
294 		memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
295 		       req->datalen);
296 
297 	if (req->ooblen) {
298 		if (req->mode == MTD_OPS_AUTO_OOB)
299 			mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
300 						    spinand->oobbuf,
301 						    req->ooboffs,
302 						    req->ooblen);
303 		else
304 			memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
305 			       req->ooblen);
306 	}
307 
308 	return 0;
309 }
310 
311 static int spinand_write_to_cache_op(struct spinand_device *spinand,
312 				     const struct nand_page_io_req *req)
313 {
314 	struct spi_mem_op op = *spinand->op_templates.write_cache;
315 	struct nand_device *nand = spinand_to_nand(spinand);
316 	struct mtd_info *mtd = nanddev_to_mtd(nand);
317 	struct nand_page_io_req adjreq = *req;
318 	unsigned int nbytes = 0;
319 	void *buf = NULL;
320 	u16 column = 0;
321 	int ret;
322 
323 	memset(spinand->databuf, 0xff,
324 	       nanddev_page_size(nand) +
325 	       nanddev_per_page_oobsize(nand));
326 
327 	if (req->datalen) {
328 		memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
329 		       req->datalen);
330 		adjreq.dataoffs = 0;
331 		adjreq.datalen = nanddev_page_size(nand);
332 		adjreq.databuf.out = spinand->databuf;
333 		nbytes = adjreq.datalen;
334 		buf = spinand->databuf;
335 	}
336 
337 	if (req->ooblen) {
338 		if (req->mode == MTD_OPS_AUTO_OOB)
339 			mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
340 						    spinand->oobbuf,
341 						    req->ooboffs,
342 						    req->ooblen);
343 		else
344 			memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
345 			       req->ooblen);
346 
347 		adjreq.ooblen = nanddev_per_page_oobsize(nand);
348 		adjreq.ooboffs = 0;
349 		nbytes += nanddev_per_page_oobsize(nand);
350 		if (!buf) {
351 			buf = spinand->oobbuf;
352 			column = nanddev_page_size(nand);
353 		}
354 	}
355 
356 	spinand_cache_op_adjust_colum(spinand, &adjreq, &column);
357 
358 	op = *spinand->op_templates.write_cache;
359 	op.addr.val = column;
360 
361 	/*
362 	 * Some controllers are limited in term of max TX data size. In this
363 	 * case, split the operation into one LOAD CACHE and one or more
364 	 * LOAD RANDOM CACHE.
365 	 */
366 	while (nbytes) {
367 		op.data.buf.out = buf;
368 		op.data.nbytes = nbytes;
369 
370 		ret = spi_mem_adjust_op_size(spinand->slave, &op);
371 		if (ret)
372 			return ret;
373 
374 		ret = spi_mem_exec_op(spinand->slave, &op);
375 		if (ret)
376 			return ret;
377 
378 		buf += op.data.nbytes;
379 		nbytes -= op.data.nbytes;
380 		op.addr.val += op.data.nbytes;
381 
382 		/*
383 		 * We need to use the RANDOM LOAD CACHE operation if there's
384 		 * more than one iteration, because the LOAD operation resets
385 		 * the cache to 0xff.
386 		 */
387 		if (nbytes) {
388 			column = op.addr.val;
389 			op = *spinand->op_templates.update_cache;
390 			op.addr.val = column;
391 		}
392 	}
393 
394 	return 0;
395 }
396 
397 static int spinand_program_op(struct spinand_device *spinand,
398 			      const struct nand_page_io_req *req)
399 {
400 	struct nand_device *nand = spinand_to_nand(spinand);
401 	unsigned int row = nanddev_pos_to_row(nand, &req->pos);
402 	struct spi_mem_op op = SPINAND_PROG_EXEC_OP(row);
403 
404 	return spi_mem_exec_op(spinand->slave, &op);
405 }
406 
407 static int spinand_erase_op(struct spinand_device *spinand,
408 			    const struct nand_pos *pos)
409 {
410 	struct nand_device *nand = &spinand->base;
411 	unsigned int row = nanddev_pos_to_row(nand, pos);
412 	struct spi_mem_op op = SPINAND_BLK_ERASE_OP(row);
413 
414 	return spi_mem_exec_op(spinand->slave, &op);
415 }
416 
417 static int spinand_wait(struct spinand_device *spinand, u8 *s)
418 {
419 	unsigned long start, stop;
420 	u8 status;
421 	int ret;
422 
423 	start = get_timer(0);
424 	stop = 400;
425 	do {
426 		ret = spinand_read_status(spinand, &status);
427 		if (ret)
428 			return ret;
429 
430 		if (!(status & STATUS_BUSY))
431 			goto out;
432 	} while (get_timer(start) < stop);
433 
434 	/*
435 	 * Extra read, just in case the STATUS_READY bit has changed
436 	 * since our last check
437 	 */
438 	ret = spinand_read_status(spinand, &status);
439 	if (ret)
440 		return ret;
441 
442 out:
443 	if (s)
444 		*s = status;
445 
446 	return status & STATUS_BUSY ? -ETIMEDOUT : 0;
447 }
448 
449 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr,
450 			      u8 ndummy, u8 *buf)
451 {
452 	struct spi_mem_op op = SPINAND_READID_OP(
453 		naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
454 	int ret;
455 
456 	ret = spi_mem_exec_op(spinand->slave, &op);
457 	if (!ret)
458 		memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
459 
460 	return ret;
461 }
462 
463 static int spinand_reset_op(struct spinand_device *spinand)
464 {
465 	struct spi_mem_op op = SPINAND_RESET_OP;
466 	int ret;
467 
468 	ret = spi_mem_exec_op(spinand->slave, &op);
469 	if (ret)
470 		return ret;
471 
472 	return spinand_wait(spinand, NULL);
473 }
474 
475 static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
476 {
477 	return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
478 }
479 
480 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
481 {
482 	struct nand_device *nand = spinand_to_nand(spinand);
483 
484 	if (spinand->eccinfo.get_status)
485 		return spinand->eccinfo.get_status(spinand, status);
486 
487 	switch (status & STATUS_ECC_MASK) {
488 	case STATUS_ECC_NO_BITFLIPS:
489 		return 0;
490 
491 	case STATUS_ECC_HAS_BITFLIPS:
492 		/*
493 		 * We have no way to know exactly how many bitflips have been
494 		 * fixed, so let's return the maximum possible value so that
495 		 * wear-leveling layers move the data immediately.
496 		 */
497 		return nand->eccreq.strength;
498 
499 	case STATUS_ECC_UNCOR_ERROR:
500 		return -EBADMSG;
501 
502 	default:
503 		break;
504 	}
505 
506 	return -EINVAL;
507 }
508 
509 static int spinand_read_page(struct spinand_device *spinand,
510 			     const struct nand_page_io_req *req,
511 			     bool ecc_enabled)
512 {
513 	u8 status = 0;
514 	int ret;
515 
516 	ret = spinand_load_page_op(spinand, req);
517 	if (ret)
518 		return ret;
519 
520 	ret = spinand_wait(spinand, &status);
521 	/*
522 	 * When there is data outside of OIP in the status, the status data is
523 	 * inaccurate and needs to be reconfirmed
524 	 */
525 	if (spinand->id.data[0] == 0x01 && status && !ret)
526 		ret = spinand_wait(spinand, &status);
527 	if (ret < 0)
528 		return ret;
529 
530 	ret = spinand_read_from_cache_op(spinand, req);
531 	if (ret)
532 		return ret;
533 
534 	if (!ecc_enabled)
535 		return 0;
536 
537 	return spinand_check_ecc_status(spinand, status);
538 }
539 
540 static int spinand_write_page(struct spinand_device *spinand,
541 			      const struct nand_page_io_req *req)
542 {
543 	u8 status;
544 	int ret;
545 
546 	ret = spinand_write_enable_op(spinand);
547 	if (ret)
548 		return ret;
549 
550 	ret = spinand_write_to_cache_op(spinand, req);
551 	if (ret)
552 		return ret;
553 
554 	ret = spinand_program_op(spinand, req);
555 	if (ret)
556 		return ret;
557 
558 	ret = spinand_wait(spinand, &status);
559 	if (!ret && (status & STATUS_PROG_FAILED))
560 		ret = -EIO;
561 
562 	return ret;
563 }
564 
565 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
566 			    struct mtd_oob_ops *ops)
567 {
568 	struct spinand_device *spinand = mtd_to_spinand(mtd);
569 	struct nand_device *nand = mtd_to_nanddev(mtd);
570 	unsigned int max_bitflips = 0;
571 	struct nand_io_iter iter;
572 	bool enable_ecc = false;
573 	bool ecc_failed = false;
574 	int ret = 0;
575 
576 	if (ops->mode != MTD_OPS_RAW && spinand->eccinfo.ooblayout)
577 		enable_ecc = true;
578 
579 #ifndef __UBOOT__
580 	mutex_lock(&spinand->lock);
581 #endif
582 
583 	nanddev_io_for_each_page(nand, from, ops, &iter) {
584 		ret = spinand_select_target(spinand, iter.req.pos.target);
585 		if (ret)
586 			break;
587 
588 		ret = spinand_ecc_enable(spinand, enable_ecc);
589 		if (ret)
590 			break;
591 
592 		ret = spinand_read_page(spinand, &iter.req, enable_ecc);
593 		if (ret < 0 && ret != -EBADMSG)
594 			break;
595 
596 		if (ret == -EBADMSG) {
597 			ecc_failed = true;
598 			mtd->ecc_stats.failed++;
599 			ret = 0;
600 		} else {
601 			mtd->ecc_stats.corrected += ret;
602 			max_bitflips = max_t(unsigned int, max_bitflips, ret);
603 		}
604 
605 		ops->retlen += iter.req.datalen;
606 		ops->oobretlen += iter.req.ooblen;
607 	}
608 
609 #ifndef __UBOOT__
610 	mutex_unlock(&spinand->lock);
611 #endif
612 	if (ecc_failed && !ret)
613 		ret = -EBADMSG;
614 
615 	return ret ? ret : max_bitflips;
616 }
617 
618 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
619 			     struct mtd_oob_ops *ops)
620 {
621 	struct spinand_device *spinand = mtd_to_spinand(mtd);
622 	struct nand_device *nand = mtd_to_nanddev(mtd);
623 	struct nand_io_iter iter;
624 	bool enable_ecc = false;
625 	int ret = 0;
626 
627 	if (ops->mode != MTD_OPS_RAW && mtd->ooblayout)
628 		enable_ecc = true;
629 
630 #ifndef __UBOOT__
631 	mutex_lock(&spinand->lock);
632 #endif
633 
634 	nanddev_io_for_each_page(nand, to, ops, &iter) {
635 		ret = spinand_select_target(spinand, iter.req.pos.target);
636 		if (ret)
637 			break;
638 
639 		ret = spinand_ecc_enable(spinand, enable_ecc);
640 		if (ret)
641 			break;
642 
643 		ret = spinand_write_page(spinand, &iter.req);
644 		if (ret)
645 			break;
646 
647 		ops->retlen += iter.req.datalen;
648 		ops->oobretlen += iter.req.ooblen;
649 	}
650 
651 #ifndef __UBOOT__
652 	mutex_unlock(&spinand->lock);
653 #endif
654 
655 	return ret;
656 }
657 
658 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
659 {
660 	struct spinand_device *spinand = nand_to_spinand(nand);
661 	u8 marker[2] = { };
662 	struct nand_page_io_req req = {
663 		.pos = *pos,
664 		.ooblen = sizeof(marker),
665 		.ooboffs = 0,
666 		.oobbuf.in = marker,
667 		.mode = MTD_OPS_RAW,
668 	};
669 
670 	spinand_select_target(spinand, pos->target);
671 	spinand_read_page(spinand, &req, false);
672 	if (marker[0] != 0xff || marker[1] != 0xff)
673 		return true;
674 
675 	return false;
676 }
677 
678 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
679 {
680 	struct nand_device *nand = mtd_to_nanddev(mtd);
681 #ifndef __UBOOT__
682 	struct spinand_device *spinand = nand_to_spinand(nand);
683 #endif
684 	struct nand_pos pos;
685 	int ret;
686 
687 	nanddev_offs_to_pos(nand, offs, &pos);
688 #ifndef __UBOOT__
689 	mutex_lock(&spinand->lock);
690 #endif
691 	ret = nanddev_isbad(nand, &pos);
692 #ifndef __UBOOT__
693 	mutex_unlock(&spinand->lock);
694 #endif
695 	return ret;
696 }
697 
698 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
699 {
700 	struct spinand_device *spinand = nand_to_spinand(nand);
701 	u8 marker[2] = { 0, 0 };
702 	struct nand_page_io_req req = {
703 		.pos = *pos,
704 		.ooboffs = 0,
705 		.ooblen = sizeof(marker),
706 		.oobbuf.out = marker,
707 		.mode = MTD_OPS_RAW,
708 	};
709 	int ret;
710 
711 	ret = spinand_select_target(spinand, pos->target);
712 	if (ret)
713 		return ret;
714 
715 	return spinand_write_page(spinand, &req);
716 }
717 
718 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
719 {
720 	struct nand_device *nand = mtd_to_nanddev(mtd);
721 #ifndef __UBOOT__
722 	struct spinand_device *spinand = nand_to_spinand(nand);
723 #endif
724 	struct nand_pos pos;
725 	int ret;
726 
727 	nanddev_offs_to_pos(nand, offs, &pos);
728 #ifndef __UBOOT__
729 	mutex_lock(&spinand->lock);
730 #endif
731 	ret = nanddev_markbad(nand, &pos);
732 #ifndef __UBOOT__
733 	mutex_unlock(&spinand->lock);
734 #endif
735 	return ret;
736 }
737 
738 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
739 {
740 	struct spinand_device *spinand = nand_to_spinand(nand);
741 	u8 status;
742 	int ret;
743 
744 	ret = spinand_select_target(spinand, pos->target);
745 	if (ret)
746 		return ret;
747 
748 	ret = spinand_write_enable_op(spinand);
749 	if (ret)
750 		return ret;
751 
752 	ret = spinand_erase_op(spinand, pos);
753 	if (ret)
754 		return ret;
755 
756 	ret = spinand_wait(spinand, &status);
757 	if (!ret && (status & STATUS_ERASE_FAILED))
758 		ret = -EIO;
759 
760 	return ret;
761 }
762 
763 static int spinand_mtd_erase(struct mtd_info *mtd,
764 			     struct erase_info *einfo)
765 {
766 #ifndef __UBOOT__
767 	struct spinand_device *spinand = mtd_to_spinand(mtd);
768 #endif
769 	int ret;
770 
771 #ifndef __UBOOT__
772 	mutex_lock(&spinand->lock);
773 #endif
774 	ret = nanddev_mtd_erase(mtd, einfo);
775 #ifndef __UBOOT__
776 	mutex_unlock(&spinand->lock);
777 #endif
778 
779 	return ret;
780 }
781 
782 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
783 {
784 #ifndef __UBOOT__
785 	struct spinand_device *spinand = mtd_to_spinand(mtd);
786 #endif
787 	struct nand_device *nand = mtd_to_nanddev(mtd);
788 	struct nand_pos pos;
789 	int ret;
790 
791 	nanddev_offs_to_pos(nand, offs, &pos);
792 #ifndef __UBOOT__
793 	mutex_lock(&spinand->lock);
794 #endif
795 	ret = nanddev_isreserved(nand, &pos);
796 #ifndef __UBOOT__
797 	mutex_unlock(&spinand->lock);
798 #endif
799 
800 	return ret;
801 }
802 
803 const struct spi_mem_op *
804 spinand_find_supported_op(struct spinand_device *spinand,
805 			  const struct spi_mem_op *ops,
806 			  unsigned int nops)
807 {
808 	unsigned int i;
809 
810 	for (i = 0; i < nops; i++) {
811 		if (spi_mem_supports_op(spinand->slave, &ops[i]))
812 			return &ops[i];
813 	}
814 
815 	return NULL;
816 }
817 
818 static const struct nand_ops spinand_ops = {
819 	.erase = spinand_erase,
820 	.markbad = spinand_markbad,
821 	.isbad = spinand_isbad,
822 };
823 
824 static const struct spinand_manufacturer *spinand_manufacturers[] = {
825 #ifdef CONFIG_SPI_NAND_GIGADEVICE
826 	&gigadevice_spinand_manufacturer,
827 #endif
828 #ifdef CONFIG_SPI_NAND_MACRONIX
829 	&macronix_spinand_manufacturer,
830 #endif
831 #ifdef CONFIG_SPI_NAND_MICRON
832 	&micron_spinand_manufacturer,
833 #endif
834 #ifdef CONFIG_SPI_NAND_TOSHIBA
835 	&toshiba_spinand_manufacturer,
836 #endif
837 #ifdef CONFIG_SPI_NAND_WINBOND
838 	&winbond_spinand_manufacturer,
839 #endif
840 #ifdef CONFIG_SPI_NAND_DOSILICON
841 	&dosilicon_spinand_manufacturer,
842 #endif
843 #ifdef CONFIG_SPI_NAND_ESMT
844 	&esmt_spinand_manufacturer,
845 #endif
846 #ifdef CONFIG_SPI_NAND_XINCUN
847 	&xincun_spinand_manufacturer,
848 #endif
849 #ifdef CONFIG_SPI_NAND_XTX
850 	&xtx_spinand_manufacturer,
851 #endif
852 #ifdef CONFIG_SPI_NAND_HYF
853 	&hyf_spinand_manufacturer,
854 #endif
855 #ifdef CONFIG_SPI_NAND_FMSH
856 	&fmsh_spinand_manufacturer,
857 #endif
858 #ifdef CONFIG_SPI_NAND_FORESEE
859 	&foresee_spinand_manufacturer,
860 #endif
861 #ifdef CONFIG_SPI_NAND_BIWIN
862 	&biwin_spinand_manufacturer,
863 #endif
864 #ifdef CONFIG_SPI_NAND_ETRON
865 	&etron_spinand_manufacturer,
866 #endif
867 #ifdef CONFIG_SPI_NAND_JSC
868 	&jsc_spinand_manufacturer,
869 #endif
870 #ifdef CONFIG_SPI_NAND_SILICONGO
871 	&silicongo_spinand_manufacturer,
872 #endif
873 #ifdef CONFIG_SPI_NAND_UNIM
874 	&unim_spinand_manufacturer,
875 #endif
876 #ifdef CONFIG_SPI_NAND_SKYHIGH
877 	&skyhigh_spinand_manufacturer,
878 #endif
879 #ifdef CONFIG_SPI_NAND_GSTO
880 	&gsto_spinand_manufacturer,
881 #endif
882 };
883 
884 static int spinand_manufacturer_match(struct spinand_device *spinand,
885 				      enum spinand_readid_method rdid_method)
886 {
887 	u8 *id = spinand->id.data;
888 	unsigned int i;
889 	int ret;
890 
891 	for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
892 		const struct spinand_manufacturer *manufacturer =
893 			spinand_manufacturers[i];
894 
895 		if (id[0] != manufacturer->id)
896 			continue;
897 
898 		ret = spinand_match_and_init(spinand,
899 					     manufacturer->chips,
900 					     manufacturer->nchips,
901 					     rdid_method);
902 		if (ret < 0)
903 			continue;
904 
905 		spinand->manufacturer = manufacturer;
906 		return 0;
907 	}
908 	return -ENOTSUPP;
909 }
910 
911 static int spinand_id_detect(struct spinand_device *spinand)
912 {
913 	u8 *id = spinand->id.data;
914 	int ret;
915 
916 	ret = spinand_read_id_op(spinand, 0, 0, id);
917 	if (ret)
918 		return ret;
919 	ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE);
920 	if (!ret)
921 		return 0;
922 
923 	ret = spinand_read_id_op(spinand, 1, 0, id);
924 	if (ret)
925 		return ret;
926 	ret = spinand_manufacturer_match(spinand,
927 					 SPINAND_READID_METHOD_OPCODE_ADDR);
928 	if (!ret)
929 		return 0;
930 
931 	ret = spinand_read_id_op(spinand, 0, 1, id);
932 	if (ret)
933 		return ret;
934 	ret = spinand_manufacturer_match(spinand,
935 					 SPINAND_READID_METHOD_OPCODE_DUMMY);
936 
937 	return ret;
938 }
939 
940 static int spinand_manufacturer_init(struct spinand_device *spinand)
941 {
942 	if (spinand->manufacturer->ops->init)
943 		return spinand->manufacturer->ops->init(spinand);
944 
945 	return 0;
946 }
947 
948 static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
949 {
950 	/* Release manufacturer private data */
951 	if (spinand->manufacturer->ops->cleanup)
952 		return spinand->manufacturer->ops->cleanup(spinand);
953 }
954 
955 static const struct spi_mem_op *
956 spinand_select_op_variant(struct spinand_device *spinand,
957 			  const struct spinand_op_variants *variants)
958 {
959 	struct nand_device *nand = spinand_to_nand(spinand);
960 	unsigned int i;
961 
962 	for (i = 0; i < variants->nops; i++) {
963 		struct spi_mem_op op = variants->ops[i];
964 		unsigned int nbytes;
965 		int ret;
966 
967 		nbytes = nanddev_per_page_oobsize(nand) +
968 			 nanddev_page_size(nand);
969 
970 		while (nbytes) {
971 			op.data.nbytes = nbytes;
972 			ret = spi_mem_adjust_op_size(spinand->slave, &op);
973 			if (ret)
974 				break;
975 
976 			if (!spi_mem_supports_op(spinand->slave, &op))
977 				break;
978 
979 			nbytes -= op.data.nbytes;
980 		}
981 
982 		if (!nbytes)
983 			return &variants->ops[i];
984 	}
985 
986 	return NULL;
987 }
988 
989 /**
990  * spinand_match_and_init() - Try to find a match between a device ID and an
991  *			      entry in a spinand_info table
992  * @spinand: SPI NAND object
993  * @table: SPI NAND device description table
994  * @table_size: size of the device description table
995  * @rdid_method: read id method to match
996  *
997  * Match between a device ID retrieved through the READ_ID command and an
998  * entry in the SPI NAND description table. If a match is found, the spinand
999  * object will be initialized with information provided by the matching
1000  * spinand_info entry.
1001  *
1002  * Return: 0 on success, a negative error code otherwise.
1003  */
1004 int spinand_match_and_init(struct spinand_device *spinand,
1005 			   const struct spinand_info *table,
1006 			   unsigned int table_size,
1007 			   enum spinand_readid_method rdid_method)
1008 {
1009 	u8 *id = spinand->id.data;
1010 	struct nand_device *nand = spinand_to_nand(spinand);
1011 	unsigned int i;
1012 
1013 	for (i = 0; i < table_size; i++) {
1014 		const struct spinand_info *info = &table[i];
1015 		const struct spi_mem_op *op;
1016 
1017 		if (rdid_method != info->devid.method)
1018 			continue;
1019 
1020 		if (memcmp(id + 1, info->devid.id, info->devid.len))
1021 			continue;
1022 
1023 		nand->memorg = table[i].memorg;
1024 		nand->eccreq = table[i].eccreq;
1025 		spinand->eccinfo = table[i].eccinfo;
1026 		spinand->flags = table[i].flags;
1027 		spinand->id.len = 1 + table[i].devid.len;
1028 		spinand->select_target = table[i].select_target;
1029 
1030 		op = spinand_select_op_variant(spinand,
1031 					       info->op_variants.read_cache);
1032 		if (!op)
1033 			return -ENOTSUPP;
1034 
1035 		spinand->op_templates.read_cache = op;
1036 
1037 		op = spinand_select_op_variant(spinand,
1038 					       info->op_variants.write_cache);
1039 		if (!op)
1040 			return -ENOTSUPP;
1041 
1042 		spinand->op_templates.write_cache = op;
1043 
1044 		op = spinand_select_op_variant(spinand,
1045 					       info->op_variants.update_cache);
1046 		spinand->op_templates.update_cache = op;
1047 
1048 		return 0;
1049 	}
1050 
1051 	return -ENOTSUPP;
1052 }
1053 
1054 static int spinand_detect(struct spinand_device *spinand)
1055 {
1056 	struct nand_device *nand = spinand_to_nand(spinand);
1057 	int ret;
1058 
1059 	ret = spinand_reset_op(spinand);
1060 	if (ret)
1061 		return ret;
1062 
1063 	ret = spinand_id_detect(spinand);
1064 	if (ret) {
1065 		dev_err(dev, "unknown raw ID %x %x %x\n",
1066 			spinand->id.data[0], spinand->id.data[1], spinand->id.data[2]);
1067 		return ret;
1068 	}
1069 
1070 	if (nand->memorg.ntargets > 1 && !spinand->select_target) {
1071 		dev_err(dev,
1072 			"SPI NANDs with more than one die must implement ->select_target()\n");
1073 		return -EINVAL;
1074 	}
1075 
1076 	dev_info(spinand->slave->dev,
1077 		 "%s SPI NAND was found.\n", spinand->manufacturer->name);
1078 	dev_info(spinand->slave->dev,
1079 		 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
1080 		 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
1081 		 nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
1082 
1083 	return 0;
1084 }
1085 
1086 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
1087 				       struct mtd_oob_region *region)
1088 {
1089 	return -ERANGE;
1090 }
1091 
1092 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
1093 					struct mtd_oob_region *region)
1094 {
1095 	if (section)
1096 		return -ERANGE;
1097 
1098 	/* Reserve 2 bytes for the BBM. */
1099 	region->offset = 2;
1100 	region->length = 62;
1101 
1102 	return 0;
1103 }
1104 
1105 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
1106 	.ecc = spinand_noecc_ooblayout_ecc,
1107 	.rfree = spinand_noecc_ooblayout_free,
1108 };
1109 
1110 static int spinand_init(struct spinand_device *spinand)
1111 {
1112 	struct mtd_info *mtd = spinand_to_mtd(spinand);
1113 	struct nand_device *nand = mtd_to_nanddev(mtd);
1114 	int ret, i;
1115 
1116 	/*
1117 	 * We need a scratch buffer because the spi_mem interface requires that
1118 	 * buf passed in spi_mem_op->data.buf be DMA-able.
1119 	 */
1120 	spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
1121 	if (!spinand->scratchbuf)
1122 		return -ENOMEM;
1123 
1124 	ret = spinand_detect(spinand);
1125 	if (ret)
1126 		goto err_free_bufs;
1127 
1128 	/*
1129 	 * Use kzalloc() instead of devm_kzalloc() here, because some drivers
1130 	 * may use this buffer for DMA access.
1131 	 * Memory allocated by devm_ does not guarantee DMA-safe alignment.
1132 	 */
1133 	spinand->databuf = kzalloc(nanddev_page_size(nand) +
1134 			       nanddev_per_page_oobsize(nand),
1135 			       GFP_KERNEL);
1136 	if (!spinand->databuf) {
1137 		ret = -ENOMEM;
1138 		goto err_free_bufs;
1139 	}
1140 
1141 	spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
1142 
1143 	ret = spinand_init_cfg_cache(spinand);
1144 	if (ret)
1145 		goto err_free_bufs;
1146 
1147 	ret = spinand_init_quad_enable(spinand);
1148 	if (ret)
1149 		goto err_free_bufs;
1150 
1151 	ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
1152 	if (ret)
1153 		goto err_free_bufs;
1154 
1155 	ret = spinand_manufacturer_init(spinand);
1156 	if (ret) {
1157 		dev_err(dev,
1158 			"Failed to initialize the SPI NAND chip (err = %d)\n",
1159 			ret);
1160 		goto err_free_bufs;
1161 	}
1162 
1163 	/* After power up, all blocks are locked, so unlock them here. */
1164 	for (i = 0; i < nand->memorg.ntargets; i++) {
1165 		ret = spinand_select_target(spinand, i);
1166 		if (ret)
1167 			goto err_free_bufs;
1168 
1169 		ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
1170 		if (ret)
1171 			goto err_free_bufs;
1172 	}
1173 
1174 	nand->bbt.option = NANDDEV_BBT_USE_FLASH;
1175 	ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
1176 	if (ret)
1177 		goto err_manuf_cleanup;
1178 
1179 	/*
1180 	 * Right now, we don't support ECC, so let the whole oob
1181 	 * area is available for user.
1182 	 */
1183 	mtd->_read_oob = spinand_mtd_read;
1184 	mtd->_write_oob = spinand_mtd_write;
1185 	mtd->_block_isbad = spinand_mtd_block_isbad;
1186 	mtd->_block_markbad = spinand_mtd_block_markbad;
1187 	mtd->_block_isreserved = spinand_mtd_block_isreserved;
1188 	mtd->_erase = spinand_mtd_erase;
1189 
1190 	if (spinand->eccinfo.ooblayout)
1191 		mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
1192 	else
1193 		mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
1194 
1195 	ret = mtd_ooblayout_count_freebytes(mtd);
1196 	if (ret < 0)
1197 		goto err_cleanup_nanddev;
1198 
1199 	mtd->oobavail = ret;
1200 
1201 	/* Propagate ECC information to mtd_info */
1202 	mtd->ecc_strength = nand->eccreq.strength;
1203 	mtd->ecc_step_size = nand->eccreq.step_size;
1204 
1205 	return 0;
1206 
1207 err_cleanup_nanddev:
1208 	nanddev_cleanup(nand);
1209 
1210 err_manuf_cleanup:
1211 	spinand_manufacturer_cleanup(spinand);
1212 
1213 err_free_bufs:
1214 	kfree(spinand->databuf);
1215 	kfree(spinand->scratchbuf);
1216 	return ret;
1217 }
1218 
1219 static void spinand_cleanup(struct spinand_device *spinand)
1220 {
1221 	struct nand_device *nand = spinand_to_nand(spinand);
1222 
1223 	nanddev_cleanup(nand);
1224 	spinand_manufacturer_cleanup(spinand);
1225 	kfree(spinand->databuf);
1226 	kfree(spinand->scratchbuf);
1227 }
1228 
1229 static int spinand_bind(struct udevice *udev)
1230 {
1231 	int ret = 0;
1232 
1233 #ifdef CONFIG_MTD_BLK
1234 	struct udevice *bdev;
1235 
1236 	ret = blk_create_devicef(udev, "mtd_blk", "blk", IF_TYPE_MTD,
1237 				 BLK_MTD_SPI_NAND, 512, 0, &bdev);
1238 	if (ret)
1239 		printf("Cannot create block device\n");
1240 #endif
1241 	return ret;
1242 }
1243 
1244 static int spinand_probe(struct udevice *dev)
1245 {
1246 	struct spinand_device *spinand = dev_get_priv(dev);
1247 	struct spi_slave *slave = dev_get_parent_priv(dev);
1248 	struct mtd_info *mtd = dev_get_uclass_priv(dev);
1249 	struct nand_device *nand = spinand_to_nand(spinand);
1250 	int ret;
1251 
1252 #ifndef __UBOOT__
1253 	spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
1254 			       GFP_KERNEL);
1255 	if (!spinand)
1256 		return -ENOMEM;
1257 
1258 	spinand->spimem = mem;
1259 	spi_mem_set_drvdata(mem, spinand);
1260 	spinand_set_of_node(spinand, mem->spi->dev.of_node);
1261 	mutex_init(&spinand->lock);
1262 
1263 	mtd = spinand_to_mtd(spinand);
1264 	mtd->dev.parent = &mem->spi->dev;
1265 #else
1266 	nand->mtd = mtd;
1267 	mtd->priv = nand;
1268 	mtd->dev = dev;
1269 	mtd->name = malloc(20);
1270 	if (!mtd->name)
1271 		return -ENOMEM;
1272 	sprintf(mtd->name, "spi-nand%d", spi_nand_idx++);
1273 	spinand->slave = slave;
1274 	spinand_set_of_node(spinand, dev->node.np);
1275 #endif
1276 
1277 	ret = spinand_init(spinand);
1278 	if (ret)
1279 		return ret;
1280 
1281 #ifndef __UBOOT__
1282 	ret = mtd_device_register(mtd, NULL, 0);
1283 #else
1284 	ret = add_mtd_device(mtd);
1285 #endif
1286 	if (ret)
1287 		goto err_spinand_cleanup;
1288 
1289 	return 0;
1290 
1291 err_spinand_cleanup:
1292 	spinand_cleanup(spinand);
1293 
1294 	return ret;
1295 }
1296 
1297 #ifndef __UBOOT__
1298 static int spinand_remove(struct udevice *slave)
1299 {
1300 	struct spinand_device *spinand;
1301 	struct mtd_info *mtd;
1302 	int ret;
1303 
1304 	spinand = spi_mem_get_drvdata(slave);
1305 	mtd = spinand_to_mtd(spinand);
1306 	free(mtd->name);
1307 
1308 	ret = mtd_device_unregister(mtd);
1309 	if (ret)
1310 		return ret;
1311 
1312 	spinand_cleanup(spinand);
1313 
1314 	return 0;
1315 }
1316 
1317 static const struct spi_device_id spinand_ids[] = {
1318 	{ .name = "spi-nand" },
1319 	{ /* sentinel */ },
1320 };
1321 
1322 #ifdef CONFIG_OF
1323 static const struct of_device_id spinand_of_ids[] = {
1324 	{ .compatible = "spi-nand" },
1325 	{ /* sentinel */ },
1326 };
1327 #endif
1328 
1329 static struct spi_mem_driver spinand_drv = {
1330 	.spidrv = {
1331 		.id_table = spinand_ids,
1332 		.driver = {
1333 			.name = "spi-nand",
1334 			.of_match_table = of_match_ptr(spinand_of_ids),
1335 		},
1336 	},
1337 	.probe = spinand_probe,
1338 	.remove = spinand_remove,
1339 };
1340 module_spi_mem_driver(spinand_drv);
1341 
1342 MODULE_DESCRIPTION("SPI NAND framework");
1343 MODULE_AUTHOR("Peter Pan<peterpandong@micron.com>");
1344 MODULE_LICENSE("GPL v2");
1345 #endif /* __UBOOT__ */
1346 
1347 static const struct udevice_id spinand_ids[] = {
1348 	{ .compatible = "spi-nand" },
1349 	{ /* sentinel */ },
1350 };
1351 
1352 U_BOOT_DRIVER(spinand) = {
1353 	.name = "spi_nand",
1354 	.id = UCLASS_MTD,
1355 	.of_match = spinand_ids,
1356 	.bind	= spinand_bind,
1357 	.priv_auto_alloc_size = sizeof(struct spinand_device),
1358 	.probe = spinand_probe,
1359 };
1360