linux/drivers/mtd/nand/raw/arasan-nand-controller.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Arasan NAND Flash Controller Driver
   4 *
   5 * Copyright (C) 2014 - 2020 Xilinx, Inc.
   6 * Author:
   7 *   Miquel Raynal <miquel.raynal@bootlin.com>
   8 * Original work (fully rewritten):
   9 *   Punnaiah Choudary Kalluri <punnaia@xilinx.com>
  10 *   Naga Sureshkumar Relli <nagasure@xilinx.com>
  11 */
  12
  13#include <linux/bch.h>
  14#include <linux/bitfield.h>
  15#include <linux/clk.h>
  16#include <linux/delay.h>
  17#include <linux/dma-mapping.h>
  18#include <linux/interrupt.h>
  19#include <linux/iopoll.h>
  20#include <linux/module.h>
  21#include <linux/mtd/mtd.h>
  22#include <linux/mtd/partitions.h>
  23#include <linux/mtd/rawnand.h>
  24#include <linux/of.h>
  25#include <linux/platform_device.h>
  26#include <linux/slab.h>
  27
  28#define PKT_REG                         0x00
  29#define   PKT_SIZE(x)                   FIELD_PREP(GENMASK(10, 0), (x))
  30#define   PKT_STEPS(x)                  FIELD_PREP(GENMASK(23, 12), (x))
  31
  32#define MEM_ADDR1_REG                   0x04
  33
  34#define MEM_ADDR2_REG                   0x08
  35#define   ADDR2_STRENGTH(x)             FIELD_PREP(GENMASK(27, 25), (x))
  36#define   ADDR2_CS(x)                   FIELD_PREP(GENMASK(31, 30), (x))
  37
  38#define CMD_REG                         0x0C
  39#define   CMD_1(x)                      FIELD_PREP(GENMASK(7, 0), (x))
  40#define   CMD_2(x)                      FIELD_PREP(GENMASK(15, 8), (x))
  41#define   CMD_PAGE_SIZE(x)              FIELD_PREP(GENMASK(25, 23), (x))
  42#define   CMD_DMA_ENABLE                BIT(27)
  43#define   CMD_NADDRS(x)                 FIELD_PREP(GENMASK(30, 28), (x))
  44#define   CMD_ECC_ENABLE                BIT(31)
  45
  46#define PROG_REG                        0x10
  47#define   PROG_PGRD                     BIT(0)
  48#define   PROG_ERASE                    BIT(2)
  49#define   PROG_STATUS                   BIT(3)
  50#define   PROG_PGPROG                   BIT(4)
  51#define   PROG_RDID                     BIT(6)
  52#define   PROG_RDPARAM                  BIT(7)
  53#define   PROG_RST                      BIT(8)
  54#define   PROG_GET_FEATURE              BIT(9)
  55#define   PROG_SET_FEATURE              BIT(10)
  56#define   PROG_CHG_RD_COL_ENH           BIT(14)
  57
  58#define INTR_STS_EN_REG                 0x14
  59#define INTR_SIG_EN_REG                 0x18
  60#define INTR_STS_REG                    0x1C
  61#define   WRITE_READY                   BIT(0)
  62#define   READ_READY                    BIT(1)
  63#define   XFER_COMPLETE                 BIT(2)
  64#define   DMA_BOUNDARY                  BIT(6)
  65#define   EVENT_MASK                    GENMASK(7, 0)
  66
  67#define READY_STS_REG                   0x20
  68
  69#define DMA_ADDR0_REG                   0x50
  70#define DMA_ADDR1_REG                   0x24
  71
  72#define FLASH_STS_REG                   0x28
  73
  74#define DATA_PORT_REG                   0x30
  75
  76#define ECC_CONF_REG                    0x34
  77#define   ECC_CONF_COL(x)               FIELD_PREP(GENMASK(15, 0), (x))
  78#define   ECC_CONF_LEN(x)               FIELD_PREP(GENMASK(26, 16), (x))
  79#define   ECC_CONF_BCH_EN               BIT(27)
  80
  81#define ECC_ERR_CNT_REG                 0x38
  82#define   GET_PKT_ERR_CNT(x)            FIELD_GET(GENMASK(7, 0), (x))
  83#define   GET_PAGE_ERR_CNT(x)           FIELD_GET(GENMASK(16, 8), (x))
  84
  85#define ECC_SP_REG                      0x3C
  86#define   ECC_SP_CMD1(x)                FIELD_PREP(GENMASK(7, 0), (x))
  87#define   ECC_SP_CMD2(x)                FIELD_PREP(GENMASK(15, 8), (x))
  88#define   ECC_SP_ADDRS(x)               FIELD_PREP(GENMASK(30, 28), (x))
  89
  90#define ECC_1ERR_CNT_REG                0x40
  91#define ECC_2ERR_CNT_REG                0x44
  92
  93#define DATA_INTERFACE_REG              0x6C
  94#define   DIFACE_SDR_MODE(x)            FIELD_PREP(GENMASK(2, 0), (x))
  95#define   DIFACE_DDR_MODE(x)            FIELD_PREP(GENMASK(5, 3), (x))
  96#define   DIFACE_SDR                    0
  97#define   DIFACE_NVDDR                  BIT(9)
  98
  99#define ANFC_MAX_CS                     2
 100#define ANFC_DFLT_TIMEOUT_US            1000000
 101#define ANFC_MAX_CHUNK_SIZE             SZ_1M
 102#define ANFC_MAX_PARAM_SIZE             SZ_4K
 103#define ANFC_MAX_STEPS                  SZ_2K
 104#define ANFC_MAX_PKT_SIZE               (SZ_2K - 1)
 105#define ANFC_MAX_ADDR_CYC               5U
 106#define ANFC_RSVD_ECC_BYTES             21
 107
 108#define ANFC_XLNX_SDR_DFLT_CORE_CLK     100000000
 109#define ANFC_XLNX_SDR_HS_CORE_CLK       80000000
 110
 111/**
 112 * struct anfc_op - Defines how to execute an operation
 113 * @pkt_reg: Packet register
 114 * @addr1_reg: Memory address 1 register
 115 * @addr2_reg: Memory address 2 register
 116 * @cmd_reg: Command register
 117 * @prog_reg: Program register
 118 * @steps: Number of "packets" to read/write
 119 * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
 120 * @len: Data transfer length
 121 * @read: Data transfer direction from the controller point of view
 122 */
 123struct anfc_op {
 124        u32 pkt_reg;
 125        u32 addr1_reg;
 126        u32 addr2_reg;
 127        u32 cmd_reg;
 128        u32 prog_reg;
 129        int steps;
 130        unsigned int rdy_timeout_ms;
 131        unsigned int len;
 132        bool read;
 133        u8 *buf;
 134};
 135
 136/**
 137 * struct anand - Defines the NAND chip related information
 138 * @node:               Used to store NAND chips into a list
 139 * @chip:               NAND chip information structure
 140 * @cs:                 Chip select line
 141 * @rb:                 Ready-busy line
 142 * @page_sz:            Register value of the page_sz field to use
 143 * @clk:                Expected clock frequency to use
 144 * @timings:            Data interface timing mode to use
 145 * @ecc_conf:           Hardware ECC configuration value
 146 * @strength:           Register value of the ECC strength
 147 * @raddr_cycles:       Row address cycle information
 148 * @caddr_cycles:       Column address cycle information
 149 * @ecc_bits:           Exact number of ECC bits per syndrome
 150 * @ecc_total:          Total number of ECC bytes
 151 * @errloc:             Array of errors located with soft BCH
 152 * @hw_ecc:             Buffer to store syndromes computed by hardware
 153 * @bch:                BCH structure
 154 */
 155struct anand {
 156        struct list_head node;
 157        struct nand_chip chip;
 158        unsigned int cs;
 159        unsigned int rb;
 160        unsigned int page_sz;
 161        unsigned long clk;
 162        u32 timings;
 163        u32 ecc_conf;
 164        u32 strength;
 165        u16 raddr_cycles;
 166        u16 caddr_cycles;
 167        unsigned int ecc_bits;
 168        unsigned int ecc_total;
 169        unsigned int *errloc;
 170        u8 *hw_ecc;
 171        struct bch_control *bch;
 172};
 173
 174/**
 175 * struct arasan_nfc - Defines the Arasan NAND flash controller driver instance
 176 * @dev:                Pointer to the device structure
 177 * @base:               Remapped register area
 178 * @controller_clk:             Pointer to the system clock
 179 * @bus_clk:            Pointer to the flash clock
 180 * @controller:         Base controller structure
 181 * @chips:              List of all NAND chips attached to the controller
 182 * @assigned_cs:        Bitmask describing already assigned CS lines
 183 * @cur_clk:            Current clock rate
 184 */
 185struct arasan_nfc {
 186        struct device *dev;
 187        void __iomem *base;
 188        struct clk *controller_clk;
 189        struct clk *bus_clk;
 190        struct nand_controller controller;
 191        struct list_head chips;
 192        unsigned long assigned_cs;
 193        unsigned int cur_clk;
 194};
 195
 196static struct anand *to_anand(struct nand_chip *nand)
 197{
 198        return container_of(nand, struct anand, chip);
 199}
 200
 201static struct arasan_nfc *to_anfc(struct nand_controller *ctrl)
 202{
 203        return container_of(ctrl, struct arasan_nfc, controller);
 204}
 205
 206static int anfc_wait_for_event(struct arasan_nfc *nfc, unsigned int event)
 207{
 208        u32 val;
 209        int ret;
 210
 211        ret = readl_relaxed_poll_timeout(nfc->base + INTR_STS_REG, val,
 212                                         val & event, 0,
 213                                         ANFC_DFLT_TIMEOUT_US);
 214        if (ret) {
 215                dev_err(nfc->dev, "Timeout waiting for event 0x%x\n", event);
 216                return -ETIMEDOUT;
 217        }
 218
 219        writel_relaxed(event, nfc->base + INTR_STS_REG);
 220
 221        return 0;
 222}
 223
 224static int anfc_wait_for_rb(struct arasan_nfc *nfc, struct nand_chip *chip,
 225                            unsigned int timeout_ms)
 226{
 227        struct anand *anand = to_anand(chip);
 228        u32 val;
 229        int ret;
 230
 231        /* There is no R/B interrupt, we must poll a register */
 232        ret = readl_relaxed_poll_timeout(nfc->base + READY_STS_REG, val,
 233                                         val & BIT(anand->rb),
 234                                         1, timeout_ms * 1000);
 235        if (ret) {
 236                dev_err(nfc->dev, "Timeout waiting for R/B 0x%x\n",
 237                        readl_relaxed(nfc->base + READY_STS_REG));
 238                return -ETIMEDOUT;
 239        }
 240
 241        return 0;
 242}
 243
 244static void anfc_trigger_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
 245{
 246        writel_relaxed(nfc_op->pkt_reg, nfc->base + PKT_REG);
 247        writel_relaxed(nfc_op->addr1_reg, nfc->base + MEM_ADDR1_REG);
 248        writel_relaxed(nfc_op->addr2_reg, nfc->base + MEM_ADDR2_REG);
 249        writel_relaxed(nfc_op->cmd_reg, nfc->base + CMD_REG);
 250        writel_relaxed(nfc_op->prog_reg, nfc->base + PROG_REG);
 251}
 252
 253static int anfc_pkt_len_config(unsigned int len, unsigned int *steps,
 254                               unsigned int *pktsize)
 255{
 256        unsigned int nb, sz;
 257
 258        for (nb = 1; nb < ANFC_MAX_STEPS; nb *= 2) {
 259                sz = len / nb;
 260                if (sz <= ANFC_MAX_PKT_SIZE)
 261                        break;
 262        }
 263
 264        if (sz * nb != len)
 265                return -ENOTSUPP;
 266
 267        if (steps)
 268                *steps = nb;
 269
 270        if (pktsize)
 271                *pktsize = sz;
 272
 273        return 0;
 274}
 275
 276/*
 277 * When using the embedded hardware ECC engine, the controller is in charge of
 278 * feeding the engine with, first, the ECC residue present in the data array.
 279 * A typical read operation is:
 280 * 1/ Assert the read operation by sending the relevant command/address cycles
 281 *    but targeting the column of the first ECC bytes in the OOB area instead of
 282 *    the main data directly.
 283 * 2/ After having read the relevant number of ECC bytes, the controller uses
 284 *    the RNDOUT/RNDSTART commands which are set into the "ECC Spare Command
 285 *    Register" to move the pointer back at the beginning of the main data.
 286 * 3/ It will read the content of the main area for a given size (pktsize) and
 287 *    will feed the ECC engine with this buffer again.
 288 * 4/ The ECC engine derives the ECC bytes for the given data and compare them
 289 *    with the ones already received. It eventually trigger status flags and
 290 *    then set the "Buffer Read Ready" flag.
 291 * 5/ The corrected data is then available for reading from the data port
 292 *    register.
 293 *
 294 * The hardware BCH ECC engine is known to be inconstent in BCH mode and never
 295 * reports uncorrectable errors. Because of this bug, we have to use the
 296 * software BCH implementation in the read path.
 297 */
 298static int anfc_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
 299                                 int oob_required, int page)
 300{
 301        struct arasan_nfc *nfc = to_anfc(chip->controller);
 302        struct mtd_info *mtd = nand_to_mtd(chip);
 303        struct anand *anand = to_anand(chip);
 304        unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
 305        unsigned int max_bitflips = 0;
 306        dma_addr_t dma_addr;
 307        int step, ret;
 308        struct anfc_op nfc_op = {
 309                .pkt_reg =
 310                        PKT_SIZE(chip->ecc.size) |
 311                        PKT_STEPS(chip->ecc.steps),
 312                .addr1_reg =
 313                        (page & 0xFF) << (8 * (anand->caddr_cycles)) |
 314                        (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
 315                .addr2_reg =
 316                        ((page >> 16) & 0xFF) |
 317                        ADDR2_STRENGTH(anand->strength) |
 318                        ADDR2_CS(anand->cs),
 319                .cmd_reg =
 320                        CMD_1(NAND_CMD_READ0) |
 321                        CMD_2(NAND_CMD_READSTART) |
 322                        CMD_PAGE_SIZE(anand->page_sz) |
 323                        CMD_DMA_ENABLE |
 324                        CMD_NADDRS(anand->caddr_cycles +
 325                                   anand->raddr_cycles),
 326                .prog_reg = PROG_PGRD,
 327        };
 328
 329        dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_FROM_DEVICE);
 330        if (dma_mapping_error(nfc->dev, dma_addr)) {
 331                dev_err(nfc->dev, "Buffer mapping error");
 332                return -EIO;
 333        }
 334
 335        writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
 336        writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
 337
 338        anfc_trigger_op(nfc, &nfc_op);
 339
 340        ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
 341        dma_unmap_single(nfc->dev, dma_addr, len, DMA_FROM_DEVICE);
 342        if (ret) {
 343                dev_err(nfc->dev, "Error reading page %d\n", page);
 344                return ret;
 345        }
 346
 347        /* Store the raw OOB bytes as well */
 348        ret = nand_change_read_column_op(chip, mtd->writesize, chip->oob_poi,
 349                                         mtd->oobsize, 0);
 350        if (ret)
 351                return ret;
 352
 353        /*
 354         * For each step, compute by softare the BCH syndrome over the raw data.
 355         * Compare the theoretical amount of errors and compare with the
 356         * hardware engine feedback.
 357         */
 358        for (step = 0; step < chip->ecc.steps; step++) {
 359                u8 *raw_buf = &buf[step * chip->ecc.size];
 360                unsigned int bit, byte;
 361                int bf, i;
 362
 363                /* Extract the syndrome, it is not necessarily aligned */
 364                memset(anand->hw_ecc, 0, chip->ecc.bytes);
 365                nand_extract_bits(anand->hw_ecc, 0,
 366                                  &chip->oob_poi[mtd->oobsize - anand->ecc_total],
 367                                  anand->ecc_bits * step, anand->ecc_bits);
 368
 369                bf = bch_decode(anand->bch, raw_buf, chip->ecc.size,
 370                                anand->hw_ecc, NULL, NULL, anand->errloc);
 371                if (!bf) {
 372                        continue;
 373                } else if (bf > 0) {
 374                        for (i = 0; i < bf; i++) {
 375                                /* Only correct the data, not the syndrome */
 376                                if (anand->errloc[i] < (chip->ecc.size * 8)) {
 377                                        bit = BIT(anand->errloc[i] & 7);
 378                                        byte = anand->errloc[i] >> 3;
 379                                        raw_buf[byte] ^= bit;
 380                                }
 381                        }
 382
 383                        mtd->ecc_stats.corrected += bf;
 384                        max_bitflips = max_t(unsigned int, max_bitflips, bf);
 385
 386                        continue;
 387                }
 388
 389                bf = nand_check_erased_ecc_chunk(raw_buf, chip->ecc.size,
 390                                                 NULL, 0, NULL, 0,
 391                                                 chip->ecc.strength);
 392                if (bf > 0) {
 393                        mtd->ecc_stats.corrected += bf;
 394                        max_bitflips = max_t(unsigned int, max_bitflips, bf);
 395                        memset(raw_buf, 0xFF, chip->ecc.size);
 396                } else if (bf < 0) {
 397                        mtd->ecc_stats.failed++;
 398                }
 399        }
 400
 401        return 0;
 402}
 403
 404static int anfc_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
 405                                  int oob_required, int page)
 406{
 407        struct anand *anand = to_anand(chip);
 408        struct arasan_nfc *nfc = to_anfc(chip->controller);
 409        struct mtd_info *mtd = nand_to_mtd(chip);
 410        unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
 411        dma_addr_t dma_addr;
 412        int ret;
 413        struct anfc_op nfc_op = {
 414                .pkt_reg =
 415                        PKT_SIZE(chip->ecc.size) |
 416                        PKT_STEPS(chip->ecc.steps),
 417                .addr1_reg =
 418                        (page & 0xFF) << (8 * (anand->caddr_cycles)) |
 419                        (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
 420                .addr2_reg =
 421                        ((page >> 16) & 0xFF) |
 422                        ADDR2_STRENGTH(anand->strength) |
 423                        ADDR2_CS(anand->cs),
 424                .cmd_reg =
 425                        CMD_1(NAND_CMD_SEQIN) |
 426                        CMD_2(NAND_CMD_PAGEPROG) |
 427                        CMD_PAGE_SIZE(anand->page_sz) |
 428                        CMD_DMA_ENABLE |
 429                        CMD_NADDRS(anand->caddr_cycles +
 430                                   anand->raddr_cycles) |
 431                        CMD_ECC_ENABLE,
 432                .prog_reg = PROG_PGPROG,
 433        };
 434
 435        writel_relaxed(anand->ecc_conf, nfc->base + ECC_CONF_REG);
 436        writel_relaxed(ECC_SP_CMD1(NAND_CMD_RNDIN) |
 437                       ECC_SP_ADDRS(anand->caddr_cycles),
 438                       nfc->base + ECC_SP_REG);
 439
 440        dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_TO_DEVICE);
 441        if (dma_mapping_error(nfc->dev, dma_addr)) {
 442                dev_err(nfc->dev, "Buffer mapping error");
 443                return -EIO;
 444        }
 445
 446        writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
 447        writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
 448
 449        anfc_trigger_op(nfc, &nfc_op);
 450        ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
 451        dma_unmap_single(nfc->dev, dma_addr, len, DMA_TO_DEVICE);
 452        if (ret) {
 453                dev_err(nfc->dev, "Error writing page %d\n", page);
 454                return ret;
 455        }
 456
 457        /* Spare data is not protected */
 458        if (oob_required)
 459                ret = nand_write_oob_std(chip, page);
 460
 461        return ret;
 462}
 463
 464/* NAND framework ->exec_op() hooks and related helpers */
 465static int anfc_parse_instructions(struct nand_chip *chip,
 466                                   const struct nand_subop *subop,
 467                                   struct anfc_op *nfc_op)
 468{
 469        struct anand *anand = to_anand(chip);
 470        const struct nand_op_instr *instr = NULL;
 471        bool first_cmd = true;
 472        unsigned int op_id;
 473        int ret, i;
 474
 475        memset(nfc_op, 0, sizeof(*nfc_op));
 476        nfc_op->addr2_reg = ADDR2_CS(anand->cs);
 477        nfc_op->cmd_reg = CMD_PAGE_SIZE(anand->page_sz);
 478
 479        for (op_id = 0; op_id < subop->ninstrs; op_id++) {
 480                unsigned int offset, naddrs, pktsize;
 481                const u8 *addrs;
 482                u8 *buf;
 483
 484                instr = &subop->instrs[op_id];
 485
 486                switch (instr->type) {
 487                case NAND_OP_CMD_INSTR:
 488                        if (first_cmd)
 489                                nfc_op->cmd_reg |= CMD_1(instr->ctx.cmd.opcode);
 490                        else
 491                                nfc_op->cmd_reg |= CMD_2(instr->ctx.cmd.opcode);
 492
 493                        first_cmd = false;
 494                        break;
 495
 496                case NAND_OP_ADDR_INSTR:
 497                        offset = nand_subop_get_addr_start_off(subop, op_id);
 498                        naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
 499                        addrs = &instr->ctx.addr.addrs[offset];
 500                        nfc_op->cmd_reg |= CMD_NADDRS(naddrs);
 501
 502                        for (i = 0; i < min(ANFC_MAX_ADDR_CYC, naddrs); i++) {
 503                                if (i < 4)
 504                                        nfc_op->addr1_reg |= (u32)addrs[i] << i * 8;
 505                                else
 506                                        nfc_op->addr2_reg |= addrs[i];
 507                        }
 508
 509                        break;
 510                case NAND_OP_DATA_IN_INSTR:
 511                        nfc_op->read = true;
 512                        fallthrough;
 513                case NAND_OP_DATA_OUT_INSTR:
 514                        offset = nand_subop_get_data_start_off(subop, op_id);
 515                        buf = instr->ctx.data.buf.in;
 516                        nfc_op->buf = &buf[offset];
 517                        nfc_op->len = nand_subop_get_data_len(subop, op_id);
 518                        ret = anfc_pkt_len_config(nfc_op->len, &nfc_op->steps,
 519                                                  &pktsize);
 520                        if (ret)
 521                                return ret;
 522
 523                        /*
 524                         * Number of DATA cycles must be aligned on 4, this
 525                         * means the controller might read/write more than
 526                         * requested. This is harmless most of the time as extra
 527                         * DATA are discarded in the write path and read pointer
 528                         * adjusted in the read path.
 529                         *
 530                         * FIXME: The core should mark operations where
 531                         * reading/writing more is allowed so the exec_op()
 532                         * implementation can take the right decision when the
 533                         * alignment constraint is not met: adjust the number of
 534                         * DATA cycles when it's allowed, reject the operation
 535                         * otherwise.
 536                         */
 537                        nfc_op->pkt_reg |= PKT_SIZE(round_up(pktsize, 4)) |
 538                                           PKT_STEPS(nfc_op->steps);
 539                        break;
 540                case NAND_OP_WAITRDY_INSTR:
 541                        nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
 542                        break;
 543                }
 544        }
 545
 546        return 0;
 547}
 548
 549static int anfc_rw_pio_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
 550{
 551        unsigned int dwords = (nfc_op->len / 4) / nfc_op->steps;
 552        unsigned int last_len = nfc_op->len % 4;
 553        unsigned int offset, dir;
 554        u8 *buf = nfc_op->buf;
 555        int ret, i;
 556
 557        for (i = 0; i < nfc_op->steps; i++) {
 558                dir = nfc_op->read ? READ_READY : WRITE_READY;
 559                ret = anfc_wait_for_event(nfc, dir);
 560                if (ret) {
 561                        dev_err(nfc->dev, "PIO %s ready signal not received\n",
 562                                nfc_op->read ? "Read" : "Write");
 563                        return ret;
 564                }
 565
 566                offset = i * (dwords * 4);
 567                if (nfc_op->read)
 568                        ioread32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
 569                                     dwords);
 570                else
 571                        iowrite32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
 572                                      dwords);
 573        }
 574
 575        if (last_len) {
 576                u32 remainder;
 577
 578                offset = nfc_op->len - last_len;
 579
 580                if (nfc_op->read) {
 581                        remainder = readl_relaxed(nfc->base + DATA_PORT_REG);
 582                        memcpy(&buf[offset], &remainder, last_len);
 583                } else {
 584                        memcpy(&remainder, &buf[offset], last_len);
 585                        writel_relaxed(remainder, nfc->base + DATA_PORT_REG);
 586                }
 587        }
 588
 589        return anfc_wait_for_event(nfc, XFER_COMPLETE);
 590}
 591
 592static int anfc_misc_data_type_exec(struct nand_chip *chip,
 593                                    const struct nand_subop *subop,
 594                                    u32 prog_reg)
 595{
 596        struct arasan_nfc *nfc = to_anfc(chip->controller);
 597        struct anfc_op nfc_op = {};
 598        int ret;
 599
 600        ret = anfc_parse_instructions(chip, subop, &nfc_op);
 601        if (ret)
 602                return ret;
 603
 604        nfc_op.prog_reg = prog_reg;
 605        anfc_trigger_op(nfc, &nfc_op);
 606
 607        if (nfc_op.rdy_timeout_ms) {
 608                ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
 609                if (ret)
 610                        return ret;
 611        }
 612
 613        return anfc_rw_pio_op(nfc, &nfc_op);
 614}
 615
 616static int anfc_param_read_type_exec(struct nand_chip *chip,
 617                                     const struct nand_subop *subop)
 618{
 619        return anfc_misc_data_type_exec(chip, subop, PROG_RDPARAM);
 620}
 621
 622static int anfc_data_read_type_exec(struct nand_chip *chip,
 623                                    const struct nand_subop *subop)
 624{
 625        u32 prog_reg = PROG_PGRD;
 626
 627        /*
 628         * Experience shows that while in SDR mode sending a CHANGE READ COLUMN
 629         * command through the READ PAGE "type" always works fine, when in
 630         * NV-DDR mode the same command simply fails. However, it was also
 631         * spotted that any CHANGE READ COLUMN command sent through the CHANGE
 632         * READ COLUMN ENHANCED "type" would correctly work in both cases (SDR
 633         * and NV-DDR). So, for simplicity, let's program the controller with
 634         * the CHANGE READ COLUMN ENHANCED "type" whenever we are requested to
 635         * perform a CHANGE READ COLUMN operation.
 636         */
 637        if (subop->instrs[0].ctx.cmd.opcode == NAND_CMD_RNDOUT &&
 638            subop->instrs[2].ctx.cmd.opcode == NAND_CMD_RNDOUTSTART)
 639                prog_reg = PROG_CHG_RD_COL_ENH;
 640
 641        return anfc_misc_data_type_exec(chip, subop, prog_reg);
 642}
 643
 644static int anfc_param_write_type_exec(struct nand_chip *chip,
 645                                      const struct nand_subop *subop)
 646{
 647        return anfc_misc_data_type_exec(chip, subop, PROG_SET_FEATURE);
 648}
 649
 650static int anfc_data_write_type_exec(struct nand_chip *chip,
 651                                     const struct nand_subop *subop)
 652{
 653        return anfc_misc_data_type_exec(chip, subop, PROG_PGPROG);
 654}
 655
 656static int anfc_misc_zerolen_type_exec(struct nand_chip *chip,
 657                                       const struct nand_subop *subop,
 658                                       u32 prog_reg)
 659{
 660        struct arasan_nfc *nfc = to_anfc(chip->controller);
 661        struct anfc_op nfc_op = {};
 662        int ret;
 663
 664        ret = anfc_parse_instructions(chip, subop, &nfc_op);
 665        if (ret)
 666                return ret;
 667
 668        nfc_op.prog_reg = prog_reg;
 669        anfc_trigger_op(nfc, &nfc_op);
 670
 671        ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
 672        if (ret)
 673                return ret;
 674
 675        if (nfc_op.rdy_timeout_ms)
 676                ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
 677
 678        return ret;
 679}
 680
 681static int anfc_status_type_exec(struct nand_chip *chip,
 682                                 const struct nand_subop *subop)
 683{
 684        struct arasan_nfc *nfc = to_anfc(chip->controller);
 685        u32 tmp;
 686        int ret;
 687
 688        /* See anfc_check_op() for details about this constraint */
 689        if (subop->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS)
 690                return -ENOTSUPP;
 691
 692        ret = anfc_misc_zerolen_type_exec(chip, subop, PROG_STATUS);
 693        if (ret)
 694                return ret;
 695
 696        tmp = readl_relaxed(nfc->base + FLASH_STS_REG);
 697        memcpy(subop->instrs[1].ctx.data.buf.in, &tmp, 1);
 698
 699        return 0;
 700}
 701
 702static int anfc_reset_type_exec(struct nand_chip *chip,
 703                                const struct nand_subop *subop)
 704{
 705        return anfc_misc_zerolen_type_exec(chip, subop, PROG_RST);
 706}
 707
 708static int anfc_erase_type_exec(struct nand_chip *chip,
 709                                const struct nand_subop *subop)
 710{
 711        return anfc_misc_zerolen_type_exec(chip, subop, PROG_ERASE);
 712}
 713
 714static int anfc_wait_type_exec(struct nand_chip *chip,
 715                               const struct nand_subop *subop)
 716{
 717        struct arasan_nfc *nfc = to_anfc(chip->controller);
 718        struct anfc_op nfc_op = {};
 719        int ret;
 720
 721        ret = anfc_parse_instructions(chip, subop, &nfc_op);
 722        if (ret)
 723                return ret;
 724
 725        return anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
 726}
 727
 728static const struct nand_op_parser anfc_op_parser = NAND_OP_PARSER(
 729        NAND_OP_PARSER_PATTERN(
 730                anfc_param_read_type_exec,
 731                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 732                NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
 733                NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
 734                NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
 735        NAND_OP_PARSER_PATTERN(
 736                anfc_param_write_type_exec,
 737                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 738                NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
 739                NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_PARAM_SIZE)),
 740        NAND_OP_PARSER_PATTERN(
 741                anfc_data_read_type_exec,
 742                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 743                NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
 744                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 745                NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
 746                NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, ANFC_MAX_CHUNK_SIZE)),
 747        NAND_OP_PARSER_PATTERN(
 748                anfc_data_write_type_exec,
 749                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 750                NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
 751                NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE),
 752                NAND_OP_PARSER_PAT_CMD_ELEM(false)),
 753        NAND_OP_PARSER_PATTERN(
 754                anfc_reset_type_exec,
 755                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 756                NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
 757        NAND_OP_PARSER_PATTERN(
 758                anfc_erase_type_exec,
 759                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 760                NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
 761                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 762                NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
 763        NAND_OP_PARSER_PATTERN(
 764                anfc_status_type_exec,
 765                NAND_OP_PARSER_PAT_CMD_ELEM(false),
 766                NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
 767        NAND_OP_PARSER_PATTERN(
 768                anfc_wait_type_exec,
 769                NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
 770        );
 771
 772static int anfc_select_target(struct nand_chip *chip, int target)
 773{
 774        struct anand *anand = to_anand(chip);
 775        struct arasan_nfc *nfc = to_anfc(chip->controller);
 776        int ret;
 777
 778        /* Update the controller timings and the potential ECC configuration */
 779        writel_relaxed(anand->timings, nfc->base + DATA_INTERFACE_REG);
 780
 781        /* Update clock frequency */
 782        if (nfc->cur_clk != anand->clk) {
 783                clk_disable_unprepare(nfc->controller_clk);
 784                ret = clk_set_rate(nfc->controller_clk, anand->clk);
 785                if (ret) {
 786                        dev_err(nfc->dev, "Failed to change clock rate\n");
 787                        return ret;
 788                }
 789
 790                ret = clk_prepare_enable(nfc->controller_clk);
 791                if (ret) {
 792                        dev_err(nfc->dev,
 793                                "Failed to re-enable the controller clock\n");
 794                        return ret;
 795                }
 796
 797                nfc->cur_clk = anand->clk;
 798        }
 799
 800        return 0;
 801}
 802
 803static int anfc_check_op(struct nand_chip *chip,
 804                         const struct nand_operation *op)
 805{
 806        const struct nand_op_instr *instr;
 807        int op_id;
 808
 809        /*
 810         * The controller abstracts all the NAND operations and do not support
 811         * data only operations.
 812         *
 813         * TODO: The nand_op_parser framework should be extended to
 814         * support custom checks on DATA instructions.
 815         */
 816        for (op_id = 0; op_id < op->ninstrs; op_id++) {
 817                instr = &op->instrs[op_id];
 818
 819                switch (instr->type) {
 820                case NAND_OP_ADDR_INSTR:
 821                        if (instr->ctx.addr.naddrs > ANFC_MAX_ADDR_CYC)
 822                                return -ENOTSUPP;
 823
 824                        break;
 825                case NAND_OP_DATA_IN_INSTR:
 826                case NAND_OP_DATA_OUT_INSTR:
 827                        if (instr->ctx.data.len > ANFC_MAX_CHUNK_SIZE)
 828                                return -ENOTSUPP;
 829
 830                        if (anfc_pkt_len_config(instr->ctx.data.len, 0, 0))
 831                                return -ENOTSUPP;
 832
 833                        break;
 834                default:
 835                        break;
 836                }
 837        }
 838
 839        /*
 840         * The controller does not allow to proceed with a CMD+DATA_IN cycle
 841         * manually on the bus by reading data from the data register. Instead,
 842         * the controller abstract a status read operation with its own status
 843         * register after ordering a read status operation. Hence, we cannot
 844         * support any CMD+DATA_IN operation other than a READ STATUS.
 845         *
 846         * TODO: The nand_op_parser() framework should be extended to describe
 847         * fixed patterns instead of open-coding this check here.
 848         */
 849        if (op->ninstrs == 2 &&
 850            op->instrs[0].type == NAND_OP_CMD_INSTR &&
 851            op->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS &&
 852            op->instrs[1].type == NAND_OP_DATA_IN_INSTR)
 853                return -ENOTSUPP;
 854
 855        return nand_op_parser_exec_op(chip, &anfc_op_parser, op, true);
 856}
 857
 858static int anfc_exec_op(struct nand_chip *chip,
 859                        const struct nand_operation *op,
 860                        bool check_only)
 861{
 862        int ret;
 863
 864        if (check_only)
 865                return anfc_check_op(chip, op);
 866
 867        ret = anfc_select_target(chip, op->cs);
 868        if (ret)
 869                return ret;
 870
 871        return nand_op_parser_exec_op(chip, &anfc_op_parser, op, check_only);
 872}
 873
 874static int anfc_setup_interface(struct nand_chip *chip, int target,
 875                                const struct nand_interface_config *conf)
 876{
 877        struct anand *anand = to_anand(chip);
 878        struct arasan_nfc *nfc = to_anfc(chip->controller);
 879        struct device_node *np = nfc->dev->of_node;
 880        const struct nand_sdr_timings *sdr;
 881        const struct nand_nvddr_timings *nvddr;
 882
 883        if (nand_interface_is_nvddr(conf)) {
 884                nvddr = nand_get_nvddr_timings(conf);
 885                if (IS_ERR(nvddr))
 886                        return PTR_ERR(nvddr);
 887        } else {
 888                sdr = nand_get_sdr_timings(conf);
 889                if (IS_ERR(sdr))
 890                        return PTR_ERR(sdr);
 891        }
 892
 893        if (target < 0)
 894                return 0;
 895
 896        if (nand_interface_is_sdr(conf))
 897                anand->timings = DIFACE_SDR |
 898                                 DIFACE_SDR_MODE(conf->timings.mode);
 899        else
 900                anand->timings = DIFACE_NVDDR |
 901                                 DIFACE_DDR_MODE(conf->timings.mode);
 902
 903        anand->clk = ANFC_XLNX_SDR_DFLT_CORE_CLK;
 904
 905        /*
 906         * Due to a hardware bug in the ZynqMP SoC, SDR timing modes 0-1 work
 907         * with f > 90MHz (default clock is 100MHz) but signals are unstable
 908         * with higher modes. Hence we decrease a little bit the clock rate to
 909         * 80MHz when using SDR modes 2-5 with this SoC.
 910         */
 911        if (of_device_is_compatible(np, "xlnx,zynqmp-nand-controller") &&
 912            nand_interface_is_sdr(conf) && conf->timings.mode >= 2)
 913                anand->clk = ANFC_XLNX_SDR_HS_CORE_CLK;
 914
 915        return 0;
 916}
 917
 918static int anfc_calc_hw_ecc_bytes(int step_size, int strength)
 919{
 920        unsigned int bch_gf_mag, ecc_bits;
 921
 922        switch (step_size) {
 923        case SZ_512:
 924                bch_gf_mag = 13;
 925                break;
 926        case SZ_1K:
 927                bch_gf_mag = 14;
 928                break;
 929        default:
 930                return -EINVAL;
 931        }
 932
 933        ecc_bits = bch_gf_mag * strength;
 934
 935        return DIV_ROUND_UP(ecc_bits, 8);
 936}
 937
 938static const int anfc_hw_ecc_512_strengths[] = {4, 8, 12};
 939
 940static const int anfc_hw_ecc_1024_strengths[] = {24};
 941
 942static const struct nand_ecc_step_info anfc_hw_ecc_step_infos[] = {
 943        {
 944                .stepsize = SZ_512,
 945                .strengths = anfc_hw_ecc_512_strengths,
 946                .nstrengths = ARRAY_SIZE(anfc_hw_ecc_512_strengths),
 947        },
 948        {
 949                .stepsize = SZ_1K,
 950                .strengths = anfc_hw_ecc_1024_strengths,
 951                .nstrengths = ARRAY_SIZE(anfc_hw_ecc_1024_strengths),
 952        },
 953};
 954
 955static const struct nand_ecc_caps anfc_hw_ecc_caps = {
 956        .stepinfos = anfc_hw_ecc_step_infos,
 957        .nstepinfos = ARRAY_SIZE(anfc_hw_ecc_step_infos),
 958        .calc_ecc_bytes = anfc_calc_hw_ecc_bytes,
 959};
 960
 961static int anfc_init_hw_ecc_controller(struct arasan_nfc *nfc,
 962                                       struct nand_chip *chip)
 963{
 964        struct anand *anand = to_anand(chip);
 965        struct mtd_info *mtd = nand_to_mtd(chip);
 966        struct nand_ecc_ctrl *ecc = &chip->ecc;
 967        unsigned int bch_prim_poly = 0, bch_gf_mag = 0, ecc_offset;
 968        int ret;
 969
 970        switch (mtd->writesize) {
 971        case SZ_512:
 972        case SZ_2K:
 973        case SZ_4K:
 974        case SZ_8K:
 975        case SZ_16K:
 976                break;
 977        default:
 978                dev_err(nfc->dev, "Unsupported page size %d\n", mtd->writesize);
 979                return -EINVAL;
 980        }
 981
 982        ret = nand_ecc_choose_conf(chip, &anfc_hw_ecc_caps, mtd->oobsize);
 983        if (ret)
 984                return ret;
 985
 986        switch (ecc->strength) {
 987        case 12:
 988                anand->strength = 0x1;
 989                break;
 990        case 8:
 991                anand->strength = 0x2;
 992                break;
 993        case 4:
 994                anand->strength = 0x3;
 995                break;
 996        case 24:
 997                anand->strength = 0x4;
 998                break;
 999        default:
1000                dev_err(nfc->dev, "Unsupported strength %d\n", ecc->strength);
1001                return -EINVAL;
1002        }
1003
1004        switch (ecc->size) {
1005        case SZ_512:
1006                bch_gf_mag = 13;
1007                bch_prim_poly = 0x201b;
1008                break;
1009        case SZ_1K:
1010                bch_gf_mag = 14;
1011                bch_prim_poly = 0x4443;
1012                break;
1013        default:
1014                dev_err(nfc->dev, "Unsupported step size %d\n", ecc->strength);
1015                return -EINVAL;
1016        }
1017
1018        mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
1019
1020        ecc->steps = mtd->writesize / ecc->size;
1021        ecc->algo = NAND_ECC_ALGO_BCH;
1022        anand->ecc_bits = bch_gf_mag * ecc->strength;
1023        ecc->bytes = DIV_ROUND_UP(anand->ecc_bits, 8);
1024        anand->ecc_total = DIV_ROUND_UP(anand->ecc_bits * ecc->steps, 8);
1025        ecc_offset = mtd->writesize + mtd->oobsize - anand->ecc_total;
1026        anand->ecc_conf = ECC_CONF_COL(ecc_offset) |
1027                          ECC_CONF_LEN(anand->ecc_total) |
1028                          ECC_CONF_BCH_EN;
1029
1030        anand->errloc = devm_kmalloc_array(nfc->dev, ecc->strength,
1031                                           sizeof(*anand->errloc), GFP_KERNEL);
1032        if (!anand->errloc)
1033                return -ENOMEM;
1034
1035        anand->hw_ecc = devm_kmalloc(nfc->dev, ecc->bytes, GFP_KERNEL);
1036        if (!anand->hw_ecc)
1037                return -ENOMEM;
1038
1039        /* Enforce bit swapping to fit the hardware */
1040        anand->bch = bch_init(bch_gf_mag, ecc->strength, bch_prim_poly, true);
1041        if (!anand->bch)
1042                return -EINVAL;
1043
1044        ecc->read_page = anfc_read_page_hw_ecc;
1045        ecc->write_page = anfc_write_page_hw_ecc;
1046
1047        return 0;
1048}
1049
1050static int anfc_attach_chip(struct nand_chip *chip)
1051{
1052        struct anand *anand = to_anand(chip);
1053        struct arasan_nfc *nfc = to_anfc(chip->controller);
1054        struct mtd_info *mtd = nand_to_mtd(chip);
1055        int ret = 0;
1056
1057        if (mtd->writesize <= SZ_512)
1058                anand->caddr_cycles = 1;
1059        else
1060                anand->caddr_cycles = 2;
1061
1062        if (chip->options & NAND_ROW_ADDR_3)
1063                anand->raddr_cycles = 3;
1064        else
1065                anand->raddr_cycles = 2;
1066
1067        switch (mtd->writesize) {
1068        case 512:
1069                anand->page_sz = 0;
1070                break;
1071        case 1024:
1072                anand->page_sz = 5;
1073                break;
1074        case 2048:
1075                anand->page_sz = 1;
1076                break;
1077        case 4096:
1078                anand->page_sz = 2;
1079                break;
1080        case 8192:
1081                anand->page_sz = 3;
1082                break;
1083        case 16384:
1084                anand->page_sz = 4;
1085                break;
1086        default:
1087                return -EINVAL;
1088        }
1089
1090        /* These hooks are valid for all ECC providers */
1091        chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
1092        chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
1093
1094        switch (chip->ecc.engine_type) {
1095        case NAND_ECC_ENGINE_TYPE_NONE:
1096        case NAND_ECC_ENGINE_TYPE_SOFT:
1097        case NAND_ECC_ENGINE_TYPE_ON_DIE:
1098                break;
1099        case NAND_ECC_ENGINE_TYPE_ON_HOST:
1100                ret = anfc_init_hw_ecc_controller(nfc, chip);
1101                break;
1102        default:
1103                dev_err(nfc->dev, "Unsupported ECC mode: %d\n",
1104                        chip->ecc.engine_type);
1105                return -EINVAL;
1106        }
1107
1108        return ret;
1109}
1110
1111static void anfc_detach_chip(struct nand_chip *chip)
1112{
1113        struct anand *anand = to_anand(chip);
1114
1115        if (anand->bch)
1116                bch_free(anand->bch);
1117}
1118
1119static const struct nand_controller_ops anfc_ops = {
1120        .exec_op = anfc_exec_op,
1121        .setup_interface = anfc_setup_interface,
1122        .attach_chip = anfc_attach_chip,
1123        .detach_chip = anfc_detach_chip,
1124};
1125
1126static int anfc_chip_init(struct arasan_nfc *nfc, struct device_node *np)
1127{
1128        struct anand *anand;
1129        struct nand_chip *chip;
1130        struct mtd_info *mtd;
1131        int cs, rb, ret;
1132
1133        anand = devm_kzalloc(nfc->dev, sizeof(*anand), GFP_KERNEL);
1134        if (!anand)
1135                return -ENOMEM;
1136
1137        /* We do not support multiple CS per chip yet */
1138        if (of_property_count_elems_of_size(np, "reg", sizeof(u32)) != 1) {
1139                dev_err(nfc->dev, "Invalid reg property\n");
1140                return -EINVAL;
1141        }
1142
1143        ret = of_property_read_u32(np, "reg", &cs);
1144        if (ret)
1145                return ret;
1146
1147        ret = of_property_read_u32(np, "nand-rb", &rb);
1148        if (ret)
1149                return ret;
1150
1151        if (cs >= ANFC_MAX_CS || rb >= ANFC_MAX_CS) {
1152                dev_err(nfc->dev, "Wrong CS %d or RB %d\n", cs, rb);
1153                return -EINVAL;
1154        }
1155
1156        if (test_and_set_bit(cs, &nfc->assigned_cs)) {
1157                dev_err(nfc->dev, "Already assigned CS %d\n", cs);
1158                return -EINVAL;
1159        }
1160
1161        anand->cs = cs;
1162        anand->rb = rb;
1163
1164        chip = &anand->chip;
1165        mtd = nand_to_mtd(chip);
1166        mtd->dev.parent = nfc->dev;
1167        chip->controller = &nfc->controller;
1168        chip->options = NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
1169                        NAND_USES_DMA;
1170
1171        nand_set_flash_node(chip, np);
1172        if (!mtd->name) {
1173                dev_err(nfc->dev, "NAND label property is mandatory\n");
1174                return -EINVAL;
1175        }
1176
1177        ret = nand_scan(chip, 1);
1178        if (ret) {
1179                dev_err(nfc->dev, "Scan operation failed\n");
1180                return ret;
1181        }
1182
1183        ret = mtd_device_register(mtd, NULL, 0);
1184        if (ret) {
1185                nand_cleanup(chip);
1186                return ret;
1187        }
1188
1189        list_add_tail(&anand->node, &nfc->chips);
1190
1191        return 0;
1192}
1193
1194static void anfc_chips_cleanup(struct arasan_nfc *nfc)
1195{
1196        struct anand *anand, *tmp;
1197        struct nand_chip *chip;
1198        int ret;
1199
1200        list_for_each_entry_safe(anand, tmp, &nfc->chips, node) {
1201                chip = &anand->chip;
1202                ret = mtd_device_unregister(nand_to_mtd(chip));
1203                WARN_ON(ret);
1204                nand_cleanup(chip);
1205                list_del(&anand->node);
1206        }
1207}
1208
1209static int anfc_chips_init(struct arasan_nfc *nfc)
1210{
1211        struct device_node *np = nfc->dev->of_node, *nand_np;
1212        int nchips = of_get_child_count(np);
1213        int ret;
1214
1215        if (!nchips || nchips > ANFC_MAX_CS) {
1216                dev_err(nfc->dev, "Incorrect number of NAND chips (%d)\n",
1217                        nchips);
1218                return -EINVAL;
1219        }
1220
1221        for_each_child_of_node(np, nand_np) {
1222                ret = anfc_chip_init(nfc, nand_np);
1223                if (ret) {
1224                        of_node_put(nand_np);
1225                        anfc_chips_cleanup(nfc);
1226                        break;
1227                }
1228        }
1229
1230        return ret;
1231}
1232
1233static void anfc_reset(struct arasan_nfc *nfc)
1234{
1235        /* Disable interrupt signals */
1236        writel_relaxed(0, nfc->base + INTR_SIG_EN_REG);
1237
1238        /* Enable interrupt status */
1239        writel_relaxed(EVENT_MASK, nfc->base + INTR_STS_EN_REG);
1240}
1241
1242static int anfc_probe(struct platform_device *pdev)
1243{
1244        struct arasan_nfc *nfc;
1245        int ret;
1246
1247        nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
1248        if (!nfc)
1249                return -ENOMEM;
1250
1251        nfc->dev = &pdev->dev;
1252        nand_controller_init(&nfc->controller);
1253        nfc->controller.ops = &anfc_ops;
1254        INIT_LIST_HEAD(&nfc->chips);
1255
1256        nfc->base = devm_platform_ioremap_resource(pdev, 0);
1257        if (IS_ERR(nfc->base))
1258                return PTR_ERR(nfc->base);
1259
1260        anfc_reset(nfc);
1261
1262        nfc->controller_clk = devm_clk_get(&pdev->dev, "controller");
1263        if (IS_ERR(nfc->controller_clk))
1264                return PTR_ERR(nfc->controller_clk);
1265
1266        nfc->bus_clk = devm_clk_get(&pdev->dev, "bus");
1267        if (IS_ERR(nfc->bus_clk))
1268                return PTR_ERR(nfc->bus_clk);
1269
1270        ret = clk_prepare_enable(nfc->controller_clk);
1271        if (ret)
1272                return ret;
1273
1274        ret = clk_prepare_enable(nfc->bus_clk);
1275        if (ret)
1276                goto disable_controller_clk;
1277
1278        ret = anfc_chips_init(nfc);
1279        if (ret)
1280                goto disable_bus_clk;
1281
1282        platform_set_drvdata(pdev, nfc);
1283
1284        return 0;
1285
1286disable_bus_clk:
1287        clk_disable_unprepare(nfc->bus_clk);
1288
1289disable_controller_clk:
1290        clk_disable_unprepare(nfc->controller_clk);
1291
1292        return ret;
1293}
1294
1295static int anfc_remove(struct platform_device *pdev)
1296{
1297        struct arasan_nfc *nfc = platform_get_drvdata(pdev);
1298
1299        anfc_chips_cleanup(nfc);
1300
1301        clk_disable_unprepare(nfc->bus_clk);
1302        clk_disable_unprepare(nfc->controller_clk);
1303
1304        return 0;
1305}
1306
1307static const struct of_device_id anfc_ids[] = {
1308        {
1309                .compatible = "xlnx,zynqmp-nand-controller",
1310        },
1311        {
1312                .compatible = "arasan,nfc-v3p10",
1313        },
1314        {}
1315};
1316MODULE_DEVICE_TABLE(of, anfc_ids);
1317
1318static struct platform_driver anfc_driver = {
1319        .driver = {
1320                .name = "arasan-nand-controller",
1321                .of_match_table = anfc_ids,
1322        },
1323        .probe = anfc_probe,
1324        .remove = anfc_remove,
1325};
1326module_platform_driver(anfc_driver);
1327
1328MODULE_LICENSE("GPL v2");
1329MODULE_AUTHOR("Punnaiah Choudary Kalluri <punnaia@xilinx.com>");
1330MODULE_AUTHOR("Naga Sureshkumar Relli <nagasure@xilinx.com>");
1331MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
1332MODULE_DESCRIPTION("Arasan NAND Flash Controller Driver");
1333