1 // SPDX-License-Identifier: GPL-2.0
3 * Arasan NAND Flash Controller Driver
5 * Copyright (C) 2014 - 2020 Xilinx, Inc.
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>
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>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
29 #define PKT_SIZE(x) FIELD_PREP(GENMASK(10, 0), (x))
30 #define PKT_STEPS(x) FIELD_PREP(GENMASK(23, 12), (x))
32 #define MEM_ADDR1_REG 0x04
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))
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)
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)
57 #define INTR_STS_EN_REG 0x14
58 #define INTR_SIG_EN_REG 0x18
59 #define INTR_STS_REG 0x1C
60 #define WRITE_READY BIT(0)
61 #define READ_READY BIT(1)
62 #define XFER_COMPLETE BIT(2)
63 #define DMA_BOUNDARY BIT(6)
64 #define EVENT_MASK GENMASK(7, 0)
66 #define READY_STS_REG 0x20
68 #define DMA_ADDR0_REG 0x50
69 #define DMA_ADDR1_REG 0x24
71 #define FLASH_STS_REG 0x28
73 #define DATA_PORT_REG 0x30
75 #define ECC_CONF_REG 0x34
76 #define ECC_CONF_COL(x) FIELD_PREP(GENMASK(15, 0), (x))
77 #define ECC_CONF_LEN(x) FIELD_PREP(GENMASK(26, 16), (x))
78 #define ECC_CONF_BCH_EN BIT(27)
80 #define ECC_ERR_CNT_REG 0x38
81 #define GET_PKT_ERR_CNT(x) FIELD_GET(GENMASK(7, 0), (x))
82 #define GET_PAGE_ERR_CNT(x) FIELD_GET(GENMASK(16, 8), (x))
84 #define ECC_SP_REG 0x3C
85 #define ECC_SP_CMD1(x) FIELD_PREP(GENMASK(7, 0), (x))
86 #define ECC_SP_CMD2(x) FIELD_PREP(GENMASK(15, 8), (x))
87 #define ECC_SP_ADDRS(x) FIELD_PREP(GENMASK(30, 28), (x))
89 #define ECC_1ERR_CNT_REG 0x40
90 #define ECC_2ERR_CNT_REG 0x44
92 #define DATA_INTERFACE_REG 0x6C
93 #define DIFACE_SDR_MODE(x) FIELD_PREP(GENMASK(2, 0), (x))
94 #define DIFACE_DDR_MODE(x) FIELD_PREP(GENMASK(5, 3), (X))
96 #define DIFACE_NVDDR BIT(9)
99 #define ANFC_DFLT_TIMEOUT_US 1000000
100 #define ANFC_MAX_CHUNK_SIZE SZ_1M
101 #define ANFC_MAX_PARAM_SIZE SZ_4K
102 #define ANFC_MAX_STEPS SZ_2K
103 #define ANFC_MAX_PKT_SIZE (SZ_2K - 1)
104 #define ANFC_MAX_ADDR_CYC 5U
105 #define ANFC_RSVD_ECC_BYTES 21
107 #define ANFC_XLNX_SDR_DFLT_CORE_CLK 100000000
108 #define ANFC_XLNX_SDR_HS_CORE_CLK 80000000
111 * struct anfc_op - Defines how to execute an operation
112 * @pkt_reg: Packet register
113 * @addr1_reg: Memory address 1 register
114 * @addr2_reg: Memory address 2 register
115 * @cmd_reg: Command register
116 * @prog_reg: Program register
117 * @steps: Number of "packets" to read/write
118 * @rdy_timeout_ms: Timeout for waits on Ready/Busy pin
119 * @len: Data transfer length
120 * @read: Data transfer direction from the controller point of view
129 unsigned int rdy_timeout_ms;
136 * struct anand - Defines the NAND chip related information
137 * @node: Used to store NAND chips into a list
138 * @chip: NAND chip information structure
139 * @cs: Chip select line
140 * @rb: Ready-busy line
141 * @page_sz: Register value of the page_sz field to use
142 * @clk: Expected clock frequency to use
143 * @timings: Data interface timing mode to use
144 * @ecc_conf: Hardware ECC configuration value
145 * @strength: Register value of the ECC strength
146 * @raddr_cycles: Row address cycle information
147 * @caddr_cycles: Column address cycle information
148 * @ecc_bits: Exact number of ECC bits per syndrome
149 * @ecc_total: Total number of ECC bytes
150 * @errloc: Array of errors located with soft BCH
151 * @hw_ecc: Buffer to store syndromes computed by hardware
152 * @bch: BCH structure
155 struct list_head node;
156 struct nand_chip chip;
159 unsigned int page_sz;
166 unsigned int ecc_bits;
167 unsigned int ecc_total;
168 unsigned int *errloc;
170 struct bch_control *bch;
174 * struct arasan_nfc - Defines the Arasan NAND flash controller driver instance
175 * @dev: Pointer to the device structure
176 * @base: Remapped register area
177 * @controller_clk: Pointer to the system clock
178 * @bus_clk: Pointer to the flash clock
179 * @controller: Base controller structure
180 * @chips: List of all NAND chips attached to the controller
181 * @assigned_cs: Bitmask describing already assigned CS lines
182 * @cur_clk: Current clock rate
187 struct clk *controller_clk;
189 struct nand_controller controller;
190 struct list_head chips;
191 unsigned long assigned_cs;
192 unsigned int cur_clk;
195 static struct anand *to_anand(struct nand_chip *nand)
197 return container_of(nand, struct anand, chip);
200 static struct arasan_nfc *to_anfc(struct nand_controller *ctrl)
202 return container_of(ctrl, struct arasan_nfc, controller);
205 static int anfc_wait_for_event(struct arasan_nfc *nfc, unsigned int event)
210 ret = readl_relaxed_poll_timeout(nfc->base + INTR_STS_REG, val,
212 ANFC_DFLT_TIMEOUT_US);
214 dev_err(nfc->dev, "Timeout waiting for event 0x%x\n", event);
218 writel_relaxed(event, nfc->base + INTR_STS_REG);
223 static int anfc_wait_for_rb(struct arasan_nfc *nfc, struct nand_chip *chip,
224 unsigned int timeout_ms)
226 struct anand *anand = to_anand(chip);
230 /* There is no R/B interrupt, we must poll a register */
231 ret = readl_relaxed_poll_timeout(nfc->base + READY_STS_REG, val,
232 val & BIT(anand->rb),
233 1, timeout_ms * 1000);
235 dev_err(nfc->dev, "Timeout waiting for R/B 0x%x\n",
236 readl_relaxed(nfc->base + READY_STS_REG));
243 static void anfc_trigger_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
245 writel_relaxed(nfc_op->pkt_reg, nfc->base + PKT_REG);
246 writel_relaxed(nfc_op->addr1_reg, nfc->base + MEM_ADDR1_REG);
247 writel_relaxed(nfc_op->addr2_reg, nfc->base + MEM_ADDR2_REG);
248 writel_relaxed(nfc_op->cmd_reg, nfc->base + CMD_REG);
249 writel_relaxed(nfc_op->prog_reg, nfc->base + PROG_REG);
252 static int anfc_pkt_len_config(unsigned int len, unsigned int *steps,
253 unsigned int *pktsize)
257 for (nb = 1; nb < ANFC_MAX_STEPS; nb *= 2) {
259 if (sz <= ANFC_MAX_PKT_SIZE)
276 * When using the embedded hardware ECC engine, the controller is in charge of
277 * feeding the engine with, first, the ECC residue present in the data array.
278 * A typical read operation is:
279 * 1/ Assert the read operation by sending the relevant command/address cycles
280 * but targeting the column of the first ECC bytes in the OOB area instead of
281 * the main data directly.
282 * 2/ After having read the relevant number of ECC bytes, the controller uses
283 * the RNDOUT/RNDSTART commands which are set into the "ECC Spare Command
284 * Register" to move the pointer back at the beginning of the main data.
285 * 3/ It will read the content of the main area for a given size (pktsize) and
286 * will feed the ECC engine with this buffer again.
287 * 4/ The ECC engine derives the ECC bytes for the given data and compare them
288 * with the ones already received. It eventually trigger status flags and
289 * then set the "Buffer Read Ready" flag.
290 * 5/ The corrected data is then available for reading from the data port
293 * The hardware BCH ECC engine is known to be inconstent in BCH mode and never
294 * reports uncorrectable errors. Because of this bug, we have to use the
295 * software BCH implementation in the read path.
297 static int anfc_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
298 int oob_required, int page)
300 struct arasan_nfc *nfc = to_anfc(chip->controller);
301 struct mtd_info *mtd = nand_to_mtd(chip);
302 struct anand *anand = to_anand(chip);
303 unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
304 unsigned int max_bitflips = 0;
307 struct anfc_op nfc_op = {
309 PKT_SIZE(chip->ecc.size) |
310 PKT_STEPS(chip->ecc.steps),
312 (page & 0xFF) << (8 * (anand->caddr_cycles)) |
313 (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
315 ((page >> 16) & 0xFF) |
316 ADDR2_STRENGTH(anand->strength) |
319 CMD_1(NAND_CMD_READ0) |
320 CMD_2(NAND_CMD_READSTART) |
321 CMD_PAGE_SIZE(anand->page_sz) |
323 CMD_NADDRS(anand->caddr_cycles +
324 anand->raddr_cycles),
325 .prog_reg = PROG_PGRD,
328 dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_FROM_DEVICE);
329 if (dma_mapping_error(nfc->dev, dma_addr)) {
330 dev_err(nfc->dev, "Buffer mapping error");
334 writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
335 writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
337 anfc_trigger_op(nfc, &nfc_op);
339 ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
340 dma_unmap_single(nfc->dev, dma_addr, len, DMA_FROM_DEVICE);
342 dev_err(nfc->dev, "Error reading page %d\n", page);
346 /* Store the raw OOB bytes as well */
347 ret = nand_change_read_column_op(chip, mtd->writesize, chip->oob_poi,
353 * For each step, compute by softare the BCH syndrome over the raw data.
354 * Compare the theoretical amount of errors and compare with the
355 * hardware engine feedback.
357 for (step = 0; step < chip->ecc.steps; step++) {
358 u8 *raw_buf = &buf[step * chip->ecc.size];
359 unsigned int bit, byte;
362 /* Extract the syndrome, it is not necessarily aligned */
363 memset(anand->hw_ecc, 0, chip->ecc.bytes);
364 nand_extract_bits(anand->hw_ecc, 0,
365 &chip->oob_poi[mtd->oobsize - anand->ecc_total],
366 anand->ecc_bits * step, anand->ecc_bits);
368 bf = bch_decode(anand->bch, raw_buf, chip->ecc.size,
369 anand->hw_ecc, NULL, NULL, anand->errloc);
373 for (i = 0; i < bf; i++) {
374 /* Only correct the data, not the syndrome */
375 if (anand->errloc[i] < (chip->ecc.size * 8)) {
376 bit = BIT(anand->errloc[i] & 7);
377 byte = anand->errloc[i] >> 3;
378 raw_buf[byte] ^= bit;
382 mtd->ecc_stats.corrected += bf;
383 max_bitflips = max_t(unsigned int, max_bitflips, bf);
388 bf = nand_check_erased_ecc_chunk(raw_buf, chip->ecc.size,
392 mtd->ecc_stats.corrected += bf;
393 max_bitflips = max_t(unsigned int, max_bitflips, bf);
394 memset(raw_buf, 0xFF, chip->ecc.size);
396 mtd->ecc_stats.failed++;
403 static int anfc_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
404 int oob_required, int page)
406 struct anand *anand = to_anand(chip);
407 struct arasan_nfc *nfc = to_anfc(chip->controller);
408 struct mtd_info *mtd = nand_to_mtd(chip);
409 unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
412 struct anfc_op nfc_op = {
414 PKT_SIZE(chip->ecc.size) |
415 PKT_STEPS(chip->ecc.steps),
417 (page & 0xFF) << (8 * (anand->caddr_cycles)) |
418 (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
420 ((page >> 16) & 0xFF) |
421 ADDR2_STRENGTH(anand->strength) |
424 CMD_1(NAND_CMD_SEQIN) |
425 CMD_2(NAND_CMD_PAGEPROG) |
426 CMD_PAGE_SIZE(anand->page_sz) |
428 CMD_NADDRS(anand->caddr_cycles +
429 anand->raddr_cycles) |
431 .prog_reg = PROG_PGPROG,
434 writel_relaxed(anand->ecc_conf, nfc->base + ECC_CONF_REG);
435 writel_relaxed(ECC_SP_CMD1(NAND_CMD_RNDIN) |
436 ECC_SP_ADDRS(anand->caddr_cycles),
437 nfc->base + ECC_SP_REG);
439 dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_TO_DEVICE);
440 if (dma_mapping_error(nfc->dev, dma_addr)) {
441 dev_err(nfc->dev, "Buffer mapping error");
445 writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
446 writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
448 anfc_trigger_op(nfc, &nfc_op);
449 ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
450 dma_unmap_single(nfc->dev, dma_addr, len, DMA_TO_DEVICE);
452 dev_err(nfc->dev, "Error writing page %d\n", page);
456 /* Spare data is not protected */
458 ret = nand_write_oob_std(chip, page);
463 /* NAND framework ->exec_op() hooks and related helpers */
464 static int anfc_parse_instructions(struct nand_chip *chip,
465 const struct nand_subop *subop,
466 struct anfc_op *nfc_op)
468 struct anand *anand = to_anand(chip);
469 const struct nand_op_instr *instr = NULL;
470 bool first_cmd = true;
474 memset(nfc_op, 0, sizeof(*nfc_op));
475 nfc_op->addr2_reg = ADDR2_CS(anand->cs);
476 nfc_op->cmd_reg = CMD_PAGE_SIZE(anand->page_sz);
478 for (op_id = 0; op_id < subop->ninstrs; op_id++) {
479 unsigned int offset, naddrs, pktsize;
483 instr = &subop->instrs[op_id];
485 switch (instr->type) {
486 case NAND_OP_CMD_INSTR:
488 nfc_op->cmd_reg |= CMD_1(instr->ctx.cmd.opcode);
490 nfc_op->cmd_reg |= CMD_2(instr->ctx.cmd.opcode);
495 case NAND_OP_ADDR_INSTR:
496 offset = nand_subop_get_addr_start_off(subop, op_id);
497 naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
498 addrs = &instr->ctx.addr.addrs[offset];
499 nfc_op->cmd_reg |= CMD_NADDRS(naddrs);
501 for (i = 0; i < min(ANFC_MAX_ADDR_CYC, naddrs); i++) {
503 nfc_op->addr1_reg |= (u32)addrs[i] << i * 8;
505 nfc_op->addr2_reg |= addrs[i];
509 case NAND_OP_DATA_IN_INSTR:
512 case NAND_OP_DATA_OUT_INSTR:
513 offset = nand_subop_get_data_start_off(subop, op_id);
514 buf = instr->ctx.data.buf.in;
515 nfc_op->buf = &buf[offset];
516 nfc_op->len = nand_subop_get_data_len(subop, op_id);
517 ret = anfc_pkt_len_config(nfc_op->len, &nfc_op->steps,
523 * Number of DATA cycles must be aligned on 4, this
524 * means the controller might read/write more than
525 * requested. This is harmless most of the time as extra
526 * DATA are discarded in the write path and read pointer
527 * adjusted in the read path.
529 * FIXME: The core should mark operations where
530 * reading/writing more is allowed so the exec_op()
531 * implementation can take the right decision when the
532 * alignment constraint is not met: adjust the number of
533 * DATA cycles when it's allowed, reject the operation
536 nfc_op->pkt_reg |= PKT_SIZE(round_up(pktsize, 4)) |
537 PKT_STEPS(nfc_op->steps);
539 case NAND_OP_WAITRDY_INSTR:
540 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
548 static int anfc_rw_pio_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
550 unsigned int dwords = (nfc_op->len / 4) / nfc_op->steps;
551 unsigned int last_len = nfc_op->len % 4;
552 unsigned int offset, dir;
553 u8 *buf = nfc_op->buf;
556 for (i = 0; i < nfc_op->steps; i++) {
557 dir = nfc_op->read ? READ_READY : WRITE_READY;
558 ret = anfc_wait_for_event(nfc, dir);
560 dev_err(nfc->dev, "PIO %s ready signal not received\n",
561 nfc_op->read ? "Read" : "Write");
565 offset = i * (dwords * 4);
567 ioread32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
570 iowrite32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
577 offset = nfc_op->len - last_len;
580 remainder = readl_relaxed(nfc->base + DATA_PORT_REG);
581 memcpy(&buf[offset], &remainder, last_len);
583 memcpy(&remainder, &buf[offset], last_len);
584 writel_relaxed(remainder, nfc->base + DATA_PORT_REG);
588 return anfc_wait_for_event(nfc, XFER_COMPLETE);
591 static int anfc_misc_data_type_exec(struct nand_chip *chip,
592 const struct nand_subop *subop,
595 struct arasan_nfc *nfc = to_anfc(chip->controller);
596 struct anfc_op nfc_op = {};
599 ret = anfc_parse_instructions(chip, subop, &nfc_op);
603 nfc_op.prog_reg = prog_reg;
604 anfc_trigger_op(nfc, &nfc_op);
606 if (nfc_op.rdy_timeout_ms) {
607 ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
612 return anfc_rw_pio_op(nfc, &nfc_op);
615 static int anfc_param_read_type_exec(struct nand_chip *chip,
616 const struct nand_subop *subop)
618 return anfc_misc_data_type_exec(chip, subop, PROG_RDPARAM);
621 static int anfc_data_read_type_exec(struct nand_chip *chip,
622 const struct nand_subop *subop)
624 return anfc_misc_data_type_exec(chip, subop, PROG_PGRD);
627 static int anfc_param_write_type_exec(struct nand_chip *chip,
628 const struct nand_subop *subop)
630 return anfc_misc_data_type_exec(chip, subop, PROG_SET_FEATURE);
633 static int anfc_data_write_type_exec(struct nand_chip *chip,
634 const struct nand_subop *subop)
636 return anfc_misc_data_type_exec(chip, subop, PROG_PGPROG);
639 static int anfc_misc_zerolen_type_exec(struct nand_chip *chip,
640 const struct nand_subop *subop,
643 struct arasan_nfc *nfc = to_anfc(chip->controller);
644 struct anfc_op nfc_op = {};
647 ret = anfc_parse_instructions(chip, subop, &nfc_op);
651 nfc_op.prog_reg = prog_reg;
652 anfc_trigger_op(nfc, &nfc_op);
654 ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
658 if (nfc_op.rdy_timeout_ms)
659 ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
664 static int anfc_status_type_exec(struct nand_chip *chip,
665 const struct nand_subop *subop)
667 struct arasan_nfc *nfc = to_anfc(chip->controller);
671 /* See anfc_check_op() for details about this constraint */
672 if (subop->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS)
675 ret = anfc_misc_zerolen_type_exec(chip, subop, PROG_STATUS);
679 tmp = readl_relaxed(nfc->base + FLASH_STS_REG);
680 memcpy(subop->instrs[1].ctx.data.buf.in, &tmp, 1);
685 static int anfc_reset_type_exec(struct nand_chip *chip,
686 const struct nand_subop *subop)
688 return anfc_misc_zerolen_type_exec(chip, subop, PROG_RST);
691 static int anfc_erase_type_exec(struct nand_chip *chip,
692 const struct nand_subop *subop)
694 return anfc_misc_zerolen_type_exec(chip, subop, PROG_ERASE);
697 static int anfc_wait_type_exec(struct nand_chip *chip,
698 const struct nand_subop *subop)
700 struct arasan_nfc *nfc = to_anfc(chip->controller);
701 struct anfc_op nfc_op = {};
704 ret = anfc_parse_instructions(chip, subop, &nfc_op);
708 return anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
711 static const struct nand_op_parser anfc_op_parser = NAND_OP_PARSER(
712 NAND_OP_PARSER_PATTERN(
713 anfc_param_read_type_exec,
714 NAND_OP_PARSER_PAT_CMD_ELEM(false),
715 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
716 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
717 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
718 NAND_OP_PARSER_PATTERN(
719 anfc_param_write_type_exec,
720 NAND_OP_PARSER_PAT_CMD_ELEM(false),
721 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
722 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_PARAM_SIZE)),
723 NAND_OP_PARSER_PATTERN(
724 anfc_data_read_type_exec,
725 NAND_OP_PARSER_PAT_CMD_ELEM(false),
726 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
727 NAND_OP_PARSER_PAT_CMD_ELEM(false),
728 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
729 NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, ANFC_MAX_CHUNK_SIZE)),
730 NAND_OP_PARSER_PATTERN(
731 anfc_data_write_type_exec,
732 NAND_OP_PARSER_PAT_CMD_ELEM(false),
733 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
734 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE),
735 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
736 NAND_OP_PARSER_PATTERN(
737 anfc_reset_type_exec,
738 NAND_OP_PARSER_PAT_CMD_ELEM(false),
739 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
740 NAND_OP_PARSER_PATTERN(
741 anfc_erase_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(false)),
746 NAND_OP_PARSER_PATTERN(
747 anfc_status_type_exec,
748 NAND_OP_PARSER_PAT_CMD_ELEM(false),
749 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
750 NAND_OP_PARSER_PATTERN(
752 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
755 static int anfc_select_target(struct nand_chip *chip, int target)
757 struct anand *anand = to_anand(chip);
758 struct arasan_nfc *nfc = to_anfc(chip->controller);
761 /* Update the controller timings and the potential ECC configuration */
762 writel_relaxed(anand->timings, nfc->base + DATA_INTERFACE_REG);
764 /* Update clock frequency */
765 if (nfc->cur_clk != anand->clk) {
766 clk_disable_unprepare(nfc->controller_clk);
767 ret = clk_set_rate(nfc->controller_clk, anand->clk);
769 dev_err(nfc->dev, "Failed to change clock rate\n");
773 ret = clk_prepare_enable(nfc->controller_clk);
776 "Failed to re-enable the controller clock\n");
780 nfc->cur_clk = anand->clk;
786 static int anfc_check_op(struct nand_chip *chip,
787 const struct nand_operation *op)
789 const struct nand_op_instr *instr;
793 * The controller abstracts all the NAND operations and do not support
794 * data only operations.
796 * TODO: The nand_op_parser framework should be extended to
797 * support custom checks on DATA instructions.
799 for (op_id = 0; op_id < op->ninstrs; op_id++) {
800 instr = &op->instrs[op_id];
802 switch (instr->type) {
803 case NAND_OP_ADDR_INSTR:
804 if (instr->ctx.addr.naddrs > ANFC_MAX_ADDR_CYC)
808 case NAND_OP_DATA_IN_INSTR:
809 case NAND_OP_DATA_OUT_INSTR:
810 if (instr->ctx.data.len > ANFC_MAX_CHUNK_SIZE)
813 if (anfc_pkt_len_config(instr->ctx.data.len, 0, 0))
823 * The controller does not allow to proceed with a CMD+DATA_IN cycle
824 * manually on the bus by reading data from the data register. Instead,
825 * the controller abstract a status read operation with its own status
826 * register after ordering a read status operation. Hence, we cannot
827 * support any CMD+DATA_IN operation other than a READ STATUS.
829 * TODO: The nand_op_parser() framework should be extended to describe
830 * fixed patterns instead of open-coding this check here.
832 if (op->ninstrs == 2 &&
833 op->instrs[0].type == NAND_OP_CMD_INSTR &&
834 op->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS &&
835 op->instrs[1].type == NAND_OP_DATA_IN_INSTR)
838 return nand_op_parser_exec_op(chip, &anfc_op_parser, op, true);
841 static int anfc_exec_op(struct nand_chip *chip,
842 const struct nand_operation *op,
848 return anfc_check_op(chip, op);
850 ret = anfc_select_target(chip, op->cs);
854 return nand_op_parser_exec_op(chip, &anfc_op_parser, op, check_only);
857 static int anfc_setup_interface(struct nand_chip *chip, int target,
858 const struct nand_interface_config *conf)
860 struct anand *anand = to_anand(chip);
861 struct arasan_nfc *nfc = to_anfc(chip->controller);
862 struct device_node *np = nfc->dev->of_node;
867 anand->timings = DIFACE_SDR | DIFACE_SDR_MODE(conf->timings.mode);
868 anand->clk = ANFC_XLNX_SDR_DFLT_CORE_CLK;
871 * Due to a hardware bug in the ZynqMP SoC, SDR timing modes 0-1 work
872 * with f > 90MHz (default clock is 100MHz) but signals are unstable
873 * with higher modes. Hence we decrease a little bit the clock rate to
874 * 80MHz when using modes 2-5 with this SoC.
876 if (of_device_is_compatible(np, "xlnx,zynqmp-nand-controller") &&
877 conf->timings.mode >= 2)
878 anand->clk = ANFC_XLNX_SDR_HS_CORE_CLK;
883 static int anfc_calc_hw_ecc_bytes(int step_size, int strength)
885 unsigned int bch_gf_mag, ecc_bits;
898 ecc_bits = bch_gf_mag * strength;
900 return DIV_ROUND_UP(ecc_bits, 8);
903 static const int anfc_hw_ecc_512_strengths[] = {4, 8, 12};
905 static const int anfc_hw_ecc_1024_strengths[] = {24};
907 static const struct nand_ecc_step_info anfc_hw_ecc_step_infos[] = {
910 .strengths = anfc_hw_ecc_512_strengths,
911 .nstrengths = ARRAY_SIZE(anfc_hw_ecc_512_strengths),
915 .strengths = anfc_hw_ecc_1024_strengths,
916 .nstrengths = ARRAY_SIZE(anfc_hw_ecc_1024_strengths),
920 static const struct nand_ecc_caps anfc_hw_ecc_caps = {
921 .stepinfos = anfc_hw_ecc_step_infos,
922 .nstepinfos = ARRAY_SIZE(anfc_hw_ecc_step_infos),
923 .calc_ecc_bytes = anfc_calc_hw_ecc_bytes,
926 static int anfc_init_hw_ecc_controller(struct arasan_nfc *nfc,
927 struct nand_chip *chip)
929 struct anand *anand = to_anand(chip);
930 struct mtd_info *mtd = nand_to_mtd(chip);
931 struct nand_ecc_ctrl *ecc = &chip->ecc;
932 unsigned int bch_prim_poly = 0, bch_gf_mag = 0, ecc_offset;
935 switch (mtd->writesize) {
943 dev_err(nfc->dev, "Unsupported page size %d\n", mtd->writesize);
947 ret = nand_ecc_choose_conf(chip, &anfc_hw_ecc_caps, mtd->oobsize);
951 switch (ecc->strength) {
953 anand->strength = 0x1;
956 anand->strength = 0x2;
959 anand->strength = 0x3;
962 anand->strength = 0x4;
965 dev_err(nfc->dev, "Unsupported strength %d\n", ecc->strength);
972 bch_prim_poly = 0x201b;
976 bch_prim_poly = 0x4443;
979 dev_err(nfc->dev, "Unsupported step size %d\n", ecc->strength);
983 mtd_set_ooblayout(mtd, &nand_ooblayout_lp_ops);
985 ecc->steps = mtd->writesize / ecc->size;
986 ecc->algo = NAND_ECC_ALGO_BCH;
987 anand->ecc_bits = bch_gf_mag * ecc->strength;
988 ecc->bytes = DIV_ROUND_UP(anand->ecc_bits, 8);
989 anand->ecc_total = DIV_ROUND_UP(anand->ecc_bits * ecc->steps, 8);
990 ecc_offset = mtd->writesize + mtd->oobsize - anand->ecc_total;
991 anand->ecc_conf = ECC_CONF_COL(ecc_offset) |
992 ECC_CONF_LEN(anand->ecc_total) |
995 anand->errloc = devm_kmalloc_array(nfc->dev, ecc->strength,
996 sizeof(*anand->errloc), GFP_KERNEL);
1000 anand->hw_ecc = devm_kmalloc(nfc->dev, ecc->bytes, GFP_KERNEL);
1004 /* Enforce bit swapping to fit the hardware */
1005 anand->bch = bch_init(bch_gf_mag, ecc->strength, bch_prim_poly, true);
1009 ecc->read_page = anfc_read_page_hw_ecc;
1010 ecc->write_page = anfc_write_page_hw_ecc;
1015 static int anfc_attach_chip(struct nand_chip *chip)
1017 struct anand *anand = to_anand(chip);
1018 struct arasan_nfc *nfc = to_anfc(chip->controller);
1019 struct mtd_info *mtd = nand_to_mtd(chip);
1022 if (mtd->writesize <= SZ_512)
1023 anand->caddr_cycles = 1;
1025 anand->caddr_cycles = 2;
1027 if (chip->options & NAND_ROW_ADDR_3)
1028 anand->raddr_cycles = 3;
1030 anand->raddr_cycles = 2;
1032 switch (mtd->writesize) {
1055 /* These hooks are valid for all ECC providers */
1056 chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
1057 chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
1059 switch (chip->ecc.engine_type) {
1060 case NAND_ECC_ENGINE_TYPE_NONE:
1061 case NAND_ECC_ENGINE_TYPE_SOFT:
1062 case NAND_ECC_ENGINE_TYPE_ON_DIE:
1064 case NAND_ECC_ENGINE_TYPE_ON_HOST:
1065 ret = anfc_init_hw_ecc_controller(nfc, chip);
1068 dev_err(nfc->dev, "Unsupported ECC mode: %d\n",
1069 chip->ecc.engine_type);
1076 static void anfc_detach_chip(struct nand_chip *chip)
1078 struct anand *anand = to_anand(chip);
1081 bch_free(anand->bch);
1084 static const struct nand_controller_ops anfc_ops = {
1085 .exec_op = anfc_exec_op,
1086 .setup_interface = anfc_setup_interface,
1087 .attach_chip = anfc_attach_chip,
1088 .detach_chip = anfc_detach_chip,
1091 static int anfc_chip_init(struct arasan_nfc *nfc, struct device_node *np)
1093 struct anand *anand;
1094 struct nand_chip *chip;
1095 struct mtd_info *mtd;
1098 anand = devm_kzalloc(nfc->dev, sizeof(*anand), GFP_KERNEL);
1102 /* We do not support multiple CS per chip yet */
1103 if (of_property_count_elems_of_size(np, "reg", sizeof(u32)) != 1) {
1104 dev_err(nfc->dev, "Invalid reg property\n");
1108 ret = of_property_read_u32(np, "reg", &cs);
1112 ret = of_property_read_u32(np, "nand-rb", &rb);
1116 if (cs >= ANFC_MAX_CS || rb >= ANFC_MAX_CS) {
1117 dev_err(nfc->dev, "Wrong CS %d or RB %d\n", cs, rb);
1121 if (test_and_set_bit(cs, &nfc->assigned_cs)) {
1122 dev_err(nfc->dev, "Already assigned CS %d\n", cs);
1129 chip = &anand->chip;
1130 mtd = nand_to_mtd(chip);
1131 mtd->dev.parent = nfc->dev;
1132 chip->controller = &nfc->controller;
1133 chip->options = NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
1136 nand_set_flash_node(chip, np);
1138 dev_err(nfc->dev, "NAND label property is mandatory\n");
1142 ret = nand_scan(chip, 1);
1144 dev_err(nfc->dev, "Scan operation failed\n");
1148 ret = mtd_device_register(mtd, NULL, 0);
1154 list_add_tail(&anand->node, &nfc->chips);
1159 static void anfc_chips_cleanup(struct arasan_nfc *nfc)
1161 struct anand *anand, *tmp;
1162 struct nand_chip *chip;
1165 list_for_each_entry_safe(anand, tmp, &nfc->chips, node) {
1166 chip = &anand->chip;
1167 ret = mtd_device_unregister(nand_to_mtd(chip));
1170 list_del(&anand->node);
1174 static int anfc_chips_init(struct arasan_nfc *nfc)
1176 struct device_node *np = nfc->dev->of_node, *nand_np;
1177 int nchips = of_get_child_count(np);
1180 if (!nchips || nchips > ANFC_MAX_CS) {
1181 dev_err(nfc->dev, "Incorrect number of NAND chips (%d)\n",
1186 for_each_child_of_node(np, nand_np) {
1187 ret = anfc_chip_init(nfc, nand_np);
1189 of_node_put(nand_np);
1190 anfc_chips_cleanup(nfc);
1198 static void anfc_reset(struct arasan_nfc *nfc)
1200 /* Disable interrupt signals */
1201 writel_relaxed(0, nfc->base + INTR_SIG_EN_REG);
1203 /* Enable interrupt status */
1204 writel_relaxed(EVENT_MASK, nfc->base + INTR_STS_EN_REG);
1207 static int anfc_probe(struct platform_device *pdev)
1209 struct arasan_nfc *nfc;
1212 nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
1216 nfc->dev = &pdev->dev;
1217 nand_controller_init(&nfc->controller);
1218 nfc->controller.ops = &anfc_ops;
1219 INIT_LIST_HEAD(&nfc->chips);
1221 nfc->base = devm_platform_ioremap_resource(pdev, 0);
1222 if (IS_ERR(nfc->base))
1223 return PTR_ERR(nfc->base);
1227 nfc->controller_clk = devm_clk_get(&pdev->dev, "controller");
1228 if (IS_ERR(nfc->controller_clk))
1229 return PTR_ERR(nfc->controller_clk);
1231 nfc->bus_clk = devm_clk_get(&pdev->dev, "bus");
1232 if (IS_ERR(nfc->bus_clk))
1233 return PTR_ERR(nfc->bus_clk);
1235 ret = clk_prepare_enable(nfc->controller_clk);
1239 ret = clk_prepare_enable(nfc->bus_clk);
1241 goto disable_controller_clk;
1243 ret = anfc_chips_init(nfc);
1245 goto disable_bus_clk;
1247 platform_set_drvdata(pdev, nfc);
1252 clk_disable_unprepare(nfc->bus_clk);
1254 disable_controller_clk:
1255 clk_disable_unprepare(nfc->controller_clk);
1260 static int anfc_remove(struct platform_device *pdev)
1262 struct arasan_nfc *nfc = platform_get_drvdata(pdev);
1264 anfc_chips_cleanup(nfc);
1266 clk_disable_unprepare(nfc->bus_clk);
1267 clk_disable_unprepare(nfc->controller_clk);
1272 static const struct of_device_id anfc_ids[] = {
1274 .compatible = "xlnx,zynqmp-nand-controller",
1277 .compatible = "arasan,nfc-v3p10",
1281 MODULE_DEVICE_TABLE(of, anfc_ids);
1283 static struct platform_driver anfc_driver = {
1285 .name = "arasan-nand-controller",
1286 .of_match_table = anfc_ids,
1288 .probe = anfc_probe,
1289 .remove = anfc_remove,
1291 module_platform_driver(anfc_driver);
1293 MODULE_LICENSE("GPL v2");
1294 MODULE_AUTHOR("Punnaiah Choudary Kalluri <punnaia@xilinx.com>");
1295 MODULE_AUTHOR("Naga Sureshkumar Relli <nagasure@xilinx.com>");
1296 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
1297 MODULE_DESCRIPTION("Arasan NAND Flash Controller Driver");