1 From 082a89a78e29b15008284df90441747cb742f149 Mon Sep 17 00:00:00 2001
2 From: Ezequiel Garcia <ezequiel.garcia@imgtec.com>
3 Date: Tue, 2 Dec 2014 09:58:52 -0300
4 Subject: mtd: Introduce SPI NAND framework
6 Add a new framework, to support SPI NAND devices. The framework registers
7 a NAND chip and handles the generic SPI NAND protocol, calling device-specific
8 hooks for each SPI NAND command.
10 The following is the stack design, from userspace to hardware. This commit
11 adds the "SPI NAND core" layer.
29 (based on http://lists.infradead.org/pipermail/linux-mtd/2014-December/056763.html)
31 Signed-off-by: Ionela Voinescu <ionela.voinescu@imgtec.com>
32 Signed-off-by: Ezequiel Garcia <ezequiel.garcia@imgtec.com>
33 Signed-off-by: Ian Pozella <Ian.Pozella@imgtec.com>
35 drivers/mtd/Kconfig | 2 +
36 drivers/mtd/Makefile | 1 +
37 drivers/mtd/spi-nand/Kconfig | 7 +
38 drivers/mtd/spi-nand/Makefile | 1 +
39 drivers/mtd/spi-nand/spi-nand-base.c | 566 +++++++++++++++++++++++++++++++++++
40 include/linux/mtd/spi-nand.h | 54 ++++
41 6 files changed, 631 insertions(+)
42 create mode 100644 drivers/mtd/spi-nand/Kconfig
43 create mode 100644 drivers/mtd/spi-nand/Makefile
44 create mode 100644 drivers/mtd/spi-nand/spi-nand-base.c
45 create mode 100644 include/linux/mtd/spi-nand.h
47 --- a/drivers/mtd/Kconfig
48 +++ b/drivers/mtd/Kconfig
49 @@ -369,6 +369,8 @@ source "drivers/mtd/onenand/Kconfig"
51 source "drivers/mtd/lpddr/Kconfig"
53 +source "drivers/mtd/spi-nand/Kconfig"
55 source "drivers/mtd/spi-nor/Kconfig"
57 source "drivers/mtd/ubi/Kconfig"
58 --- a/drivers/mtd/Makefile
59 +++ b/drivers/mtd/Makefile
60 @@ -35,5 +35,6 @@ inftl-objs := inftlcore.o inftlmount.o
62 obj-y += chips/ lpddr/ maps/ devices/ nand/ onenand/ tests/
64 +obj-$(CONFIG_MTD_SPI_NAND) += spi-nand/
65 obj-$(CONFIG_MTD_SPI_NOR) += spi-nor/
66 obj-$(CONFIG_MTD_UBI) += ubi/
68 +++ b/drivers/mtd/spi-nand/Kconfig
70 +menuconfig MTD_SPI_NAND
71 + tristate "SPI NAND device support"
75 + This is the framework for the SPI NAND.
78 +++ b/drivers/mtd/spi-nand/Makefile
80 +obj-$(CONFIG_MTD_SPI_NAND) += spi-nand-base.o
82 +++ b/drivers/mtd/spi-nand/spi-nand-base.c
85 + * Copyright (C) 2014 Imagination Technologies Ltd.
87 + * This program is free software; you can redistribute it and/or modify
88 + * it under the terms of the GNU General Public License as published by
89 + * the Free Software Foundation; version 2 of the License.
92 + * 1. Erase and program operations need to call write_enable() first,
93 + * to clear the enable bit. This bit is cleared automatically after
94 + * the erase or program operation.
98 +#include <linux/device.h>
99 +#include <linux/err.h>
100 +#include <linux/errno.h>
101 +#include <linux/kernel.h>
102 +#include <linux/module.h>
103 +#include <linux/mtd/nand.h>
104 +#include <linux/mtd/mtd.h>
105 +#include <linux/mtd/partitions.h>
106 +#include <linux/mtd/spi-nand.h>
107 +#include <linux/of.h>
108 +#include <linux/slab.h>
110 +/* Registers common to all devices */
111 +#define SPI_NAND_LOCK_REG 0xa0
112 +#define SPI_NAND_PROT_UNLOCK_ALL 0x0
114 +#define SPI_NAND_FEATURE_REG 0xb0
115 +#define SPI_NAND_ECC_EN BIT(4)
116 +#define SPI_NAND_QUAD_EN BIT(0)
118 +#define SPI_NAND_STATUS_REG 0xc0
119 +#define SPI_NAND_STATUS_REG_ECC_MASK 0x3
120 +#define SPI_NAND_STATUS_REG_ECC_SHIFT 4
121 +#define SPI_NAND_STATUS_REG_PROG_FAIL BIT(3)
122 +#define SPI_NAND_STATUS_REG_ERASE_FAIL BIT(2)
123 +#define SPI_NAND_STATUS_REG_WREN BIT(1)
124 +#define SPI_NAND_STATUS_REG_BUSY BIT(0)
126 +#define SPI_NAND_CMD_BUF_LEN 8
128 +/* Rewind and fill the buffer with 0xff */
129 +static void spi_nand_clear_buffer(struct spi_nand *snand)
131 + snand->buf_start = 0;
132 + memset(snand->data_buf, 0xff, snand->buf_size);
135 +static int spi_nand_enable_ecc(struct spi_nand *snand)
139 + ret = snand->read_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
143 + snand->buf[0] |= SPI_NAND_ECC_EN;
144 + ret = snand->write_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
152 +static int spi_nand_disable_ecc(struct spi_nand *snand)
156 + ret = snand->read_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
160 + snand->buf[0] &= ~SPI_NAND_ECC_EN;
161 + ret = snand->write_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
164 + snand->ecc = false;
169 +static int spi_nand_enable_quad(struct spi_nand *snand)
173 + ret = snand->read_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
177 + snand->buf[0] |= SPI_NAND_QUAD_EN;
178 + ret = snand->write_reg(snand, SPI_NAND_FEATURE_REG, snand->buf);
185 + * Wait until the status register busy bit is cleared.
186 + * Returns a negatie errno on error or time out, and a non-negative status
187 + * value if the device is ready.
189 +static int spi_nand_wait_till_ready(struct spi_nand *snand)
191 + unsigned long deadline = jiffies + msecs_to_jiffies(100);
192 + bool timeout = false;
196 + * Perhaps we should set a different timeout for each
197 + * operation (reset, read, write, erase).
200 + if (time_after_eq(jiffies, deadline))
203 + ret = snand->read_reg(snand, SPI_NAND_STATUS_REG, snand->buf);
205 + dev_err(snand->dev, "error reading status register\n");
207 + } else if (!(snand->buf[0] & SPI_NAND_STATUS_REG_BUSY)) {
208 + return snand->buf[0];
214 + dev_err(snand->dev, "operation timed out\n");
219 +static int spi_nand_reset(struct spi_nand *snand)
223 + ret = snand->reset(snand);
225 + dev_err(snand->dev, "reset command failed\n");
230 + * The NAND core won't wait after a device reset, so we need
233 + ret = spi_nand_wait_till_ready(snand);
239 +static int spi_nand_status(struct spi_nand *snand)
243 + ret = snand->read_reg(snand, SPI_NAND_STATUS_REG, snand->buf);
245 + dev_err(snand->dev, "error reading status register\n");
248 + status = snand->buf[0];
250 + /* Convert this into standard NAND_STATUS values */
251 + if (status & SPI_NAND_STATUS_REG_BUSY)
254 + snand->buf[0] = NAND_STATUS_READY;
256 + if (status & SPI_NAND_STATUS_REG_PROG_FAIL ||
257 + status & SPI_NAND_STATUS_REG_ERASE_FAIL)
258 + snand->buf[0] |= NAND_STATUS_FAIL;
261 + * Since we unlock the entire device at initialization, unconditionally
262 + * set the WP bit to indicate it's not protected.
264 + snand->buf[0] |= NAND_STATUS_WP;
268 +static int spi_nand_erase(struct spi_nand *snand, int page_addr)
272 + ret = snand->write_enable(snand);
274 + dev_err(snand->dev, "write enable command failed\n");
278 + ret = snand->block_erase(snand, page_addr);
280 + dev_err(snand->dev, "block erase command failed\n");
287 +static int spi_nand_write(struct spi_nand *snand)
291 + /* Enable quad mode */
292 + ret = spi_nand_enable_quad(snand);
294 + dev_err(snand->dev, "error %d enabling quad mode\n", ret);
297 + /* Store the page to cache */
298 + ret = snand->store_cache(snand, 0, snand->buf_size, snand->data_buf);
300 + dev_err(snand->dev, "error %d storing page 0x%x to cache\n",
301 + ret, snand->page_addr);
305 + ret = snand->write_enable(snand);
307 + dev_err(snand->dev, "write enable command failed\n");
311 + /* Get page from the device cache into our internal buffer */
312 + ret = snand->write_page(snand, snand->page_addr);
314 + dev_err(snand->dev, "error %d reading page 0x%x from cache\n",
315 + ret, snand->page_addr);
322 +static int spi_nand_read_id(struct spi_nand *snand)
326 + ret = snand->read_id(snand, snand->data_buf);
328 + dev_err(snand->dev, "error %d reading ID\n", ret);
334 +static int spi_nand_read_page(struct spi_nand *snand, unsigned int page_addr,
335 + unsigned int page_offset, size_t length)
337 + unsigned int corrected = 0, ecc_error = 0;
340 + /* Load a page into the cache register */
341 + ret = snand->load_page(snand, page_addr);
343 + dev_err(snand->dev, "error %d loading page 0x%x to cache\n",
348 + ret = spi_nand_wait_till_ready(snand);
353 + snand->get_ecc_status(ret, &corrected, &ecc_error);
354 + snand->bitflips = corrected;
357 + * If there's an ECC error, print a message and notify MTD
358 + * about it. Then complete the read, to load actual data on
359 + * the buffer (instead of the status result).
362 + dev_err(snand->dev,
363 + "internal ECC error reading page 0x%x\n",
365 + snand->nand_chip.mtd.ecc_stats.failed++;
367 + snand->nand_chip.mtd.ecc_stats.corrected += corrected;
371 + /* Enable quad mode */
372 + ret = spi_nand_enable_quad(snand);
374 + dev_err(snand->dev, "error %d enabling quad mode\n", ret);
377 + /* Get page from the device cache into our internal buffer */
378 + ret = snand->read_cache(snand, page_offset, length, snand->data_buf);
380 + dev_err(snand->dev, "error %d reading page 0x%x from cache\n",
387 +static u8 spi_nand_read_byte(struct mtd_info *mtd)
389 + struct nand_chip *chip = mtd_to_nand(mtd);
390 + struct spi_nand *snand = nand_get_controller_data(chip);
393 + if (snand->buf_start < snand->buf_size)
394 + val = snand->data_buf[snand->buf_start++];
398 +static void spi_nand_write_buf(struct mtd_info *mtd, const u8 *buf, int len)
400 + struct nand_chip *chip = mtd_to_nand(mtd);
401 + struct spi_nand *snand = nand_get_controller_data(chip);
402 + size_t n = min_t(size_t, len, snand->buf_size - snand->buf_start);
404 + memcpy(snand->data_buf + snand->buf_start, buf, n);
405 + snand->buf_start += n;
408 +static void spi_nand_read_buf(struct mtd_info *mtd, u8 *buf, int len)
410 + struct nand_chip *chip = mtd_to_nand(mtd);
411 + struct spi_nand *snand = nand_get_controller_data(chip);
412 + size_t n = min_t(size_t, len, snand->buf_size - snand->buf_start);
414 + memcpy(buf, snand->data_buf + snand->buf_start, n);
415 + snand->buf_start += n;
418 +static int spi_nand_write_page_hwecc(struct mtd_info *mtd,
419 + struct nand_chip *chip, const uint8_t *buf, int oob_required,
422 + chip->write_buf(mtd, buf, mtd->writesize);
423 + chip->write_buf(mtd, chip->oob_poi, mtd->oobsize);
428 +static int spi_nand_read_page_hwecc(struct mtd_info *mtd,
429 + struct nand_chip *chip, uint8_t *buf, int oob_required,
432 + struct spi_nand *snand = nand_get_controller_data(chip);
434 + chip->read_buf(mtd, buf, mtd->writesize);
435 + chip->read_buf(mtd, chip->oob_poi, mtd->oobsize);
437 + return snand->bitflips;
440 +static int spi_nand_waitfunc(struct mtd_info *mtd, struct nand_chip *chip)
442 + struct spi_nand *snand = nand_get_controller_data(chip);
445 + ret = spi_nand_wait_till_ready(snand);
448 + return NAND_STATUS_FAIL;
449 + } else if (ret & SPI_NAND_STATUS_REG_PROG_FAIL) {
450 + dev_err(snand->dev, "page program failed\n");
451 + return NAND_STATUS_FAIL;
452 + } else if (ret & SPI_NAND_STATUS_REG_ERASE_FAIL) {
453 + dev_err(snand->dev, "block erase failed\n");
454 + return NAND_STATUS_FAIL;
457 + return NAND_STATUS_READY;
460 +static void spi_nand_cmdfunc(struct mtd_info *mtd, unsigned int command,
461 + int column, int page_addr)
463 + struct nand_chip *chip = mtd_to_nand(mtd);
464 + struct spi_nand *snand = nand_get_controller_data(chip);
467 + * In case there's any unsupported command, let's make sure
468 + * we don't keep garbage around in the buffer.
470 + if (command != NAND_CMD_PAGEPROG) {
471 + spi_nand_clear_buffer(snand);
472 + snand->page_addr = 0;
476 + case NAND_CMD_READ0:
477 + spi_nand_read_page(snand, page_addr, 0, mtd->writesize);
479 + case NAND_CMD_READOOB:
480 + spi_nand_disable_ecc(snand);
481 + spi_nand_read_page(snand, page_addr, mtd->writesize,
483 + spi_nand_enable_ecc(snand);
485 + case NAND_CMD_READID:
486 + spi_nand_read_id(snand);
488 + case NAND_CMD_ERASE1:
489 + spi_nand_erase(snand, page_addr);
491 + case NAND_CMD_ERASE2:
492 + /* There's nothing to do here, as the erase is one-step */
494 + case NAND_CMD_SEQIN:
495 + snand->buf_start = column;
496 + snand->page_addr = page_addr;
498 + case NAND_CMD_PAGEPROG:
499 + spi_nand_write(snand);
501 + case NAND_CMD_STATUS:
502 + spi_nand_status(snand);
504 + case NAND_CMD_RESET:
505 + spi_nand_reset(snand);
508 + dev_err(&mtd->dev, "unknown command 0x%x\n", command);
512 +static void spi_nand_select_chip(struct mtd_info *mtd, int chip)
514 + /* We need this to override the default */
517 +int spi_nand_check(struct spi_nand *snand)
521 + if (!snand->read_cache)
523 + if (!snand->load_page)
525 + if (!snand->store_cache)
527 + if (!snand->write_page)
529 + if (!snand->write_reg)
531 + if (!snand->read_reg)
533 + if (!snand->block_erase)
537 + if (!snand->write_enable)
539 + if (!snand->write_disable)
541 + if (!snand->get_ecc_status)
546 +int spi_nand_register(struct spi_nand *snand, struct nand_flash_dev *flash_ids)
548 + struct nand_chip *chip = &snand->nand_chip;
549 + struct mtd_info *mtd = nand_to_mtd(chip);
550 + struct device_node *np = snand->dev->of_node;
551 + const char __maybe_unused *of_mtd_name = NULL;
554 + /* Let's check all the hooks are in-place so we don't panic later */
555 + ret = spi_nand_check(snand);
559 + nand_set_controller_data(chip, snand);
560 + nand_set_flash_node(chip, np);
561 + chip->read_buf = spi_nand_read_buf;
562 + chip->write_buf = spi_nand_write_buf;
563 + chip->read_byte = spi_nand_read_byte;
564 + chip->cmdfunc = spi_nand_cmdfunc;
565 + chip->waitfunc = spi_nand_waitfunc;
566 + chip->select_chip = spi_nand_select_chip;
567 + chip->options |= NAND_NO_SUBPAGE_WRITE;
568 + chip->bits_per_cell = 1;
570 + mtd_set_ooblayout(mtd, snand->ooblayout);
571 + chip->ecc.read_page = spi_nand_read_page_hwecc;
572 + chip->ecc.write_page = spi_nand_write_page_hwecc;
573 + chip->ecc.mode = NAND_ECC_HW;
575 + if (of_property_read_bool(np, "nand-on-flash-bbt"))
576 + chip->bbt_options |= NAND_BBT_USE_FLASH | NAND_BBT_NO_OOB;
578 +#ifdef CONFIG_MTD_OF_PARTS
579 + of_property_read_string(np, "linux,mtd-name", &of_mtd_name);
582 + mtd->name = of_mtd_name;
584 + mtd->name = snand->name;
585 + mtd->owner = THIS_MODULE;
587 + /* Allocate buffer to be used to read/write the internal registers */
588 + snand->buf = kmalloc(SPI_NAND_CMD_BUF_LEN, GFP_KERNEL);
592 + /* This is enabled at device power up but we'd better make sure */
593 + ret = spi_nand_enable_ecc(snand);
597 + /* Preallocate buffer for flash identification (NAND_CMD_READID) */
598 + snand->buf_size = SPI_NAND_CMD_BUF_LEN;
599 + snand->data_buf = kmalloc(snand->buf_size, GFP_KERNEL);
601 + ret = nand_scan_ident(mtd, 1, flash_ids);
606 + * SPI NAND has on-die ECC, which means we can correct as much as
607 + * we are required to. This must be done after identification of
610 + chip->ecc.strength = chip->ecc_strength_ds;
611 + chip->ecc.size = chip->ecc_step_ds;
614 + * Unlock all the device before calling nand_scan_tail. This is needed
615 + * in case the in-flash bad block table needs to be created.
616 + * We could override __nand_unlock(), but since it's not currently used
617 + * by the NAND core we call this explicitly.
619 + snand->buf[0] = SPI_NAND_PROT_UNLOCK_ALL;
620 + ret = snand->write_reg(snand, SPI_NAND_LOCK_REG, snand->buf);
624 + /* Free the buffer and allocate a good one, to fit a page plus OOB */
625 + kfree(snand->data_buf);
627 + snand->buf_size = mtd->writesize + mtd->oobsize;
628 + snand->data_buf = kmalloc(snand->buf_size, GFP_KERNEL);
629 + if (!snand->data_buf)
632 + ret = nand_scan_tail(mtd);
636 + return mtd_device_register(mtd, NULL, 0);
638 +EXPORT_SYMBOL_GPL(spi_nand_register);
640 +void spi_nand_unregister(struct spi_nand *snand)
643 + kfree(snand->data_buf);
645 +EXPORT_SYMBOL_GPL(spi_nand_unregister);
647 +MODULE_AUTHOR("Ezequiel Garcia <ezequiel.garcia@imgtec.com>");
648 +MODULE_DESCRIPTION("Framework for SPI NAND");
649 +MODULE_LICENSE("GPL v2");
651 +++ b/include/linux/mtd/spi-nand.h
654 + * Copyright (C) 2014 Imagination Technologies Ltd.
656 + * This program is free software; you can redistribute it and/or modify
657 + * it under the terms of the GNU General Public License as published by
658 + * the Free Software Foundation; version 2 of the License.
661 +#ifndef __LINUX_MTD_SPI_NAND_H
662 +#define __LINUX_MTD_SPI_NAND_H
664 +#include <linux/mtd/mtd.h>
665 +#include <linux/mtd/nand.h>
668 + struct nand_chip nand_chip;
669 + struct device *dev;
672 + u8 *buf, *data_buf;
675 + unsigned int page_addr;
676 + unsigned int bitflips;
678 + struct mtd_ooblayout_ops *ooblayout;
680 + int (*reset)(struct spi_nand *snand);
681 + int (*read_id)(struct spi_nand *snand, u8 *buf);
683 + int (*write_disable)(struct spi_nand *snand);
684 + int (*write_enable)(struct spi_nand *snand);
686 + int (*read_reg)(struct spi_nand *snand, u8 opcode, u8 *buf);
687 + int (*write_reg)(struct spi_nand *snand, u8 opcode, u8 *buf);
688 + void (*get_ecc_status)(unsigned int status,
689 + unsigned int *corrected,
690 + unsigned int *ecc_errors);
692 + int (*store_cache)(struct spi_nand *snand, unsigned int page_offset,
693 + size_t length, u8 *write_buf);
694 + int (*write_page)(struct spi_nand *snand, unsigned int page_addr);
695 + int (*load_page)(struct spi_nand *snand, unsigned int page_addr);
696 + int (*read_cache)(struct spi_nand *snand, unsigned int page_offset,
697 + size_t length, u8 *read_buf);
698 + int (*block_erase)(struct spi_nand *snand, unsigned int page_addr);
703 +int spi_nand_register(struct spi_nand *snand, struct nand_flash_dev *flash_ids);
704 +void spi_nand_unregister(struct spi_nand *snand);