Re: [PATCH v3 4/6] mtd: rawnand: add NVIDIA Tegra NAND Flash controller driver

From: Randolph Maaßen
Date: Mon Jun 04 2018 - 13:24:19 EST


Am Freitag, den 01.06.2018, 00:16 +0200 schrieb Stefan Agner:
> Add support for the NAND flash controller found on NVIDIA
> Tegra 2 SoCs. This implementation does not make use of the
> command queue feature. Regular operations/data transfers are
> done in PIO mode. Page read/writes with hardware ECC make
> use of the DMA for data transfer.
>
> Signed-off-by: Lucas Stach <dev@xxxxxxxxxx>
> Signed-off-by: Stefan Agner <stefan@xxxxxxxx>
> ---
> ÂMAINTAINERSÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ|ÂÂÂÂ7 +
> Âdrivers/mtd/nand/raw/KconfigÂÂÂÂÂÂ|ÂÂÂÂ6 +
> Âdrivers/mtd/nand/raw/MakefileÂÂÂÂÂ|ÂÂÂÂ1 +
> Âdrivers/mtd/nand/raw/tegra_nand.c | 1143
> +++++++++++++++++++++++++++++
> Â4 files changed, 1157 insertions(+)
> Âcreate mode 100644 drivers/mtd/nand/raw/tegra_nand.c
>
> diff --git a/MAINTAINERS b/MAINTAINERS
> index 58b9861ccf99..c2e5571c85d4 100644
> --- a/MAINTAINERS
> +++ b/MAINTAINERS
> @@ -13844,6 +13844,13 @@ M: Laxman Dewangan <ldewangan@nvidia.
> com>
> ÂS: Supported
> ÂF: drivers/input/keyboard/tegra-kbc.c
> Â
> +TEGRA NAND DRIVER
> +M: Stefan Agner <stefan@xxxxxxxx>
> +M: Lucas Stach <dev@xxxxxxxxxx>
> +S: Maintained
> +F: Documentation/devicetree/bindings/mtd/nvidia-tegra20-
> nand.txt
> +F: drivers/mtd/nand/raw/tegra_nand.c
> +
> ÂTEGRA PWM DRIVER
> ÂM: Thierry Reding <thierry.reding@xxxxxxxxx>
> ÂS: Supported
> diff --git a/drivers/mtd/nand/raw/Kconfig
> b/drivers/mtd/nand/raw/Kconfig
> index 19a2b283fbbe..e9093f52371e 100644
> --- a/drivers/mtd/nand/raw/Kconfig
> +++ b/drivers/mtd/nand/raw/Kconfig
> @@ -534,4 +534,10 @@ config MTD_NAND_MTK
> Â ÂÂEnables support for NAND controller on MTK SoCs.
> Â ÂÂThis controller is found on mt27xx, mt81xx, mt65xx SoCs.
> Â
> +config MTD_NAND_TEGRA
> + tristate "Support for NAND controller on NVIDIA Tegra"
> + depends on ARCH_TEGRA || COMPILE_TEST
> + help
> + ÂÂEnables support for NAND flash controller on NVIDIA Tegra
> SoC.
> +
> Âendif # MTD_NAND
> diff --git a/drivers/mtd/nand/raw/Makefile
> b/drivers/mtd/nand/raw/Makefile
> index 165b7ef9e9a1..d5a5f9832b88 100644
> --- a/drivers/mtd/nand/raw/Makefile
> +++ b/drivers/mtd/nand/raw/Makefile
> @@ -56,6 +56,7 @@ obj-$(CONFIG_MTD_NAND_HISI504) ÂÂÂÂÂÂÂÂ+=
> hisi504_nand.o
> Âobj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand/
> Âobj-$(CONFIG_MTD_NAND_QCOM) += qcom_nandc.o
> Âobj-$(CONFIG_MTD_NAND_MTK) += mtk_ecc.o mtk_nand.o
> +obj-$(CONFIG_MTD_NAND_TEGRA) += tegra_nand.o
> Â
> Ânand-objs := nand_base.o nand_bbt.o nand_timings.o nand_ids.o
> Ânand-objs += nand_amd.o
> diff --git a/drivers/mtd/nand/raw/tegra_nand.c
> b/drivers/mtd/nand/raw/tegra_nand.c
> new file mode 100644
> index 000000000000..e9664f2938a3
> --- /dev/null
> +++ b/drivers/mtd/nand/raw/tegra_nand.c
> @@ -0,0 +1,1143 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/*
> + * Copyright (C) 2018 Stefan Agner <stefan@xxxxxxxx>
> + * Copyright (C) 2014-2015 Lucas Stach <dev@xxxxxxxxxx>
> + * Copyright (C) 2012 Avionic Design GmbH
> + */
> +
> +#include <linux/clk.h>
> +#include <linux/completion.h>
> +#include <linux/delay.h>
> +#include <linux/dma-mapping.h>
> +#include <linux/err.h>
> +#include <linux/gpio/consumer.h>
> +#include <linux/interrupt.h>
> +#include <linux/io.h>
> +#include <linux/module.h>
> +#include <linux/mtd/partitions.h>
> +#include <linux/mtd/rawnand.h>
> +#include <linux/of.h>
> +#include <linux/platform_device.h>
> +#include <linux/reset.h>
> +
> +#define CMD 0x00
> +#defineÂÂÂCMD_GO BIT(31)
> +#defineÂÂÂCMD_CLE BIT(30)
> +#defineÂÂÂCMD_ALE BIT(29)
> +#defineÂÂÂCMD_PIO BIT(28)
> +#defineÂÂÂCMD_TX BIT(27)
> +#defineÂÂÂCMD_RX BIT(26)
> +#defineÂÂÂCMD_SEC_CMD BIT(25)
> +#defineÂÂÂCMD_AFT_DAT BIT(24)
> +#defineÂÂÂCMD_TRANS_SIZE(x) (((x - 1) & 0xf)
> << 20)
> +#defineÂÂÂCMD_A_VALID BIT(19)
> +#defineÂÂÂCMD_B_VALID BIT(18)
> +#defineÂÂÂCMD_RD_STATUS_CHK BIT(17)
> +#defineÂÂÂCMD_RBSY_CHK BIT(16)
> +#defineÂÂÂCMD_CE(x) BIT((8 + ((x) &
> 0x7)))
> +#defineÂÂÂCMD_CLE_SIZE(x) (((x - 1) & 0x3) <<
> 4)
> +#defineÂÂÂCMD_ALE_SIZE(x) (((x - 1) & 0xf) <<
> 0)
> +
> +#define STATUS 0x04
> +
> +#define ISR 0x08
> +#defineÂÂÂISR_CORRFAIL_ERR BIT(24)
> +#defineÂÂÂISR_UND BIT(7)
> +#defineÂÂÂISR_OVR BIT(6)
> +#defineÂÂÂISR_CMD_DONE BIT(5)
> +#defineÂÂÂISR_ECC_ERR BIT(4)
> +
> +#define IER 0x0c
> +#defineÂÂÂIER_ERR_TRIG_VAL(x) (((x) & 0xf) <<
> 16)
> +#defineÂÂÂIER_UND BIT(7)
> +#defineÂÂÂIER_OVR BIT(6)
> +#defineÂÂÂIER_CMD_DONE BIT(5)
> +#defineÂÂÂIER_ECC_ERR BIT(4)
> +#defineÂÂÂIER_GIE BIT(0)
> +
> +#define CFG 0x10
> +#defineÂÂÂCFG_HW_ECC BIT(31)
> +#defineÂÂÂCFG_ECC_SEL BIT(30)
> +#defineÂÂÂCFG_ERR_COR BIT(29)
> +#defineÂÂÂCFG_PIPE_EN BIT(28)
> +#defineÂÂÂCFG_TVAL_4 (0 << 24)
> +#defineÂÂÂCFG_TVAL_6 (1 << 24)
> +#defineÂÂÂCFG_TVAL_8 (2 << 24)
> +#defineÂÂÂCFG_SKIP_SPARE BIT(23)
> +#defineÂÂÂCFG_BUS_WIDTH_16 BIT(21)
> +#defineÂÂÂCFG_COM_BSY BIT(20)
> +#defineÂÂÂCFG_PS_256 (0 << 16)
> +#defineÂÂÂCFG_PS_512 (1 << 16)
> +#defineÂÂÂCFG_PS_1024 (2 << 16)
> +#defineÂÂÂCFG_PS_2048 (3 << 16)
> +#defineÂÂÂCFG_PS_4096 (4 << 16)
> +#defineÂÂÂCFG_SKIP_SPARE_SIZE_4 (0 << 14)
> +#defineÂÂÂCFG_SKIP_SPARE_SIZE_8 (1 << 14)
> +#defineÂÂÂCFG_SKIP_SPARE_SIZE_12 (2 << 14)
> +#defineÂÂÂCFG_SKIP_SPARE_SIZE_16 (3 << 14)
> +#defineÂÂÂCFG_TAG_BYTE_SIZE(x) ((x) & 0xff)
> +
> +#define TIMING_1 0x14
> +#defineÂÂÂTIMING_TRP_RESP(x) (((x) & 0xf) <<
> 28)
> +#defineÂÂÂTIMING_TWB(x) (((x) & 0xf)
> << 24)
> +#defineÂÂÂTIMING_TCR_TAR_TRR(x) (((x) & 0xf)
> << 20)
> +#defineÂÂÂTIMING_TWHR(x) (((x) & 0xf) << 16)
> +#defineÂÂÂTIMING_TCS(x) (((x) & 0x3)
> << 14)
> +#defineÂÂÂTIMING_TWH(x) (((x) & 0x3)
> << 12)
> +#defineÂÂÂTIMING_TWP(x) (((x) & 0xf)
> <<ÂÂ8)
> +#defineÂÂÂTIMING_TRH(x) (((x) & 0x3)
> <<ÂÂ4)
> +#defineÂÂÂTIMING_TRP(x) (((x) & 0xf)
> <<ÂÂ0)
> +
> +#define RESP 0x18
> +
> +#define TIMING_2 0x1c
> +#defineÂÂÂTIMING_TADL(x) ((x) & 0xf)
> +
> +#define CMD_1 0x20
> +#define CMD_2 0x24
> +#define ADDR_1 0x28
> +#define ADDR_2 0x2c
> +
> +#define DMA_CTRL 0x30
> +#defineÂÂÂDMA_CTRL_GO BIT(31)
> +#defineÂÂÂDMA_CTRL_IN (0 << 30)
> +#defineÂÂÂDMA_CTRL_OUT BIT(30)
> +#defineÂÂÂDMA_CTRL_PERF_EN BIT(29)
> +#defineÂÂÂDMA_CTRL_IE_DONE BIT(28)
> +#defineÂÂÂDMA_CTRL_REUSE BIT(27)
> +#defineÂÂÂDMA_CTRL_BURST_1 (2 << 24)
> +#defineÂÂÂDMA_CTRL_BURST_4 (3 << 24)
> +#defineÂÂÂDMA_CTRL_BURST_8 (4 << 24)
> +#defineÂÂÂDMA_CTRL_BURST_16 (5 << 24)
> +#defineÂÂÂDMA_CTRL_IS_DONE BIT(20)
> +#defineÂÂÂDMA_CTRL_EN_A BIT(2)
> +#defineÂÂÂDMA_CTRL_EN_B BIT(1)
> +
> +#define DMA_CFG_A 0x34
> +#define DMA_CFG_B 0x38
> +
> +#define FIFO_CTRL 0x3c
> +#defineÂÂÂFIFO_CTRL_CLR_ALL BIT(3)
> +
> +#define DATA_PTR 0x40
> +#define TAG_PTR 0x44
> +#define ECC_PTR 0x48
> +
> +#define DEC_STATUS 0x4c
> +#defineÂÂÂDEC_STATUS_A_ECC_FAIL BIT(1)
> +#defineÂÂÂDEC_STATUS_ERR_COUNT_MASK 0x00ff0000
> +#defineÂÂÂDEC_STATUS_ERR_COUNT_SHIFT 16
> +
> +#define HWSTATUS_CMD 0x50
> +#define HWSTATUS_MASK 0x54
> +#defineÂÂÂHWSTATUS_RDSTATUS_MASK(x) (((x) & 0xff) <<
> 24)
> +#defineÂÂÂHWSTATUS_RDSTATUS_VALUE(x) (((x) & 0xff) <<
> 16)
> +#defineÂÂÂHWSTATUS_RBSY_MASK(x) (((x) & 0xff)
> << 8)
> +#defineÂÂÂHWSTATUS_RBSY_VALUE(x) (((x) & 0xff) << 0)
> +
> +#define BCH_CONFIG 0xcc
> +#defineÂÂÂBCH_ENABLE BIT(0)
> +#defineÂÂÂBCH_TVAL_4 (0 << 4)
> +#defineÂÂÂBCH_TVAL_8 (1 << 4)
> +#defineÂÂÂBCH_TVAL_14 (2 << 4)
> +#defineÂÂÂBCH_TVAL_16 (3 << 4)
> +
> +#define DEC_STAT_RESULT 0xd0
> +#define DEC_STAT_BUF 0xd4
> +#defineÂÂÂDEC_STAT_BUF_FAIL_SEC_FLAG_MASK 0xff000000
> +#defineÂÂÂDEC_STAT_BUF_FAIL_SEC_FLAG_SHIFT 24
> +#defineÂÂÂDEC_STAT_BUF_CORR_SEC_FLAG_MASK 0x00ff0000
> +#defineÂÂÂDEC_STAT_BUF_CORR_SEC_FLAG_SHIFT 16
> +#defineÂÂÂDEC_STAT_BUF_MAX_CORR_CNT_MASK 0x00001f00
> +#defineÂÂÂDEC_STAT_BUF_MAX_CORR_CNT_SHIFT 8
> +
> +#define OFFSET(val, off) ((val) < (off) ? 0 : (val) -
> (off))
> +
> +#define SKIP_SPARE_BYTES 4
> +#define BITS_PER_STEP_RS 18
> +#define BITS_PER_STEP_BCH 13
> +
> +struct tegra_nand_controller {
> + struct nand_hw_control controller;
> + void __iomem *regs;
> + struct clk *clk;
> + struct device *dev;
> + struct completion command_complete;
> + struct completion dma_complete;
> + bool last_read_error;
> + int cur_chip;
> + struct nand_chip *chip;
> +};
> +
> +struct tegra_nand_chip {
> + struct nand_chip chip;
> + struct gpio_desc *wp_gpio;
> + struct mtd_oob_region tag;
> +};
> +
> +static inline struct tegra_nand_controller *to_tegra_ctrl(
> + struct
> nand_hw_control *hw_ctrl)
> +{
> + return container_of(hw_ctrl, struct tegra_nand_controller,
> controller);
> +}
> +
> +static inline struct tegra_nand_chip *to_tegra_chip(struct nand_chip
> *chip)
> +{
> + return container_of(chip, struct tegra_nand_chip, chip);
> +}
> +
> +static int tegra_nand_ooblayout_rs_ecc(struct mtd_info *mtd, int
> section,
> + ÂÂÂÂÂÂÂstruct mtd_oob_region
> *oobregion)
> +{
> + struct nand_chip *chip = mtd_to_nand(mtd);
> + int bytes_per_step = DIV_ROUND_UP(BITS_PER_STEP_RS * chip-
> >ecc.strength,
> + ÂÂBITS_PER_BYTE);
> +
> + if (section > 0)
> + return -ERANGE;
> +
> + oobregion->offset = SKIP_SPARE_BYTES;
> + oobregion->length = round_up(bytes_per_step * chip-
> >ecc.steps, 4);
> +
> + return 0;
> +}
> +
> +static int tegra_nand_ooblayout_rs_free(struct mtd_info *mtd, int
> section,
> + struct mtd_oob_region
> *oobregion)
> +{
> + struct nand_chip *chip = mtd_to_nand(mtd);
> + int bytes_per_step = DIV_ROUND_UP(BITS_PER_STEP_RS * chip-
> >ecc.strength,
> + ÂÂBITS_PER_BYTE);
> +
> + if (section > 0)
> + return -ERANGE;
> +
> + oobregion->offset = SKIP_SPARE_BYTES +
> + ÂÂÂÂround_up(bytes_per_step * chip-
> >ecc.steps, 4);
> + oobregion->length = mtd->oobsize - oobregion->offset;
> +
> + return 0;
> +}
> +
> +static const struct mtd_ooblayout_ops tegra_nand_oob_rs_ops = {
> + .ecc = tegra_nand_ooblayout_rs_ecc,
> + .free = tegra_nand_ooblayout_rs_free,
> +};
> +
> +static int tegra_nand_ooblayout_bch_ecc(struct mtd_info *mtd, int
> section,
> + ÂÂÂÂÂÂÂstruct mtd_oob_region
> *oobregion)
> +{
> + struct nand_chip *chip = mtd_to_nand(mtd);
> + int bytes_per_step = DIV_ROUND_UP(BITS_PER_STEP_BCH * chip-
> >ecc.strength,
> + ÂÂBITS_PER_BYTE);
> +
> + if (section > 0)
> + return -ERANGE;
> +
> + oobregion->offset = SKIP_SPARE_BYTES;
> + oobregion->length = round_up(bytes_per_step * chip-
> >ecc.steps, 4);
> +
> + return 0;
> +}
> +
> +static int tegra_nand_ooblayout_bch_free(struct mtd_info *mtd, int
> section,
> + struct mtd_oob_region
> *oobregion)
> +{
> + struct nand_chip *chip = mtd_to_nand(mtd);
> + int bytes_per_step = DIV_ROUND_UP(BITS_PER_STEP_BCH * chip-
> >ecc.strength,
> + ÂÂBITS_PER_BYTE);
> +
> + if (section > 0)
> + return -ERANGE;
> +
> + oobregion->offset = SKIP_SPARE_BYTES +
> + ÂÂÂÂround_up(bytes_per_step * chip-
> >ecc.steps, 4);
> + oobregion->length = mtd->oobsize - oobregion->offset;
> +
> + return 0;
> +}
> +
> +/*
> + * Layout with tag bytes is
> + *
> + * ---------------------------------------------------------------
> -----------
> + * | main areaÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂÂ| skip bytes | tag bytes |
> parity | .. |
> + * ---------------------------------------------------------------
> -----------
> + *
> + * If not tag bytes are written, parity moves right after skip
> bytes!
> + */
> +static const struct mtd_ooblayout_ops tegra_nand_oob_bch_ops = {
> + .ecc = tegra_nand_ooblayout_bch_ecc,
> + .free = tegra_nand_ooblayout_bch_free,
> +};
> +
> +static irqreturn_t tegra_nand_irq(int irq, void *data)
> +{
> + struct tegra_nand_controller *ctrl = data;
> + u32 isr, dma;
> +
> + isr = readl_relaxed(ctrl->regs + ISR);
> + dma = readl_relaxed(ctrl->regs + DMA_CTRL);
> + dev_dbg(ctrl->dev, "isr %08x\n", isr);
> +
> + if (!isr && !(dma & DMA_CTRL_IS_DONE))
> + return IRQ_NONE;
> +
> + /*
> + Â* The bit name is somewhat missleading: This is also set
> when
> + Â* HW ECC was successful. The data sheet states:
> + Â* Correctable OR Un-correctable errors occurred in the DMA
> transfer...
> + Â*/
> + if (isr & ISR_CORRFAIL_ERR)
> + ctrl->last_read_error = true;
> +
> + if (isr & ISR_CMD_DONE)
> + complete(&ctrl->command_complete);
> +
> + if (isr & ISR_UND)
> + dev_err(ctrl->dev, "FIFO underrun\n");
> +
> + if (isr & ISR_OVR)
> + dev_err(ctrl->dev, "FIFO overrun\n");
> +
> + /* handle DMA interrupts */
> + if (dma & DMA_CTRL_IS_DONE) {
> + writel_relaxed(dma, ctrl->regs + DMA_CTRL);
> + complete(&ctrl->dma_complete);
> + }
> +
> + /* clear interrupts */
> + writel_relaxed(isr, ctrl->regs + ISR);
> +
> + return IRQ_HANDLED;
> +}
> +
> +static const char * const tegra_nand_reg_names[] = {
> + "COMMAND",
> + "STATUS",
> + "ISR",
> + "IER",
> + "CONFIG",
> + "TIMING",
> + NULL,
> + "TIMING2",
> + "CMD_REG1",
> + "CMD_REG2",
> + "ADDR_REG1",
> + "ADDR_REG2",
> + "DMA_MST_CTRL",
> + "DMA_CFG_A",
> + "DMA_CFG_B",
> + "FIFO_CTRL",
> +};
> +
> +static void tegra_nand_dump_reg(struct tegra_nand_controller *ctrl)
> +{
> + u32 reg;
> + int i;
> +
> + dev_err(ctrl->dev, "Tegra NAND controller register dump\n");
> + for (i = 0; i < ARRAY_SIZE(tegra_nand_reg_names); i++) {
> + const char *reg_name = tegra_nand_reg_names[i];
> +
> + if (!reg_name)
> + continue;
> +
> + reg = readl_relaxed(ctrl->regs + (i * 4));
> + dev_err(ctrl->dev, "%s: 0x%08x\n", reg_name, reg);
> + }
> +}
> +
> +static int tegra_nand_cmd(struct nand_chip *chip,
> + Âconst struct nand_subop *subop)
> +{
> + const struct nand_op_instr *instr;
> + const struct nand_op_instr *instr_data_in = NULL;
> + struct tegra_nand_controller *ctrl = to_tegra_ctrl(chip-
> >controller);
> + unsigned int op_id, size = 0, offset = 0;
> + bool first_cmd = true;
> + u32 reg, cmd = 0;
> + int ret;
> +
> + for (op_id = 0; op_id < subop->ninstrs; op_id++) {
> + unsigned int naddrs, i;
> + const u8 *addrs;
> + u32 addr1 = 0, addr2 = 0;
> +
> + instr = &subop->instrs[op_id];
> +
> + switch (instr->type) {
> + case NAND_OP_CMD_INSTR:
> + if (first_cmd) {
> + cmd |= CMD_CLE;
> + writel_relaxed(instr-
> >ctx.cmd.opcode,
> + ÂÂÂÂÂÂÂctrl->regs + CMD_1);
> + } else {
> + cmd |= CMD_SEC_CMD;
> + writel_relaxed(instr-
> >ctx.cmd.opcode,
> + ÂÂÂÂÂÂÂctrl->regs + CMD_2);
> + }
> + first_cmd = false;
> + break;
> + case NAND_OP_ADDR_INSTR:
> + offset =
> nand_subop_get_addr_start_off(subop, op_id);
> + naddrs = nand_subop_get_num_addr_cyc(subop,
> op_id);
> + addrs = &instr->ctx.addr.addrs[offset];
> +
> + cmd |= CMD_ALE | CMD_ALE_SIZE(naddrs);
> + for (i = 0; i < min_t(unsigned int, 4,
> naddrs); i++)
> + addr1 |= *addrs++ << (BITS_PER_BYTE
> * i);
> + naddrs -= i;
> + for (i = 0; i < min_t(unsigned int, 4,
> naddrs); i++)
> + addr2 |= *addrs++ << (BITS_PER_BYTE
> * i);
> + writel_relaxed(addr1, ctrl->regs + ADDR_1);
> + writel_relaxed(addr2, ctrl->regs + ADDR_2);
> + break;
> +
> + case NAND_OP_DATA_IN_INSTR:
> + size = nand_subop_get_data_len(subop,
> op_id);
> + offset =
> nand_subop_get_data_start_off(subop, op_id);
> +
> + cmd |= CMD_TRANS_SIZE(size) | CMD_PIO |
> CMD_RX |
> + CMD_A_VALID;
> +
> + instr_data_in = instr;
> + break;
> +
> + case NAND_OP_DATA_OUT_INSTR:
> + size = nand_subop_get_data_len(subop,
> op_id);
> + offset =
> nand_subop_get_data_start_off(subop, op_id);
> +
> + cmd |= CMD_TRANS_SIZE(size) | CMD_PIO |
> CMD_TX |
> + CMD_A_VALID;
> +
> + memcpy(&reg, instr->ctx.data.buf.out +
> offset, size);
> + writel_relaxed(reg, ctrl->regs + RESP);
> +
> + break;
> + case NAND_OP_WAITRDY_INSTR:
> + cmd |= CMD_RBSY_CHK;
> + break;
> +
> + }
> + }
> +
> + cmd |= CMD_GO | CMD_CE(ctrl->cur_chip);
> + writel_relaxed(cmd, ctrl->regs + CMD);
> + ret = wait_for_completion_timeout(&ctrl->command_complete,
> + ÂÂmsecs_to_jiffies(500));
> + if (!ret) {
> + dev_err(ctrl->dev, "CMD timeout\n");
> + tegra_nand_dump_reg(ctrl);
> + return -ETIMEDOUT;
> + }
> +
> + if (instr_data_in) {
> + reg = readl_relaxed(ctrl->regs + RESP);
> + memcpy(instr_data_in->ctx.data.buf.in + offset,
> &reg, size);
> + }
> +
> + return 0;
> +}
> +
> +static const struct nand_op_parser tegra_nand_op_parser =
> NAND_OP_PARSER(
> + NAND_OP_PARSER_PATTERN(tegra_nand_cmd,
> + NAND_OP_PARSER_PAT_CMD_ELEM(true),
> + NAND_OP_PARSER_PAT_ADDR_ELEM(true, 8),
> + NAND_OP_PARSER_PAT_CMD_ELEM(true),
> + NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
> + NAND_OP_PARSER_PATTERN(tegra_nand_cmd,
> + NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, 4)),
> + NAND_OP_PARSER_PATTERN(tegra_nand_cmd,
> + NAND_OP_PARSER_PAT_CMD_ELEM(true),
> + NAND_OP_PARSER_PAT_ADDR_ELEM(true, 8),
> + NAND_OP_PARSER_PAT_CMD_ELEM(true),
> + NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
> + NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, 4)),
> + );
> +
> +static int tegra_nand_exec_op(struct nand_chip *chip,
> + ÂÂÂÂÂconst struct nand_operation *op,
> + ÂÂÂÂÂbool check_only)
> +{
> + return nand_op_parser_exec_op(chip, &tegra_nand_op_parser,
> op,
> + ÂÂÂÂÂÂcheck_only);
> +}
> +static void tegra_nand_select_chip(struct mtd_info *mtd, int
> chip_nr)
> +{
> + struct nand_chip *chip = mtd_to_nand(mtd);
> + struct tegra_nand_controller *ctrl = to_tegra_ctrl(chip-
> >controller);
> +
> + ctrl->cur_chip = chip_nr;
> +}
> +
> +static void tegra_nand_hw_ecc(struct tegra_nand_controller *ctrl,
> + ÂÂÂÂÂÂstruct nand_chip *chip, bool enable)
> +{
> + u32 reg;
> +
> + switch (chip->ecc.algo) {
> + case NAND_ECC_RS:
> + reg = readl_relaxed(ctrl->regs + CFG);
> + if (enable)
> + reg |= CFG_HW_ECC | CFG_ERR_COR;
> + else
> + reg &= ~(CFG_HW_ECC | CFG_ERR_COR);
> + writel_relaxed(reg, ctrl->regs + CFG);
> + break;
> + case NAND_ECC_BCH:
> + reg = readl_relaxed(ctrl->regs + BCH_CONFIG);
> + if (enable)
> + reg |= BCH_ENABLE;
> + else
> + reg &= ~BCH_ENABLE;
> + writel_relaxed(reg, ctrl->regs + BCH_CONFIG);
> + break;
> + default:
> + dev_err(ctrl->dev, "Unsupported hardware ECC
> algorithm\n");
> + break;
> + }
> +}
> +
> +static int tegra_nand_page_xfer(struct mtd_info *mtd, struct
> nand_chip *chip,
> + void *buf, int oob_required, int
> page,
> + bool read)
> +{
> + struct tegra_nand_controller *ctrl = to_tegra_ctrl(chip-
> >controller);
> + struct tegra_nand_chip *nand = to_tegra_chip(chip);
> + enum dma_data_direction dir = read ? DMA_FROM_DEVICE :
> DMA_TO_DEVICE;
> + dma_addr_t dma_addr;
> + u32 cmd, dma_ctrl;
> + int ret, dma_len;
> +
> + if (read) {
> + writel_relaxed(NAND_CMD_READ0, ctrl->regs + CMD_1);
> + writel_relaxed(NAND_CMD_READSTART, ctrl->regs +
> CMD_2);
> + } else {
> + writel_relaxed(NAND_CMD_SEQIN, ctrl->regs + CMD_1);
> + writel_relaxed(NAND_CMD_PAGEPROG, ctrl->regs +
> CMD_2);
> + }
> + cmd = CMD_CLE | CMD_SEC_CMD;
> +
> + /* Lower 16-bits are column, always 0 */
> + writel_relaxed(page << 16, ctrl->regs + ADDR_1);
> +
> + if (chip->options & NAND_ROW_ADDR_3) {
> + writel_relaxed(page >> 16, ctrl->regs + ADDR_2);
> + cmd |= CMD_ALE | CMD_ALE_SIZE(5);
> + } else {
> + cmd |= CMD_ALE | CMD_ALE_SIZE(4);
> + }
> +
> + dma_len = mtd->writesize + (oob_required ? mtd->oobsize :
> 0);
> + dma_addr = dma_map_single(ctrl->dev, buf, dma_len, dir);
> + ret = dma_mapping_error(ctrl->dev, dma_addr);
> + if (ret) {
> + dev_err(ctrl->dev, "dma mapping error\n");
> + return -EINVAL;
> + }
> +
> + writel_relaxed(mtd->writesize - 1, ctrl->regs + DMA_CFG_A);
> + writel_relaxed(dma_addr, ctrl->regs + DATA_PTR);
> +
> + if (oob_required) {
> + dma_addr_t dma_addr_tag = dma_addr + mtd->writesize;
> +
> + writel_relaxed(nand->tag.length - 1, ctrl->regs +
> DMA_CFG_B);
> + writel_relaxed(dma_addr_tag + nand->tag.offset,
> + ÂÂÂÂÂÂÂctrl->regs + TAG_PTR);
> + } else {
> + writel_relaxed(0, ctrl->regs + DMA_CFG_B);
> + writel_relaxed(0, ctrl->regs + TAG_PTR);
> + }
> +
> + dma_ctrl = DMA_CTRL_GO | DMA_CTRL_PERF_EN |
> + ÂÂÂDMA_CTRL_IE_DONE | DMA_CTRL_IS_DONE |
> + ÂÂÂDMA_CTRL_BURST_16 | DMA_CTRL_EN_A;
> + if (oob_required)
> + dma_ctrl |= DMA_CTRL_EN_B;
> + if (read)
> + dma_ctrl |= DMA_CTRL_IN | DMA_CTRL_REUSE;
> + else
> + dma_ctrl |= DMA_CTRL_OUT;
> +
> + writel_relaxed(dma_ctrl, ctrl->regs + DMA_CTRL);
> +
> + cmd |= CMD_GO | CMD_RBSY_CHK | CMD_TRANS_SIZE(9) |
> + ÂÂÂÂÂÂÂCMD_CE(ctrl->cur_chip) | CMD_A_VALID;
> + if (oob_required)
> + cmd |= CMD_B_VALID;
> + if (read)
> + cmd |= CMD_RX;
> + else
> + cmd |= CMD_TX | CMD_AFT_DAT;
> +
> + writel_relaxed(cmd, ctrl->regs + CMD);
> +
> + ret = wait_for_completion_timeout(&ctrl->command_complete,
> + ÂÂmsecs_to_jiffies(500));
> + if (!ret) {
> + dev_err(ctrl->dev, "CMD timeout\n");
> + tegra_nand_dump_reg(ctrl);
> + ret = -ETIMEDOUT;
> + goto err_unmap_dma;
> + }
> +
> + ret = wait_for_completion_timeout(&ctrl->dma_complete,
> + ÂÂmsecs_to_jiffies(500));
> + if (!ret) {
> + dev_err(ctrl->dev, "DMA timeout\n");
> + tegra_nand_dump_reg(ctrl);
> + ret = -ETIMEDOUT;
> + goto err_unmap_dma;
> + }
> + ret = 0;
> +
> +err_unmap_dma:
> + dma_unmap_single(ctrl->dev, dma_addr, dma_len, dir);
> +
> + return ret;
> +}
> +
> +static int tegra_nand_read_page_hwecc(struct mtd_info *mtd,
> + ÂÂÂÂÂÂstruct nand_chip *chip,
> + ÂÂÂÂÂÂuint8_t *buf, int
> oob_required, int page)
> +{
> + struct tegra_nand_controller *ctrl = to_tegra_ctrl(chip-
> >controller);
> + u32 dec_stat, max_corr_cnt;
> + unsigned long fail_sec_flag;
> + int ret;
> +
> + tegra_nand_hw_ecc(ctrl, chip, true);
> + ret = tegra_nand_page_xfer(mtd, chip, buf, oob_required,
> page, true);
> + tegra_nand_hw_ecc(ctrl, chip, false);
> + if (ret)
> + return ret;
> +
> + /* No correctable or un-correctable errors, page must have 0
> bitflips */
> + if (!ctrl->last_read_error)
> + return 0;
> +
> + /*
> + Â* Correctable or un-correctable errors occurred. Use
> DEC_STAT_BUF
> + Â* which contains information for all ECC selections.
> + Â*
> + Â* Note that since we do not use Command Queues DEC_RESULT
> does not
> + Â* state the number of pages we can read from the
> DEC_STAT_BUF. But
> + Â* since CORRFAIL_ERR did occur during page read we do have
> a valid
> + Â* result in DEC_STAT_BUF.
> + Â*/
> + ctrl->last_read_error = false;
> + dec_stat = readl_relaxed(ctrl->regs + DEC_STAT_BUF);
> +
> + fail_sec_flag = (dec_stat & DEC_STAT_BUF_FAIL_SEC_FLAG_MASK)
> >>
> + DEC_STAT_BUF_FAIL_SEC_FLAG_SHIFT;
> +
> + max_corr_cnt = (dec_stat & DEC_STAT_BUF_MAX_CORR_CNT_MASK)
> >>
> + ÂÂÂÂÂÂÂDEC_STAT_BUF_MAX_CORR_CNT_SHIFT;
> +
> + if (fail_sec_flag) {
> + int bit, max_bitflips = 0;
> +
> + /*
> + Â* Check if all sectors in a page failed. If only
> some failed
> + Â* its definitly not an erased page and we can
> return error
> + Â* stats right away.
> + Â*
> + Â* E.g. controller might return fail_sec_flag with
> 0x4, which
> + Â* would mean only the third sector failed to
> correct.
> + Â*/
> + if (fail_sec_flag ^ GENMASK(chip->ecc.steps - 1, 0))
> {
> + mtd->ecc_stats.failed +=
> hweight8(fail_sec_flag);
> + return max_corr_cnt;
> + }
> +
> + /*
> + Â* All sectors failed to correct, but the ECC isn't
> smart
> + Â* enough to figure out if a page is really
> completely erased.
> + Â* We check the read data here to figure out if it's
> a
> + Â* legitimate ECC error or only an erased page.
> + Â*/
> + for_each_set_bit(bit, &fail_sec_flag, chip-
> >ecc.steps) {
> + u8 *data = buf + (chip->ecc.size * bit);
> +
> + ret = nand_check_erased_ecc_chunk(data,
> chip->ecc.size,
> + ÂÂNULL, 0,
> + ÂÂNULL, 0,
> + ÂÂchip-
> >ecc.strength);
> + if (ret < 0)
> + mtd->ecc_stats.failed++;
> + else
> + max_bitflips = max(ret,
> max_bitflips);
> + }
> +
> + return max_t(unsigned int, max_corr_cnt,
> max_bitflips);
> + } else {
> + int corr_sec_flag;
> +
> + corr_sec_flag = (dec_stat &
> DEC_STAT_BUF_CORR_SEC_FLAG_MASK) >>
> + DEC_STAT_BUF_CORR_SEC_FLAG_SHIFT;
> +
> + /*
> + Â* The value returned in the register is the maximum
> of
> + Â* bitflips encountered in any of the ECC regions.
> As there is
> + Â* no way to get the number of bitflips in a
> specific regions
> + Â* we are not able to deliver correct stats but
> instead
> + Â* overestimate the number of corrected bitflips by
> assuming
> + Â* that all regions where errors have been corrected
> + Â* encountered the maximum number of bitflips.
> + Â*/
> + mtd->ecc_stats.corrected += max_corr_cnt *
> hweight8(corr_sec_flag);
> +
> + return max_corr_cnt;
> + }
> +
> +}
> +
> +static int tegra_nand_write_page_hwecc(struct mtd_info *mtd,
> + ÂÂÂÂÂÂÂstruct nand_chip *chip,
> + ÂÂÂÂÂÂÂconst uint8_t *buf, int
> oob_required,
> + ÂÂÂÂÂÂÂint page)
> +{
> + struct tegra_nand_controller *ctrl = to_tegra_ctrl(chip-
> >controller);
> + int ret;
> +
> + tegra_nand_hw_ecc(ctrl, chip, true);
> + ret = tegra_nand_page_xfer(mtd, chip, (void *)buf,
> oob_required, page,
> + ÂÂÂfalse);
> + tegra_nand_hw_ecc(ctrl, chip, false);
> +
> + return ret;
> +}
> +
> +static void tegra_nand_setup_timing(struct tegra_nand_controller
> *ctrl,
> + ÂÂÂÂconst struct nand_sdr_timings
> *timings)
> +{
> + /*
> + Â* The period (and all other timings in this function) is in
> ps,
> + Â* so need to take care here to avoid integer overflows.
> + Â*/
> + unsigned int rate = clk_get_rate(ctrl->clk) / 1000000;
> + unsigned int period = DIV_ROUND_UP(1000000, rate);
> + u32 val, reg = 0;
> +
> + val = DIV_ROUND_UP(max3(timings->tAR_min, timings->tRR_min,
> + timings->tRC_min), period);
> + reg |= TIMING_TCR_TAR_TRR(OFFSET(val, 3));
> +
> + val = DIV_ROUND_UP(max(max(timings->tCS_min, timings-
> >tCH_min),
> + ÂÂÂÂÂÂÂmax(timings->tALS_min, timings-
> >tALH_min)),
> + ÂÂÂperiod);
> + reg |= TIMING_TCS(OFFSET(val, 2));
> +
> + val = DIV_ROUND_UP(max(timings->tRP_min, timings->tREA_max)
> + 6000,
> + ÂÂÂperiod);
> + reg |= TIMING_TRP(OFFSET(val, 1)) |
> TIMING_TRP_RESP(OFFSET(val, 1));
> +
> + reg |= TIMING_TWB(OFFSET(DIV_ROUND_UP(timings->tWB_max,
> period), 1));
> + reg |= TIMING_TWHR(OFFSET(DIV_ROUND_UP(timings->tWHR_min,
> period), 1));
> + reg |= TIMING_TWH(OFFSET(DIV_ROUND_UP(timings->tWH_min,
> period), 1));
> + reg |= TIMING_TWP(OFFSET(DIV_ROUND_UP(timings->tWP_min,
> period), 1));
> + reg |= TIMING_TRH(OFFSET(DIV_ROUND_UP(timings->tREH_min,
> period), 1));
> +
> + writel_relaxed(reg, ctrl->regs + TIMING_1);
> +
> + val = DIV_ROUND_UP(timings->tADL_min, period);
> + reg = TIMING_TADL(OFFSET(val, 3));
> +
> + writel_relaxed(reg, ctrl->regs + TIMING_2);
> +}
> +
> +static int tegra_nand_setup_data_interface(struct mtd_info *mtd, int
> csline,
> + ÂÂÂconst struct
> nand_data_interface *conf)
> +{
> + struct nand_chip *chip = mtd_to_nand(mtd);
> + struct tegra_nand_controller *ctrl = to_tegra_ctrl(chip-
> >controller);
> + const struct nand_sdr_timings *timings;
> +
> + timings = nand_get_sdr_timings(conf);
> + if (IS_ERR(timings))
> + return PTR_ERR(timings);
> +
> + if (csline == NAND_DATA_IFACE_CHECK_ONLY)
> + return 0;
> +
> + tegra_nand_setup_timing(ctrl, timings);
> +
> + return 0;
> +}
> +
> +
> +const int rs_strength_bootable[] = { 4 };
> +const int rs_strength[] = { 4, 6, 8 };
> +const int bch_strength_bootable[] = { 8, 16 };
> +const int bch_strength[] = { 4, 8, 14, 16 };
> +
> +static int tegra_nand_get_strength(struct nand_chip *chip, const int
> *strength,
> + ÂÂÂint strength_len, int oobsize)
> +{
> + bool maximize = chip->ecc.options & NAND_ECC_MAXIMIZE;
> + int i;
> +
> + /*
> + Â* Loop through available strengths. Backwards in case we
> try to
> + Â* maximize the BCH strength.
> + Â*/
> + for (i = 0; i < strength_len; i++) {
> + int strength_sel, bytes_per_step, bytes_per_page;
> +
> + if (maximize) {
> + strength_sel = strength[strength_len - i -
> 1];
> + } else {
> + strength_sel = strength[i];
> +
> + if (strength_sel < chip->ecc_strength_ds)
> + continue;
> + }
> +
> + bytes_per_step = DIV_ROUND_UP(BITS_PER_STEP_BCH *
> strength_sel,
> + ÂÂÂÂÂÂBITS_PER_BYTE);
> + bytes_per_page = round_up(bytes_per_step * chip-
> >ecc.steps, 4);
> +
> + /* Check whether strength fits OOB */
> + if (bytes_per_page < (oobsize - SKIP_SPARE_BYTES))
> + return strength_sel;
> + }
> +
> + return -EINVAL;
> +}
> +
> +static int tegra_nand_select_strength(struct nand_chip *chip, int
> oobsize)
> +{
> + const int *strength;
> + int strength_len;
> +
> + switch (chip->ecc.algo) {
> + case NAND_ECC_RS:
> + if (chip->options & NAND_IS_BOOT_MEDIUM) {
> + strength = rs_strength_bootable;
> + strength_len =
> ARRAY_SIZE(rs_strength_bootable);
> + } else {
> + strength = rs_strength;
> + strength_len = ARRAY_SIZE(rs_strength);
> + }
> + break;
> + case NAND_ECC_BCH:
> + if (chip->options & NAND_IS_BOOT_MEDIUM) {
> + strength = bch_strength_bootable;
> + strength_len =
> ARRAY_SIZE(bch_strength_bootable);
> + } else {
> + strength = bch_strength;
> + strength_len = ARRAY_SIZE(bch_strength);
> + }
> + break;
> + default:
> + return -EINVAL;
> + }
> +
> + return tegra_nand_get_strength(chip, strength, strength_len,
> oobsize);
> +}
> +
> +static int tegra_nand_chips_init(struct device *dev,
> + Âstruct tegra_nand_controller *ctrl)
> +{
> + struct device_node *np = dev->of_node;
> + struct device_node *np_nand;
> + int nchips = of_get_child_count(np);
> + struct tegra_nand_chip *nand;
> + struct mtd_info *mtd;
> + struct nand_chip *chip;
> + unsigned long config, bch_config = 0;
> + int bits_per_step;
> + int ret;
> +
> + if (nchips != 1) {
> + dev_err(dev, "Currently only one NAND chip
> supported\n");
> + return -EINVAL;
> + }
> +
> + np_nand = of_get_next_child(np, NULL);
> +
> + nand = devm_kzalloc(dev, sizeof(*nand), GFP_KERNEL);
> + if (!nand)
> + return -ENOMEM;
> +
> + nand->wp_gpio = devm_gpiod_get_optional(dev, "wp",
> GPIOD_OUT_LOW);
> +
> + if (IS_ERR(nand->wp_gpio)) {
> + ret = PTR_ERR(nand->wp_gpio);
> + dev_err(dev, "Failed to request WP GPIO: %d\n",
> ret);
> + return ret;
> + }
> +
> + chip = &nand->chip;
> + chip->controller = &ctrl->controller;
> +
> + mtd = nand_to_mtd(chip);
> +
> + mtd->dev.parent = dev;
> + if (!mtd->name)
> + mtd->name = "tegra_nand";
> + mtd->owner = THIS_MODULE;
> +
> + nand_set_flash_node(chip, np_nand);

Hi,
i just tried this driver and it works great so far, thanks.
I just found, that assigning the of node after setting the mtd->name
makes it impossible to assign a name via devicetree label. I have read
the discussion about the label on this list, so I'm curious if this is
intentional? Setting mtd->name after nand_set_flash_node() enables the
label parameter.

> +
> + chip->options = NAND_NO_SUBPAGE_WRITE |
> NAND_USE_BOUNCE_BUFFER;
> + chip->exec_op = tegra_nand_exec_op;
> + chip->select_chip = tegra_nand_select_chip;
> + chip->setup_data_interface =
> tegra_nand_setup_data_interface;
> +
> + ret = nand_scan_ident(mtd, 1, NULL);
> + if (ret)
> + return ret;
> +
> + if (chip->bbt_options & NAND_BBT_USE_FLASH)
> + chip->bbt_options |= NAND_BBT_NO_OOB;
> +
> + chip->ecc.mode = NAND_ECC_HW;
> + chip->ecc.size = 512;
> + chip->ecc.steps = mtd->writesize / chip->ecc.size;
> + if (chip->ecc_step_ds != 512) {
> + dev_err(dev, "Unsupported step size %d\n", chip-
> >ecc_step_ds);
> + return -EINVAL;
> + }
> +
> + chip->ecc.read_page = tegra_nand_read_page_hwecc;
> + chip->ecc.write_page = tegra_nand_write_page_hwecc;
> +
> + config = readl_relaxed(ctrl->regs + CFG);
> + config |= CFG_PIPE_EN | CFG_SKIP_SPARE |
> CFG_SKIP_SPARE_SIZE_4;
> +
> + if (chip->options & NAND_BUSWIDTH_16)
> + config |= CFG_BUS_WIDTH_16;
> +
> + if (chip->ecc.algo == NAND_ECC_UNKNOWN) {
> + if (mtd->writesize < 2048)
> + chip->ecc.algo = NAND_ECC_RS;
> + else
> + chip->ecc.algo = NAND_ECC_BCH;
> + }
> +
> + if (chip->ecc.algo == NAND_ECC_BCH && mtd->writesize < 2048)
> {
> + dev_err(dev, "BCH supportes 2K or 4K page size
> only\n");
> + return -EINVAL;
> + }
> +
> + if (!chip->ecc.strength) {
> + ret = tegra_nand_select_strength(chip, mtd-
> >oobsize);
> + if (ret < 0) {
> + dev_err(dev, "No valid strenght found,
> minimum %d\n",
> + chip->ecc_strength_ds);
> + return ret;
> + }
> +
> + chip->ecc.strength = ret;
> + }
> +
> + switch (chip->ecc.algo) {
> + case NAND_ECC_RS:
> + bits_per_step = BITS_PER_STEP_RS * chip-
> >ecc.strength;
> + mtd_set_ooblayout(mtd, &tegra_nand_oob_rs_ops);
> + switch (chip->ecc.strength) {
> + case 4:
> + config |= CFG_ECC_SEL | CFG_TVAL_4;
> + break;
> + case 6:
> + config |= CFG_ECC_SEL | CFG_TVAL_6;
> + break;
> + case 8:
> + config |= CFG_ECC_SEL | CFG_TVAL_8;
> + break;
> + default:
> + dev_err(dev, "ECC strength %d not
> supported\n",
> + chip->ecc.strength);
> + return -EINVAL;
> + }
> + break;
> + case NAND_ECC_BCH:
> + bits_per_step = BITS_PER_STEP_BCH * chip-
> >ecc.strength;
> + mtd_set_ooblayout(mtd, &tegra_nand_oob_bch_ops);
> + switch (chip->ecc.strength) {
> + case 4:
> + bch_config = BCH_TVAL_4;
> + break;
> + case 8:
> + bch_config = BCH_TVAL_8;
> + break;
> + case 14:
> + bch_config = BCH_TVAL_14;
> + break;
> + case 16:
> + bch_config = BCH_TVAL_16;
> + break;
> + default:
> + dev_err(dev, "ECC strength %d not
> supported\n",
> + chip->ecc.strength);
> + return -EINVAL;
> + }
> + break;
> + default:
> + dev_err(dev, "ECC algorithm not supported\n");
> + return -EINVAL;
> + }
> +
> + dev_info(dev, "Using %s with strength %d per 512 byte
> step\n",
> + chip->ecc.algo == NAND_ECC_BCH ? "BCH" :
> "RS",
> + chip->ecc.strength);
> +
> + chip->ecc.bytes = DIV_ROUND_UP(bits_per_step,
> BITS_PER_BYTE);
> +
> + switch (mtd->writesize) {
> + case 256:
> + config |= CFG_PS_256;
> + break;
> + case 512:
> + config |= CFG_PS_512;
> + break;
> + case 1024:
> + config |= CFG_PS_1024;
> + break;
> + case 2048:
> + config |= CFG_PS_2048;
> + break;
> + case 4096:
> + config |= CFG_PS_4096;
> + break;
> + default:
> + dev_err(dev, "Unsupported writesize %d\n", mtd-
> >writesize);
> + return -ENODEV;
> + }
> +
> + writel_relaxed(config, ctrl->regs + CFG);
> + writel_relaxed(bch_config, ctrl->regs + BCH_CONFIG);
> +
> + ret = nand_scan_tail(mtd);
> + if (ret)
> + return ret;
> +
> + mtd_ooblayout_free(mtd, 0, &nand->tag);
> +
> + config |= CFG_TAG_BYTE_SIZE(nand->tag.length - 1);
> + writel_relaxed(config, ctrl->regs + CFG);
> +
> + ret = mtd_device_register(mtd, NULL, 0);
> + if (ret) {
> + dev_err(dev, "Failed to register mtd device: %d\n",
> ret);
> + nand_cleanup(chip);
> + return ret;
> + }
> +
> + ctrl->chip = chip;
> +
> + return 0;
> +}
> +
> +static int tegra_nand_probe(struct platform_device *pdev)
> +{
> + struct reset_control *rst;
> + struct tegra_nand_controller *ctrl;
> + struct resource *res;
> + unsigned long reg;
> + int irq, err = 0;
> +
> + ctrl = devm_kzalloc(&pdev->dev, sizeof(*ctrl), GFP_KERNEL);
> + if (!ctrl)
> + return -ENOMEM;
> +
> + ctrl->dev = &pdev->dev;
> + nand_hw_control_init(&ctrl->controller);
> +
> + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
> + ctrl->regs = devm_ioremap_resource(&pdev->dev, res);
> + if (IS_ERR(ctrl->regs))
> + return PTR_ERR(ctrl->regs);
> +
> + rst = devm_reset_control_get(&pdev->dev, "nand");
> + if (IS_ERR(rst))
> + return PTR_ERR(rst);
> +
> + ctrl->clk = devm_clk_get(&pdev->dev, "nand");
> + if (IS_ERR(ctrl->clk))
> + return PTR_ERR(ctrl->clk);
> +
> + err = clk_prepare_enable(ctrl->clk);
> + if (err)
> + return err;
> +
> + err = reset_control_reset(rst);
> + if (err)
> + goto err_disable_clk;
> +
> + reg = HWSTATUS_RDSTATUS_MASK(1) | HWSTATUS_RDSTATUS_VALUE(0)
> |
> + HWSTATUS_RBSY_MASK(NAND_STATUS_READY) |
> + HWSTATUS_RBSY_VALUE(NAND_STATUS_READY);
> + writel_relaxed(NAND_CMD_STATUS, ctrl->regs + HWSTATUS_CMD);
> + writel_relaxed(reg, ctrl->regs + HWSTATUS_MASK);
> +
> + init_completion(&ctrl->command_complete);
> + init_completion(&ctrl->dma_complete);
> +
> + /* clear interrupts */
> + reg = readl_relaxed(ctrl->regs + ISR);
> + writel_relaxed(reg, ctrl->regs + ISR);
> +
> + irq = platform_get_irq(pdev, 0);
> + err = devm_request_irq(&pdev->dev, irq, tegra_nand_irq, 0,
> + ÂÂÂÂÂÂÂdev_name(&pdev->dev), ctrl);
> + if (err)
> + goto err_disable_clk;
> +
> + writel_relaxed(DMA_CTRL_IS_DONE, ctrl->regs + DMA_CTRL);
> +
> + /* enable interrupts */
> + reg = IER_UND | IER_OVR | IER_CMD_DONE | IER_GIE;
> + writel_relaxed(reg, ctrl->regs + IER);
> +
> + /* reset config */
> + writel_relaxed(0, ctrl->regs + CFG);
> +
> + err = tegra_nand_chips_init(ctrl->dev, ctrl);
> + if (err)
> + goto err_disable_clk;
> +
> + platform_set_drvdata(pdev, ctrl);
> +
> + return 0;
> +
> +err_disable_clk:
> + clk_disable_unprepare(ctrl->clk);
> + return err;
> +}
> +
> +static int tegra_nand_remove(struct platform_device *pdev)
> +{
> + struct tegra_nand_controller *ctrl =
> platform_get_drvdata(pdev);
> +
> + nand_release(nand_to_mtd(ctrl->chip));
> +
> + clk_disable_unprepare(ctrl->clk);
> +
> + return 0;
> +}
> +
> +static const struct of_device_id tegra_nand_of_match[] = {
> + { .compatible = "nvidia,tegra20-nand" },
> + { /* sentinel */ }
> +};
> +
> +static struct platform_driver tegra_nand_driver = {
> + .driver = {
> + .name = "tegra-nand",
> + .of_match_table = tegra_nand_of_match,
> + },
> + .probe = tegra_nand_probe,
> + .remove = tegra_nand_remove,
> +};
> +module_platform_driver(tegra_nand_driver);
> +
> +MODULE_DESCRIPTION("NVIDIA Tegra NAND driver");
> +MODULE_AUTHOR("Thierry Reding <thierry.reding@xxxxxxxxxx>");
> +MODULE_AUTHOR("Lucas Stach <dev@xxxxxxxxxx>");
> +MODULE_AUTHOR("Stefan Agner <stefan@xxxxxxxx>");
> +MODULE_LICENSE("GPL v2");
> +MODULE_DEVICE_TABLE(of, tegra_nand_of_match);

Sorry for any noise/mistake, I'm new to kernel development
Greetings
Randolph MaaÃen