1 From 8170bafa8936e9fbfdce992932a63bd20eca3bc3 Mon Sep 17 00:00:00 2001
2 From: Chuanhong Guo <gch981213@gmail.com>
3 Date: Sat, 2 Apr 2022 10:16:11 +0800
4 Subject: [PATCH v6 2/5] spi: add driver for MTK SPI NAND Flash Interface
6 This driver implements support for the SPI-NAND mode of MTK NAND Flash
7 Interface as a SPI-MEM controller with pipelined ECC capability.
9 Signed-off-by: Chuanhong Guo <gch981213@gmail.com>
10 Tested-by: Daniel Golle <daniel@makrotopia.org>
16 use streamed DMA api to avoid an extra memory copy during read
17 make ECC engine config a per-nand context
18 take user-requested ECC strength into account
23 print page format with dev_dbg
24 replace uint*_t copied from vendor driver with u*
27 add missing nfi mode register configuration in probe
28 fix an off-by-one bug in mtk_snand_mac_io
30 drivers/spi/Kconfig | 10 +
31 drivers/spi/Makefile | 1 +
32 drivers/spi/spi-mtk-snfi.c | 1470 ++++++++++++++++++++++++++++++++++++
33 3 files changed, 1481 insertions(+)
34 create mode 100644 drivers/spi/spi-mtk-snfi.c
36 --- a/drivers/spi/Kconfig
37 +++ b/drivers/spi/Kconfig
38 @@ -530,6 +530,16 @@ config SPI_MTK_NOR
39 SPI interface as well as several SPI NOR specific instructions
40 via SPI MEM interface.
43 + tristate "MediaTek SPI NAND Flash Interface"
44 + depends on ARCH_MEDIATEK || COMPILE_TEST
45 + depends on MTD_NAND_ECC_MEDIATEK
47 + This enables support for SPI-NAND mode on the MediaTek NAND
48 + Flash Interface found on MediaTek ARM SoCs. This controller
49 + is implemented as a SPI-MEM controller with pipelined ECC
53 tristate "Nuvoton NPCM FLASH Interface Unit"
54 depends on ARCH_NPCM || COMPILE_TEST
55 --- a/drivers/spi/Makefile
56 +++ b/drivers/spi/Makefile
57 @@ -71,6 +71,7 @@ obj-$(CONFIG_SPI_MPC52xx) += spi-mpc52x
58 obj-$(CONFIG_SPI_MT65XX) += spi-mt65xx.o
59 obj-$(CONFIG_SPI_MT7621) += spi-mt7621.o
60 obj-$(CONFIG_SPI_MTK_NOR) += spi-mtk-nor.o
61 +obj-$(CONFIG_SPI_MTK_SNFI) += spi-mtk-snfi.o
62 obj-$(CONFIG_SPI_MXIC) += spi-mxic.o
63 obj-$(CONFIG_SPI_MXS) += spi-mxs.o
64 obj-$(CONFIG_SPI_NPCM_FIU) += spi-npcm-fiu.o
66 +++ b/drivers/spi/spi-mtk-snfi.c
68 +// SPDX-License-Identifier: GPL-2.0
70 +// Driver for the SPI-NAND mode of Mediatek NAND Flash Interface
72 +// Copyright (c) 2022 Chuanhong Guo <gch981213@gmail.com>
74 +// This driver is based on the SPI-NAND mtd driver from Mediatek SDK:
76 +// Copyright (C) 2020 MediaTek Inc.
77 +// Author: Weijie Gao <weijie.gao@mediatek.com>
79 +// This controller organize the page data as several interleaved sectors
80 +// like the following: (sizeof(FDM + ECC) = snf->nfi_cfg.spare_size)
81 +// +---------+------+------+---------+------+------+-----+
82 +// | Sector1 | FDM1 | ECC1 | Sector2 | FDM2 | ECC2 | ... |
83 +// +---------+------+------+---------+------+------+-----+
84 +// With auto-format turned on, DMA only returns this part:
85 +// +---------+---------+-----+
86 +// | Sector1 | Sector2 | ... |
87 +// +---------+---------+-----+
88 +// The FDM data will be filled to the registers, and ECC parity data isn't
90 +// With auto-format off, all ((Sector+FDM+ECC)*nsectors) will be read over DMA
91 +// in it's original order shown in the first table. ECC can't be turned on when
92 +// auto-format is off.
94 +// However, Linux SPI-NAND driver expects the data returned as:
98 +// where the page data is continuously stored instead of interleaved.
99 +// So we assume all instructions matching the page_op template between ECC
100 +// prepare_io_req and finish_io_req are for page cache r/w.
101 +// Here's how this spi-mem driver operates when reading:
102 +// 1. Always set snf->autofmt = true in prepare_io_req (even when ECC is off).
103 +// 2. Perform page ops and let the controller fill the DMA bounce buffer with
104 +// de-interleaved sector data and set FDM registers.
105 +// 3. Return the data as:
106 +// +---------+---------+-----+------+------+-----+
107 +// | Sector1 | Sector2 | ... | FDM1 | FDM2 | ... |
108 +// +---------+---------+-----+------+------+-----+
109 +// 4. For other matching spi_mem ops outside a prepare/finish_io_req pair,
110 +// read the data with auto-format off into the bounce buffer and copy
111 +// needed data to the buffer specified in the request.
113 +// Write requests operates in a similar manner.
114 +// As a limitation of this strategy, we won't be able to access any ECC parity
115 +// data at all in Linux.
117 +// Here's the bad block mark situation on MTK chips:
118 +// In older chips like mt7622, MTK uses the first FDM byte in the first sector
119 +// as the bad block mark. After de-interleaving, this byte appears at [pagesize]
120 +// in the returned data, which is the BBM position expected by kernel. However,
121 +// the conventional bad block mark is the first byte of the OOB, which is part
122 +// of the last sector data in the interleaved layout. Instead of fixing their
123 +// hardware, MTK decided to address this inconsistency in software. On these
124 +// later chips, the BootROM expects the following:
125 +// 1. The [pagesize] byte on a nand page is used as BBM, which will appear at
126 +// (page_size - (nsectors - 1) * spare_size) in the DMA buffer.
127 +// 2. The original byte stored at that position in the DMA buffer will be stored
128 +// as the first byte of the FDM section in the last sector.
129 +// We can't disagree with the BootROM, so after de-interleaving, we need to
130 +// perform the following swaps in read:
131 +// 1. Store the BBM at [page_size - (nsectors - 1) * spare_size] to [page_size],
132 +// which is the expected BBM position by kernel.
133 +// 2. Store the page data byte at [pagesize + (nsectors-1) * fdm] back to
134 +// [page_size - (nsectors - 1) * spare_size]
135 +// Similarly, when writing, we need to perform swaps in the other direction.
137 +#include <linux/kernel.h>
138 +#include <linux/module.h>
139 +#include <linux/init.h>
140 +#include <linux/device.h>
141 +#include <linux/mutex.h>
142 +#include <linux/clk.h>
143 +#include <linux/interrupt.h>
144 +#include <linux/dma-mapping.h>
145 +#include <linux/iopoll.h>
146 +#include <linux/of_platform.h>
147 +#include <linux/mtd/nand-ecc-mtk.h>
148 +#include <linux/spi/spi.h>
149 +#include <linux/spi/spi-mem.h>
150 +#include <linux/mtd/nand.h>
153 +#define NFI_CNFG 0x000
154 +#define CNFG_OP_MODE_S 12
155 +#define CNFG_OP_MODE_CUST 6
156 +#define CNFG_OP_MODE_PROGRAM 3
157 +#define CNFG_AUTO_FMT_EN BIT(9)
158 +#define CNFG_HW_ECC_EN BIT(8)
159 +#define CNFG_DMA_BURST_EN BIT(2)
160 +#define CNFG_READ_MODE BIT(1)
161 +#define CNFG_DMA_MODE BIT(0)
163 +#define NFI_PAGEFMT 0x0004
164 +#define NFI_SPARE_SIZE_LS_S 16
165 +#define NFI_FDM_ECC_NUM_S 12
166 +#define NFI_FDM_NUM_S 8
167 +#define NFI_SPARE_SIZE_S 4
168 +#define NFI_SEC_SEL_512 BIT(2)
169 +#define NFI_PAGE_SIZE_S 0
170 +#define NFI_PAGE_SIZE_512_2K 0
171 +#define NFI_PAGE_SIZE_2K_4K 1
172 +#define NFI_PAGE_SIZE_4K_8K 2
173 +#define NFI_PAGE_SIZE_8K_16K 3
175 +#define NFI_CON 0x008
176 +#define CON_SEC_NUM_S 12
177 +#define CON_BWR BIT(9)
178 +#define CON_BRD BIT(8)
179 +#define CON_NFI_RST BIT(1)
180 +#define CON_FIFO_FLUSH BIT(0)
182 +#define NFI_INTR_EN 0x010
183 +#define NFI_INTR_STA 0x014
184 +#define NFI_IRQ_INTR_EN BIT(31)
185 +#define NFI_IRQ_CUS_READ BIT(8)
186 +#define NFI_IRQ_CUS_PG BIT(7)
188 +#define NFI_CMD 0x020
189 +#define NFI_CMD_DUMMY_READ 0x00
190 +#define NFI_CMD_DUMMY_WRITE 0x80
192 +#define NFI_STRDATA 0x040
193 +#define STR_DATA BIT(0)
195 +#define NFI_STA 0x060
196 +#define NFI_NAND_FSM GENMASK(28, 24)
197 +#define NFI_FSM GENMASK(19, 16)
198 +#define READ_EMPTY BIT(12)
200 +#define NFI_FIFOSTA 0x064
201 +#define FIFO_WR_REMAIN_S 8
202 +#define FIFO_RD_REMAIN_S 0
204 +#define NFI_ADDRCNTR 0x070
205 +#define SEC_CNTR GENMASK(16, 12)
206 +#define SEC_CNTR_S 12
207 +#define NFI_SEC_CNTR(val) (((val)&SEC_CNTR) >> SEC_CNTR_S)
209 +#define NFI_STRADDR 0x080
211 +#define NFI_BYTELEN 0x084
212 +#define BUS_SEC_CNTR(val) (((val)&SEC_CNTR) >> SEC_CNTR_S)
214 +#define NFI_FDM0L 0x0a0
215 +#define NFI_FDM0M 0x0a4
216 +#define NFI_FDML(n) (NFI_FDM0L + (n)*8)
217 +#define NFI_FDMM(n) (NFI_FDM0M + (n)*8)
219 +#define NFI_DEBUG_CON1 0x220
220 +#define WBUF_EN BIT(2)
222 +#define NFI_MASTERSTA 0x224
223 +#define MAS_ADDR GENMASK(11, 9)
224 +#define MAS_RD GENMASK(8, 6)
225 +#define MAS_WR GENMASK(5, 3)
226 +#define MAS_RDDLY GENMASK(2, 0)
227 +#define NFI_MASTERSTA_MASK_7622 (MAS_ADDR | MAS_RD | MAS_WR | MAS_RDDLY)
230 +#define SNF_MAC_CTL 0x500
231 +#define MAC_XIO_SEL BIT(4)
232 +#define SF_MAC_EN BIT(3)
233 +#define SF_TRIG BIT(2)
234 +#define WIP_READY BIT(1)
237 +#define SNF_MAC_OUTL 0x504
238 +#define SNF_MAC_INL 0x508
240 +#define SNF_RD_CTL2 0x510
241 +#define DATA_READ_DUMMY_S 8
242 +#define DATA_READ_MAX_DUMMY 0xf
243 +#define DATA_READ_CMD_S 0
245 +#define SNF_RD_CTL3 0x514
247 +#define SNF_PG_CTL1 0x524
248 +#define PG_LOAD_CMD_S 8
250 +#define SNF_PG_CTL2 0x528
252 +#define SNF_MISC_CTL 0x538
253 +#define SW_RST BIT(28)
254 +#define FIFO_RD_LTC_S 25
255 +#define PG_LOAD_X4_EN BIT(20)
256 +#define DATA_READ_MODE_S 16
257 +#define DATA_READ_MODE GENMASK(18, 16)
258 +#define DATA_READ_MODE_X1 0
259 +#define DATA_READ_MODE_X2 1
260 +#define DATA_READ_MODE_X4 2
261 +#define DATA_READ_MODE_DUAL 5
262 +#define DATA_READ_MODE_QUAD 6
263 +#define PG_LOAD_CUSTOM_EN BIT(7)
264 +#define DATARD_CUSTOM_EN BIT(6)
265 +#define CS_DESELECT_CYC_S 0
267 +#define SNF_MISC_CTL2 0x53c
268 +#define PROGRAM_LOAD_BYTE_NUM_S 16
269 +#define READ_DATA_BYTE_NUM_S 11
271 +#define SNF_DLY_CTL3 0x548
272 +#define SFCK_SAM_DLY_S 0
274 +#define SNF_STA_CTL1 0x550
275 +#define CUS_PG_DONE BIT(28)
276 +#define CUS_READ_DONE BIT(27)
277 +#define SPI_STATE_S 0
278 +#define SPI_STATE GENMASK(3, 0)
280 +#define SNF_CFG 0x55c
281 +#define SPI_MODE BIT(0)
283 +#define SNF_GPRAM 0x800
284 +#define SNF_GPRAM_SIZE 0xa0
286 +#define SNFI_POLL_INTERVAL 1000000
288 +static const u8 mt7622_spare_sizes[] = { 16, 26, 27, 28 };
290 +struct mtk_snand_caps {
298 + bool empty_page_check;
299 + u32 mastersta_mask;
301 + const u8 *spare_sizes;
302 + u32 num_spare_size;
305 +static const struct mtk_snand_caps mt7622_snand_caps = {
306 + .sector_size = 512,
312 + .empty_page_check = false,
313 + .mastersta_mask = NFI_MASTERSTA_MASK_7622,
314 + .spare_sizes = mt7622_spare_sizes,
315 + .num_spare_size = ARRAY_SIZE(mt7622_spare_sizes)
318 +static const struct mtk_snand_caps mt7629_snand_caps = {
319 + .sector_size = 512,
325 + .empty_page_check = false,
326 + .mastersta_mask = NFI_MASTERSTA_MASK_7622,
327 + .spare_sizes = mt7622_spare_sizes,
328 + .num_spare_size = ARRAY_SIZE(mt7622_spare_sizes)
331 +struct mtk_snand_conf {
339 + struct spi_controller *ctlr;
340 + struct device *dev;
341 + struct clk *nfi_clk;
342 + struct clk *pad_clk;
343 + void __iomem *nfi_base;
345 + struct completion op_done;
346 + const struct mtk_snand_caps *caps;
347 + struct mtk_ecc_config *ecc_cfg;
348 + struct mtk_ecc *ecc;
349 + struct mtk_snand_conf nfi_cfg;
350 + struct mtk_ecc_stats ecc_stats;
351 + struct nand_ecc_engine ecc_eng;
357 +static struct mtk_snand *nand_to_mtk_snand(struct nand_device *nand)
359 + struct nand_ecc_engine *eng = nand->ecc.engine;
361 + return container_of(eng, struct mtk_snand, ecc_eng);
364 +static inline int snand_prepare_bouncebuf(struct mtk_snand *snf, size_t size)
366 + if (snf->buf_len >= size)
369 + snf->buf = kmalloc(size, GFP_KERNEL);
372 + snf->buf_len = size;
373 + memset(snf->buf, 0xff, snf->buf_len);
377 +static inline u32 nfi_read32(struct mtk_snand *snf, u32 reg)
379 + return readl(snf->nfi_base + reg);
382 +static inline void nfi_write32(struct mtk_snand *snf, u32 reg, u32 val)
384 + writel(val, snf->nfi_base + reg);
387 +static inline void nfi_write16(struct mtk_snand *snf, u32 reg, u16 val)
389 + writew(val, snf->nfi_base + reg);
392 +static inline void nfi_rmw32(struct mtk_snand *snf, u32 reg, u32 clr, u32 set)
396 + val = readl(snf->nfi_base + reg);
399 + writel(val, snf->nfi_base + reg);
402 +static void nfi_read_data(struct mtk_snand *snf, u32 reg, u8 *data, u32 len)
404 + u32 i, val = 0, es = sizeof(u32);
406 + for (i = reg; i < reg + len; i++) {
407 + if (i == reg || i % es == 0)
408 + val = nfi_read32(snf, i & ~(es - 1));
410 + *data++ = (u8)(val >> (8 * (i % es)));
414 +static int mtk_nfi_reset(struct mtk_snand *snf)
416 + u32 val, fifo_mask;
419 + nfi_write32(snf, NFI_CON, CON_FIFO_FLUSH | CON_NFI_RST);
421 + ret = readw_poll_timeout(snf->nfi_base + NFI_MASTERSTA, val,
422 + !(val & snf->caps->mastersta_mask), 0,
423 + SNFI_POLL_INTERVAL);
425 + dev_err(snf->dev, "NFI master is still busy after reset\n");
429 + ret = readl_poll_timeout(snf->nfi_base + NFI_STA, val,
430 + !(val & (NFI_FSM | NFI_NAND_FSM)), 0,
431 + SNFI_POLL_INTERVAL);
433 + dev_err(snf->dev, "Failed to reset NFI\n");
437 + fifo_mask = ((snf->caps->fifo_size - 1) << FIFO_RD_REMAIN_S) |
438 + ((snf->caps->fifo_size - 1) << FIFO_WR_REMAIN_S);
439 + ret = readw_poll_timeout(snf->nfi_base + NFI_FIFOSTA, val,
440 + !(val & fifo_mask), 0, SNFI_POLL_INTERVAL);
442 + dev_err(snf->dev, "NFI FIFOs are not empty\n");
449 +static int mtk_snand_mac_reset(struct mtk_snand *snf)
454 + nfi_rmw32(snf, SNF_MISC_CTL, 0, SW_RST);
456 + ret = readl_poll_timeout(snf->nfi_base + SNF_STA_CTL1, val,
457 + !(val & SPI_STATE), 0, SNFI_POLL_INTERVAL);
459 + dev_err(snf->dev, "Failed to reset SNFI MAC\n");
461 + nfi_write32(snf, SNF_MISC_CTL,
462 + (2 << FIFO_RD_LTC_S) | (10 << CS_DESELECT_CYC_S));
467 +static int mtk_snand_mac_trigger(struct mtk_snand *snf, u32 outlen, u32 inlen)
472 + nfi_write32(snf, SNF_MAC_CTL, SF_MAC_EN);
473 + nfi_write32(snf, SNF_MAC_OUTL, outlen);
474 + nfi_write32(snf, SNF_MAC_INL, inlen);
476 + nfi_write32(snf, SNF_MAC_CTL, SF_MAC_EN | SF_TRIG);
478 + ret = readl_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val,
479 + val & WIP_READY, 0, SNFI_POLL_INTERVAL);
481 + dev_err(snf->dev, "Timed out waiting for WIP_READY\n");
485 + ret = readl_poll_timeout(snf->nfi_base + SNF_MAC_CTL, val, !(val & WIP),
486 + 0, SNFI_POLL_INTERVAL);
488 + dev_err(snf->dev, "Timed out waiting for WIP cleared\n");
491 + nfi_write32(snf, SNF_MAC_CTL, 0);
496 +static int mtk_snand_mac_io(struct mtk_snand *snf, const struct spi_mem_op *op)
501 + const u8 *tx_buf = NULL;
506 + if (op->data.dir == SPI_MEM_DATA_IN) {
507 + rx_len = op->data.nbytes;
508 + rx_buf = op->data.buf.in;
510 + tx_buf = op->data.buf.out;
513 + mtk_snand_mac_reset(snf);
515 + for (i = 0; i < op->cmd.nbytes; i++, reg_offs++) {
516 + b = (op->cmd.opcode >> ((op->cmd.nbytes - i - 1) * 8)) & 0xff;
517 + val |= b << (8 * (reg_offs % 4));
518 + if (reg_offs % 4 == 3) {
519 + nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
524 + for (i = 0; i < op->addr.nbytes; i++, reg_offs++) {
525 + b = (op->addr.val >> ((op->addr.nbytes - i - 1) * 8)) & 0xff;
526 + val |= b << (8 * (reg_offs % 4));
527 + if (reg_offs % 4 == 3) {
528 + nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
533 + for (i = 0; i < op->dummy.nbytes; i++, reg_offs++) {
534 + if (reg_offs % 4 == 3) {
535 + nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
540 + if (op->data.dir == SPI_MEM_DATA_OUT) {
541 + for (i = 0; i < op->data.nbytes; i++, reg_offs++) {
542 + val |= tx_buf[i] << (8 * (reg_offs % 4));
543 + if (reg_offs % 4 == 3) {
544 + nfi_write32(snf, SNF_GPRAM + reg_offs - 3, val);
551 + nfi_write32(snf, SNF_GPRAM + (reg_offs & ~3), val);
553 + for (i = 0; i < reg_offs; i += 4)
554 + dev_dbg(snf->dev, "%d: %08X", i,
555 + nfi_read32(snf, SNF_GPRAM + i));
557 + dev_dbg(snf->dev, "SNF TX: %u RX: %u", reg_offs, rx_len);
559 + ret = mtk_snand_mac_trigger(snf, reg_offs, rx_len);
566 + nfi_read_data(snf, SNF_GPRAM + reg_offs, rx_buf, rx_len);
570 +static int mtk_snand_setup_pagefmt(struct mtk_snand *snf, u32 page_size,
573 + int spare_idx = -1;
574 + u32 spare_size, spare_size_shift, pagesize_idx;
575 + u32 sector_size_512;
579 + // skip if it's already configured as required.
580 + if (snf->nfi_cfg.page_size == page_size &&
581 + snf->nfi_cfg.oob_size == oob_size)
584 + nsectors = page_size / snf->caps->sector_size;
585 + if (nsectors > snf->caps->max_sectors) {
586 + dev_err(snf->dev, "too many sectors required.\n");
590 + if (snf->caps->sector_size == 512) {
591 + sector_size_512 = NFI_SEC_SEL_512;
592 + spare_size_shift = NFI_SPARE_SIZE_S;
594 + sector_size_512 = 0;
595 + spare_size_shift = NFI_SPARE_SIZE_LS_S;
598 + switch (page_size) {
600 + pagesize_idx = NFI_PAGE_SIZE_512_2K;
603 + if (snf->caps->sector_size == 512)
604 + pagesize_idx = NFI_PAGE_SIZE_2K_4K;
606 + pagesize_idx = NFI_PAGE_SIZE_512_2K;
609 + if (snf->caps->sector_size == 512)
610 + pagesize_idx = NFI_PAGE_SIZE_4K_8K;
612 + pagesize_idx = NFI_PAGE_SIZE_2K_4K;
615 + if (snf->caps->sector_size == 512)
616 + pagesize_idx = NFI_PAGE_SIZE_8K_16K;
618 + pagesize_idx = NFI_PAGE_SIZE_4K_8K;
621 + pagesize_idx = NFI_PAGE_SIZE_8K_16K;
624 + dev_err(snf->dev, "unsupported page size.\n");
628 + spare_size = oob_size / nsectors;
629 + // If we're using the 1KB sector size, HW will automatically double the
630 + // spare size. We should only use half of the value in this case.
631 + if (snf->caps->sector_size == 1024)
634 + for (i = snf->caps->num_spare_size - 1; i >= 0; i--) {
635 + if (snf->caps->spare_sizes[i] <= spare_size) {
636 + spare_size = snf->caps->spare_sizes[i];
637 + if (snf->caps->sector_size == 1024)
644 + if (spare_idx < 0) {
645 + dev_err(snf->dev, "unsupported spare size: %u\n", spare_size);
649 + nfi_write32(snf, NFI_PAGEFMT,
650 + (snf->caps->fdm_ecc_size << NFI_FDM_ECC_NUM_S) |
651 + (snf->caps->fdm_size << NFI_FDM_NUM_S) |
652 + (spare_idx << spare_size_shift) |
653 + (pagesize_idx << NFI_PAGE_SIZE_S) |
656 + snf->nfi_cfg.page_size = page_size;
657 + snf->nfi_cfg.oob_size = oob_size;
658 + snf->nfi_cfg.nsectors = nsectors;
659 + snf->nfi_cfg.spare_size = spare_size;
661 + dev_dbg(snf->dev, "page format: (%u + %u) * %u\n",
662 + snf->caps->sector_size, spare_size, nsectors);
663 + return snand_prepare_bouncebuf(snf, page_size + oob_size);
665 + dev_err(snf->dev, "page size %u + %u is not supported\n", page_size,
667 + return -EOPNOTSUPP;
670 +static int mtk_snand_ooblayout_ecc(struct mtd_info *mtd, int section,
671 + struct mtd_oob_region *oobecc)
673 + // ECC area is not accessible
677 +static int mtk_snand_ooblayout_free(struct mtd_info *mtd, int section,
678 + struct mtd_oob_region *oobfree)
680 + struct nand_device *nand = mtd_to_nanddev(mtd);
681 + struct mtk_snand *ms = nand_to_mtk_snand(nand);
683 + if (section >= ms->nfi_cfg.nsectors)
686 + oobfree->length = ms->caps->fdm_size - 1;
687 + oobfree->offset = section * ms->caps->fdm_size + 1;
691 +static const struct mtd_ooblayout_ops mtk_snand_ooblayout = {
692 + .ecc = mtk_snand_ooblayout_ecc,
693 + .free = mtk_snand_ooblayout_free,
696 +static int mtk_snand_ecc_init_ctx(struct nand_device *nand)
698 + struct mtk_snand *snf = nand_to_mtk_snand(nand);
699 + struct nand_ecc_props *conf = &nand->ecc.ctx.conf;
700 + struct nand_ecc_props *reqs = &nand->ecc.requirements;
701 + struct nand_ecc_props *user = &nand->ecc.user_conf;
702 + struct mtd_info *mtd = nanddev_to_mtd(nand);
703 + int step_size = 0, strength = 0, desired_correction = 0, steps;
704 + bool ecc_user = false;
706 + u32 parity_bits, max_ecc_bytes;
707 + struct mtk_ecc_config *ecc_cfg;
709 + ret = mtk_snand_setup_pagefmt(snf, nand->memorg.pagesize,
710 + nand->memorg.oobsize);
714 + ecc_cfg = kzalloc(sizeof(*ecc_cfg), GFP_KERNEL);
718 + nand->ecc.ctx.priv = ecc_cfg;
720 + if (user->step_size && user->strength) {
721 + step_size = user->step_size;
722 + strength = user->strength;
724 + } else if (reqs->step_size && reqs->strength) {
725 + step_size = reqs->step_size;
726 + strength = reqs->strength;
729 + if (step_size && strength) {
730 + steps = mtd->writesize / step_size;
731 + desired_correction = steps * strength;
732 + strength = desired_correction / snf->nfi_cfg.nsectors;
735 + ecc_cfg->mode = ECC_NFI_MODE;
736 + ecc_cfg->sectors = snf->nfi_cfg.nsectors;
737 + ecc_cfg->len = snf->caps->sector_size + snf->caps->fdm_ecc_size;
739 + // calculate the max possible strength under current page format
740 + parity_bits = mtk_ecc_get_parity_bits(snf->ecc);
741 + max_ecc_bytes = snf->nfi_cfg.spare_size - snf->caps->fdm_size;
742 + ecc_cfg->strength = max_ecc_bytes * 8 / parity_bits;
743 + mtk_ecc_adjust_strength(snf->ecc, &ecc_cfg->strength);
745 + // if there's a user requested strength, find the minimum strength that
746 + // meets the requirement. Otherwise use the maximum strength which is
747 + // expected by BootROM.
748 + if (ecc_user && strength) {
749 + u32 s_next = ecc_cfg->strength - 1;
752 + mtk_ecc_adjust_strength(snf->ecc, &s_next);
753 + if (s_next >= ecc_cfg->strength)
755 + if (s_next < strength)
757 + s_next = ecc_cfg->strength - 1;
761 + mtd_set_ooblayout(mtd, &mtk_snand_ooblayout);
763 + conf->step_size = snf->caps->sector_size;
764 + conf->strength = ecc_cfg->strength;
766 + if (ecc_cfg->strength < strength)
767 + dev_warn(snf->dev, "unable to fulfill ECC of %u bits.\n",
769 + dev_info(snf->dev, "ECC strength: %u bits per %u bytes\n",
770 + ecc_cfg->strength, snf->caps->sector_size);
775 +static void mtk_snand_ecc_cleanup_ctx(struct nand_device *nand)
777 + struct mtk_ecc_config *ecc_cfg = nand_to_ecc_ctx(nand);
782 +static int mtk_snand_ecc_prepare_io_req(struct nand_device *nand,
783 + struct nand_page_io_req *req)
785 + struct mtk_snand *snf = nand_to_mtk_snand(nand);
786 + struct mtk_ecc_config *ecc_cfg = nand_to_ecc_ctx(nand);
789 + ret = mtk_snand_setup_pagefmt(snf, nand->memorg.pagesize,
790 + nand->memorg.oobsize);
793 + snf->autofmt = true;
794 + snf->ecc_cfg = ecc_cfg;
798 +static int mtk_snand_ecc_finish_io_req(struct nand_device *nand,
799 + struct nand_page_io_req *req)
801 + struct mtk_snand *snf = nand_to_mtk_snand(nand);
802 + struct mtd_info *mtd = nanddev_to_mtd(nand);
804 + snf->ecc_cfg = NULL;
805 + snf->autofmt = false;
806 + if ((req->mode == MTD_OPS_RAW) || (req->type != NAND_PAGE_READ))
809 + if (snf->ecc_stats.failed)
810 + mtd->ecc_stats.failed += snf->ecc_stats.failed;
811 + mtd->ecc_stats.corrected += snf->ecc_stats.corrected;
812 + return snf->ecc_stats.failed ? -EBADMSG : snf->ecc_stats.bitflips;
815 +static struct nand_ecc_engine_ops mtk_snfi_ecc_engine_ops = {
816 + .init_ctx = mtk_snand_ecc_init_ctx,
817 + .cleanup_ctx = mtk_snand_ecc_cleanup_ctx,
818 + .prepare_io_req = mtk_snand_ecc_prepare_io_req,
819 + .finish_io_req = mtk_snand_ecc_finish_io_req,
822 +static void mtk_snand_read_fdm(struct mtk_snand *snf, u8 *buf)
828 + for (i = 0; i < snf->nfi_cfg.nsectors; i++) {
829 + vall = nfi_read32(snf, NFI_FDML(i));
830 + valm = nfi_read32(snf, NFI_FDMM(i));
832 + for (j = 0; j < snf->caps->fdm_size; j++)
833 + oobptr[j] = (j >= 4 ? valm : vall) >> ((j % 4) * 8);
835 + oobptr += snf->caps->fdm_size;
839 +static void mtk_snand_write_fdm(struct mtk_snand *snf, const u8 *buf)
841 + u32 fdm_size = snf->caps->fdm_size;
842 + const u8 *oobptr = buf;
846 + for (i = 0; i < snf->nfi_cfg.nsectors; i++) {
850 + for (j = 0; j < 8; j++) {
852 + vall |= (j < fdm_size ? oobptr[j] : 0xff)
855 + valm |= (j < fdm_size ? oobptr[j] : 0xff)
859 + nfi_write32(snf, NFI_FDML(i), vall);
860 + nfi_write32(snf, NFI_FDMM(i), valm);
862 + oobptr += fdm_size;
866 +static void mtk_snand_bm_swap(struct mtk_snand *snf, u8 *buf)
868 + u32 buf_bbm_pos, fdm_bbm_pos;
870 + if (!snf->caps->bbm_swap || snf->nfi_cfg.nsectors == 1)
873 + // swap [pagesize] byte on nand with the first fdm byte
874 + // in the last sector.
875 + buf_bbm_pos = snf->nfi_cfg.page_size -
876 + (snf->nfi_cfg.nsectors - 1) * snf->nfi_cfg.spare_size;
877 + fdm_bbm_pos = snf->nfi_cfg.page_size +
878 + (snf->nfi_cfg.nsectors - 1) * snf->caps->fdm_size;
880 + swap(snf->buf[fdm_bbm_pos], buf[buf_bbm_pos]);
883 +static void mtk_snand_fdm_bm_swap(struct mtk_snand *snf)
885 + u32 fdm_bbm_pos1, fdm_bbm_pos2;
887 + if (!snf->caps->bbm_swap || snf->nfi_cfg.nsectors == 1)
890 + // swap the first fdm byte in the first and the last sector.
891 + fdm_bbm_pos1 = snf->nfi_cfg.page_size;
892 + fdm_bbm_pos2 = snf->nfi_cfg.page_size +
893 + (snf->nfi_cfg.nsectors - 1) * snf->caps->fdm_size;
894 + swap(snf->buf[fdm_bbm_pos1], snf->buf[fdm_bbm_pos2]);
897 +static int mtk_snand_read_page_cache(struct mtk_snand *snf,
898 + const struct spi_mem_op *op)
900 + u8 *buf = snf->buf;
901 + u8 *buf_fdm = buf + snf->nfi_cfg.page_size;
902 + // the address part to be sent by the controller
903 + u32 op_addr = op->addr.val;
904 + // where to start copying data from bounce buffer
906 + u32 dummy_clk = (op->dummy.nbytes * BITS_PER_BYTE / op->dummy.buswidth);
908 + u32 dma_len = snf->buf_len;
910 + u32 rd_mode, rd_bytes, val;
911 + dma_addr_t buf_dma;
913 + if (snf->autofmt) {
917 + dma_len = snf->nfi_cfg.page_size;
918 + op_mode = CNFG_AUTO_FMT_EN;
920 + op_mode |= CNFG_HW_ECC_EN;
921 + // extract the plane bit:
922 + // Find the highest bit set in (pagesize+oobsize).
923 + // Bits higher than that in op->addr are kept and sent over SPI
924 + // Lower bits are used as an offset for copying data from DMA
926 + last_bit = fls(snf->nfi_cfg.page_size + snf->nfi_cfg.oob_size);
927 + mask = (1 << last_bit) - 1;
928 + rd_offset = op_addr & mask;
931 + // check if we can dma to the caller memory
932 + if (rd_offset == 0 && op->data.nbytes >= snf->nfi_cfg.page_size)
933 + buf = op->data.buf.in;
935 + mtk_snand_mac_reset(snf);
936 + mtk_nfi_reset(snf);
938 + // command and dummy cycles
939 + nfi_write32(snf, SNF_RD_CTL2,
940 + (dummy_clk << DATA_READ_DUMMY_S) |
941 + (op->cmd.opcode << DATA_READ_CMD_S));
944 + nfi_write32(snf, SNF_RD_CTL3, op_addr);
946 + // Set read op_mode
947 + if (op->data.buswidth == 4)
948 + rd_mode = op->addr.buswidth == 4 ? DATA_READ_MODE_QUAD :
950 + else if (op->data.buswidth == 2)
951 + rd_mode = op->addr.buswidth == 2 ? DATA_READ_MODE_DUAL :
954 + rd_mode = DATA_READ_MODE_X1;
955 + rd_mode <<= DATA_READ_MODE_S;
956 + nfi_rmw32(snf, SNF_MISC_CTL, DATA_READ_MODE,
957 + rd_mode | DATARD_CUSTOM_EN);
959 + // Set bytes to read
960 + rd_bytes = (snf->nfi_cfg.spare_size + snf->caps->sector_size) *
961 + snf->nfi_cfg.nsectors;
962 + nfi_write32(snf, SNF_MISC_CTL2,
963 + (rd_bytes << PROGRAM_LOAD_BYTE_NUM_S) | rd_bytes);
965 + // NFI read prepare
966 + nfi_write16(snf, NFI_CNFG,
967 + (CNFG_OP_MODE_CUST << CNFG_OP_MODE_S) | CNFG_DMA_BURST_EN |
968 + CNFG_READ_MODE | CNFG_DMA_MODE | op_mode);
970 + nfi_write32(snf, NFI_CON, (snf->nfi_cfg.nsectors << CON_SEC_NUM_S));
972 + buf_dma = dma_map_single(snf->dev, buf, dma_len, DMA_FROM_DEVICE);
973 + if (dma_mapping_error(snf->dev, buf_dma)) {
974 + dev_err(snf->dev, "DMA mapping failed.\n");
977 + nfi_write32(snf, NFI_STRADDR, buf_dma);
978 + if (op->data.ecc) {
979 + snf->ecc_cfg->op = ECC_DECODE;
980 + ret = mtk_ecc_enable(snf->ecc, snf->ecc_cfg);
984 + // Prepare for custom read interrupt
985 + nfi_write32(snf, NFI_INTR_EN, NFI_IRQ_INTR_EN | NFI_IRQ_CUS_READ);
986 + reinit_completion(&snf->op_done);
988 + // Trigger NFI into custom mode
989 + nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_READ);
992 + nfi_rmw32(snf, NFI_CON, 0, CON_BRD);
993 + nfi_write16(snf, NFI_STRDATA, STR_DATA);
995 + if (!wait_for_completion_timeout(
996 + &snf->op_done, usecs_to_jiffies(SNFI_POLL_INTERVAL))) {
997 + dev_err(snf->dev, "DMA timed out for reading from cache.\n");
1002 + // Wait for BUS_SEC_CNTR returning expected value
1003 + ret = readl_poll_timeout(snf->nfi_base + NFI_BYTELEN, val,
1004 + BUS_SEC_CNTR(val) >= snf->nfi_cfg.nsectors, 0,
1005 + SNFI_POLL_INTERVAL);
1007 + dev_err(snf->dev, "Timed out waiting for BUS_SEC_CNTR\n");
1011 + // Wait for bus becoming idle
1012 + ret = readl_poll_timeout(snf->nfi_base + NFI_MASTERSTA, val,
1013 + !(val & snf->caps->mastersta_mask), 0,
1014 + SNFI_POLL_INTERVAL);
1016 + dev_err(snf->dev, "Timed out waiting for bus becoming idle\n");
1020 + if (op->data.ecc) {
1021 + ret = mtk_ecc_wait_done(snf->ecc, ECC_DECODE);
1023 + dev_err(snf->dev, "wait ecc done timeout\n");
1026 + // save status before disabling ecc
1027 + mtk_ecc_get_stats(snf->ecc, &snf->ecc_stats,
1028 + snf->nfi_cfg.nsectors);
1031 + dma_unmap_single(snf->dev, buf_dma, dma_len, DMA_FROM_DEVICE);
1033 + if (snf->autofmt) {
1034 + mtk_snand_read_fdm(snf, buf_fdm);
1035 + if (snf->caps->bbm_swap) {
1036 + mtk_snand_bm_swap(snf, buf);
1037 + mtk_snand_fdm_bm_swap(snf);
1042 + if (nfi_read32(snf, NFI_STA) & READ_EMPTY) {
1043 + memset(op->data.buf.in, 0xff, op->data.nbytes);
1044 + snf->ecc_stats.bitflips = 0;
1045 + snf->ecc_stats.failed = 0;
1046 + snf->ecc_stats.corrected = 0;
1048 + if (buf == op->data.buf.in) {
1049 + u32 cap_len = snf->buf_len - snf->nfi_cfg.page_size;
1050 + u32 req_left = op->data.nbytes - snf->nfi_cfg.page_size;
1053 + memcpy(op->data.buf.in + snf->nfi_cfg.page_size,
1055 + cap_len < req_left ? cap_len : req_left);
1056 + } else if (rd_offset < snf->buf_len) {
1057 + u32 cap_len = snf->buf_len - rd_offset;
1059 + if (op->data.nbytes < cap_len)
1060 + cap_len = op->data.nbytes;
1061 + memcpy(op->data.buf.in, snf->buf + rd_offset, cap_len);
1066 + mtk_ecc_disable(snf->ecc);
1068 + // unmap dma only if any error happens. (otherwise it's done before
1071 + dma_unmap_single(snf->dev, buf_dma, dma_len, DMA_FROM_DEVICE);
1074 + nfi_write32(snf, NFI_CON, 0);
1075 + nfi_write16(snf, NFI_CNFG, 0);
1077 + // Clear SNF done flag
1078 + nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_READ_DONE);
1079 + nfi_write32(snf, SNF_STA_CTL1, 0);
1081 + // Disable interrupt
1082 + nfi_read32(snf, NFI_INTR_STA);
1083 + nfi_write32(snf, NFI_INTR_EN, 0);
1085 + nfi_rmw32(snf, SNF_MISC_CTL, DATARD_CUSTOM_EN, 0);
1089 +static int mtk_snand_write_page_cache(struct mtk_snand *snf,
1090 + const struct spi_mem_op *op)
1092 + // the address part to be sent by the controller
1093 + u32 op_addr = op->addr.val;
1094 + // where to start copying data from bounce buffer
1095 + u32 wr_offset = 0;
1099 + u32 dma_len = snf->buf_len;
1100 + u32 wr_bytes, val;
1102 + dma_addr_t buf_dma;
1104 + if (snf->autofmt) {
1108 + dma_len = snf->nfi_cfg.page_size;
1109 + op_mode = CNFG_AUTO_FMT_EN;
1111 + op_mode |= CNFG_HW_ECC_EN;
1113 + last_bit = fls(snf->nfi_cfg.page_size + snf->nfi_cfg.oob_size);
1114 + mask = (1 << last_bit) - 1;
1115 + wr_offset = op_addr & mask;
1118 + mtk_snand_mac_reset(snf);
1119 + mtk_nfi_reset(snf);
1122 + memset(snf->buf, 0xff, wr_offset);
1124 + cap_len = snf->buf_len - wr_offset;
1125 + if (op->data.nbytes < cap_len)
1126 + cap_len = op->data.nbytes;
1127 + memcpy(snf->buf + wr_offset, op->data.buf.out, cap_len);
1128 + if (snf->autofmt) {
1129 + if (snf->caps->bbm_swap) {
1130 + mtk_snand_fdm_bm_swap(snf);
1131 + mtk_snand_bm_swap(snf, snf->buf);
1133 + mtk_snand_write_fdm(snf, snf->buf + snf->nfi_cfg.page_size);
1137 + nfi_write32(snf, SNF_PG_CTL1, (op->cmd.opcode << PG_LOAD_CMD_S));
1140 + nfi_write32(snf, SNF_PG_CTL2, op_addr);
1142 + // Set read op_mode
1143 + if (op->data.buswidth == 4)
1144 + wr_mode = PG_LOAD_X4_EN;
1146 + nfi_rmw32(snf, SNF_MISC_CTL, PG_LOAD_X4_EN,
1147 + wr_mode | PG_LOAD_CUSTOM_EN);
1149 + // Set bytes to write
1150 + wr_bytes = (snf->nfi_cfg.spare_size + snf->caps->sector_size) *
1151 + snf->nfi_cfg.nsectors;
1152 + nfi_write32(snf, SNF_MISC_CTL2,
1153 + (wr_bytes << PROGRAM_LOAD_BYTE_NUM_S) | wr_bytes);
1155 + // NFI write prepare
1156 + nfi_write16(snf, NFI_CNFG,
1157 + (CNFG_OP_MODE_PROGRAM << CNFG_OP_MODE_S) |
1158 + CNFG_DMA_BURST_EN | CNFG_DMA_MODE | op_mode);
1160 + nfi_write32(snf, NFI_CON, (snf->nfi_cfg.nsectors << CON_SEC_NUM_S));
1161 + buf_dma = dma_map_single(snf->dev, snf->buf, dma_len, DMA_TO_DEVICE);
1162 + if (dma_mapping_error(snf->dev, buf_dma)) {
1163 + dev_err(snf->dev, "DMA mapping failed.\n");
1166 + nfi_write32(snf, NFI_STRADDR, buf_dma);
1167 + if (op->data.ecc) {
1168 + snf->ecc_cfg->op = ECC_ENCODE;
1169 + ret = mtk_ecc_enable(snf->ecc, snf->ecc_cfg);
1173 + // Prepare for custom write interrupt
1174 + nfi_write32(snf, NFI_INTR_EN, NFI_IRQ_INTR_EN | NFI_IRQ_CUS_PG);
1175 + reinit_completion(&snf->op_done);
1178 + // Trigger NFI into custom mode
1179 + nfi_write16(snf, NFI_CMD, NFI_CMD_DUMMY_WRITE);
1181 + // Start DMA write
1182 + nfi_rmw32(snf, NFI_CON, 0, CON_BWR);
1183 + nfi_write16(snf, NFI_STRDATA, STR_DATA);
1185 + if (!wait_for_completion_timeout(
1186 + &snf->op_done, usecs_to_jiffies(SNFI_POLL_INTERVAL))) {
1187 + dev_err(snf->dev, "DMA timed out for program load.\n");
1192 + // Wait for NFI_SEC_CNTR returning expected value
1193 + ret = readl_poll_timeout(snf->nfi_base + NFI_ADDRCNTR, val,
1194 + NFI_SEC_CNTR(val) >= snf->nfi_cfg.nsectors, 0,
1195 + SNFI_POLL_INTERVAL);
1197 + dev_err(snf->dev, "Timed out waiting for NFI_SEC_CNTR\n");
1201 + mtk_ecc_disable(snf->ecc);
1203 + dma_unmap_single(snf->dev, buf_dma, dma_len, DMA_TO_DEVICE);
1206 + nfi_write32(snf, NFI_CON, 0);
1207 + nfi_write16(snf, NFI_CNFG, 0);
1209 + // Clear SNF done flag
1210 + nfi_rmw32(snf, SNF_STA_CTL1, 0, CUS_PG_DONE);
1211 + nfi_write32(snf, SNF_STA_CTL1, 0);
1213 + // Disable interrupt
1214 + nfi_read32(snf, NFI_INTR_STA);
1215 + nfi_write32(snf, NFI_INTR_EN, 0);
1217 + nfi_rmw32(snf, SNF_MISC_CTL, PG_LOAD_CUSTOM_EN, 0);
1223 + * mtk_snand_is_page_ops() - check if the op is a controller supported page op.
1224 + * @op spi-mem op to check
1226 + * Check whether op can be executed with read_from_cache or program_load
1227 + * mode in the controller.
1228 + * This controller can execute typical Read From Cache and Program Load
1229 + * instructions found on SPI-NAND with 2-byte address.
1230 + * DTR and cmd buswidth & nbytes should be checked before calling this.
1232 + * Return: true if the op matches the instruction template
1234 +static bool mtk_snand_is_page_ops(const struct spi_mem_op *op)
1236 + if (op->addr.nbytes != 2)
1239 + if (op->addr.buswidth != 1 && op->addr.buswidth != 2 &&
1240 + op->addr.buswidth != 4)
1243 + // match read from page instructions
1244 + if (op->data.dir == SPI_MEM_DATA_IN) {
1245 + // check dummy cycle first
1246 + if (op->dummy.nbytes * BITS_PER_BYTE / op->dummy.buswidth >
1247 + DATA_READ_MAX_DUMMY)
1249 + // quad io / quad out
1250 + if ((op->addr.buswidth == 4 || op->addr.buswidth == 1) &&
1251 + op->data.buswidth == 4)
1254 + // dual io / dual out
1255 + if ((op->addr.buswidth == 2 || op->addr.buswidth == 1) &&
1256 + op->data.buswidth == 2)
1260 + if (op->addr.buswidth == 1 && op->data.buswidth == 1)
1262 + } else if (op->data.dir == SPI_MEM_DATA_OUT) {
1263 + // check dummy cycle first
1264 + if (op->dummy.nbytes)
1266 + // program load quad out
1267 + if (op->addr.buswidth == 1 && op->data.buswidth == 4)
1270 + if (op->addr.buswidth == 1 && op->data.buswidth == 1)
1276 +static bool mtk_snand_supports_op(struct spi_mem *mem,
1277 + const struct spi_mem_op *op)
1279 + if (!spi_mem_default_supports_op(mem, op))
1281 + if (op->cmd.nbytes != 1 || op->cmd.buswidth != 1)
1283 + if (mtk_snand_is_page_ops(op))
1285 + return ((op->addr.nbytes == 0 || op->addr.buswidth == 1) &&
1286 + (op->dummy.nbytes == 0 || op->dummy.buswidth == 1) &&
1287 + (op->data.nbytes == 0 || op->data.buswidth == 1));
1290 +static int mtk_snand_adjust_op_size(struct spi_mem *mem, struct spi_mem_op *op)
1292 + struct mtk_snand *ms = spi_controller_get_devdata(mem->spi->master);
1293 + // page ops transfer size must be exactly ((sector_size + spare_size) *
1294 + // nsectors). Limit the op size if the caller requests more than that.
1295 + // exec_op will read more than needed and discard the leftover if the
1296 + // caller requests less data.
1297 + if (mtk_snand_is_page_ops(op)) {
1299 + // skip adjust_op_size for page ops
1302 + l = ms->caps->sector_size + ms->nfi_cfg.spare_size;
1303 + l *= ms->nfi_cfg.nsectors;
1304 + if (op->data.nbytes > l)
1305 + op->data.nbytes = l;
1307 + size_t hl = op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes;
1309 + if (hl >= SNF_GPRAM_SIZE)
1310 + return -EOPNOTSUPP;
1311 + if (op->data.nbytes > SNF_GPRAM_SIZE - hl)
1312 + op->data.nbytes = SNF_GPRAM_SIZE - hl;
1317 +static int mtk_snand_exec_op(struct spi_mem *mem, const struct spi_mem_op *op)
1319 + struct mtk_snand *ms = spi_controller_get_devdata(mem->spi->master);
1321 + dev_dbg(ms->dev, "OP %02x ADDR %08llX@%d:%u DATA %d:%u", op->cmd.opcode,
1322 + op->addr.val, op->addr.buswidth, op->addr.nbytes,
1323 + op->data.buswidth, op->data.nbytes);
1324 + if (mtk_snand_is_page_ops(op)) {
1325 + if (op->data.dir == SPI_MEM_DATA_IN)
1326 + return mtk_snand_read_page_cache(ms, op);
1328 + return mtk_snand_write_page_cache(ms, op);
1330 + return mtk_snand_mac_io(ms, op);
1334 +static const struct spi_controller_mem_ops mtk_snand_mem_ops = {
1335 + .adjust_op_size = mtk_snand_adjust_op_size,
1336 + .supports_op = mtk_snand_supports_op,
1337 + .exec_op = mtk_snand_exec_op,
1340 +static const struct spi_controller_mem_caps mtk_snand_mem_caps = {
1344 +static irqreturn_t mtk_snand_irq(int irq, void *id)
1346 + struct mtk_snand *snf = id;
1349 + sta = nfi_read32(snf, NFI_INTR_STA);
1350 + ien = nfi_read32(snf, NFI_INTR_EN);
1355 + nfi_write32(snf, NFI_INTR_EN, 0);
1356 + complete(&snf->op_done);
1357 + return IRQ_HANDLED;
1360 +static const struct of_device_id mtk_snand_ids[] = {
1361 + { .compatible = "mediatek,mt7622-snand", .data = &mt7622_snand_caps },
1362 + { .compatible = "mediatek,mt7629-snand", .data = &mt7629_snand_caps },
1366 +MODULE_DEVICE_TABLE(of, mtk_snand_ids);
1368 +static int mtk_snand_enable_clk(struct mtk_snand *ms)
1372 + ret = clk_prepare_enable(ms->nfi_clk);
1374 + dev_err(ms->dev, "unable to enable nfi clk\n");
1377 + ret = clk_prepare_enable(ms->pad_clk);
1379 + dev_err(ms->dev, "unable to enable pad clk\n");
1384 + clk_disable_unprepare(ms->nfi_clk);
1388 +static void mtk_snand_disable_clk(struct mtk_snand *ms)
1390 + clk_disable_unprepare(ms->pad_clk);
1391 + clk_disable_unprepare(ms->nfi_clk);
1394 +static int mtk_snand_probe(struct platform_device *pdev)
1396 + struct device_node *np = pdev->dev.of_node;
1397 + const struct of_device_id *dev_id;
1398 + struct spi_controller *ctlr;
1399 + struct mtk_snand *ms;
1402 + dev_id = of_match_node(mtk_snand_ids, np);
1406 + ctlr = devm_spi_alloc_master(&pdev->dev, sizeof(*ms));
1409 + platform_set_drvdata(pdev, ctlr);
1411 + ms = spi_controller_get_devdata(ctlr);
1414 + ms->caps = dev_id->data;
1416 + ms->ecc = of_mtk_ecc_get(np);
1417 + if (IS_ERR(ms->ecc))
1418 + return PTR_ERR(ms->ecc);
1419 + else if (!ms->ecc)
1422 + ms->nfi_base = devm_platform_ioremap_resource(pdev, 0);
1423 + if (IS_ERR(ms->nfi_base)) {
1424 + ret = PTR_ERR(ms->nfi_base);
1428 + ms->dev = &pdev->dev;
1430 + ms->nfi_clk = devm_clk_get(&pdev->dev, "nfi_clk");
1431 + if (IS_ERR(ms->nfi_clk)) {
1432 + ret = PTR_ERR(ms->nfi_clk);
1433 + dev_err(&pdev->dev, "unable to get nfi_clk, err = %d\n", ret);
1437 + ms->pad_clk = devm_clk_get(&pdev->dev, "pad_clk");
1438 + if (IS_ERR(ms->pad_clk)) {
1439 + ret = PTR_ERR(ms->pad_clk);
1440 + dev_err(&pdev->dev, "unable to get pad_clk, err = %d\n", ret);
1444 + ret = mtk_snand_enable_clk(ms);
1448 + init_completion(&ms->op_done);
1450 + ms->irq = platform_get_irq(pdev, 0);
1451 + if (ms->irq < 0) {
1455 + ret = devm_request_irq(ms->dev, ms->irq, mtk_snand_irq, 0x0,
1458 + dev_err(ms->dev, "failed to request snfi irq\n");
1462 + ret = dma_set_mask(ms->dev, DMA_BIT_MASK(32));
1464 + dev_err(ms->dev, "failed to set dma mask\n");
1468 + // switch to SNFI mode
1469 + nfi_write32(ms, SNF_CFG, SPI_MODE);
1471 + // setup an initial page format for ops matching page_cache_op template
1472 + // before ECC is called.
1473 + ret = mtk_snand_setup_pagefmt(ms, ms->caps->sector_size,
1474 + ms->caps->spare_sizes[0]);
1476 + dev_err(ms->dev, "failed to set initial page format\n");
1480 + // setup ECC engine
1481 + ms->ecc_eng.dev = &pdev->dev;
1482 + ms->ecc_eng.integration = NAND_ECC_ENGINE_INTEGRATION_PIPELINED;
1483 + ms->ecc_eng.ops = &mtk_snfi_ecc_engine_ops;
1484 + ms->ecc_eng.priv = ms;
1486 + ret = nand_ecc_register_on_host_hw_engine(&ms->ecc_eng);
1488 + dev_err(&pdev->dev, "failed to register ecc engine.\n");
1492 + ctlr->num_chipselect = 1;
1493 + ctlr->mem_ops = &mtk_snand_mem_ops;
1494 + ctlr->mem_caps = &mtk_snand_mem_caps;
1495 + ctlr->bits_per_word_mask = SPI_BPW_MASK(8);
1496 + ctlr->mode_bits = SPI_RX_DUAL | SPI_RX_QUAD | SPI_TX_DUAL | SPI_TX_QUAD;
1497 + ctlr->dev.of_node = pdev->dev.of_node;
1498 + ret = spi_register_controller(ctlr);
1500 + dev_err(&pdev->dev, "spi_register_controller failed.\n");
1506 + mtk_snand_disable_clk(ms);
1508 + mtk_ecc_release(ms->ecc);
1512 +static int mtk_snand_remove(struct platform_device *pdev)
1514 + struct spi_controller *ctlr = platform_get_drvdata(pdev);
1515 + struct mtk_snand *ms = spi_controller_get_devdata(ctlr);
1517 + spi_unregister_controller(ctlr);
1518 + mtk_snand_disable_clk(ms);
1519 + mtk_ecc_release(ms->ecc);
1524 +static struct platform_driver mtk_snand_driver = {
1525 + .probe = mtk_snand_probe,
1526 + .remove = mtk_snand_remove,
1528 + .name = "mtk-snand",
1529 + .of_match_table = mtk_snand_ids,
1533 +module_platform_driver(mtk_snand_driver);
1535 +MODULE_LICENSE("GPL");
1536 +MODULE_AUTHOR("Chuanhong Guo <gch981213@gmail.com>");
1537 +MODULE_DESCRIPTION("MeidaTek SPI-NAND Flash Controller Driver");