1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2016-2017 Micron Technology, Inc.
10 #define pr_fmt(fmt) "spi-nand: " fmt
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>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/spi/spi.h>
21 #include <linux/spi/spi-mem.h>
23 static int spinand_read_reg_op(struct spinand_device *spinand, u8 reg, u8 *val)
25 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(reg,
29 ret = spi_mem_exec_op(spinand->spimem, &op);
33 *val = *spinand->scratchbuf;
37 int spinand_write_reg_op(struct spinand_device *spinand, u8 reg, u8 val)
39 struct spi_mem_op op = SPINAND_SET_FEATURE_OP(reg,
42 *spinand->scratchbuf = val;
43 return spi_mem_exec_op(spinand->spimem, &op);
46 static int spinand_read_status(struct spinand_device *spinand, u8 *status)
48 return spinand_read_reg_op(spinand, REG_STATUS, status);
51 static int spinand_get_cfg(struct spinand_device *spinand, u8 *cfg)
53 struct nand_device *nand = spinand_to_nand(spinand);
55 if (WARN_ON(spinand->cur_target < 0 ||
56 spinand->cur_target >= nand->memorg.ntargets))
59 *cfg = spinand->cfg_cache[spinand->cur_target];
63 static int spinand_set_cfg(struct spinand_device *spinand, u8 cfg)
65 struct nand_device *nand = spinand_to_nand(spinand);
68 if (WARN_ON(spinand->cur_target < 0 ||
69 spinand->cur_target >= nand->memorg.ntargets))
72 if (spinand->cfg_cache[spinand->cur_target] == cfg)
75 ret = spinand_write_reg_op(spinand, REG_CFG, cfg);
79 spinand->cfg_cache[spinand->cur_target] = cfg;
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
89 * Update the configuration register.
91 * Return: 0 on success, a negative error code otherwise.
93 int spinand_upd_cfg(struct spinand_device *spinand, u8 mask, u8 val)
98 ret = spinand_get_cfg(spinand, &cfg);
105 return spinand_set_cfg(spinand, cfg);
109 * spinand_select_target() - Select a specific NAND target/die
110 * @spinand: the spinand device
111 * @target: the target/die to select
113 * Select a new target/die. If chip only has one die, this function is a NOOP.
115 * Return: 0 on success, a negative error code otherwise.
117 int spinand_select_target(struct spinand_device *spinand, unsigned int target)
119 struct nand_device *nand = spinand_to_nand(spinand);
122 if (WARN_ON(target >= nand->memorg.ntargets))
125 if (spinand->cur_target == target)
128 if (nand->memorg.ntargets == 1) {
129 spinand->cur_target = target;
133 ret = spinand->select_target(spinand, target);
137 spinand->cur_target = target;
141 static int spinand_read_cfg(struct spinand_device *spinand)
143 struct nand_device *nand = spinand_to_nand(spinand);
147 for (target = 0; target < nand->memorg.ntargets; target++) {
148 ret = spinand_select_target(spinand, target);
153 * We use spinand_read_reg_op() instead of spinand_get_cfg()
154 * here to bypass the config cache.
156 ret = spinand_read_reg_op(spinand, REG_CFG,
157 &spinand->cfg_cache[target]);
165 static int spinand_init_cfg_cache(struct spinand_device *spinand)
167 struct nand_device *nand = spinand_to_nand(spinand);
168 struct device *dev = &spinand->spimem->spi->dev;
170 spinand->cfg_cache = devm_kcalloc(dev,
171 nand->memorg.ntargets,
172 sizeof(*spinand->cfg_cache),
174 if (!spinand->cfg_cache)
180 static int spinand_init_quad_enable(struct spinand_device *spinand)
184 if (!(spinand->flags & SPINAND_HAS_QE_BIT))
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)
192 return spinand_upd_cfg(spinand, CFG_QUAD_ENABLE,
193 enable ? CFG_QUAD_ENABLE : 0);
196 static int spinand_ecc_enable(struct spinand_device *spinand,
199 return spinand_upd_cfg(spinand, CFG_ECC_ENABLE,
200 enable ? CFG_ECC_ENABLE : 0);
203 static int spinand_cont_read_enable(struct spinand_device *spinand,
206 return spinand->set_cont_read(spinand, enable);
209 static int spinand_check_ecc_status(struct spinand_device *spinand, u8 status)
211 struct nand_device *nand = spinand_to_nand(spinand);
213 if (spinand->eccinfo.get_status)
214 return spinand->eccinfo.get_status(spinand, status);
216 switch (status & STATUS_ECC_MASK) {
217 case STATUS_ECC_NO_BITFLIPS:
220 case STATUS_ECC_HAS_BITFLIPS:
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.
226 return nanddev_get_ecc_conf(nand)->strength;
228 case STATUS_ECC_UNCOR_ERROR:
238 static int spinand_noecc_ooblayout_ecc(struct mtd_info *mtd, int section,
239 struct mtd_oob_region *region)
244 static int spinand_noecc_ooblayout_free(struct mtd_info *mtd, int section,
245 struct mtd_oob_region *region)
250 /* Reserve 2 bytes for the BBM. */
257 static const struct mtd_ooblayout_ops spinand_noecc_ooblayout = {
258 .ecc = spinand_noecc_ooblayout_ecc,
259 .free = spinand_noecc_ooblayout_free,
262 static int spinand_ondie_ecc_init_ctx(struct nand_device *nand)
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;
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;
272 engine_conf = kzalloc(sizeof(*engine_conf), GFP_KERNEL);
276 nand->ecc.ctx.priv = engine_conf;
278 if (spinand->eccinfo.ooblayout)
279 mtd_set_ooblayout(mtd, spinand->eccinfo.ooblayout);
281 mtd_set_ooblayout(mtd, &spinand_noecc_ooblayout);
286 static void spinand_ondie_ecc_cleanup_ctx(struct nand_device *nand)
288 kfree(nand->ecc.ctx.priv);
291 static int spinand_ondie_ecc_prepare_io_req(struct nand_device *nand,
292 struct nand_page_io_req *req)
294 struct spinand_device *spinand = nand_to_spinand(nand);
295 bool enable = (req->mode != MTD_OPS_RAW);
297 if (!enable && spinand->flags & SPINAND_NO_RAW_ACCESS)
300 memset(spinand->oobbuf, 0xff, nanddev_per_page_oobsize(nand));
302 /* Only enable or disable the engine */
303 return spinand_ecc_enable(spinand, enable);
306 static int spinand_ondie_ecc_finish_io_req(struct nand_device *nand,
307 struct nand_page_io_req *req)
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);
314 if (req->mode == MTD_OPS_RAW)
317 /* Nothing to do when finishing a page write */
318 if (req->type == NAND_PAGE_WRITE)
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) {
329 * Continuous reads don't allow us to get the detail,
330 * so we may exagerate the actual number of corrected bitflips.
332 if (!req->continuous)
335 pages = req->datalen / nanddev_page_size(nand);
337 mtd->ecc_stats.corrected += ret * pages;
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,
350 static struct nand_ecc_engine spinand_ondie_ecc_engine = {
351 .ops = &spinand_ondie_ecc_engine_ops,
354 static void spinand_ondie_ecc_save_status(struct nand_device *nand, u8 status)
356 struct spinand_ondie_ecc_conf *engine_conf = nand->ecc.ctx.priv;
358 if (nand->ecc.ctx.conf.engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE &&
360 engine_conf->status = status;
363 static int spinand_write_enable_op(struct spinand_device *spinand)
365 struct spi_mem_op op = SPINAND_WR_EN_DIS_OP(true);
367 return spi_mem_exec_op(spinand->spimem, &op);
370 static int spinand_load_page_op(struct spinand_device *spinand,
371 const struct nand_page_io_req *req)
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_OP(row);
377 return spi_mem_exec_op(spinand->spimem, &op);
380 static int spinand_read_from_cache_op(struct spinand_device *spinand,
381 const struct nand_page_io_req *req)
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;
392 buf = spinand->databuf;
393 if (!req->continuous)
394 nbytes = nanddev_page_size(nand);
396 nbytes = round_up(req->dataoffs + req->datalen,
397 nanddev_page_size(nand));
402 nbytes += nanddev_per_page_oobsize(nand);
404 buf = spinand->oobbuf;
405 column = nanddev_page_size(nand);
409 if (req->mode == MTD_OPS_RAW)
410 rdesc = spinand->dirmaps[req->pos.plane].rdesc;
412 rdesc = spinand->dirmaps[req->pos.plane].rdesc_ecc;
414 if (spinand->flags & SPINAND_HAS_READ_PLANE_SELECT_BIT)
415 column |= req->pos.plane << fls(nanddev_page_size(nand));
418 ret = spi_mem_dirmap_read(rdesc, column, nbytes, buf);
422 if (!ret || ret > nbytes)
430 * Dirmap accesses are allowed to toggle the CS.
431 * Toggling the CS during a continuous read is forbidden.
433 if (nbytes && req->continuous)
438 memcpy(req->databuf.in, spinand->databuf + req->dataoffs,
442 if (req->mode == MTD_OPS_AUTO_OOB)
443 mtd_ooblayout_get_databytes(mtd, req->oobbuf.in,
448 memcpy(req->oobbuf.in, spinand->oobbuf + req->ooboffs,
455 static int spinand_write_to_cache_op(struct spinand_device *spinand,
456 const struct nand_page_io_req *req)
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;
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.
472 * Only reset the data buffer manually, the OOB buffer is prepared by
473 * ECC engines ->prepare_io_req() callback.
475 nbytes = nanddev_page_size(nand) + nanddev_per_page_oobsize(nand);
476 memset(spinand->databuf, 0xff, nanddev_page_size(nand));
479 memcpy(spinand->databuf + req->dataoffs, req->databuf.out,
483 if (req->mode == MTD_OPS_AUTO_OOB)
484 mtd_ooblayout_set_databytes(mtd, req->oobbuf.out,
489 memcpy(spinand->oobbuf + req->ooboffs, req->oobbuf.out,
493 if (req->mode == MTD_OPS_RAW)
494 wdesc = spinand->dirmaps[req->pos.plane].wdesc;
496 wdesc = spinand->dirmaps[req->pos.plane].wdesc_ecc;
498 if (spinand->flags & SPINAND_HAS_PROG_PLANE_SELECT_BIT)
499 column |= req->pos.plane << fls(nanddev_page_size(nand));
502 ret = spi_mem_dirmap_write(wdesc, column, nbytes, buf);
506 if (!ret || ret > nbytes)
517 static int spinand_program_op(struct spinand_device *spinand,
518 const struct nand_page_io_req *req)
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_OP(row);
524 return spi_mem_exec_op(spinand->spimem, &op);
527 static int spinand_erase_op(struct spinand_device *spinand,
528 const struct nand_pos *pos)
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_OP(row);
534 return spi_mem_exec_op(spinand->spimem, &op);
537 static int spinand_wait(struct spinand_device *spinand,
538 unsigned long initial_delay_us,
539 unsigned long poll_delay_us,
542 struct spi_mem_op op = SPINAND_GET_FEATURE_OP(REG_STATUS,
543 spinand->scratchbuf);
547 ret = spi_mem_poll_status(spinand->spimem, &op, STATUS_BUSY, 0,
550 SPINAND_WAITRDY_TIMEOUT_MS);
554 status = *spinand->scratchbuf;
555 if (!(status & STATUS_BUSY))
559 * Extra read, just in case the STATUS_READY bit has changed
560 * since our last check
562 ret = spinand_read_status(spinand, &status);
570 return status & STATUS_BUSY ? -ETIMEDOUT : 0;
573 static int spinand_read_id_op(struct spinand_device *spinand, u8 naddr,
576 struct spi_mem_op op = SPINAND_READID_OP(
577 naddr, ndummy, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
580 ret = spi_mem_exec_op(spinand->spimem, &op);
582 memcpy(buf, spinand->scratchbuf, SPINAND_MAX_ID_LEN);
587 static int spinand_reset_op(struct spinand_device *spinand)
589 struct spi_mem_op op = SPINAND_RESET_OP;
592 ret = spi_mem_exec_op(spinand->spimem, &op);
596 return spinand_wait(spinand,
597 SPINAND_RESET_INITIAL_DELAY_US,
598 SPINAND_RESET_POLL_DELAY_US,
602 static int spinand_lock_block(struct spinand_device *spinand, u8 lock)
604 return spinand_write_reg_op(spinand, REG_BLOCK_LOCK, lock);
607 static int spinand_read_page(struct spinand_device *spinand,
608 const struct nand_page_io_req *req)
610 struct nand_device *nand = spinand_to_nand(spinand);
614 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
618 ret = spinand_load_page_op(spinand, req);
622 ret = spinand_wait(spinand,
623 SPINAND_READ_INITIAL_DELAY_US,
624 SPINAND_READ_POLL_DELAY_US,
629 spinand_ondie_ecc_save_status(nand, status);
631 ret = spinand_read_from_cache_op(spinand, req);
635 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
638 static int spinand_write_page(struct spinand_device *spinand,
639 const struct nand_page_io_req *req)
641 struct nand_device *nand = spinand_to_nand(spinand);
645 ret = nand_ecc_prepare_io_req(nand, (struct nand_page_io_req *)req);
649 ret = spinand_write_enable_op(spinand);
653 ret = spinand_write_to_cache_op(spinand, req);
657 ret = spinand_program_op(spinand, req);
661 ret = spinand_wait(spinand,
662 SPINAND_WRITE_INITIAL_DELAY_US,
663 SPINAND_WRITE_POLL_DELAY_US,
665 if (!ret && (status & STATUS_PROG_FAILED))
668 return nand_ecc_finish_io_req(nand, (struct nand_page_io_req *)req);
671 static int spinand_mtd_regular_page_read(struct mtd_info *mtd, loff_t from,
672 struct mtd_oob_ops *ops,
673 unsigned int *max_bitflips)
675 struct spinand_device *spinand = mtd_to_spinand(mtd);
676 struct nand_device *nand = mtd_to_nanddev(mtd);
677 struct nand_io_iter iter;
678 bool disable_ecc = false;
679 bool ecc_failed = false;
682 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
685 nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) {
687 iter.req.mode = MTD_OPS_RAW;
689 ret = spinand_select_target(spinand, iter.req.pos.target);
693 ret = spinand_read_page(spinand, &iter.req);
694 if (ret < 0 && ret != -EBADMSG)
700 *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
703 ops->retlen += iter.req.datalen;
704 ops->oobretlen += iter.req.ooblen;
707 if (ecc_failed && !ret)
713 static int spinand_mtd_continuous_page_read(struct mtd_info *mtd, loff_t from,
714 struct mtd_oob_ops *ops,
715 unsigned int *max_bitflips)
717 struct spinand_device *spinand = mtd_to_spinand(mtd);
718 struct nand_device *nand = mtd_to_nanddev(mtd);
719 struct nand_io_iter iter;
723 ret = spinand_cont_read_enable(spinand, true);
728 * The cache is divided into two halves. While one half of the cache has
729 * the requested data, the other half is loaded with the next chunk of data.
730 * Therefore, the host can read out the data continuously from page to page.
731 * Each data read must be a multiple of 4-bytes and full pages should be read;
732 * otherwise, the data output might get out of sequence from one read command
735 nanddev_io_for_each_block(nand, NAND_PAGE_READ, from, ops, &iter) {
736 ret = spinand_select_target(spinand, iter.req.pos.target);
740 ret = nand_ecc_prepare_io_req(nand, &iter.req);
744 ret = spinand_load_page_op(spinand, &iter.req);
748 ret = spinand_wait(spinand, SPINAND_READ_INITIAL_DELAY_US,
749 SPINAND_READ_POLL_DELAY_US, NULL);
753 ret = spinand_read_from_cache_op(spinand, &iter.req);
757 ops->retlen += iter.req.datalen;
759 ret = spinand_read_status(spinand, &status);
763 spinand_ondie_ecc_save_status(nand, status);
765 ret = nand_ecc_finish_io_req(nand, &iter.req);
769 *max_bitflips = max_t(unsigned int, *max_bitflips, ret);
775 * Once all the data has been read out, the host can either pull CS#
776 * high and wait for tRST or manually clear the bit in the configuration
777 * register to terminate the continuous read operation. We have no
778 * guarantee the SPI controller drivers will effectively deassert the CS
779 * when we expect them to, so take the register based approach.
781 spinand_cont_read_enable(spinand, false);
786 static void spinand_cont_read_init(struct spinand_device *spinand)
788 struct nand_device *nand = spinand_to_nand(spinand);
789 enum nand_ecc_engine_type engine_type = nand->ecc.ctx.conf.engine_type;
791 /* OOBs cannot be retrieved so external/on-host ECC engine won't work */
792 if (spinand->set_cont_read &&
793 (engine_type == NAND_ECC_ENGINE_TYPE_ON_DIE ||
794 engine_type == NAND_ECC_ENGINE_TYPE_NONE)) {
795 spinand->cont_read_possible = true;
799 static bool spinand_use_cont_read(struct mtd_info *mtd, loff_t from,
800 struct mtd_oob_ops *ops)
802 struct nand_device *nand = mtd_to_nanddev(mtd);
803 struct spinand_device *spinand = nand_to_spinand(nand);
804 struct nand_pos start_pos, end_pos;
806 if (!spinand->cont_read_possible)
809 /* OOBs won't be retrieved */
810 if (ops->ooblen || ops->oobbuf)
813 nanddev_offs_to_pos(nand, from, &start_pos);
814 nanddev_offs_to_pos(nand, from + ops->len - 1, &end_pos);
817 * Continuous reads never cross LUN boundaries. Some devices don't
818 * support crossing planes boundaries. Some devices don't even support
819 * crossing blocks boundaries. The common case being to read through UBI,
820 * we will very rarely read two consequent blocks or more, so it is safer
821 * and easier (can be improved) to only enable continuous reads when
822 * reading within the same erase block.
824 if (start_pos.target != end_pos.target ||
825 start_pos.plane != end_pos.plane ||
826 start_pos.eraseblock != end_pos.eraseblock)
829 return start_pos.page < end_pos.page;
832 static int spinand_mtd_read(struct mtd_info *mtd, loff_t from,
833 struct mtd_oob_ops *ops)
835 struct spinand_device *spinand = mtd_to_spinand(mtd);
836 struct mtd_ecc_stats old_stats;
837 unsigned int max_bitflips = 0;
840 mutex_lock(&spinand->lock);
842 old_stats = mtd->ecc_stats;
844 if (spinand_use_cont_read(mtd, from, ops))
845 ret = spinand_mtd_continuous_page_read(mtd, from, ops, &max_bitflips);
847 ret = spinand_mtd_regular_page_read(mtd, from, ops, &max_bitflips);
850 ops->stats->uncorrectable_errors +=
851 mtd->ecc_stats.failed - old_stats.failed;
852 ops->stats->corrected_bitflips +=
853 mtd->ecc_stats.corrected - old_stats.corrected;
856 mutex_unlock(&spinand->lock);
858 return ret ? ret : max_bitflips;
861 static int spinand_mtd_write(struct mtd_info *mtd, loff_t to,
862 struct mtd_oob_ops *ops)
864 struct spinand_device *spinand = mtd_to_spinand(mtd);
865 struct nand_device *nand = mtd_to_nanddev(mtd);
866 struct nand_io_iter iter;
867 bool disable_ecc = false;
870 if (ops->mode == MTD_OPS_RAW || !mtd->ooblayout)
873 mutex_lock(&spinand->lock);
875 nanddev_io_for_each_page(nand, NAND_PAGE_WRITE, to, ops, &iter) {
877 iter.req.mode = MTD_OPS_RAW;
879 ret = spinand_select_target(spinand, iter.req.pos.target);
883 ret = spinand_write_page(spinand, &iter.req);
887 ops->retlen += iter.req.datalen;
888 ops->oobretlen += iter.req.ooblen;
891 mutex_unlock(&spinand->lock);
896 static bool spinand_isbad(struct nand_device *nand, const struct nand_pos *pos)
898 struct spinand_device *spinand = nand_to_spinand(nand);
900 struct nand_page_io_req req = {
902 .ooblen = sizeof(marker),
909 spinand_select_target(spinand, pos->target);
911 ret = spinand_read_page(spinand, &req);
912 if (ret == -EOPNOTSUPP) {
913 /* Retry with ECC in case raw access is not supported */
914 req.mode = MTD_OPS_PLACE_OOB;
915 spinand_read_page(spinand, &req);
918 if (marker[0] != 0xff || marker[1] != 0xff)
924 static int spinand_mtd_block_isbad(struct mtd_info *mtd, loff_t offs)
926 struct nand_device *nand = mtd_to_nanddev(mtd);
927 struct spinand_device *spinand = nand_to_spinand(nand);
931 nanddev_offs_to_pos(nand, offs, &pos);
932 mutex_lock(&spinand->lock);
933 ret = nanddev_isbad(nand, &pos);
934 mutex_unlock(&spinand->lock);
939 static int spinand_markbad(struct nand_device *nand, const struct nand_pos *pos)
941 struct spinand_device *spinand = nand_to_spinand(nand);
943 struct nand_page_io_req req = {
946 .ooblen = sizeof(marker),
947 .oobbuf.out = marker,
952 ret = spinand_select_target(spinand, pos->target);
956 ret = spinand_write_page(spinand, &req);
957 if (ret == -EOPNOTSUPP) {
958 /* Retry with ECC in case raw access is not supported */
959 req.mode = MTD_OPS_PLACE_OOB;
960 ret = spinand_write_page(spinand, &req);
966 static int spinand_mtd_block_markbad(struct mtd_info *mtd, loff_t offs)
968 struct nand_device *nand = mtd_to_nanddev(mtd);
969 struct spinand_device *spinand = nand_to_spinand(nand);
973 nanddev_offs_to_pos(nand, offs, &pos);
974 mutex_lock(&spinand->lock);
975 ret = nanddev_markbad(nand, &pos);
976 mutex_unlock(&spinand->lock);
981 static int spinand_erase(struct nand_device *nand, const struct nand_pos *pos)
983 struct spinand_device *spinand = nand_to_spinand(nand);
987 ret = spinand_select_target(spinand, pos->target);
991 ret = spinand_write_enable_op(spinand);
995 ret = spinand_erase_op(spinand, pos);
999 ret = spinand_wait(spinand,
1000 SPINAND_ERASE_INITIAL_DELAY_US,
1001 SPINAND_ERASE_POLL_DELAY_US,
1004 if (!ret && (status & STATUS_ERASE_FAILED))
1010 static int spinand_mtd_erase(struct mtd_info *mtd,
1011 struct erase_info *einfo)
1013 struct spinand_device *spinand = mtd_to_spinand(mtd);
1016 mutex_lock(&spinand->lock);
1017 ret = nanddev_mtd_erase(mtd, einfo);
1018 mutex_unlock(&spinand->lock);
1023 static int spinand_mtd_block_isreserved(struct mtd_info *mtd, loff_t offs)
1025 struct spinand_device *spinand = mtd_to_spinand(mtd);
1026 struct nand_device *nand = mtd_to_nanddev(mtd);
1027 struct nand_pos pos;
1030 nanddev_offs_to_pos(nand, offs, &pos);
1031 mutex_lock(&spinand->lock);
1032 ret = nanddev_isreserved(nand, &pos);
1033 mutex_unlock(&spinand->lock);
1038 static int spinand_create_dirmap(struct spinand_device *spinand,
1041 struct nand_device *nand = spinand_to_nand(spinand);
1042 struct spi_mem_dirmap_info info = {
1043 .length = nanddev_page_size(nand) +
1044 nanddev_per_page_oobsize(nand),
1046 struct spi_mem_dirmap_desc *desc;
1048 if (spinand->cont_read_possible)
1049 info.length = nanddev_eraseblock_size(nand);
1051 /* The plane number is passed in MSB just above the column address */
1052 info.offset = plane << fls(nand->memorg.pagesize);
1054 info.op_tmpl = *spinand->op_templates.update_cache;
1055 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1056 spinand->spimem, &info);
1058 return PTR_ERR(desc);
1060 spinand->dirmaps[plane].wdesc = desc;
1062 info.op_tmpl = *spinand->op_templates.read_cache;
1063 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1064 spinand->spimem, &info);
1066 return PTR_ERR(desc);
1068 spinand->dirmaps[plane].rdesc = desc;
1070 if (nand->ecc.engine->integration != NAND_ECC_ENGINE_INTEGRATION_PIPELINED) {
1071 spinand->dirmaps[plane].wdesc_ecc = spinand->dirmaps[plane].wdesc;
1072 spinand->dirmaps[plane].rdesc_ecc = spinand->dirmaps[plane].rdesc;
1077 info.op_tmpl = *spinand->op_templates.update_cache;
1078 info.op_tmpl.data.ecc = true;
1079 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1080 spinand->spimem, &info);
1082 return PTR_ERR(desc);
1084 spinand->dirmaps[plane].wdesc_ecc = desc;
1086 info.op_tmpl = *spinand->op_templates.read_cache;
1087 info.op_tmpl.data.ecc = true;
1088 desc = devm_spi_mem_dirmap_create(&spinand->spimem->spi->dev,
1089 spinand->spimem, &info);
1091 return PTR_ERR(desc);
1093 spinand->dirmaps[plane].rdesc_ecc = desc;
1098 static int spinand_create_dirmaps(struct spinand_device *spinand)
1100 struct nand_device *nand = spinand_to_nand(spinand);
1103 spinand->dirmaps = devm_kzalloc(&spinand->spimem->spi->dev,
1104 sizeof(*spinand->dirmaps) *
1105 nand->memorg.planes_per_lun,
1107 if (!spinand->dirmaps)
1110 for (i = 0; i < nand->memorg.planes_per_lun; i++) {
1111 ret = spinand_create_dirmap(spinand, i);
1119 static const struct nand_ops spinand_ops = {
1120 .erase = spinand_erase,
1121 .markbad = spinand_markbad,
1122 .isbad = spinand_isbad,
1125 static const struct spinand_manufacturer *spinand_manufacturers[] = {
1126 &alliancememory_spinand_manufacturer,
1127 &ato_spinand_manufacturer,
1128 &esmt_c8_spinand_manufacturer,
1129 &foresee_spinand_manufacturer,
1130 &gigadevice_spinand_manufacturer,
1131 ¯onix_spinand_manufacturer,
1132 µn_spinand_manufacturer,
1133 ¶gon_spinand_manufacturer,
1134 &skyhigh_spinand_manufacturer,
1135 &toshiba_spinand_manufacturer,
1136 &winbond_spinand_manufacturer,
1137 &xtx_spinand_manufacturer,
1140 static int spinand_manufacturer_match(struct spinand_device *spinand,
1141 enum spinand_readid_method rdid_method)
1143 u8 *id = spinand->id.data;
1147 for (i = 0; i < ARRAY_SIZE(spinand_manufacturers); i++) {
1148 const struct spinand_manufacturer *manufacturer =
1149 spinand_manufacturers[i];
1151 if (id[0] != manufacturer->id)
1154 ret = spinand_match_and_init(spinand,
1155 manufacturer->chips,
1156 manufacturer->nchips,
1161 spinand->manufacturer = manufacturer;
1167 static int spinand_id_detect(struct spinand_device *spinand)
1169 u8 *id = spinand->id.data;
1172 ret = spinand_read_id_op(spinand, 0, 0, id);
1175 ret = spinand_manufacturer_match(spinand, SPINAND_READID_METHOD_OPCODE);
1179 ret = spinand_read_id_op(spinand, 1, 0, id);
1182 ret = spinand_manufacturer_match(spinand,
1183 SPINAND_READID_METHOD_OPCODE_ADDR);
1187 ret = spinand_read_id_op(spinand, 0, 1, id);
1190 ret = spinand_manufacturer_match(spinand,
1191 SPINAND_READID_METHOD_OPCODE_DUMMY);
1196 static int spinand_manufacturer_init(struct spinand_device *spinand)
1198 if (spinand->manufacturer->ops->init)
1199 return spinand->manufacturer->ops->init(spinand);
1204 static void spinand_manufacturer_cleanup(struct spinand_device *spinand)
1206 /* Release manufacturer private data */
1207 if (spinand->manufacturer->ops->cleanup)
1208 return spinand->manufacturer->ops->cleanup(spinand);
1211 static const struct spi_mem_op *
1212 spinand_select_op_variant(struct spinand_device *spinand,
1213 const struct spinand_op_variants *variants)
1215 struct nand_device *nand = spinand_to_nand(spinand);
1216 const struct spi_mem_op *best_variant = NULL;
1217 u64 best_op_duration_ns = ULLONG_MAX;
1220 for (i = 0; i < variants->nops; i++) {
1221 struct spi_mem_op op = variants->ops[i];
1222 u64 op_duration_ns = 0;
1223 unsigned int nbytes;
1226 nbytes = nanddev_per_page_oobsize(nand) +
1227 nanddev_page_size(nand);
1230 op.data.nbytes = nbytes;
1231 ret = spi_mem_adjust_op_size(spinand->spimem, &op);
1235 spi_mem_adjust_op_freq(spinand->spimem, &op);
1237 if (!spi_mem_supports_op(spinand->spimem, &op))
1240 nbytes -= op.data.nbytes;
1242 op_duration_ns += spi_mem_calc_op_duration(&op);
1245 if (!nbytes && op_duration_ns < best_op_duration_ns) {
1246 best_op_duration_ns = op_duration_ns;
1247 best_variant = &variants->ops[i];
1251 return best_variant;
1255 * spinand_match_and_init() - Try to find a match between a device ID and an
1256 * entry in a spinand_info table
1257 * @spinand: SPI NAND object
1258 * @table: SPI NAND device description table
1259 * @table_size: size of the device description table
1260 * @rdid_method: read id method to match
1262 * Match between a device ID retrieved through the READ_ID command and an
1263 * entry in the SPI NAND description table. If a match is found, the spinand
1264 * object will be initialized with information provided by the matching
1265 * spinand_info entry.
1267 * Return: 0 on success, a negative error code otherwise.
1269 int spinand_match_and_init(struct spinand_device *spinand,
1270 const struct spinand_info *table,
1271 unsigned int table_size,
1272 enum spinand_readid_method rdid_method)
1274 u8 *id = spinand->id.data;
1275 struct nand_device *nand = spinand_to_nand(spinand);
1278 for (i = 0; i < table_size; i++) {
1279 const struct spinand_info *info = &table[i];
1280 const struct spi_mem_op *op;
1282 if (rdid_method != info->devid.method)
1285 if (memcmp(id + 1, info->devid.id, info->devid.len))
1288 nand->memorg = table[i].memorg;
1289 nanddev_set_ecc_requirements(nand, &table[i].eccreq);
1290 spinand->eccinfo = table[i].eccinfo;
1291 spinand->flags = table[i].flags;
1292 spinand->id.len = 1 + table[i].devid.len;
1293 spinand->select_target = table[i].select_target;
1294 spinand->set_cont_read = table[i].set_cont_read;
1296 op = spinand_select_op_variant(spinand,
1297 info->op_variants.read_cache);
1301 spinand->op_templates.read_cache = op;
1303 op = spinand_select_op_variant(spinand,
1304 info->op_variants.write_cache);
1308 spinand->op_templates.write_cache = op;
1310 op = spinand_select_op_variant(spinand,
1311 info->op_variants.update_cache);
1312 spinand->op_templates.update_cache = op;
1320 static int spinand_detect(struct spinand_device *spinand)
1322 struct device *dev = &spinand->spimem->spi->dev;
1323 struct nand_device *nand = spinand_to_nand(spinand);
1326 ret = spinand_reset_op(spinand);
1330 ret = spinand_id_detect(spinand);
1332 dev_err(dev, "unknown raw ID %*phN\n", SPINAND_MAX_ID_LEN,
1337 if (nand->memorg.ntargets > 1 && !spinand->select_target) {
1339 "SPI NANDs with more than one die must implement ->select_target()\n");
1343 dev_info(&spinand->spimem->spi->dev,
1344 "%s SPI NAND was found.\n", spinand->manufacturer->name);
1345 dev_info(&spinand->spimem->spi->dev,
1346 "%llu MiB, block size: %zu KiB, page size: %zu, OOB size: %u\n",
1347 nanddev_size(nand) >> 20, nanddev_eraseblock_size(nand) >> 10,
1348 nanddev_page_size(nand), nanddev_per_page_oobsize(nand));
1353 static int spinand_init_flash(struct spinand_device *spinand)
1355 struct device *dev = &spinand->spimem->spi->dev;
1356 struct nand_device *nand = spinand_to_nand(spinand);
1359 ret = spinand_read_cfg(spinand);
1363 ret = spinand_init_quad_enable(spinand);
1367 ret = spinand_upd_cfg(spinand, CFG_OTP_ENABLE, 0);
1371 ret = spinand_manufacturer_init(spinand);
1374 "Failed to initialize the SPI NAND chip (err = %d)\n",
1379 /* After power up, all blocks are locked, so unlock them here. */
1380 for (i = 0; i < nand->memorg.ntargets; i++) {
1381 ret = spinand_select_target(spinand, i);
1385 ret = spinand_lock_block(spinand, BL_ALL_UNLOCKED);
1391 spinand_manufacturer_cleanup(spinand);
1396 static void spinand_mtd_resume(struct mtd_info *mtd)
1398 struct spinand_device *spinand = mtd_to_spinand(mtd);
1401 ret = spinand_reset_op(spinand);
1405 ret = spinand_init_flash(spinand);
1409 spinand_ecc_enable(spinand, false);
1412 static int spinand_init(struct spinand_device *spinand)
1414 struct device *dev = &spinand->spimem->spi->dev;
1415 struct mtd_info *mtd = spinand_to_mtd(spinand);
1416 struct nand_device *nand = mtd_to_nanddev(mtd);
1420 * We need a scratch buffer because the spi_mem interface requires that
1421 * buf passed in spi_mem_op->data.buf be DMA-able.
1423 spinand->scratchbuf = kzalloc(SPINAND_MAX_ID_LEN, GFP_KERNEL);
1424 if (!spinand->scratchbuf)
1427 ret = spinand_detect(spinand);
1432 * Use kzalloc() instead of devm_kzalloc() here, because some drivers
1433 * may use this buffer for DMA access.
1434 * Memory allocated by devm_ does not guarantee DMA-safe alignment.
1436 spinand->databuf = kzalloc(nanddev_eraseblock_size(nand),
1438 if (!spinand->databuf) {
1443 spinand->oobbuf = spinand->databuf + nanddev_page_size(nand);
1445 ret = spinand_init_cfg_cache(spinand);
1449 ret = spinand_init_flash(spinand);
1453 ret = nanddev_init(nand, &spinand_ops, THIS_MODULE);
1455 goto err_manuf_cleanup;
1457 /* SPI-NAND default ECC engine is on-die */
1458 nand->ecc.defaults.engine_type = NAND_ECC_ENGINE_TYPE_ON_DIE;
1459 nand->ecc.ondie_engine = &spinand_ondie_ecc_engine;
1461 spinand_ecc_enable(spinand, false);
1462 ret = nanddev_ecc_engine_init(nand);
1464 goto err_cleanup_nanddev;
1467 * Continuous read can only be enabled with an on-die ECC engine, so the
1468 * ECC initialization must have happened previously.
1470 spinand_cont_read_init(spinand);
1472 mtd->_read_oob = spinand_mtd_read;
1473 mtd->_write_oob = spinand_mtd_write;
1474 mtd->_block_isbad = spinand_mtd_block_isbad;
1475 mtd->_block_markbad = spinand_mtd_block_markbad;
1476 mtd->_block_isreserved = spinand_mtd_block_isreserved;
1477 mtd->_erase = spinand_mtd_erase;
1478 mtd->_max_bad_blocks = nanddev_mtd_max_bad_blocks;
1479 mtd->_resume = spinand_mtd_resume;
1481 if (nand->ecc.engine) {
1482 ret = mtd_ooblayout_count_freebytes(mtd);
1484 goto err_cleanup_ecc_engine;
1487 mtd->oobavail = ret;
1489 /* Propagate ECC information to mtd_info */
1490 mtd->ecc_strength = nanddev_get_ecc_conf(nand)->strength;
1491 mtd->ecc_step_size = nanddev_get_ecc_conf(nand)->step_size;
1492 mtd->bitflip_threshold = DIV_ROUND_UP(mtd->ecc_strength * 3, 4);
1494 ret = spinand_create_dirmaps(spinand);
1497 "Failed to create direct mappings for read/write operations (err = %d)\n",
1499 goto err_cleanup_ecc_engine;
1504 err_cleanup_ecc_engine:
1505 nanddev_ecc_engine_cleanup(nand);
1507 err_cleanup_nanddev:
1508 nanddev_cleanup(nand);
1511 spinand_manufacturer_cleanup(spinand);
1514 kfree(spinand->databuf);
1515 kfree(spinand->scratchbuf);
1519 static void spinand_cleanup(struct spinand_device *spinand)
1521 struct nand_device *nand = spinand_to_nand(spinand);
1523 nanddev_cleanup(nand);
1524 spinand_manufacturer_cleanup(spinand);
1525 kfree(spinand->databuf);
1526 kfree(spinand->scratchbuf);
1529 static int spinand_probe(struct spi_mem *mem)
1531 struct spinand_device *spinand;
1532 struct mtd_info *mtd;
1535 spinand = devm_kzalloc(&mem->spi->dev, sizeof(*spinand),
1540 spinand->spimem = mem;
1541 spi_mem_set_drvdata(mem, spinand);
1542 spinand_set_of_node(spinand, mem->spi->dev.of_node);
1543 mutex_init(&spinand->lock);
1544 mtd = spinand_to_mtd(spinand);
1545 mtd->dev.parent = &mem->spi->dev;
1547 ret = spinand_init(spinand);
1551 ret = mtd_device_register(mtd, NULL, 0);
1553 goto err_spinand_cleanup;
1557 err_spinand_cleanup:
1558 spinand_cleanup(spinand);
1563 static int spinand_remove(struct spi_mem *mem)
1565 struct spinand_device *spinand;
1566 struct mtd_info *mtd;
1569 spinand = spi_mem_get_drvdata(mem);
1570 mtd = spinand_to_mtd(spinand);
1572 ret = mtd_device_unregister(mtd);
1576 spinand_cleanup(spinand);
1581 static const struct spi_device_id spinand_ids[] = {
1582 { .name = "spi-nand" },
1585 MODULE_DEVICE_TABLE(spi, spinand_ids);
1588 static const struct of_device_id spinand_of_ids[] = {
1589 { .compatible = "spi-nand" },
1592 MODULE_DEVICE_TABLE(of, spinand_of_ids);
1595 static struct spi_mem_driver spinand_drv = {
1597 .id_table = spinand_ids,
1600 .of_match_table = of_match_ptr(spinand_of_ids),
1603 .probe = spinand_probe,
1604 .remove = spinand_remove,
1606 module_spi_mem_driver(spinand_drv);
1608 MODULE_DESCRIPTION("SPI NAND framework");
1610 MODULE_LICENSE("GPL v2");