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)
275 static int anfc_select_target(struct nand_chip *chip, int target)
277 struct anand *anand = to_anand(chip);
278 struct arasan_nfc *nfc = to_anfc(chip->controller);
281 /* Update the controller timings and the potential ECC configuration */
282 writel_relaxed(anand->timings, nfc->base + DATA_INTERFACE_REG);
284 /* Update clock frequency */
285 if (nfc->cur_clk != anand->clk) {
286 clk_disable_unprepare(nfc->bus_clk);
287 ret = clk_set_rate(nfc->bus_clk, anand->clk);
289 dev_err(nfc->dev, "Failed to change clock rate\n");
293 ret = clk_prepare_enable(nfc->bus_clk);
296 "Failed to re-enable the bus clock\n");
300 nfc->cur_clk = anand->clk;
307 * When using the embedded hardware ECC engine, the controller is in charge of
308 * feeding the engine with, first, the ECC residue present in the data array.
309 * A typical read operation is:
310 * 1/ Assert the read operation by sending the relevant command/address cycles
311 * but targeting the column of the first ECC bytes in the OOB area instead of
312 * the main data directly.
313 * 2/ After having read the relevant number of ECC bytes, the controller uses
314 * the RNDOUT/RNDSTART commands which are set into the "ECC Spare Command
315 * Register" to move the pointer back at the beginning of the main data.
316 * 3/ It will read the content of the main area for a given size (pktsize) and
317 * will feed the ECC engine with this buffer again.
318 * 4/ The ECC engine derives the ECC bytes for the given data and compare them
319 * with the ones already received. It eventually trigger status flags and
320 * then set the "Buffer Read Ready" flag.
321 * 5/ The corrected data is then available for reading from the data port
324 * The hardware BCH ECC engine is known to be inconstent in BCH mode and never
325 * reports uncorrectable errors. Because of this bug, we have to use the
326 * software BCH implementation in the read path.
328 static int anfc_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
329 int oob_required, int page)
331 struct arasan_nfc *nfc = to_anfc(chip->controller);
332 struct mtd_info *mtd = nand_to_mtd(chip);
333 struct anand *anand = to_anand(chip);
334 unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
335 unsigned int max_bitflips = 0;
338 struct anfc_op nfc_op = {
340 PKT_SIZE(chip->ecc.size) |
341 PKT_STEPS(chip->ecc.steps),
343 (page & 0xFF) << (8 * (anand->caddr_cycles)) |
344 (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
346 ((page >> 16) & 0xFF) |
347 ADDR2_STRENGTH(anand->strength) |
350 CMD_1(NAND_CMD_READ0) |
351 CMD_2(NAND_CMD_READSTART) |
352 CMD_PAGE_SIZE(anand->page_sz) |
354 CMD_NADDRS(anand->caddr_cycles +
355 anand->raddr_cycles),
356 .prog_reg = PROG_PGRD,
359 dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_FROM_DEVICE);
360 if (dma_mapping_error(nfc->dev, dma_addr)) {
361 dev_err(nfc->dev, "Buffer mapping error");
365 writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
366 writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
368 anfc_trigger_op(nfc, &nfc_op);
370 ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
371 dma_unmap_single(nfc->dev, dma_addr, len, DMA_FROM_DEVICE);
373 dev_err(nfc->dev, "Error reading page %d\n", page);
377 /* Store the raw OOB bytes as well */
378 ret = nand_change_read_column_op(chip, mtd->writesize, chip->oob_poi,
384 * For each step, compute by softare the BCH syndrome over the raw data.
385 * Compare the theoretical amount of errors and compare with the
386 * hardware engine feedback.
388 for (step = 0; step < chip->ecc.steps; step++) {
389 u8 *raw_buf = &buf[step * chip->ecc.size];
390 unsigned int bit, byte;
393 /* Extract the syndrome, it is not necessarily aligned */
394 memset(anand->hw_ecc, 0, chip->ecc.bytes);
395 nand_extract_bits(anand->hw_ecc, 0,
396 &chip->oob_poi[mtd->oobsize - anand->ecc_total],
397 anand->ecc_bits * step, anand->ecc_bits);
399 bf = bch_decode(anand->bch, raw_buf, chip->ecc.size,
400 anand->hw_ecc, NULL, NULL, anand->errloc);
404 for (i = 0; i < bf; i++) {
405 /* Only correct the data, not the syndrome */
406 if (anand->errloc[i] < (chip->ecc.size * 8)) {
407 bit = BIT(anand->errloc[i] & 7);
408 byte = anand->errloc[i] >> 3;
409 raw_buf[byte] ^= bit;
413 mtd->ecc_stats.corrected += bf;
414 max_bitflips = max_t(unsigned int, max_bitflips, bf);
419 bf = nand_check_erased_ecc_chunk(raw_buf, chip->ecc.size,
423 mtd->ecc_stats.corrected += bf;
424 max_bitflips = max_t(unsigned int, max_bitflips, bf);
425 memset(raw_buf, 0xFF, chip->ecc.size);
427 mtd->ecc_stats.failed++;
434 static int anfc_sel_read_page_hw_ecc(struct nand_chip *chip, u8 *buf,
435 int oob_required, int page)
439 ret = anfc_select_target(chip, chip->cur_cs);
443 return anfc_read_page_hw_ecc(chip, buf, oob_required, page);
446 static int anfc_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
447 int oob_required, int page)
449 struct anand *anand = to_anand(chip);
450 struct arasan_nfc *nfc = to_anfc(chip->controller);
451 struct mtd_info *mtd = nand_to_mtd(chip);
452 unsigned int len = mtd->writesize + (oob_required ? mtd->oobsize : 0);
455 struct anfc_op nfc_op = {
457 PKT_SIZE(chip->ecc.size) |
458 PKT_STEPS(chip->ecc.steps),
460 (page & 0xFF) << (8 * (anand->caddr_cycles)) |
461 (((page >> 8) & 0xFF) << (8 * (1 + anand->caddr_cycles))),
463 ((page >> 16) & 0xFF) |
464 ADDR2_STRENGTH(anand->strength) |
467 CMD_1(NAND_CMD_SEQIN) |
468 CMD_2(NAND_CMD_PAGEPROG) |
469 CMD_PAGE_SIZE(anand->page_sz) |
471 CMD_NADDRS(anand->caddr_cycles +
472 anand->raddr_cycles) |
474 .prog_reg = PROG_PGPROG,
477 writel_relaxed(anand->ecc_conf, nfc->base + ECC_CONF_REG);
478 writel_relaxed(ECC_SP_CMD1(NAND_CMD_RNDIN) |
479 ECC_SP_ADDRS(anand->caddr_cycles),
480 nfc->base + ECC_SP_REG);
482 dma_addr = dma_map_single(nfc->dev, (void *)buf, len, DMA_TO_DEVICE);
483 if (dma_mapping_error(nfc->dev, dma_addr)) {
484 dev_err(nfc->dev, "Buffer mapping error");
488 writel_relaxed(lower_32_bits(dma_addr), nfc->base + DMA_ADDR0_REG);
489 writel_relaxed(upper_32_bits(dma_addr), nfc->base + DMA_ADDR1_REG);
491 anfc_trigger_op(nfc, &nfc_op);
492 ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
493 dma_unmap_single(nfc->dev, dma_addr, len, DMA_TO_DEVICE);
495 dev_err(nfc->dev, "Error writing page %d\n", page);
499 /* Spare data is not protected */
501 ret = nand_write_oob_std(chip, page);
506 static int anfc_sel_write_page_hw_ecc(struct nand_chip *chip, const u8 *buf,
507 int oob_required, int page)
511 ret = anfc_select_target(chip, chip->cur_cs);
515 return anfc_write_page_hw_ecc(chip, buf, oob_required, page);
518 /* NAND framework ->exec_op() hooks and related helpers */
519 static int anfc_parse_instructions(struct nand_chip *chip,
520 const struct nand_subop *subop,
521 struct anfc_op *nfc_op)
523 struct anand *anand = to_anand(chip);
524 const struct nand_op_instr *instr = NULL;
525 bool first_cmd = true;
529 memset(nfc_op, 0, sizeof(*nfc_op));
530 nfc_op->addr2_reg = ADDR2_CS(anand->cs);
531 nfc_op->cmd_reg = CMD_PAGE_SIZE(anand->page_sz);
533 for (op_id = 0; op_id < subop->ninstrs; op_id++) {
534 unsigned int offset, naddrs, pktsize;
538 instr = &subop->instrs[op_id];
540 switch (instr->type) {
541 case NAND_OP_CMD_INSTR:
543 nfc_op->cmd_reg |= CMD_1(instr->ctx.cmd.opcode);
545 nfc_op->cmd_reg |= CMD_2(instr->ctx.cmd.opcode);
550 case NAND_OP_ADDR_INSTR:
551 offset = nand_subop_get_addr_start_off(subop, op_id);
552 naddrs = nand_subop_get_num_addr_cyc(subop, op_id);
553 addrs = &instr->ctx.addr.addrs[offset];
554 nfc_op->cmd_reg |= CMD_NADDRS(naddrs);
556 for (i = 0; i < min(ANFC_MAX_ADDR_CYC, naddrs); i++) {
558 nfc_op->addr1_reg |= (u32)addrs[i] << i * 8;
560 nfc_op->addr2_reg |= addrs[i];
564 case NAND_OP_DATA_IN_INSTR:
567 case NAND_OP_DATA_OUT_INSTR:
568 offset = nand_subop_get_data_start_off(subop, op_id);
569 buf = instr->ctx.data.buf.in;
570 nfc_op->buf = &buf[offset];
571 nfc_op->len = nand_subop_get_data_len(subop, op_id);
572 ret = anfc_pkt_len_config(nfc_op->len, &nfc_op->steps,
578 * Number of DATA cycles must be aligned on 4, this
579 * means the controller might read/write more than
580 * requested. This is harmless most of the time as extra
581 * DATA are discarded in the write path and read pointer
582 * adjusted in the read path.
584 * FIXME: The core should mark operations where
585 * reading/writing more is allowed so the exec_op()
586 * implementation can take the right decision when the
587 * alignment constraint is not met: adjust the number of
588 * DATA cycles when it's allowed, reject the operation
591 nfc_op->pkt_reg |= PKT_SIZE(round_up(pktsize, 4)) |
592 PKT_STEPS(nfc_op->steps);
594 case NAND_OP_WAITRDY_INSTR:
595 nfc_op->rdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
603 static int anfc_rw_pio_op(struct arasan_nfc *nfc, struct anfc_op *nfc_op)
605 unsigned int dwords = (nfc_op->len / 4) / nfc_op->steps;
606 unsigned int last_len = nfc_op->len % 4;
607 unsigned int offset, dir;
608 u8 *buf = nfc_op->buf;
611 for (i = 0; i < nfc_op->steps; i++) {
612 dir = nfc_op->read ? READ_READY : WRITE_READY;
613 ret = anfc_wait_for_event(nfc, dir);
615 dev_err(nfc->dev, "PIO %s ready signal not received\n",
616 nfc_op->read ? "Read" : "Write");
620 offset = i * (dwords * 4);
622 ioread32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
625 iowrite32_rep(nfc->base + DATA_PORT_REG, &buf[offset],
632 offset = nfc_op->len - last_len;
635 remainder = readl_relaxed(nfc->base + DATA_PORT_REG);
636 memcpy(&buf[offset], &remainder, last_len);
638 memcpy(&remainder, &buf[offset], last_len);
639 writel_relaxed(remainder, nfc->base + DATA_PORT_REG);
643 return anfc_wait_for_event(nfc, XFER_COMPLETE);
646 static int anfc_misc_data_type_exec(struct nand_chip *chip,
647 const struct nand_subop *subop,
650 struct arasan_nfc *nfc = to_anfc(chip->controller);
651 struct anfc_op nfc_op = {};
654 ret = anfc_parse_instructions(chip, subop, &nfc_op);
658 nfc_op.prog_reg = prog_reg;
659 anfc_trigger_op(nfc, &nfc_op);
661 if (nfc_op.rdy_timeout_ms) {
662 ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
667 return anfc_rw_pio_op(nfc, &nfc_op);
670 static int anfc_param_read_type_exec(struct nand_chip *chip,
671 const struct nand_subop *subop)
673 return anfc_misc_data_type_exec(chip, subop, PROG_RDPARAM);
676 static int anfc_data_read_type_exec(struct nand_chip *chip,
677 const struct nand_subop *subop)
679 return anfc_misc_data_type_exec(chip, subop, PROG_PGRD);
682 static int anfc_param_write_type_exec(struct nand_chip *chip,
683 const struct nand_subop *subop)
685 return anfc_misc_data_type_exec(chip, subop, PROG_SET_FEATURE);
688 static int anfc_data_write_type_exec(struct nand_chip *chip,
689 const struct nand_subop *subop)
691 return anfc_misc_data_type_exec(chip, subop, PROG_PGPROG);
694 static int anfc_misc_zerolen_type_exec(struct nand_chip *chip,
695 const struct nand_subop *subop,
698 struct arasan_nfc *nfc = to_anfc(chip->controller);
699 struct anfc_op nfc_op = {};
702 ret = anfc_parse_instructions(chip, subop, &nfc_op);
706 nfc_op.prog_reg = prog_reg;
707 anfc_trigger_op(nfc, &nfc_op);
709 ret = anfc_wait_for_event(nfc, XFER_COMPLETE);
713 if (nfc_op.rdy_timeout_ms)
714 ret = anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
719 static int anfc_status_type_exec(struct nand_chip *chip,
720 const struct nand_subop *subop)
722 struct arasan_nfc *nfc = to_anfc(chip->controller);
726 /* See anfc_check_op() for details about this constraint */
727 if (subop->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS)
730 ret = anfc_misc_zerolen_type_exec(chip, subop, PROG_STATUS);
734 tmp = readl_relaxed(nfc->base + FLASH_STS_REG);
735 memcpy(subop->instrs[1].ctx.data.buf.in, &tmp, 1);
740 static int anfc_reset_type_exec(struct nand_chip *chip,
741 const struct nand_subop *subop)
743 return anfc_misc_zerolen_type_exec(chip, subop, PROG_RST);
746 static int anfc_erase_type_exec(struct nand_chip *chip,
747 const struct nand_subop *subop)
749 return anfc_misc_zerolen_type_exec(chip, subop, PROG_ERASE);
752 static int anfc_wait_type_exec(struct nand_chip *chip,
753 const struct nand_subop *subop)
755 struct arasan_nfc *nfc = to_anfc(chip->controller);
756 struct anfc_op nfc_op = {};
759 ret = anfc_parse_instructions(chip, subop, &nfc_op);
763 return anfc_wait_for_rb(nfc, chip, nfc_op.rdy_timeout_ms);
766 static const struct nand_op_parser anfc_op_parser = NAND_OP_PARSER(
767 NAND_OP_PARSER_PATTERN(
768 anfc_param_read_type_exec,
769 NAND_OP_PARSER_PAT_CMD_ELEM(false),
770 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
771 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
772 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
773 NAND_OP_PARSER_PATTERN(
774 anfc_param_write_type_exec,
775 NAND_OP_PARSER_PAT_CMD_ELEM(false),
776 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
777 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_PARAM_SIZE)),
778 NAND_OP_PARSER_PATTERN(
779 anfc_data_read_type_exec,
780 NAND_OP_PARSER_PAT_CMD_ELEM(false),
781 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
782 NAND_OP_PARSER_PAT_CMD_ELEM(false),
783 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
784 NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, ANFC_MAX_CHUNK_SIZE)),
785 NAND_OP_PARSER_PATTERN(
786 anfc_data_write_type_exec,
787 NAND_OP_PARSER_PAT_CMD_ELEM(false),
788 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
789 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE),
790 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
791 NAND_OP_PARSER_PATTERN(
792 anfc_reset_type_exec,
793 NAND_OP_PARSER_PAT_CMD_ELEM(false),
794 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
795 NAND_OP_PARSER_PATTERN(
796 anfc_erase_type_exec,
797 NAND_OP_PARSER_PAT_CMD_ELEM(false),
798 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC),
799 NAND_OP_PARSER_PAT_CMD_ELEM(false),
800 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
801 NAND_OP_PARSER_PATTERN(
802 anfc_status_type_exec,
803 NAND_OP_PARSER_PAT_CMD_ELEM(false),
804 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE)),
805 NAND_OP_PARSER_PATTERN(
807 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
810 static int anfc_check_op(struct nand_chip *chip,
811 const struct nand_operation *op)
813 const struct nand_op_instr *instr;
817 * The controller abstracts all the NAND operations and do not support
818 * data only operations.
820 * TODO: The nand_op_parser framework should be extended to
821 * support custom checks on DATA instructions.
823 for (op_id = 0; op_id < op->ninstrs; op_id++) {
824 instr = &op->instrs[op_id];
826 switch (instr->type) {
827 case NAND_OP_ADDR_INSTR:
828 if (instr->ctx.addr.naddrs > ANFC_MAX_ADDR_CYC)
832 case NAND_OP_DATA_IN_INSTR:
833 case NAND_OP_DATA_OUT_INSTR:
834 if (instr->ctx.data.len > ANFC_MAX_CHUNK_SIZE)
837 if (anfc_pkt_len_config(instr->ctx.data.len, 0, 0))
847 * The controller does not allow to proceed with a CMD+DATA_IN cycle
848 * manually on the bus by reading data from the data register. Instead,
849 * the controller abstract a status read operation with its own status
850 * register after ordering a read status operation. Hence, we cannot
851 * support any CMD+DATA_IN operation other than a READ STATUS.
853 * TODO: The nand_op_parser() framework should be extended to describe
854 * fixed patterns instead of open-coding this check here.
856 if (op->ninstrs == 2 &&
857 op->instrs[0].type == NAND_OP_CMD_INSTR &&
858 op->instrs[0].ctx.cmd.opcode != NAND_CMD_STATUS &&
859 op->instrs[1].type == NAND_OP_DATA_IN_INSTR)
862 return nand_op_parser_exec_op(chip, &anfc_op_parser, op, true);
865 static int anfc_exec_op(struct nand_chip *chip,
866 const struct nand_operation *op,
872 return anfc_check_op(chip, op);
874 ret = anfc_select_target(chip, op->cs);
878 return nand_op_parser_exec_op(chip, &anfc_op_parser, op, check_only);
881 static int anfc_setup_interface(struct nand_chip *chip, int target,
882 const struct nand_interface_config *conf)
884 struct anand *anand = to_anand(chip);
885 struct arasan_nfc *nfc = to_anfc(chip->controller);
886 struct device_node *np = nfc->dev->of_node;
887 const struct nand_sdr_timings *sdr;
888 const struct nand_nvddr_timings *nvddr;
890 if (nand_interface_is_nvddr(conf)) {
891 nvddr = nand_get_nvddr_timings(conf);
893 return PTR_ERR(nvddr);
896 * The controller only supports data payload requests which are
897 * a multiple of 4. In practice, most data accesses are 4-byte
898 * aligned and this is not an issue. However, rounding up will
899 * simply be refused by the controller if we reached the end of
900 * the device *and* we are using the NV-DDR interface(!). In
901 * this situation, unaligned data requests ending at the device
902 * boundary will confuse the controller and cannot be performed.
904 * This is something that happens in nand_read_subpage() when
905 * selecting software ECC support and must be avoided.
907 if (chip->ecc.engine_type == NAND_ECC_ENGINE_TYPE_SOFT)
910 sdr = nand_get_sdr_timings(conf);
918 if (nand_interface_is_sdr(conf))
919 anand->timings = DIFACE_SDR |
920 DIFACE_SDR_MODE(conf->timings.mode);
922 anand->timings = DIFACE_NVDDR |
923 DIFACE_DDR_MODE(conf->timings.mode);
925 if (nand_interface_is_sdr(conf)) {
926 anand->clk = ANFC_XLNX_SDR_DFLT_CORE_CLK;
928 /* ONFI timings are defined in picoseconds */
929 anand->clk = div_u64((u64)NSEC_PER_SEC * 1000,
930 conf->timings.nvddr.tCK_min);
934 * Due to a hardware bug in the ZynqMP SoC, SDR timing modes 0-1 work
935 * with f > 90MHz (default clock is 100MHz) but signals are unstable
936 * with higher modes. Hence we decrease a little bit the clock rate to
937 * 80MHz when using SDR modes 2-5 with this SoC.
939 if (of_device_is_compatible(np, "xlnx,zynqmp-nand-controller") &&
940 nand_interface_is_sdr(conf) && conf->timings.mode >= 2)
941 anand->clk = ANFC_XLNX_SDR_HS_CORE_CLK;
946 static int anfc_calc_hw_ecc_bytes(int step_size, int strength)
948 unsigned int bch_gf_mag, ecc_bits;
961 ecc_bits = bch_gf_mag * strength;
963 return DIV_ROUND_UP(ecc_bits, 8);
966 static const int anfc_hw_ecc_512_strengths[] = {4, 8, 12};
968 static const int anfc_hw_ecc_1024_strengths[] = {24};
970 static const struct nand_ecc_step_info anfc_hw_ecc_step_infos[] = {
973 .strengths = anfc_hw_ecc_512_strengths,
974 .nstrengths = ARRAY_SIZE(anfc_hw_ecc_512_strengths),
978 .strengths = anfc_hw_ecc_1024_strengths,
979 .nstrengths = ARRAY_SIZE(anfc_hw_ecc_1024_strengths),
983 static const struct nand_ecc_caps anfc_hw_ecc_caps = {
984 .stepinfos = anfc_hw_ecc_step_infos,
985 .nstepinfos = ARRAY_SIZE(anfc_hw_ecc_step_infos),
986 .calc_ecc_bytes = anfc_calc_hw_ecc_bytes,
989 static int anfc_init_hw_ecc_controller(struct arasan_nfc *nfc,
990 struct nand_chip *chip)
992 struct anand *anand = to_anand(chip);
993 struct mtd_info *mtd = nand_to_mtd(chip);
994 struct nand_ecc_ctrl *ecc = &chip->ecc;
995 unsigned int bch_prim_poly = 0, bch_gf_mag = 0, ecc_offset;
998 switch (mtd->writesize) {
1006 dev_err(nfc->dev, "Unsupported page size %d\n", mtd->writesize);
1010 ret = nand_ecc_choose_conf(chip, &anfc_hw_ecc_caps, mtd->oobsize);
1014 switch (ecc->strength) {
1016 anand->strength = 0x1;
1019 anand->strength = 0x2;
1022 anand->strength = 0x3;
1025 anand->strength = 0x4;
1028 dev_err(nfc->dev, "Unsupported strength %d\n", ecc->strength);
1032 switch (ecc->size) {
1035 bch_prim_poly = 0x201b;
1039 bch_prim_poly = 0x4443;
1042 dev_err(nfc->dev, "Unsupported step size %d\n", ecc->strength);
1046 mtd_set_ooblayout(mtd, nand_get_large_page_ooblayout());
1048 ecc->steps = mtd->writesize / ecc->size;
1049 ecc->algo = NAND_ECC_ALGO_BCH;
1050 anand->ecc_bits = bch_gf_mag * ecc->strength;
1051 ecc->bytes = DIV_ROUND_UP(anand->ecc_bits, 8);
1052 anand->ecc_total = DIV_ROUND_UP(anand->ecc_bits * ecc->steps, 8);
1053 ecc_offset = mtd->writesize + mtd->oobsize - anand->ecc_total;
1054 anand->ecc_conf = ECC_CONF_COL(ecc_offset) |
1055 ECC_CONF_LEN(anand->ecc_total) |
1058 anand->errloc = devm_kmalloc_array(nfc->dev, ecc->strength,
1059 sizeof(*anand->errloc), GFP_KERNEL);
1063 anand->hw_ecc = devm_kmalloc(nfc->dev, ecc->bytes, GFP_KERNEL);
1067 /* Enforce bit swapping to fit the hardware */
1068 anand->bch = bch_init(bch_gf_mag, ecc->strength, bch_prim_poly, true);
1072 ecc->read_page = anfc_sel_read_page_hw_ecc;
1073 ecc->write_page = anfc_sel_write_page_hw_ecc;
1078 static int anfc_attach_chip(struct nand_chip *chip)
1080 struct anand *anand = to_anand(chip);
1081 struct arasan_nfc *nfc = to_anfc(chip->controller);
1082 struct mtd_info *mtd = nand_to_mtd(chip);
1085 if (mtd->writesize <= SZ_512)
1086 anand->caddr_cycles = 1;
1088 anand->caddr_cycles = 2;
1090 if (chip->options & NAND_ROW_ADDR_3)
1091 anand->raddr_cycles = 3;
1093 anand->raddr_cycles = 2;
1095 switch (mtd->writesize) {
1118 /* These hooks are valid for all ECC providers */
1119 chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
1120 chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
1122 switch (chip->ecc.engine_type) {
1123 case NAND_ECC_ENGINE_TYPE_NONE:
1124 case NAND_ECC_ENGINE_TYPE_SOFT:
1125 case NAND_ECC_ENGINE_TYPE_ON_DIE:
1127 case NAND_ECC_ENGINE_TYPE_ON_HOST:
1128 ret = anfc_init_hw_ecc_controller(nfc, chip);
1131 dev_err(nfc->dev, "Unsupported ECC mode: %d\n",
1132 chip->ecc.engine_type);
1139 static void anfc_detach_chip(struct nand_chip *chip)
1141 struct anand *anand = to_anand(chip);
1144 bch_free(anand->bch);
1147 static const struct nand_controller_ops anfc_ops = {
1148 .exec_op = anfc_exec_op,
1149 .setup_interface = anfc_setup_interface,
1150 .attach_chip = anfc_attach_chip,
1151 .detach_chip = anfc_detach_chip,
1154 static int anfc_chip_init(struct arasan_nfc *nfc, struct device_node *np)
1156 struct anand *anand;
1157 struct nand_chip *chip;
1158 struct mtd_info *mtd;
1161 anand = devm_kzalloc(nfc->dev, sizeof(*anand), GFP_KERNEL);
1165 /* We do not support multiple CS per chip yet */
1166 if (of_property_count_elems_of_size(np, "reg", sizeof(u32)) != 1) {
1167 dev_err(nfc->dev, "Invalid reg property\n");
1171 ret = of_property_read_u32(np, "reg", &cs);
1175 ret = of_property_read_u32(np, "nand-rb", &rb);
1179 if (cs >= ANFC_MAX_CS || rb >= ANFC_MAX_CS) {
1180 dev_err(nfc->dev, "Wrong CS %d or RB %d\n", cs, rb);
1184 if (test_and_set_bit(cs, &nfc->assigned_cs)) {
1185 dev_err(nfc->dev, "Already assigned CS %d\n", cs);
1192 chip = &anand->chip;
1193 mtd = nand_to_mtd(chip);
1194 mtd->dev.parent = nfc->dev;
1195 chip->controller = &nfc->controller;
1196 chip->options = NAND_BUSWIDTH_AUTO | NAND_NO_SUBPAGE_WRITE |
1199 nand_set_flash_node(chip, np);
1201 dev_err(nfc->dev, "NAND label property is mandatory\n");
1205 ret = nand_scan(chip, 1);
1207 dev_err(nfc->dev, "Scan operation failed\n");
1211 ret = mtd_device_register(mtd, NULL, 0);
1217 list_add_tail(&anand->node, &nfc->chips);
1222 static void anfc_chips_cleanup(struct arasan_nfc *nfc)
1224 struct anand *anand, *tmp;
1225 struct nand_chip *chip;
1228 list_for_each_entry_safe(anand, tmp, &nfc->chips, node) {
1229 chip = &anand->chip;
1230 ret = mtd_device_unregister(nand_to_mtd(chip));
1233 list_del(&anand->node);
1237 static int anfc_chips_init(struct arasan_nfc *nfc)
1239 struct device_node *np = nfc->dev->of_node, *nand_np;
1240 int nchips = of_get_child_count(np);
1243 if (!nchips || nchips > ANFC_MAX_CS) {
1244 dev_err(nfc->dev, "Incorrect number of NAND chips (%d)\n",
1249 for_each_child_of_node(np, nand_np) {
1250 ret = anfc_chip_init(nfc, nand_np);
1252 of_node_put(nand_np);
1253 anfc_chips_cleanup(nfc);
1261 static void anfc_reset(struct arasan_nfc *nfc)
1263 /* Disable interrupt signals */
1264 writel_relaxed(0, nfc->base + INTR_SIG_EN_REG);
1266 /* Enable interrupt status */
1267 writel_relaxed(EVENT_MASK, nfc->base + INTR_STS_EN_REG);
1270 static int anfc_probe(struct platform_device *pdev)
1272 struct arasan_nfc *nfc;
1275 nfc = devm_kzalloc(&pdev->dev, sizeof(*nfc), GFP_KERNEL);
1279 nfc->dev = &pdev->dev;
1280 nand_controller_init(&nfc->controller);
1281 nfc->controller.ops = &anfc_ops;
1282 INIT_LIST_HEAD(&nfc->chips);
1284 nfc->base = devm_platform_ioremap_resource(pdev, 0);
1285 if (IS_ERR(nfc->base))
1286 return PTR_ERR(nfc->base);
1290 nfc->controller_clk = devm_clk_get(&pdev->dev, "controller");
1291 if (IS_ERR(nfc->controller_clk))
1292 return PTR_ERR(nfc->controller_clk);
1294 nfc->bus_clk = devm_clk_get(&pdev->dev, "bus");
1295 if (IS_ERR(nfc->bus_clk))
1296 return PTR_ERR(nfc->bus_clk);
1298 ret = clk_prepare_enable(nfc->controller_clk);
1302 ret = clk_prepare_enable(nfc->bus_clk);
1304 goto disable_controller_clk;
1306 ret = anfc_chips_init(nfc);
1308 goto disable_bus_clk;
1310 platform_set_drvdata(pdev, nfc);
1315 clk_disable_unprepare(nfc->bus_clk);
1317 disable_controller_clk:
1318 clk_disable_unprepare(nfc->controller_clk);
1323 static int anfc_remove(struct platform_device *pdev)
1325 struct arasan_nfc *nfc = platform_get_drvdata(pdev);
1327 anfc_chips_cleanup(nfc);
1329 clk_disable_unprepare(nfc->bus_clk);
1330 clk_disable_unprepare(nfc->controller_clk);
1335 static const struct of_device_id anfc_ids[] = {
1337 .compatible = "xlnx,zynqmp-nand-controller",
1340 .compatible = "arasan,nfc-v3p10",
1344 MODULE_DEVICE_TABLE(of, anfc_ids);
1346 static struct platform_driver anfc_driver = {
1348 .name = "arasan-nand-controller",
1349 .of_match_table = anfc_ids,
1351 .probe = anfc_probe,
1352 .remove = anfc_remove,
1354 module_platform_driver(anfc_driver);
1356 MODULE_LICENSE("GPL v2");
1357 MODULE_AUTHOR("Punnaiah Choudary Kalluri <punnaia@xilinx.com>");
1358 MODULE_AUTHOR("Naga Sureshkumar Relli <nagasure@xilinx.com>");
1359 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
1360 MODULE_DESCRIPTION("Arasan NAND Flash Controller Driver");