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
130 unsigned int rdy_timeout_ms
;
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
156 struct list_head node
;
157 struct nand_chip chip
;
160 unsigned int page_sz
;
167 unsigned int ecc_bits
;
168 unsigned int ecc_total
;
169 unsigned int *errloc
;
171 struct bch_control
*bch
;
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
188 struct clk
*controller_clk
;
190 struct nand_controller controller
;
191 struct list_head chips
;
192 unsigned long assigned_cs
;
193 unsigned int cur_clk
;
196 static struct anand
*to_anand(struct nand_chip
*nand
)
198 return container_of(nand
, struct anand
, chip
);
201 static struct arasan_nfc
*to_anfc(struct nand_controller
*ctrl
)
203 return container_of(ctrl
, struct arasan_nfc
, controller
);
206 static int anfc_wait_for_event(struct arasan_nfc
*nfc
, unsigned int event
)
211 ret
= readl_relaxed_poll_timeout(nfc
->base
+ INTR_STS_REG
, val
,
213 ANFC_DFLT_TIMEOUT_US
);
215 dev_err(nfc
->dev
, "Timeout waiting for event 0x%x\n", event
);
219 writel_relaxed(event
, nfc
->base
+ INTR_STS_REG
);
224 static int anfc_wait_for_rb(struct arasan_nfc
*nfc
, struct nand_chip
*chip
,
225 unsigned int timeout_ms
)
227 struct anand
*anand
= to_anand(chip
);
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);
236 dev_err(nfc
->dev
, "Timeout waiting for R/B 0x%x\n",
237 readl_relaxed(nfc
->base
+ READY_STS_REG
));
244 static void anfc_trigger_op(struct arasan_nfc
*nfc
, struct anfc_op
*nfc_op
)
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
);
253 static int anfc_pkt_len_config(unsigned int len
, unsigned int *steps
,
254 unsigned int *pktsize
)
258 for (nb
= 1; nb
< ANFC_MAX_STEPS
; nb
*= 2) {
260 if (sz
<= ANFC_MAX_PKT_SIZE
)
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
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.
298 static int anfc_read_page_hw_ecc(struct nand_chip
*chip
, u8
*buf
,
299 int oob_required
, int page
)
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;
308 struct anfc_op nfc_op
= {
310 PKT_SIZE(chip
->ecc
.size
) |
311 PKT_STEPS(chip
->ecc
.steps
),
313 (page
& 0xFF) << (8 * (anand
->caddr_cycles
)) |
314 (((page
>> 8) & 0xFF) << (8 * (1 + anand
->caddr_cycles
))),
316 ((page
>> 16) & 0xFF) |
317 ADDR2_STRENGTH(anand
->strength
) |
320 CMD_1(NAND_CMD_READ0
) |
321 CMD_2(NAND_CMD_READSTART
) |
322 CMD_PAGE_SIZE(anand
->page_sz
) |
324 CMD_NADDRS(anand
->caddr_cycles
+
325 anand
->raddr_cycles
),
326 .prog_reg
= PROG_PGRD
,
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");
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
);
338 anfc_trigger_op(nfc
, &nfc_op
);
340 ret
= anfc_wait_for_event(nfc
, XFER_COMPLETE
);
341 dma_unmap_single(nfc
->dev
, dma_addr
, len
, DMA_FROM_DEVICE
);
343 dev_err(nfc
->dev
, "Error reading page %d\n", page
);
347 /* Store the raw OOB bytes as well */
348 ret
= nand_change_read_column_op(chip
, mtd
->writesize
, chip
->oob_poi
,
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.
358 for (step
= 0; step
< chip
->ecc
.steps
; step
++) {
359 u8
*raw_buf
= &buf
[step
* chip
->ecc
.size
];
360 unsigned int bit
, byte
;
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
);
369 bf
= bch_decode(anand
->bch
, raw_buf
, chip
->ecc
.size
,
370 anand
->hw_ecc
, NULL
, NULL
, anand
->errloc
);
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
;
383 mtd
->ecc_stats
.corrected
+= bf
;
384 max_bitflips
= max_t(unsigned int, max_bitflips
, bf
);
389 bf
= nand_check_erased_ecc_chunk(raw_buf
, chip
->ecc
.size
,
393 mtd
->ecc_stats
.corrected
+= bf
;
394 max_bitflips
= max_t(unsigned int, max_bitflips
, bf
);
395 memset(raw_buf
, 0xFF, chip
->ecc
.size
);
397 mtd
->ecc_stats
.failed
++;
404 static int anfc_write_page_hw_ecc(struct nand_chip
*chip
, const u8
*buf
,
405 int oob_required
, int page
)
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);
413 struct anfc_op nfc_op
= {
415 PKT_SIZE(chip
->ecc
.size
) |
416 PKT_STEPS(chip
->ecc
.steps
),
418 (page
& 0xFF) << (8 * (anand
->caddr_cycles
)) |
419 (((page
>> 8) & 0xFF) << (8 * (1 + anand
->caddr_cycles
))),
421 ((page
>> 16) & 0xFF) |
422 ADDR2_STRENGTH(anand
->strength
) |
425 CMD_1(NAND_CMD_SEQIN
) |
426 CMD_2(NAND_CMD_PAGEPROG
) |
427 CMD_PAGE_SIZE(anand
->page_sz
) |
429 CMD_NADDRS(anand
->caddr_cycles
+
430 anand
->raddr_cycles
) |
432 .prog_reg
= PROG_PGPROG
,
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
);
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");
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
);
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
);
453 dev_err(nfc
->dev
, "Error writing page %d\n", page
);
457 /* Spare data is not protected */
459 ret
= nand_write_oob_std(chip
, page
);
464 /* NAND framework ->exec_op() hooks and related helpers */
465 static int anfc_parse_instructions(struct nand_chip
*chip
,
466 const struct nand_subop
*subop
,
467 struct anfc_op
*nfc_op
)
469 struct anand
*anand
= to_anand(chip
);
470 const struct nand_op_instr
*instr
= NULL
;
471 bool first_cmd
= true;
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
);
479 for (op_id
= 0; op_id
< subop
->ninstrs
; op_id
++) {
480 unsigned int offset
, naddrs
, pktsize
;
484 instr
= &subop
->instrs
[op_id
];
486 switch (instr
->type
) {
487 case NAND_OP_CMD_INSTR
:
489 nfc_op
->cmd_reg
|= CMD_1(instr
->ctx
.cmd
.opcode
);
491 nfc_op
->cmd_reg
|= CMD_2(instr
->ctx
.cmd
.opcode
);
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
);
502 for (i
= 0; i
< min(ANFC_MAX_ADDR_CYC
, naddrs
); i
++) {
504 nfc_op
->addr1_reg
|= (u32
)addrs
[i
] << i
* 8;
506 nfc_op
->addr2_reg
|= addrs
[i
];
510 case NAND_OP_DATA_IN_INSTR
:
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
,
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.
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
537 nfc_op
->pkt_reg
|= PKT_SIZE(round_up(pktsize
, 4)) |
538 PKT_STEPS(nfc_op
->steps
);
540 case NAND_OP_WAITRDY_INSTR
:
541 nfc_op
->rdy_timeout_ms
= instr
->ctx
.waitrdy
.timeout_ms
;
549 static int anfc_rw_pio_op(struct arasan_nfc
*nfc
, struct anfc_op
*nfc_op
)
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
;
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
);
561 dev_err(nfc
->dev
, "PIO %s ready signal not received\n",
562 nfc_op
->read
? "Read" : "Write");
566 offset
= i
* (dwords
* 4);
568 ioread32_rep(nfc
->base
+ DATA_PORT_REG
, &buf
[offset
],
571 iowrite32_rep(nfc
->base
+ DATA_PORT_REG
, &buf
[offset
],
578 offset
= nfc_op
->len
- last_len
;
581 remainder
= readl_relaxed(nfc
->base
+ DATA_PORT_REG
);
582 memcpy(&buf
[offset
], &remainder
, last_len
);
584 memcpy(&remainder
, &buf
[offset
], last_len
);
585 writel_relaxed(remainder
, nfc
->base
+ DATA_PORT_REG
);
589 return anfc_wait_for_event(nfc
, XFER_COMPLETE
);
592 static int anfc_misc_data_type_exec(struct nand_chip
*chip
,
593 const struct nand_subop
*subop
,
596 struct arasan_nfc
*nfc
= to_anfc(chip
->controller
);
597 struct anfc_op nfc_op
= {};
600 ret
= anfc_parse_instructions(chip
, subop
, &nfc_op
);
604 nfc_op
.prog_reg
= prog_reg
;
605 anfc_trigger_op(nfc
, &nfc_op
);
607 if (nfc_op
.rdy_timeout_ms
) {
608 ret
= anfc_wait_for_rb(nfc
, chip
, nfc_op
.rdy_timeout_ms
);
613 return anfc_rw_pio_op(nfc
, &nfc_op
);
616 static int anfc_param_read_type_exec(struct nand_chip
*chip
,
617 const struct nand_subop
*subop
)
619 return anfc_misc_data_type_exec(chip
, subop
, PROG_RDPARAM
);
622 static int anfc_data_read_type_exec(struct nand_chip
*chip
,
623 const struct nand_subop
*subop
)
625 return anfc_misc_data_type_exec(chip
, subop
, PROG_PGRD
);
628 static int anfc_param_write_type_exec(struct nand_chip
*chip
,
629 const struct nand_subop
*subop
)
631 return anfc_misc_data_type_exec(chip
, subop
, PROG_SET_FEATURE
);
634 static int anfc_data_write_type_exec(struct nand_chip
*chip
,
635 const struct nand_subop
*subop
)
637 return anfc_misc_data_type_exec(chip
, subop
, PROG_PGPROG
);
640 static int anfc_misc_zerolen_type_exec(struct nand_chip
*chip
,
641 const struct nand_subop
*subop
,
644 struct arasan_nfc
*nfc
= to_anfc(chip
->controller
);
645 struct anfc_op nfc_op
= {};
648 ret
= anfc_parse_instructions(chip
, subop
, &nfc_op
);
652 nfc_op
.prog_reg
= prog_reg
;
653 anfc_trigger_op(nfc
, &nfc_op
);
655 ret
= anfc_wait_for_event(nfc
, XFER_COMPLETE
);
659 if (nfc_op
.rdy_timeout_ms
)
660 ret
= anfc_wait_for_rb(nfc
, chip
, nfc_op
.rdy_timeout_ms
);
665 static int anfc_status_type_exec(struct nand_chip
*chip
,
666 const struct nand_subop
*subop
)
668 struct arasan_nfc
*nfc
= to_anfc(chip
->controller
);
672 /* See anfc_check_op() for details about this constraint */
673 if (subop
->instrs
[0].ctx
.cmd
.opcode
!= NAND_CMD_STATUS
)
676 ret
= anfc_misc_zerolen_type_exec(chip
, subop
, PROG_STATUS
);
680 tmp
= readl_relaxed(nfc
->base
+ FLASH_STS_REG
);
681 memcpy(subop
->instrs
[1].ctx
.data
.buf
.in
, &tmp
, 1);
686 static int anfc_reset_type_exec(struct nand_chip
*chip
,
687 const struct nand_subop
*subop
)
689 return anfc_misc_zerolen_type_exec(chip
, subop
, PROG_RST
);
692 static int anfc_erase_type_exec(struct nand_chip
*chip
,
693 const struct nand_subop
*subop
)
695 return anfc_misc_zerolen_type_exec(chip
, subop
, PROG_ERASE
);
698 static int anfc_wait_type_exec(struct nand_chip
*chip
,
699 const struct nand_subop
*subop
)
701 struct arasan_nfc
*nfc
= to_anfc(chip
->controller
);
702 struct anfc_op nfc_op
= {};
705 ret
= anfc_parse_instructions(chip
, subop
, &nfc_op
);
709 return anfc_wait_for_rb(nfc
, chip
, nfc_op
.rdy_timeout_ms
);
712 static const struct nand_op_parser anfc_op_parser
= NAND_OP_PARSER(
713 NAND_OP_PARSER_PATTERN(
714 anfc_param_read_type_exec
,
715 NAND_OP_PARSER_PAT_CMD_ELEM(false),
716 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC
),
717 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
718 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE
)),
719 NAND_OP_PARSER_PATTERN(
720 anfc_param_write_type_exec
,
721 NAND_OP_PARSER_PAT_CMD_ELEM(false),
722 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC
),
723 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_PARAM_SIZE
)),
724 NAND_OP_PARSER_PATTERN(
725 anfc_data_read_type_exec
,
726 NAND_OP_PARSER_PAT_CMD_ELEM(false),
727 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC
),
728 NAND_OP_PARSER_PAT_CMD_ELEM(false),
729 NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
730 NAND_OP_PARSER_PAT_DATA_IN_ELEM(true, ANFC_MAX_CHUNK_SIZE
)),
731 NAND_OP_PARSER_PATTERN(
732 anfc_data_write_type_exec
,
733 NAND_OP_PARSER_PAT_CMD_ELEM(false),
734 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC
),
735 NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, ANFC_MAX_CHUNK_SIZE
),
736 NAND_OP_PARSER_PAT_CMD_ELEM(false)),
737 NAND_OP_PARSER_PATTERN(
738 anfc_reset_type_exec
,
739 NAND_OP_PARSER_PAT_CMD_ELEM(false),
740 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
741 NAND_OP_PARSER_PATTERN(
742 anfc_erase_type_exec
,
743 NAND_OP_PARSER_PAT_CMD_ELEM(false),
744 NAND_OP_PARSER_PAT_ADDR_ELEM(false, ANFC_MAX_ADDR_CYC
),
745 NAND_OP_PARSER_PAT_CMD_ELEM(false),
746 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
747 NAND_OP_PARSER_PATTERN(
748 anfc_status_type_exec
,
749 NAND_OP_PARSER_PAT_CMD_ELEM(false),
750 NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, ANFC_MAX_CHUNK_SIZE
)),
751 NAND_OP_PARSER_PATTERN(
753 NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
756 static int anfc_select_target(struct nand_chip
*chip
, int target
)
758 struct anand
*anand
= to_anand(chip
);
759 struct arasan_nfc
*nfc
= to_anfc(chip
->controller
);
762 /* Update the controller timings and the potential ECC configuration */
763 writel_relaxed(anand
->timings
, nfc
->base
+ DATA_INTERFACE_REG
);
765 /* Update clock frequency */
766 if (nfc
->cur_clk
!= anand
->clk
) {
767 clk_disable_unprepare(nfc
->controller_clk
);
768 ret
= clk_set_rate(nfc
->controller_clk
, anand
->clk
);
770 dev_err(nfc
->dev
, "Failed to change clock rate\n");
774 ret
= clk_prepare_enable(nfc
->controller_clk
);
777 "Failed to re-enable the controller clock\n");
781 nfc
->cur_clk
= anand
->clk
;
787 static int anfc_check_op(struct nand_chip
*chip
,
788 const struct nand_operation
*op
)
790 const struct nand_op_instr
*instr
;
794 * The controller abstracts all the NAND operations and do not support
795 * data only operations.
797 * TODO: The nand_op_parser framework should be extended to
798 * support custom checks on DATA instructions.
800 for (op_id
= 0; op_id
< op
->ninstrs
; op_id
++) {
801 instr
= &op
->instrs
[op_id
];
803 switch (instr
->type
) {
804 case NAND_OP_ADDR_INSTR
:
805 if (instr
->ctx
.addr
.naddrs
> ANFC_MAX_ADDR_CYC
)
809 case NAND_OP_DATA_IN_INSTR
:
810 case NAND_OP_DATA_OUT_INSTR
:
811 if (instr
->ctx
.data
.len
> ANFC_MAX_CHUNK_SIZE
)
814 if (anfc_pkt_len_config(instr
->ctx
.data
.len
, 0, 0))
824 * The controller does not allow to proceed with a CMD+DATA_IN cycle
825 * manually on the bus by reading data from the data register. Instead,
826 * the controller abstract a status read operation with its own status
827 * register after ordering a read status operation. Hence, we cannot
828 * support any CMD+DATA_IN operation other than a READ STATUS.
830 * TODO: The nand_op_parser() framework should be extended to describe
831 * fixed patterns instead of open-coding this check here.
833 if (op
->ninstrs
== 2 &&
834 op
->instrs
[0].type
== NAND_OP_CMD_INSTR
&&
835 op
->instrs
[0].ctx
.cmd
.opcode
!= NAND_CMD_STATUS
&&
836 op
->instrs
[1].type
== NAND_OP_DATA_IN_INSTR
)
839 return nand_op_parser_exec_op(chip
, &anfc_op_parser
, op
, true);
842 static int anfc_exec_op(struct nand_chip
*chip
,
843 const struct nand_operation
*op
,
849 return anfc_check_op(chip
, op
);
851 ret
= anfc_select_target(chip
, op
->cs
);
855 return nand_op_parser_exec_op(chip
, &anfc_op_parser
, op
, check_only
);
858 static int anfc_setup_interface(struct nand_chip
*chip
, int target
,
859 const struct nand_interface_config
*conf
)
861 struct anand
*anand
= to_anand(chip
);
862 struct arasan_nfc
*nfc
= to_anfc(chip
->controller
);
863 struct device_node
*np
= nfc
->dev
->of_node
;
868 anand
->timings
= DIFACE_SDR
| DIFACE_SDR_MODE(conf
->timings
.mode
);
869 anand
->clk
= ANFC_XLNX_SDR_DFLT_CORE_CLK
;
872 * Due to a hardware bug in the ZynqMP SoC, SDR timing modes 0-1 work
873 * with f > 90MHz (default clock is 100MHz) but signals are unstable
874 * with higher modes. Hence we decrease a little bit the clock rate to
875 * 80MHz when using modes 2-5 with this SoC.
877 if (of_device_is_compatible(np
, "xlnx,zynqmp-nand-controller") &&
878 conf
->timings
.mode
>= 2)
879 anand
->clk
= ANFC_XLNX_SDR_HS_CORE_CLK
;
884 static int anfc_calc_hw_ecc_bytes(int step_size
, int strength
)
886 unsigned int bch_gf_mag
, ecc_bits
;
899 ecc_bits
= bch_gf_mag
* strength
;
901 return DIV_ROUND_UP(ecc_bits
, 8);
904 static const int anfc_hw_ecc_512_strengths
[] = {4, 8, 12};
906 static const int anfc_hw_ecc_1024_strengths
[] = {24};
908 static const struct nand_ecc_step_info anfc_hw_ecc_step_infos
[] = {
911 .strengths
= anfc_hw_ecc_512_strengths
,
912 .nstrengths
= ARRAY_SIZE(anfc_hw_ecc_512_strengths
),
916 .strengths
= anfc_hw_ecc_1024_strengths
,
917 .nstrengths
= ARRAY_SIZE(anfc_hw_ecc_1024_strengths
),
921 static const struct nand_ecc_caps anfc_hw_ecc_caps
= {
922 .stepinfos
= anfc_hw_ecc_step_infos
,
923 .nstepinfos
= ARRAY_SIZE(anfc_hw_ecc_step_infos
),
924 .calc_ecc_bytes
= anfc_calc_hw_ecc_bytes
,
927 static int anfc_init_hw_ecc_controller(struct arasan_nfc
*nfc
,
928 struct nand_chip
*chip
)
930 struct anand
*anand
= to_anand(chip
);
931 struct mtd_info
*mtd
= nand_to_mtd(chip
);
932 struct nand_ecc_ctrl
*ecc
= &chip
->ecc
;
933 unsigned int bch_prim_poly
= 0, bch_gf_mag
= 0, ecc_offset
;
936 switch (mtd
->writesize
) {
944 dev_err(nfc
->dev
, "Unsupported page size %d\n", mtd
->writesize
);
948 ret
= nand_ecc_choose_conf(chip
, &anfc_hw_ecc_caps
, mtd
->oobsize
);
952 switch (ecc
->strength
) {
954 anand
->strength
= 0x1;
957 anand
->strength
= 0x2;
960 anand
->strength
= 0x3;
963 anand
->strength
= 0x4;
966 dev_err(nfc
->dev
, "Unsupported strength %d\n", ecc
->strength
);
973 bch_prim_poly
= 0x201b;
977 bch_prim_poly
= 0x4443;
980 dev_err(nfc
->dev
, "Unsupported step size %d\n", ecc
->strength
);
984 mtd_set_ooblayout(mtd
, nand_get_large_page_ooblayout());
986 ecc
->steps
= mtd
->writesize
/ ecc
->size
;
987 ecc
->algo
= NAND_ECC_ALGO_BCH
;
988 anand
->ecc_bits
= bch_gf_mag
* ecc
->strength
;
989 ecc
->bytes
= DIV_ROUND_UP(anand
->ecc_bits
, 8);
990 anand
->ecc_total
= DIV_ROUND_UP(anand
->ecc_bits
* ecc
->steps
, 8);
991 ecc_offset
= mtd
->writesize
+ mtd
->oobsize
- anand
->ecc_total
;
992 anand
->ecc_conf
= ECC_CONF_COL(ecc_offset
) |
993 ECC_CONF_LEN(anand
->ecc_total
) |
996 anand
->errloc
= devm_kmalloc_array(nfc
->dev
, ecc
->strength
,
997 sizeof(*anand
->errloc
), GFP_KERNEL
);
1001 anand
->hw_ecc
= devm_kmalloc(nfc
->dev
, ecc
->bytes
, GFP_KERNEL
);
1005 /* Enforce bit swapping to fit the hardware */
1006 anand
->bch
= bch_init(bch_gf_mag
, ecc
->strength
, bch_prim_poly
, true);
1010 ecc
->read_page
= anfc_read_page_hw_ecc
;
1011 ecc
->write_page
= anfc_write_page_hw_ecc
;
1016 static int anfc_attach_chip(struct nand_chip
*chip
)
1018 struct anand
*anand
= to_anand(chip
);
1019 struct arasan_nfc
*nfc
= to_anfc(chip
->controller
);
1020 struct mtd_info
*mtd
= nand_to_mtd(chip
);
1023 if (mtd
->writesize
<= SZ_512
)
1024 anand
->caddr_cycles
= 1;
1026 anand
->caddr_cycles
= 2;
1028 if (chip
->options
& NAND_ROW_ADDR_3
)
1029 anand
->raddr_cycles
= 3;
1031 anand
->raddr_cycles
= 2;
1033 switch (mtd
->writesize
) {
1056 /* These hooks are valid for all ECC providers */
1057 chip
->ecc
.read_page_raw
= nand_monolithic_read_page_raw
;
1058 chip
->ecc
.write_page_raw
= nand_monolithic_write_page_raw
;
1060 switch (chip
->ecc
.engine_type
) {
1061 case NAND_ECC_ENGINE_TYPE_NONE
:
1062 case NAND_ECC_ENGINE_TYPE_SOFT
:
1063 case NAND_ECC_ENGINE_TYPE_ON_DIE
:
1065 case NAND_ECC_ENGINE_TYPE_ON_HOST
:
1066 ret
= anfc_init_hw_ecc_controller(nfc
, chip
);
1069 dev_err(nfc
->dev
, "Unsupported ECC mode: %d\n",
1070 chip
->ecc
.engine_type
);
1077 static void anfc_detach_chip(struct nand_chip
*chip
)
1079 struct anand
*anand
= to_anand(chip
);
1082 bch_free(anand
->bch
);
1085 static const struct nand_controller_ops anfc_ops
= {
1086 .exec_op
= anfc_exec_op
,
1087 .setup_interface
= anfc_setup_interface
,
1088 .attach_chip
= anfc_attach_chip
,
1089 .detach_chip
= anfc_detach_chip
,
1092 static int anfc_chip_init(struct arasan_nfc
*nfc
, struct device_node
*np
)
1094 struct anand
*anand
;
1095 struct nand_chip
*chip
;
1096 struct mtd_info
*mtd
;
1099 anand
= devm_kzalloc(nfc
->dev
, sizeof(*anand
), GFP_KERNEL
);
1103 /* We do not support multiple CS per chip yet */
1104 if (of_property_count_elems_of_size(np
, "reg", sizeof(u32
)) != 1) {
1105 dev_err(nfc
->dev
, "Invalid reg property\n");
1109 ret
= of_property_read_u32(np
, "reg", &cs
);
1113 ret
= of_property_read_u32(np
, "nand-rb", &rb
);
1117 if (cs
>= ANFC_MAX_CS
|| rb
>= ANFC_MAX_CS
) {
1118 dev_err(nfc
->dev
, "Wrong CS %d or RB %d\n", cs
, rb
);
1122 if (test_and_set_bit(cs
, &nfc
->assigned_cs
)) {
1123 dev_err(nfc
->dev
, "Already assigned CS %d\n", cs
);
1130 chip
= &anand
->chip
;
1131 mtd
= nand_to_mtd(chip
);
1132 mtd
->dev
.parent
= nfc
->dev
;
1133 chip
->controller
= &nfc
->controller
;
1134 chip
->options
= NAND_BUSWIDTH_AUTO
| NAND_NO_SUBPAGE_WRITE
|
1137 nand_set_flash_node(chip
, np
);
1139 dev_err(nfc
->dev
, "NAND label property is mandatory\n");
1143 ret
= nand_scan(chip
, 1);
1145 dev_err(nfc
->dev
, "Scan operation failed\n");
1149 ret
= mtd_device_register(mtd
, NULL
, 0);
1155 list_add_tail(&anand
->node
, &nfc
->chips
);
1160 static void anfc_chips_cleanup(struct arasan_nfc
*nfc
)
1162 struct anand
*anand
, *tmp
;
1163 struct nand_chip
*chip
;
1166 list_for_each_entry_safe(anand
, tmp
, &nfc
->chips
, node
) {
1167 chip
= &anand
->chip
;
1168 ret
= mtd_device_unregister(nand_to_mtd(chip
));
1171 list_del(&anand
->node
);
1175 static int anfc_chips_init(struct arasan_nfc
*nfc
)
1177 struct device_node
*np
= nfc
->dev
->of_node
, *nand_np
;
1178 int nchips
= of_get_child_count(np
);
1181 if (!nchips
|| nchips
> ANFC_MAX_CS
) {
1182 dev_err(nfc
->dev
, "Incorrect number of NAND chips (%d)\n",
1187 for_each_child_of_node(np
, nand_np
) {
1188 ret
= anfc_chip_init(nfc
, nand_np
);
1190 of_node_put(nand_np
);
1191 anfc_chips_cleanup(nfc
);
1199 static void anfc_reset(struct arasan_nfc
*nfc
)
1201 /* Disable interrupt signals */
1202 writel_relaxed(0, nfc
->base
+ INTR_SIG_EN_REG
);
1204 /* Enable interrupt status */
1205 writel_relaxed(EVENT_MASK
, nfc
->base
+ INTR_STS_EN_REG
);
1208 static int anfc_probe(struct platform_device
*pdev
)
1210 struct arasan_nfc
*nfc
;
1213 nfc
= devm_kzalloc(&pdev
->dev
, sizeof(*nfc
), GFP_KERNEL
);
1217 nfc
->dev
= &pdev
->dev
;
1218 nand_controller_init(&nfc
->controller
);
1219 nfc
->controller
.ops
= &anfc_ops
;
1220 INIT_LIST_HEAD(&nfc
->chips
);
1222 nfc
->base
= devm_platform_ioremap_resource(pdev
, 0);
1223 if (IS_ERR(nfc
->base
))
1224 return PTR_ERR(nfc
->base
);
1228 nfc
->controller_clk
= devm_clk_get(&pdev
->dev
, "controller");
1229 if (IS_ERR(nfc
->controller_clk
))
1230 return PTR_ERR(nfc
->controller_clk
);
1232 nfc
->bus_clk
= devm_clk_get(&pdev
->dev
, "bus");
1233 if (IS_ERR(nfc
->bus_clk
))
1234 return PTR_ERR(nfc
->bus_clk
);
1236 ret
= clk_prepare_enable(nfc
->controller_clk
);
1240 ret
= clk_prepare_enable(nfc
->bus_clk
);
1242 goto disable_controller_clk
;
1244 ret
= anfc_chips_init(nfc
);
1246 goto disable_bus_clk
;
1248 platform_set_drvdata(pdev
, nfc
);
1253 clk_disable_unprepare(nfc
->bus_clk
);
1255 disable_controller_clk
:
1256 clk_disable_unprepare(nfc
->controller_clk
);
1261 static int anfc_remove(struct platform_device
*pdev
)
1263 struct arasan_nfc
*nfc
= platform_get_drvdata(pdev
);
1265 anfc_chips_cleanup(nfc
);
1267 clk_disable_unprepare(nfc
->bus_clk
);
1268 clk_disable_unprepare(nfc
->controller_clk
);
1273 static const struct of_device_id anfc_ids
[] = {
1275 .compatible
= "xlnx,zynqmp-nand-controller",
1278 .compatible
= "arasan,nfc-v3p10",
1282 MODULE_DEVICE_TABLE(of
, anfc_ids
);
1284 static struct platform_driver anfc_driver
= {
1286 .name
= "arasan-nand-controller",
1287 .of_match_table
= anfc_ids
,
1289 .probe
= anfc_probe
,
1290 .remove
= anfc_remove
,
1292 module_platform_driver(anfc_driver
);
1294 MODULE_LICENSE("GPL v2");
1295 MODULE_AUTHOR("Punnaiah Choudary Kalluri <punnaia@xilinx.com>");
1296 MODULE_AUTHOR("Naga Sureshkumar Relli <nagasure@xilinx.com>");
1297 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
1298 MODULE_DESCRIPTION("Arasan NAND Flash Controller Driver");