1 From 24db36ad20239841b897efb41442841ebf5d2f78 Mon Sep 17 00:00:00 2001
2 From: Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
3 Date: Wed, 2 Mar 2016 12:00:12 -0500
4 Subject: [PATCH 58/91] mtd: mediatek: driver for MTK Smart Device Gen1 NAND
6 This patch adds support for mediatek's SDG1 NFC nand controller
9 UBIFS support has been successfully tested.
11 Signed-off-by: Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
13 drivers/mtd/nand/Kconfig | 6 +
14 drivers/mtd/nand/Makefile | 1 +
15 drivers/mtd/nand/mtksdg1_nand.c | 1535 +++++++++++++++++++++++++++++++++++
16 drivers/mtd/nand/mtksdg1_nand_ecc.h | 75 ++
17 drivers/mtd/nand/mtksdg1_nand_nfi.h | 119 +++
18 5 files changed, 1736 insertions(+)
19 create mode 100644 drivers/mtd/nand/mtksdg1_nand.c
20 create mode 100644 drivers/mtd/nand/mtksdg1_nand_ecc.h
21 create mode 100644 drivers/mtd/nand/mtksdg1_nand_nfi.h
23 --- a/drivers/mtd/nand/Kconfig
24 +++ b/drivers/mtd/nand/Kconfig
25 @@ -546,4 +546,10 @@ config MTD_NAND_HISI504
27 Enables support for NAND controller on Hisilicon SoC Hip04.
29 +config MTD_NAND_MTKSDG1
30 + tristate "Support for NAND controller on MTK Smart Device SoCs"
33 + Enables support for NAND controller on MTK Smart Device SoCs.
36 --- a/drivers/mtd/nand/Makefile
37 +++ b/drivers/mtd/nand/Makefile
38 @@ -55,5 +55,6 @@ obj-$(CONFIG_MTD_NAND_BCM47XXNFLASH) +=
39 obj-$(CONFIG_MTD_NAND_SUNXI) += sunxi_nand.o
40 obj-$(CONFIG_MTD_NAND_HISI504) += hisi504_nand.o
41 obj-$(CONFIG_MTD_NAND_BRCMNAND) += brcmnand/
42 +obj-$(CONFIG_MTD_NAND_MTKSDG1) += mtksdg1_nand.o
44 nand-objs := nand_base.o nand_bbt.o nand_timings.o
46 +++ b/drivers/mtd/nand/mtksdg1_nand.c
49 + * MTK smart device NAND Flash controller driver.
50 + * Copyright (C) 2015-2016 MediaTek Inc.
51 + * Authors: Xiaolei Li <xiaolei.li@mediatek.com>
52 + * Jorge Ramirez-Ortiz <jorge.ramirez-ortiz@linaro.org>
54 + * This program is free software; you can redistribute it and/or modify
55 + * it under the terms of the GNU General Public License version 2 as
56 + * published by the Free Software Foundation.
58 + * This program is distributed in the hope that it will be useful,
59 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
60 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
61 + * GNU General Public License for more details.
64 +#include <linux/platform_device.h>
65 +#include <linux/dma-mapping.h>
66 +#include <linux/interrupt.h>
67 +#include <linux/of_mtd.h>
68 +#include <linux/delay.h>
69 +#include <linux/clk.h>
70 +#include <linux/mtd/partitions.h>
71 +#include <linux/mtd/nand.h>
72 +#include <linux/mtd/mtd.h>
73 +#include <linux/module.h>
75 +#include "mtksdg1_nand_nfi.h"
76 +#include "mtksdg1_nand_ecc.h"
78 +#define MTK_IRQ_ECC "mtksdg1-nand-ecc"
79 +#define MTK_IRQ_NFI "mtksdg1-nand-nfi"
80 +#define MTK_NAME "mtksdg1-nand"
82 +#define KB(x) ((x) * 1024UL)
83 +#define MB(x) (KB(x) * 1024UL)
85 +#define SECTOR_SHIFT (10)
86 +#define SECTOR_SIZE (1UL << SECTOR_SHIFT)
87 +#define BYTES_TO_SECTORS(x) ((x) >> SECTOR_SHIFT)
88 +#define SECTORS_TO_BYTES(x) ((x) << SECTOR_SHIFT)
90 +#define MTK_TIMEOUT (500)
91 +#define MTK_RESET_TIMEOUT (1 * HZ)
93 +#define MTK_ECC_PARITY_BITS (14)
94 +#define MTK_NAND_MAX_CHIP (2)
96 +#define MTK_OOB_ON (1)
97 +#define MTK_OOB_OFF (0)
99 +/* raw accesses do not use ECC (ecc = !raw) */
100 +#define MTK_ECC_OFF (1)
101 +#define MTK_ECC_ON (0)
103 +struct mtk_nfc_clk {
104 + struct clk *nfiecc_clk;
105 + struct clk *nfi_clk;
106 + struct clk *pad_clk;
109 +struct mtk_nfc_saved_reg {
123 +struct mtk_nfc_host {
124 + struct mtk_nfc_clk clk;
125 + struct nand_chip chip;
126 + struct device *dev;
129 + struct completion complete;
130 + void __iomem *base;
134 + struct completion complete;
135 + void __iomem *base;
139 + u32 fdm_reg[MTKSDG1_NFI_FDM_REG_SIZE / sizeof(u32)];
144 +#ifdef CONFIG_PM_SLEEP
145 + struct mtk_nfc_saved_reg saved_reg;
149 +static struct nand_ecclayout nand_2k_64 = {
150 + .oobfree = { {0, 16} },
153 +static struct nand_ecclayout nand_4k_128 = {
154 + .oobfree = { {0, 32} },
157 +/* NFI register access */
158 +static inline void mtk_nfi_writel(struct mtk_nfc_host *host, u32 val, u32 reg)
160 + writel(val, host->nfi.base + reg);
162 +static inline void mtk_nfi_writew(struct mtk_nfc_host *host, u16 val, u32 reg)
164 + writew(val, host->nfi.base + reg);
166 +static inline u32 mtk_nfi_readl(struct mtk_nfc_host *host, u32 reg)
168 + return readl_relaxed(host->nfi.base + reg);
170 +static inline u16 mtk_nfi_readw(struct mtk_nfc_host *host, u32 reg)
172 + return readw_relaxed(host->nfi.base + reg);
174 +static inline u8 mtk_nfi_readb(struct mtk_nfc_host *host, u32 reg)
176 + return readb_relaxed(host->nfi.base + reg);
179 +/* ECC register access */
180 +static inline void mtk_ecc_writel(struct mtk_nfc_host *host, u32 val, u32 reg)
182 + writel(val, host->ecc.base + reg);
184 +static inline void mtk_ecc_writew(struct mtk_nfc_host *host, u16 val, u32 reg)
186 + writew(val, host->ecc.base + reg);
188 +static inline u32 mtk_ecc_readl(struct mtk_nfc_host *host, u32 reg)
190 + return readl_relaxed(host->ecc.base + reg);
192 +static inline u16 mtk_ecc_readw(struct mtk_nfc_host *host, u32 reg)
194 + return readw_relaxed(host->ecc.base + reg);
197 +static void mtk_nfc_hw_reset(struct mtk_nfc_host *host)
199 + unsigned long timeout = MTK_RESET_TIMEOUT;
200 + struct device *dev = host->dev;
203 + /* reset the state machine, data fifo and fdm data */
204 + mtk_nfi_writel(host, CON_FIFO_FLUSH | CON_NFI_RST, MTKSDG1_NFI_CON);
205 + timeout += jiffies;
207 + val = mtk_nfi_readl(host, MTKSDG1_NFI_MASTER_STA);
208 + val &= MASTER_STA_MASK;
211 + usleep_range(50, 100);
213 + } while (time_before(jiffies, timeout));
215 + dev_warn(dev, "nfi master active after in reset [0x%x] = 0x%x\n",
216 + MTKSDG1_NFI_MASTER_STA, val);
219 +static int mtk_nfc_set_command(struct mtk_nfc_host *host, u8 command)
221 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
222 + struct device *dev = host->dev;
225 + mtk_nfi_writel(host, command, MTKSDG1_NFI_CMD);
227 + /* wait for the NFI core to enter command mode */
228 + timeout += jiffies;
230 + val = mtk_nfi_readl(host, MTKSDG1_NFI_STA);
236 + } while (time_before(jiffies, timeout));
237 + dev_warn(dev, "nfi core timed out entering command mode\n");
242 +static int mtk_nfc_set_address(struct mtk_nfc_host *host, u32 column, u32 row,
243 + u8 colnob, u8 row_nob)
245 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
246 + struct device *dev = host->dev;
249 + addr_nob = colnob | (row_nob << ADDR_ROW_NOB_SHIFT);
250 + mtk_nfi_writel(host, column, MTKSDG1_NFI_COLADDR);
251 + mtk_nfi_writel(host, row, MTKSDG1_NFI_ROWADDR);
252 + mtk_nfi_writel(host, addr_nob, MTKSDG1_NFI_ADDRNOB);
254 + /* wait for the NFI core to enter address mode */
255 + timeout += jiffies;
257 + val = mtk_nfi_readl(host, MTKSDG1_NFI_STA);
263 + } while (time_before(jiffies, timeout));
265 + dev_warn(dev, "nfi core timed out entering address mode\n");
270 +static inline void mtk_ecc_encoder_idle(struct mtk_nfc_host *host)
272 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
273 + struct device *dev = host->dev;
276 + timeout += jiffies;
278 + val = mtk_ecc_readl(host, MTKSDG1_ECC_ENCIDLE);
284 + } while (time_before(jiffies, timeout));
286 + dev_warn(dev, "hw init ecc encoder not idle\n");
289 +static inline void mtk_ecc_decoder_idle(struct mtk_nfc_host *host)
291 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
292 + struct device *dev = host->dev;
295 + timeout += jiffies;
297 + val = mtk_ecc_readw(host, MTKSDG1_ECC_DECIDLE);
303 + } while (time_before(jiffies, timeout));
305 + dev_warn(dev, "hw init ecc decoder not idle\n");
308 +static int mtk_nfc_transfer_done(struct mtk_nfc_host *host, u32 sectors)
310 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
313 + /* wait for the sector count */
314 + timeout += jiffies;
316 + cnt = mtk_nfi_readl(host, MTKSDG1_NFI_ADDRCNTR);
318 + if (cnt >= sectors)
322 + } while (time_before(jiffies, timeout));
327 +static int mtk_nfc_subpage_done(struct mtk_nfc_host *host, int sectors)
329 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
332 + timeout += jiffies;
334 + val = mtk_nfi_readl(host, MTKSDG1_NFI_BYTELEN);
336 + if (val >= sectors)
340 + } while (time_before(jiffies, timeout));
345 +static inline int mtk_nfc_data_ready(struct mtk_nfc_host *host)
347 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
350 + timeout += jiffies;
352 + val = mtk_nfi_readw(host, MTKSDG1_NFI_PIO_DIRDY);
358 + } while (time_before(jiffies, timeout));
360 + /* data _MUST_ not be accessed */
364 +static int mtk_nfc_hw_runtime_config(struct mtd_info *mtd)
366 + struct nand_chip *chip = mtd_to_nand(mtd);
367 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
368 + struct device *dev = host->dev;
369 + u32 dec_size, enc_size;
370 + u32 ecc_bit, ecc_level;
375 + if (chip->chipsize > MB(32))
376 + host->row_nob = chip->chipsize > MB(128) ? 3 : 2;
378 + spare = mtd->oobsize / BYTES_TO_SECTORS(mtd->writesize);
381 + ecc_bit = ECC_CNFG_4BIT;
385 + ecc_bit = ECC_CNFG_12BIT;
389 + dev_err(dev, "invalid spare size per sector: %d\n", spare);
393 + chip->ecc.strength = ecc_level;
394 + chip->ecc.size = SECTOR_SIZE;
396 + switch (mtd->writesize) {
398 + fmt = PAGEFMT_512_2K;
399 + chip->ecc.layout = &nand_2k_64;
402 + fmt = PAGEFMT_2K_4K;
403 + chip->ecc.layout = &nand_4k_128;
406 + fmt = PAGEFMT_4K_8K;
409 + dev_err(dev, "invalid page size: %d\n", mtd->writesize);
413 + /* configure PAGE FMT */
415 + reg |= PAGEFMT_SPARE_16 << PAGEFMT_SPARE_SHIFT;
416 + reg |= MTKSDG1_NFI_FDM_REG_SIZE << PAGEFMT_FDM_SHIFT;
417 + reg |= MTKSDG1_NFI_FDM_REG_SIZE << PAGEFMT_FDM_ECC_SHIFT;
418 + mtk_nfi_writew(host, reg, MTKSDG1_NFI_PAGEFMT);
420 + /* configure ECC encoder (in bits) */
421 + enc_size = (SECTOR_SIZE + MTKSDG1_NFI_FDM_REG_SIZE) << 3;
422 + reg = ecc_bit | ECC_NFI_MODE | (enc_size << ECC_MS_SHIFT);
423 + mtk_ecc_writel(host, reg, MTKSDG1_ECC_ENCCNFG);
425 + /* configure ECC decoder (inbits) */
426 + dec_size = enc_size + ecc_level * MTK_ECC_PARITY_BITS;
427 + reg = ecc_bit | ECC_NFI_MODE | (dec_size << ECC_MS_SHIFT);
428 + reg |= (DEC_CNFG_CORRECT | DEC_EMPTY_EN);
429 + mtk_ecc_writel(host, reg, MTKSDG1_ECC_DECCNFG);
434 +static void mtk_nfc_device_reset(struct mtk_nfc_host *host)
436 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
437 + struct device *dev = host->dev;
441 + mtk_nfc_hw_reset(host);
443 + /* enable reset done interrupt */
444 + mtk_nfi_writew(host, INTR_RST_DONE_EN, MTKSDG1_NFI_INTR_EN);
446 + /* configure FSM for reset operation */
447 + mtk_nfi_writew(host, CNFG_OP_RESET, MTKSDG1_NFI_CNFG);
449 + init_completion(&host->nfi.complete);
451 + mtk_nfc_set_command(host, NAND_CMD_RESET);
452 + rc = wait_for_completion_timeout(&host->nfi.complete, timeout);
454 + chip = mtk_nfi_readw(host, MTKSDG1_NFI_CSEL);
455 + dev_err(dev, "device(%d) reset timeout\n", chip);
459 +static void mtk_nfc_select_chip(struct mtd_info *mtd, int chip)
461 + struct nand_chip *nand = mtd_to_nand(mtd);
462 + struct mtk_nfc_host *host = nand_get_controller_data(nand);
467 + mtk_nfi_writel(host, chip, MTKSDG1_NFI_CSEL);
470 +static inline bool mtk_nfc_cmd_supported(unsigned command)
473 + case NAND_CMD_RESET:
474 + case NAND_CMD_READID:
475 + case NAND_CMD_STATUS:
476 + case NAND_CMD_READOOB:
477 + case NAND_CMD_ERASE1:
478 + case NAND_CMD_ERASE2:
479 + case NAND_CMD_SEQIN:
480 + case NAND_CMD_PAGEPROG:
481 + case NAND_CMD_CACHEDPROG:
482 + case NAND_CMD_READ0:
489 +static void mtk_nfc_cmdfunc(struct mtd_info *mtd, unsigned command, int column,
492 + struct mtk_nfc_host *host = nand_get_controller_data(mtd_to_nand(mtd));
493 + unsigned long const cmd_timeout = msecs_to_jiffies(MTK_TIMEOUT);
494 + struct completion *p = &host->nfi.complete;
498 + if (mtk_nfc_cmd_supported(command))
499 + mtk_nfc_hw_reset(host);
502 + case NAND_CMD_RESET:
503 + mtk_nfc_device_reset(host);
505 + case NAND_CMD_READID:
506 + val = CNFG_READ_EN | CNFG_BYTE_RW | CNFG_OP_SRD;
507 + mtk_nfi_writew(host, val, MTKSDG1_NFI_CNFG);
508 + mtk_nfc_set_command(host, NAND_CMD_READID);
509 + mtk_nfc_set_address(host, column, 0, 1, 0);
510 + mtk_nfi_writel(host, CON_SRD, MTKSDG1_NFI_CON);
512 + case NAND_CMD_STATUS:
513 + val = CNFG_READ_EN | CNFG_BYTE_RW | CNFG_OP_SRD;
514 + mtk_nfi_writew(host, val, MTKSDG1_NFI_CNFG);
515 + mtk_nfc_set_command(host, NAND_CMD_STATUS);
516 + mtk_nfi_writel(host, CON_SRD, MTKSDG1_NFI_CON);
518 + case NAND_CMD_READOOB:
519 + val = CNFG_READ_EN | CNFG_BYTE_RW | CNFG_OP_READ;
520 + mtk_nfi_writew(host, val, MTKSDG1_NFI_CNFG);
521 + mtk_nfc_set_command(host, NAND_CMD_READ0);
522 + column += mtd->writesize;
523 + mtk_nfc_set_address(host, column, page_addr, 2, host->row_nob);
524 + val = CON_BRD | (1 << CON_SEC_SHIFT);
525 + mtk_nfi_writel(host, val, MTKSDG1_NFI_CON);
527 + case NAND_CMD_ERASE1:
528 + mtk_nfi_writew(host, INTR_ERS_DONE_EN, MTKSDG1_NFI_INTR_EN);
529 + mtk_nfi_writew(host, CNFG_OP_ERASE, MTKSDG1_NFI_CNFG);
530 + mtk_nfc_set_command(host, NAND_CMD_ERASE1);
531 + mtk_nfc_set_address(host, 0, page_addr, 0, host->row_nob);
533 + case NAND_CMD_ERASE2:
534 + init_completion(p);
535 + mtk_nfc_set_command(host, NAND_CMD_ERASE2);
536 + rc = wait_for_completion_timeout(p, cmd_timeout);
538 + dev_err(host->dev, "erase command timeout\n");
540 + case NAND_CMD_SEQIN:
541 + mtk_nfi_writew(host, CNFG_OP_PRGM, MTKSDG1_NFI_CNFG);
542 + mtk_nfc_set_command(host, NAND_CMD_SEQIN);
543 + mtk_nfc_set_address(host, column, page_addr, 2, host->row_nob);
545 + case NAND_CMD_PAGEPROG:
546 + case NAND_CMD_CACHEDPROG:
547 + mtk_nfi_writew(host, INTR_BUSY_RT_EN, MTKSDG1_NFI_INTR_EN);
548 + init_completion(p);
549 + mtk_nfc_set_command(host, command);
550 + rc = wait_for_completion_timeout(p, cmd_timeout);
552 + dev_err(host->dev, "pageprogr command timeout\n");
554 + case NAND_CMD_READ0:
555 + val = CNFG_OP_READ | CNFG_READ_EN;
556 + mtk_nfi_writew(host, val, MTKSDG1_NFI_CNFG);
557 + mtk_nfc_set_command(host, NAND_CMD_READ0);
560 + dev_warn(host->dev, "command 0x%x not supported\n", command);
565 +static uint8_t mtk_nfc_read_byte(struct mtd_info *mtd)
567 + struct nand_chip *chip = mtd_to_nand(mtd);
568 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
571 + rc = mtk_nfc_data_ready(host);
573 + dev_err(host->dev, "data not ready\n");
574 + return NAND_STATUS_FAIL;
577 + return mtk_nfi_readb(host, MTKSDG1_NFI_DATAR);
580 +static void mtk_nfc_write_fdm(struct nand_chip *chip, u32 sectors)
582 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
586 + for (i = 0; i < sectors ; i++) {
587 + /* read FDM from OOB into private area */
588 + src = chip->oob_poi + i * MTKSDG1_NFI_FDM_REG_SIZE;
589 + dst = (u8 *)host->fdm_reg;
590 + memcpy(dst, src, MTKSDG1_NFI_FDM_REG_SIZE);
592 + /* write FDM to registers */
593 + for (j = 0; j < ARRAY_SIZE(host->fdm_reg); j++) {
594 + reg = MTKSDG1_NFI_FDM0L + i * MTKSDG1_NFI_FDM_REG_SIZE;
595 + reg += j * sizeof(host->fdm_reg[0]);
596 + mtk_nfi_writel(host, host->fdm_reg[j], reg);
601 +static int mtk_nfc_write_page(struct mtd_info *mtd,
602 + struct nand_chip *chip, const uint8_t *buf,
603 + int oob_on, int page, int raw)
606 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
607 + struct completion *nfi = &host->nfi.complete;
608 + struct device *dev = host->dev;
609 + const bool use_ecc = !raw;
610 + void *q = (void *) buf;
611 + dma_addr_t dma_addr;
616 + dmasize = mtd->writesize + (raw ? mtd->oobsize : 0);
618 + dma_addr = dma_map_single(dev, q, dmasize, DMA_TO_DEVICE);
619 + if (dma_mapping_error(host->dev, dma_addr)) {
620 + dev_err(host->dev, "dma mapping error\n");
624 + reg = mtk_nfi_readw(host, MTKSDG1_NFI_CNFG);
625 + reg |= CNFG_AHB | CNFG_DMA_BURST_EN;
628 + * OOB will be generated
629 + * - FDM: from register
632 + reg |= CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN;
633 + mtk_nfi_writew(host, reg, MTKSDG1_NFI_CNFG);
635 + mtk_ecc_encoder_idle(host);
636 + mtk_ecc_writew(host, ENC_EN, MTKSDG1_ECC_ENCCON);
638 + /* write OOB into the FDM registers (OOB area in MTK NAND) */
640 + mtk_nfc_write_fdm(chip, chip->ecc.steps);
642 + /* OOB is part of the DMA transfer */
643 + mtk_nfi_writew(host, reg, MTKSDG1_NFI_CNFG);
646 + mtk_nfi_writel(host, chip->ecc.steps << CON_SEC_SHIFT, MTKSDG1_NFI_CON);
647 + mtk_nfi_writel(host, lower_32_bits(dma_addr), MTKSDG1_NFI_STRADDR);
648 + mtk_nfi_writew(host, INTR_AHB_DONE_EN, MTKSDG1_NFI_INTR_EN);
650 + init_completion(nfi);
653 + reg = mtk_nfi_readl(host, MTKSDG1_NFI_CON) | CON_BWR;
654 + mtk_nfi_writel(host, reg, MTKSDG1_NFI_CON);
656 + ret = wait_for_completion_timeout(nfi, msecs_to_jiffies(MTK_TIMEOUT));
658 + dev_err(dev, "program ahb done timeout\n");
659 + mtk_nfi_writew(host, 0, MTKSDG1_NFI_INTR_EN);
664 + ret = mtk_nfc_transfer_done(host, chip->ecc.steps);
666 + dev_err(dev, "hwecc write timeout\n");
668 + dma_unmap_single(host->dev, dma_addr, dmasize, DMA_TO_DEVICE);
671 + mtk_ecc_encoder_idle(host);
672 + mtk_ecc_writew(host, ENC_DE, MTKSDG1_ECC_ENCCON);
675 + mtk_nfi_writel(host, 0, MTKSDG1_NFI_CON);
680 +static int mtk_nfc_write_page_hwecc(struct mtd_info *mtd,
681 + struct nand_chip *chip, const uint8_t *buf,
682 + int oob_on, int page)
684 + return mtk_nfc_write_page(mtd, chip, buf, oob_on, page, MTK_ECC_ON);
687 +static int mtk_nfc_write_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
688 + const uint8_t *buf, int oob_on, int pg)
690 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
691 + uint8_t *src, *dst;
695 + memset(host->buffer, 0xff, mtd->writesize + mtd->oobsize);
697 + /* MTK internal 4KB page data layout:
698 + * ----------------------------------
699 + * PAGE = 4KB, SECTOR = 1KB, OOB=128B
700 + * page = sector_oob1 + sector_oob2 + sector_oob3 + sector_oob4
701 + * sector_oob = data (1KB) + FDM (8B) + ECC parity (21B) + free (3B)
704 + len = SECTOR_SIZE + mtd->oobsize / chip->ecc.steps;
706 + for (i = 0; i < chip->ecc.steps; i++) {
709 + src = (uint8_t *) buf + i * SECTOR_SIZE;
710 + dst = host->buffer + i * len;
711 + memcpy(dst, src, SECTOR_SIZE);
715 + src = chip->oob_poi + i * MTKSDG1_NFI_FDM_REG_SIZE;
716 + dst = host->buffer + i * len + SECTOR_SIZE;
717 + memcpy(dst, src, MTKSDG1_NFI_FDM_REG_SIZE);
721 + return mtk_nfc_write_page(mtd, chip, host->buffer, MTK_OOB_OFF, pg,
725 +static int mtk_nfc_sector_encode(struct nand_chip *chip, u8 *data)
727 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
728 + struct completion *ecc = &host->ecc.complete;
729 + u32 reg, parity_bytes, i;
730 + dma_addr_t dma_addr;
731 + u32 *parity_region;
735 + dmasize = SECTOR_SIZE + MTKSDG1_NFI_FDM_REG_SIZE;
736 + dma_addr = dma_map_single(host->dev, data, dmasize, DMA_TO_DEVICE);
737 + if (dma_mapping_error(host->dev, dma_addr)) {
738 + dev_err(host->dev, "dma mapping error\n");
742 + /* enable the encoder in DMA mode to calculate the ECC bytes */
743 + reg = mtk_ecc_readl(host, MTKSDG1_ECC_ENCCNFG);
744 + reg &= (~ECC_ENC_MODE_MASK);
745 + reg |= ECC_DMA_MODE;
746 + mtk_ecc_writel(host, reg, MTKSDG1_ECC_ENCCNFG);
748 + mtk_ecc_writel(host, ENC_IRQEN, MTKSDG1_ECC_ENCIRQ_EN);
749 + mtk_ecc_writel(host, lower_32_bits(dma_addr), MTKSDG1_ECC_ENCDIADDR);
751 + init_completion(ecc);
752 + mtk_ecc_writew(host, ENC_EN, MTKSDG1_ECC_ENCCON);
754 + rc = wait_for_completion_timeout(ecc, msecs_to_jiffies(MTK_TIMEOUT));
756 + dev_err(host->dev, "ecc encode done timeout\n");
757 + mtk_ecc_writel(host, 0, MTKSDG1_ECC_ENCIRQ_EN);
762 + mtk_ecc_encoder_idle(host);
765 + * Program ECC bytes to OOB
766 + * per sector oob = FDM + ECC + SPARE
769 + parity_region = (u32 *) (data + SECTOR_SIZE + MTKSDG1_NFI_FDM_REG_SIZE);
770 + parity_bytes = (chip->ecc.strength * MTK_ECC_PARITY_BITS + 7) >> 3;
772 + /* write the parity bytes generated by the ECC back to the OOB region */
773 + for (i = 0; i < parity_bytes; i += sizeof(u32))
774 + *parity_region++ = mtk_ecc_readl(host, MTKSDG1_ECC_ENCPAR0 + i);
778 + dma_unmap_single(host->dev, dma_addr, dmasize, DMA_TO_DEVICE);
780 + mtk_ecc_writew(host, 0, MTKSDG1_ECC_ENCCON);
781 + reg = mtk_ecc_readl(host, MTKSDG1_ECC_ENCCNFG);
782 + reg &= (~ECC_ENC_MODE_MASK);
783 + reg |= ECC_NFI_MODE;
784 + mtk_ecc_writel(host, reg, MTKSDG1_ECC_ENCCNFG);
789 +static int mtk_nfc_write_subpage_hwecc(struct mtd_info *mtd,
790 + struct nand_chip *chip, uint32_t offset, uint32_t data_len,
791 + const uint8_t *buf, int oob_on, int pg)
793 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
794 + uint8_t *src, *dst;
799 + start = BYTES_TO_SECTORS(offset);
800 + end = BYTES_TO_SECTORS(offset + data_len + SECTOR_SIZE - 1);
802 + len = SECTOR_SIZE + mtd->oobsize / chip->ecc.steps;
804 + memset(host->buffer, 0xff, mtd->writesize + mtd->oobsize);
805 + for (i = 0; i < chip->ecc.steps; i++) {
808 + src = (uint8_t *) buf + i * SECTOR_SIZE;
809 + dst = host->buffer + i * len;
810 + memcpy(dst, src, SECTOR_SIZE);
820 + src = chip->oob_poi + i * MTKSDG1_NFI_FDM_REG_SIZE;
821 + dst = host->buffer + i * len + SECTOR_SIZE;
822 + memcpy(dst, src, MTKSDG1_NFI_FDM_REG_SIZE);
825 + /* point to the start of data */
826 + src = host->buffer + i * len;
828 + /* program the CRC back to the OOB */
829 + ret = mtk_nfc_sector_encode(chip, src);
834 + /* use the data in the private buffer (now with FDM and CRC) to perform
837 + src = host->buffer;
838 + return mtk_nfc_write_page(mtd, chip, src, MTK_OOB_OFF, pg, MTK_ECC_OFF);
841 +static int mtk_nfc_write_oob(struct mtd_info *mtd, struct nand_chip *chip,
844 + u8 *buf = chip->buffers->databuf;
847 + memset(buf, 0xff, mtd->writesize);
848 + chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
849 + ret = mtk_nfc_write_page_hwecc(mtd, chip, buf, MTK_OOB_ON, page);
853 + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
854 + ret = chip->waitfunc(mtd, chip);
856 + return ret & NAND_STATUS_FAIL ? -EIO : 0;
859 +static int mtk_nfc_write_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
864 + chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, page);
865 + ret = mtk_nfc_write_page_raw(mtd, chip, NULL, MTK_OOB_ON, page);
869 + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
870 + ret = chip->waitfunc(mtd, chip);
872 + return ret & NAND_STATUS_FAIL ? -EIO : 0;
875 +static int mtk_nfc_ecc_check(struct mtd_info *mtd, struct nand_chip *chip,
878 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
879 + u32 offset, i, err, max_bitflip;
883 + for (i = 0; i < sectors; i++) {
884 + offset = (i >> 2) << 2;
885 + err = mtk_ecc_readl(host, MTKSDG1_ECC_DECENUM0 + offset);
886 + err = err >> ((i % 4) * 8);
888 + if (err == ERR_MASK) {
889 + /* uncorrectable errors */
890 + mtd->ecc_stats.failed++;
894 + mtd->ecc_stats.corrected += err;
895 + max_bitflip = max_t(u32, max_bitflip, err);
898 + return max_bitflip;
901 +static void mtk_nfc_read_fdm(struct nand_chip *chip, u32 sectors)
903 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
907 + for (i = 0; i < sectors; i++) {
908 + /* read FDM register into host memory */
909 + for (j = 0; j < ARRAY_SIZE(host->fdm_reg); j++) {
910 + reg = MTKSDG1_NFI_FDM0L + i * MTKSDG1_NFI_FDM_REG_SIZE;
911 + reg += j * sizeof(host->fdm_reg[0]);
912 + host->fdm_reg[j] = mtk_nfi_readl(host, reg);
915 + /* copy FDM register from host to OOB */
916 + src = (u8 *)host->fdm_reg;
917 + dst = chip->oob_poi + i * MTKSDG1_NFI_FDM_REG_SIZE;
918 + memcpy(dst, src, MTKSDG1_NFI_FDM_REG_SIZE);
922 +static int mtk_nfc_update_oob(struct mtd_info *mtd, struct nand_chip *chip,
923 + u8 *buf, u32 sectors)
925 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
926 + int i, bitflips = 0;
928 + /* if the page is empty, no bitflips and clear data and oob */
929 + if (mtk_nfi_readl(host, MTKSDG1_NFI_STA) & STA_EMP_PAGE) {
930 + memset(buf, 0xff, SECTORS_TO_BYTES(sectors));
932 + /* empty page: update OOB with 0xFF */
933 + for (i = 0; i < sectors; i++) {
934 + memset(chip->oob_poi + i * MTKSDG1_NFI_FDM_REG_SIZE,
935 + 0xff, MTKSDG1_NFI_FDM_REG_SIZE);
938 + /* update OOB with HW info */
939 + mtk_nfc_read_fdm(chip, sectors);
941 + /* return the bitflips */
942 + bitflips = mtk_nfc_ecc_check(mtd, chip, sectors);
948 +static int mtk_nfc_block_markbad(struct mtd_info *mtd, loff_t ofs)
950 + struct nand_chip *chip = mtd_to_nand(mtd);
951 + u8 *buf = chip->buffers->databuf;
954 + /* block_markbad writes 0x00 at data and OOB */
955 + memset(buf, 0x00, mtd->writesize + mtd->oobsize);
957 + /* Write to first/last page(s) if necessary */
958 + if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
959 + ofs += mtd->erasesize - mtd->writesize;
963 + pg = (int)(ofs >> chip->page_shift);
966 + * write 0x00 to DATA & OOB in flash
967 + * No need to reorganize the page since it is all 0x00
969 + chip->cmdfunc(mtd, NAND_CMD_SEQIN, 0x00, pg);
970 + rc = mtk_nfc_write_page(mtd, chip, buf, MTK_OOB_OFF, pg,
975 + chip->cmdfunc(mtd, NAND_CMD_PAGEPROG, -1, -1);
976 + rc = chip->waitfunc(mtd, chip);
977 + rc = rc & NAND_STATUS_FAIL ? -EIO : 0;
981 + ofs += mtd->writesize;
984 + } while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
989 +static int mtk_nfc_read_subpage(struct mtd_info *mtd, struct nand_chip *chip,
990 + uint32_t data_offs, uint32_t readlen, uint8_t *bufpoi,
993 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
994 + unsigned long timeout = msecs_to_jiffies(MTK_TIMEOUT);
995 + u32 reg, column, spare, sectors, start, end;
996 + struct completion *nfi, *ecc;
997 + const bool use_ecc = !raw;
998 + int bitflips = -EIO;
999 + dma_addr_t dma_addr;
1004 + nfi = &host->nfi.complete;
1005 + ecc = &host->ecc.complete;
1007 + start = BYTES_TO_SECTORS(data_offs);
1008 + end = BYTES_TO_SECTORS(data_offs + readlen + SECTOR_SIZE - 1);
1009 + sectors = end - start;
1011 + spare = mtd->oobsize / chip->ecc.steps;
1012 + column = start * (SECTOR_SIZE + spare);
1014 + len = SECTORS_TO_BYTES(sectors) + (raw ? sectors * spare : 0);
1015 + buf = bufpoi + SECTORS_TO_BYTES(start);
1017 + /* map the device memory */
1018 + dma_addr = dma_map_single(host->dev, buf, len, DMA_FROM_DEVICE);
1019 + if (dma_mapping_error(host->dev, dma_addr)) {
1020 + dev_err(host->dev, "dma mapping error\n");
1024 + /* configure the transfer */
1025 + reg = mtk_nfi_readw(host, MTKSDG1_NFI_CNFG);
1026 + reg |= CNFG_DMA_BURST_EN | CNFG_AHB;
1028 + reg |= CNFG_AUTO_FMT_EN | CNFG_HW_ECC_EN;
1029 + mtk_nfi_writew(host, reg, MTKSDG1_NFI_CNFG);
1031 + /* enable encoder */
1032 + mtk_ecc_decoder_idle(host);
1033 + mtk_ecc_writel(host, DEC_EN, MTKSDG1_ECC_DECCON);
1035 + mtk_nfi_writew(host, reg, MTKSDG1_NFI_CNFG);
1037 + mtk_nfi_writel(host, sectors << CON_SEC_SHIFT, MTKSDG1_NFI_CON);
1038 + mtk_nfi_writew(host, INTR_BUSY_RT_EN, MTKSDG1_NFI_INTR_EN);
1040 + init_completion(nfi);
1042 + mtk_nfc_set_address(host, column, page, 2, host->row_nob);
1043 + mtk_nfc_set_command(host, NAND_CMD_READSTART);
1044 + rc = wait_for_completion_timeout(nfi, timeout);
1046 + dev_err(host->dev, "read busy return timeout\n");
1050 + mtk_nfi_writew(host, INTR_AHB_DONE_EN, MTKSDG1_NFI_INTR_EN);
1051 + mtk_nfi_writel(host, lower_32_bits(dma_addr), MTKSDG1_NFI_STRADDR);
1054 + /* program ECC with sector count */
1055 + host->ecc.dec_sec = sectors;
1056 + init_completion(ecc);
1057 + mtk_ecc_writew(host, DEC_IRQEN, MTKSDG1_ECC_DECIRQ_EN);
1060 + init_completion(nfi);
1063 + reg = mtk_nfi_readl(host, MTKSDG1_NFI_CON) | CON_BRD;
1064 + mtk_nfi_writel(host, reg, MTKSDG1_NFI_CON);
1066 + rc = wait_for_completion_timeout(nfi, timeout);
1068 + dev_warn(host->dev, "read ahb/dma done timeout\n");
1070 + /* DMA interrupt didn't trigger, check page done just in case */
1071 + rc = mtk_nfc_subpage_done(host, sectors);
1073 + dev_err(host->dev, "subpage done timeout\n");
1077 + /* raw transfer successful */
1081 + rc = wait_for_completion_timeout(ecc, timeout);
1083 + dev_err(host->dev, "ecc decode timeout\n");
1084 + host->ecc.dec_sec = 0;
1085 + bitflips = -ETIMEDOUT;
1088 + bitflips = mtk_nfc_update_oob(mtd, chip, buf, sectors);
1092 + dma_unmap_single(host->dev, dma_addr, len, DMA_FROM_DEVICE);
1095 + /* make sure the ECC dec irq is disabled */
1096 + mtk_ecc_writew(host, 0, MTKSDG1_ECC_DECIRQ_EN);
1097 + mtk_ecc_decoder_idle(host);
1099 + /* disable ECC dec */
1100 + mtk_ecc_writew(host, 0, MTKSDG1_ECC_DECCON);
1103 + mtk_nfi_writel(host, 0, MTKSDG1_NFI_CON);
1108 +static int mtk_nfc_read_subpage_hwecc(struct mtd_info *mtd,
1109 + struct nand_chip *chip, uint32_t data_offs,
1110 + uint32_t readlen, uint8_t *bufpoi, int page)
1112 + return mtk_nfc_read_subpage(mtd, chip, data_offs, readlen,
1113 + bufpoi, page, MTK_ECC_ON);
1116 +static int mtk_nfc_read_page_hwecc(struct mtd_info *mtd, struct nand_chip *chip,
1117 + uint8_t *buf, int oob_on, int page)
1119 + return mtk_nfc_read_subpage_hwecc(mtd, chip, 0, mtd->writesize,
1123 +static int mtk_nfc_read_page_raw(struct mtd_info *mtd, struct nand_chip *chip,
1124 + uint8_t *buf, int oob_on, int page)
1126 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
1127 + uint8_t *src, *dst;
1131 + dst = host->buffer;
1132 + memset(dst, 0xff, mtd->writesize + mtd->oobsize);
1133 + ret = mtk_nfc_read_subpage(mtd, chip, 0, mtd->writesize, dst, page, 1);
1137 + len = SECTOR_SIZE + mtd->oobsize / chip->ecc.steps;
1139 + /* copy to the output buffer */
1140 + for (i = 0; i < chip->ecc.steps; i++) {
1142 + /* copy sector data */
1144 + src = host->buffer + i * len;
1145 + dst = buf + i * SECTOR_SIZE;
1146 + memcpy(dst, src, SECTOR_SIZE);
1149 + /* copy FDM data to OOB */
1151 + src = host->buffer + i * len + SECTOR_SIZE;
1152 + dst = chip->oob_poi + i * MTKSDG1_NFI_FDM_REG_SIZE;
1153 + memcpy(dst, src, MTKSDG1_NFI_FDM_REG_SIZE);
1160 +static void mtk_nfc_switch_oob(struct mtd_info *mtd, struct nand_chip *chip,
1163 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
1169 + spare = mtd->oobsize / chip->ecc.steps;
1170 + sectors = mtd->writesize / (SECTOR_SIZE + spare);
1173 + * MTK: DATA+oob1, DATA+oob2, DATA+oob3 ...
1176 + /* point to the last oob_i from the NAND device*/
1177 + bufpoi = buf + mtd->writesize - (sectors * spare);
1178 + len = sizeof(host->fdm_reg);
1180 + /* copy NAND oob to private area */
1181 + memcpy(host->fdm_reg, bufpoi, len);
1183 + /* copy oob_poi to NAND */
1184 + memcpy(bufpoi, chip->oob_poi, len);
1186 + /* copy NAND oob to oob_poi */
1187 + memcpy(chip->oob_poi, host->fdm_reg, sizeof(host->fdm_reg));
1188 + memset(host->fdm_reg, 0x00, len);
1191 +static int mtk_nfc_read_oob(struct mtd_info *mtd, struct nand_chip *chip,
1194 + struct mtk_nfc_host *host = nand_get_controller_data(chip);
1195 + u8 *buf = chip->buffers->databuf;
1196 + struct mtd_ecc_stats stats;
1199 + stats = mtd->ecc_stats;
1201 + memset(buf, 0xff, mtd->writesize);
1202 + chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1204 + ret = mtk_nfc_read_page_hwecc(mtd, chip, buf, 1, page);
1206 + if (host->switch_oob)
1207 + mtk_nfc_switch_oob(mtd, chip, buf);
1209 + if (ret < mtd->bitflip_threshold)
1210 + mtd->ecc_stats.corrected = stats.corrected;
1215 +static int mtk_nfc_read_oob_raw(struct mtd_info *mtd, struct nand_chip *chip,
1218 + chip->cmdfunc(mtd, NAND_CMD_READ0, 0, page);
1220 + return mtk_nfc_read_page_raw(mtd, chip, NULL, MTK_OOB_ON, page);
1223 +static inline void mtk_nfc_hw_init(struct mtk_nfc_host *host)
1225 + mtk_nfi_writel(host, 0x10804211, MTKSDG1_NFI_ACCCON);
1226 + mtk_nfi_writew(host, 0xf1, MTKSDG1_NFI_CNRNB);
1227 + mtk_nfc_hw_reset(host);
1229 + /* clear interrupt */
1230 + mtk_nfi_readl(host, MTKSDG1_NFI_INTR_STA);
1231 + mtk_nfi_writel(host, 0, MTKSDG1_NFI_INTR_EN);
1233 + /* ECC encoder init */
1234 + mtk_ecc_encoder_idle(host);
1235 + mtk_ecc_writew(host, ENC_DE, MTKSDG1_ECC_ENCCON);
1237 + /* ECC decoder init */
1238 + mtk_ecc_decoder_idle(host);
1239 + mtk_ecc_writel(host, DEC_DE, MTKSDG1_ECC_DECCON);
1242 +static irqreturn_t mtk_nfi_irq(int irq, void *devid)
1244 + struct mtk_nfc_host *host = devid;
1247 + sta = mtk_nfi_readw(host, MTKSDG1_NFI_INTR_STA);
1248 + ien = mtk_nfi_readw(host, MTKSDG1_NFI_INTR_EN);
1253 + mtk_nfi_writew(host, ~sta & ien, MTKSDG1_NFI_INTR_EN);
1254 + complete(&host->nfi.complete);
1256 + return IRQ_HANDLED;
1259 +static irqreturn_t mtk_ecc_irq(int irq, void *devid)
1261 + struct mtk_nfc_host *host = devid;
1262 + u32 reg_val, mask;
1264 + reg_val = mtk_ecc_readw(host, MTKSDG1_ECC_DECIRQ_STA);
1265 + if (reg_val & DEC_IRQEN) {
1266 + if (host->ecc.dec_sec) {
1267 + mask = 1 << (host->ecc.dec_sec - 1);
1268 + reg_val = mtk_ecc_readw(host, MTKSDG1_ECC_DECDONE);
1269 + if (mask & reg_val) {
1270 + host->ecc.dec_sec = 0;
1271 + complete(&host->ecc.complete);
1272 + mtk_ecc_writew(host, 0, MTKSDG1_ECC_DECIRQ_EN);
1275 + dev_warn(host->dev, "spurious DEC_IRQ\n");
1277 + return IRQ_HANDLED;
1280 + reg_val = mtk_ecc_readl(host, MTKSDG1_ECC_ENCIRQ_STA);
1281 + if (reg_val & ENC_IRQEN) {
1282 + complete(&host->ecc.complete);
1283 + mtk_ecc_writel(host, 0, MTKSDG1_ECC_ENCIRQ_EN);
1285 + return IRQ_HANDLED;
1291 +static int mtk_nfc_enable_clk(struct device *dev, struct mtk_nfc_clk *clk)
1295 + ret = clk_prepare_enable(clk->nfi_clk);
1297 + dev_err(dev, "failed to enable nfi clk\n");
1301 + ret = clk_prepare_enable(clk->nfiecc_clk);
1303 + dev_err(dev, "failed to enable nfiecc clk\n");
1304 + goto out_nfiecc_clk_disable;
1307 + ret = clk_prepare_enable(clk->pad_clk);
1309 + dev_err(dev, "failed to enable pad clk\n");
1310 + goto out_pad_clk_disable;
1315 +out_pad_clk_disable:
1316 + clk_disable_unprepare(clk->nfiecc_clk);
1318 +out_nfiecc_clk_disable:
1319 + clk_disable_unprepare(clk->nfi_clk);
1324 +static void mtk_nfc_disable_clk(struct mtk_nfc_clk *clk)
1326 + clk_disable_unprepare(clk->nfi_clk);
1327 + clk_disable_unprepare(clk->nfiecc_clk);
1328 + clk_disable_unprepare(clk->pad_clk);
1331 +static int mtk_nfc_probe(struct platform_device *pdev)
1333 + struct device *dev = &pdev->dev;
1334 + struct device_node *np = dev->of_node;
1335 + struct mtk_nfc_host *host;
1336 + struct nand_chip *chip;
1337 + struct mtd_info *mtd;
1338 + struct resource *res;
1342 + host = devm_kzalloc(dev, sizeof(*host), GFP_KERNEL);
1346 + chip = &host->chip;
1347 + mtd = nand_to_mtd(chip);
1350 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1351 + host->nfi.base = devm_ioremap_resource(dev, res);
1352 + if (IS_ERR(host->nfi.base)) {
1353 + ret = PTR_ERR(host->nfi.base);
1354 + dev_err(dev, "no nfi base\n");
1358 + res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1359 + host->ecc.base = devm_ioremap_resource(dev, res);
1360 + if (IS_ERR(host->ecc.base)) {
1361 + ret = PTR_ERR(host->ecc.base);
1362 + dev_err(dev, "no ecc base\n");
1366 + host->clk.nfi_clk = devm_clk_get(dev, "nfi_clk");
1367 + if (IS_ERR(host->clk.nfi_clk)) {
1368 + dev_err(dev, "no clk\n");
1369 + ret = PTR_ERR(host->clk.nfi_clk);
1373 + host->clk.nfiecc_clk = devm_clk_get(dev, "nfiecc_clk");
1374 + if (IS_ERR(host->clk.nfiecc_clk)) {
1375 + dev_err(dev, "no ecc clk\n");
1376 + ret = PTR_ERR(host->clk.nfiecc_clk);
1380 + host->clk.pad_clk = devm_clk_get(dev, "pad_clk");
1381 + if (IS_ERR(host->clk.pad_clk)) {
1382 + dev_err(dev, "no pad clk\n");
1383 + ret = PTR_ERR(host->clk.pad_clk);
1387 + ret = mtk_nfc_enable_clk(dev, &host->clk);
1391 + irq = platform_get_irq(pdev, 0);
1393 + dev_err(dev, "no nfi irq resource\n");
1398 + ret = devm_request_irq(dev, irq, mtk_nfi_irq, 0x0, MTK_IRQ_NFI, host);
1400 + dev_err(dev, "failed to request nfi irq\n");
1404 + irq = platform_get_irq(pdev, 1);
1406 + dev_err(dev, "no ecc irq resource\n");
1411 + ret = devm_request_irq(dev, irq, mtk_ecc_irq, 0x0, MTK_IRQ_ECC, host);
1413 + dev_err(dev, "failed to request ecc irq\n");
1417 + ret = dma_set_mask(dev, DMA_BIT_MASK(32));
1419 + dev_err(dev, "failed to set dma mask\n");
1423 + platform_set_drvdata(pdev, host);
1425 + mtd_set_of_node(mtd, np);
1426 + mtd->owner = THIS_MODULE;
1427 + mtd->dev.parent = dev;
1428 + mtd->name = MTK_NAME;
1430 + nand_set_controller_data(chip, host);
1431 + chip->options |= NAND_USE_BOUNCE_BUFFER | NAND_SUBPAGE_READ;
1432 + chip->block_markbad = mtk_nfc_block_markbad;
1433 + chip->select_chip = mtk_nfc_select_chip;
1434 + chip->read_byte = mtk_nfc_read_byte;
1435 + chip->cmdfunc = mtk_nfc_cmdfunc;
1436 + chip->ecc.mode = NAND_ECC_HW;
1437 + chip->ecc.write_subpage = mtk_nfc_write_subpage_hwecc;
1438 + chip->ecc.write_page_raw = mtk_nfc_write_page_raw;
1439 + chip->ecc.write_page = mtk_nfc_write_page_hwecc;
1440 + chip->ecc.write_oob_raw = mtk_nfc_write_oob_raw;
1441 + chip->ecc.write_oob = mtk_nfc_write_oob;
1442 + chip->ecc.read_subpage = mtk_nfc_read_subpage_hwecc;
1443 + chip->ecc.read_page_raw = mtk_nfc_read_page_raw;
1444 + chip->ecc.read_oob_raw = mtk_nfc_read_oob_raw;
1445 + chip->ecc.read_page = mtk_nfc_read_page_hwecc;
1446 + chip->ecc.read_oob = mtk_nfc_read_oob;
1448 + mtk_nfc_hw_init(host);
1450 + ret = nand_scan_ident(mtd, MTK_NAND_MAX_CHIP, NULL);
1456 + ret = mtk_nfc_hw_runtime_config(mtd);
1458 + dev_err(dev, "nand device not supported\n");
1462 + len = mtd->writesize + mtd->oobsize;
1463 + host->buffer = devm_kzalloc(dev, len, GFP_KERNEL);
1464 + if (!host->buffer) {
1469 + /* required to create bbt table if not present */
1470 + host->switch_oob = true;
1471 + ret = nand_scan_tail(mtd);
1476 + host->switch_oob = false;
1478 + ret = mtd_device_parse_register(mtd, NULL, NULL, NULL, 0);
1480 + dev_err(dev, "mtd parse partition error\n");
1487 + nand_release(mtd);
1490 + mtk_nfc_disable_clk(&host->clk);
1495 +static int mtk_nfc_remove(struct platform_device *pdev)
1497 + struct mtk_nfc_host *host = platform_get_drvdata(pdev);
1498 + struct mtd_info *mtd = nand_to_mtd(&host->chip);
1500 + nand_release(mtd);
1501 + mtk_nfc_disable_clk(&host->clk);
1506 +#ifdef CONFIG_PM_SLEEP
1507 +static int mtk_nfc_suspend(struct device *dev)
1509 + struct mtk_nfc_host *host = dev_get_drvdata(dev);
1510 + struct mtk_nfc_saved_reg *reg = &host->saved_reg;
1512 + reg->nfi.emp_thresh = mtk_nfi_readl(host, MTKSDG1_NFI_EMPTY_THRESH);
1513 + reg->ecc.enccnfg = mtk_ecc_readl(host, MTKSDG1_ECC_ENCCNFG);
1514 + reg->ecc.deccnfg = mtk_ecc_readl(host, MTKSDG1_ECC_DECCNFG);
1515 + reg->nfi.pagefmt = mtk_nfi_readw(host, MTKSDG1_NFI_PAGEFMT);
1516 + reg->nfi.acccon = mtk_nfi_readl(host, MTKSDG1_NFI_ACCCON);
1517 + reg->nfi.cnrnb = mtk_nfi_readw(host, MTKSDG1_NFI_CNRNB);
1518 + reg->nfi.csel = mtk_nfi_readw(host, MTKSDG1_NFI_CSEL);
1520 + mtk_nfc_disable_clk(&host->clk);
1525 +static int mtk_nfc_resume(struct device *dev)
1527 + struct mtk_nfc_host *host = dev_get_drvdata(dev);
1528 + struct mtk_nfc_saved_reg *reg = &host->saved_reg;
1529 + struct nand_chip *chip = &host->chip;
1530 + struct mtd_info *mtd = nand_to_mtd(chip);
1536 + ret = mtk_nfc_enable_clk(dev, &host->clk);
1540 + for (i = 0; i < chip->numchips; i++) {
1541 + chip->select_chip(mtd, i);
1542 + chip->cmdfunc(mtd, NAND_CMD_RESET, -1, -1);
1545 + mtk_nfi_writel(host, reg->nfi.emp_thresh, MTKSDG1_NFI_EMPTY_THRESH);
1546 + mtk_nfi_writew(host, reg->nfi.pagefmt, MTKSDG1_NFI_PAGEFMT);
1547 + mtk_ecc_writel(host, reg->ecc.enccnfg, MTKSDG1_ECC_ENCCNFG);
1548 + mtk_ecc_writel(host, reg->ecc.deccnfg, MTKSDG1_ECC_DECCNFG);
1549 + mtk_nfi_writel(host, reg->nfi.acccon, MTKSDG1_NFI_ACCCON);
1550 + mtk_nfi_writew(host, reg->nfi.cnrnb, MTKSDG1_NFI_CNRNB);
1551 + mtk_nfi_writew(host, reg->nfi.csel, MTKSDG1_NFI_CSEL);
1556 +static SIMPLE_DEV_PM_OPS(mtk_nfc_pm_ops, mtk_nfc_suspend, mtk_nfc_resume);
1559 +static const struct of_device_id mtk_nfc_id_table[] = {
1560 + { .compatible = "mediatek,mt2701-nfc" },
1563 +MODULE_DEVICE_TABLE(of, mtk_nfc_id_table);
1565 +static struct platform_driver mtk_nfc_driver = {
1566 + .probe = mtk_nfc_probe,
1567 + .remove = mtk_nfc_remove,
1570 + .of_match_table = mtk_nfc_id_table,
1571 +#ifdef CONFIG_PM_SLEEP
1572 + .pm = &mtk_nfc_pm_ops,
1577 +module_platform_driver(mtk_nfc_driver);
1579 +MODULE_LICENSE("GPL");
1580 +MODULE_AUTHOR("Xiaolei Li <xiaolei.li@mediatek.com>");
1581 +MODULE_DESCRIPTION("MTK Nand Flash Controller Driver");
1584 +++ b/drivers/mtd/nand/mtksdg1_nand_ecc.h
1587 + * MTK smart device ECC engine register.
1588 + * Copyright (C) 2015-2016 MediaTek Inc.
1589 + * Author: Xiaolei.Li <xiaolei.li@mediatek.com>
1591 + * This program is free software; you can redistribute it and/or modify
1592 + * it under the terms of the GNU General Public License version 2 as
1593 + * published by the Free Software Foundation.
1595 + * This program is distributed in the hope that it will be useful,
1596 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1597 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1598 + * GNU General Public License for more details.
1601 +#ifndef MTKSDG1_NAND_ECC_H
1602 +#define MTKSDG1_NAND_ECC_H
1604 +/* ECC engine register definition */
1605 +#define MTKSDG1_ECC_ENCCON (0x00)
1609 +#define MTKSDG1_ECC_ENCCNFG (0x04)
1610 +#define ECC_CNFG_4BIT (0)
1611 +#define ECC_CNFG_12BIT (4)
1612 +#define ECC_NFI_MODE BIT(5)
1613 +#define ECC_DMA_MODE (0)
1614 +#define ECC_ENC_MODE_MASK (0x3 << 5)
1615 +#define ECC_MS_SHIFT (16)
1617 +#define MTKSDG1_ECC_ENCDIADDR (0x08)
1619 +#define MTKSDG1_ECC_ENCIDLE (0x0C)
1620 +#define ENC_IDLE BIT(0)
1622 +#define MTKSDG1_ECC_ENCPAR0 (0x10)
1623 +#define MTKSDG1_ECC_ENCSTA (0x7C)
1625 +#define MTKSDG1_ECC_ENCIRQ_EN (0x80)
1626 +#define ENC_IRQEN BIT(0)
1628 +#define MTKSDG1_ECC_ENCIRQ_STA (0x84)
1630 +#define MTKSDG1_ECC_DECCON (0x100)
1634 +#define MTKSDG1_ECC_DECCNFG (0x104)
1635 +#define DEC_EMPTY_EN BIT(31)
1636 +#define DEC_CNFG_FER (0x1 << 12)
1637 +#define DEC_CNFG_EL (0x2 << 12)
1638 +#define DEC_CNFG_CORRECT (0x3 << 12)
1640 +#define MTKSDG1_ECC_DECIDLE (0x10C)
1641 +#define DEC_IDLE BIT(0)
1643 +#define MTKSDG1_ECC_DECFER (0x110)
1645 +#define MTKSDG1_ECC_DECENUM0 (0x114)
1646 +#define ERR_MASK (0x3f)
1648 +#define MTKSDG1_ECC_DECDONE (0x124)
1650 +#define MTKSDG1_ECC_DECEL0 (0x128)
1652 +#define MTKSDG1_ECC_DECIRQ_EN (0x200)
1653 +#define DEC_IRQEN BIT(0)
1655 +#define MTKSDG1_ECC_DECIRQ_STA (0x204)
1657 +#define MTKSDG1_ECC_DECFSM (0x208)
1658 +#define DECFSM_MASK (0x7f0f0f0f)
1659 +#define DECFSM_IDLE (0x01010101)
1662 +++ b/drivers/mtd/nand/mtksdg1_nand_nfi.h
1665 + * MTK smart device NAND Flash controller register.
1666 + * Copyright (C) 2015-2016 MediaTek Inc.
1667 + * Author: Xiaolei.Li <xiaolei.li@mediatek.com>
1669 + * This program is free software; you can redistribute it and/or modify
1670 + * it under the terms of the GNU General Public License version 2 as
1671 + * published by the Free Software Foundation.
1673 + * This program is distributed in the hope that it will be useful,
1674 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1675 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1676 + * GNU General Public License for more details.
1679 +#ifndef MTKSDG1_NAND_NFI_H
1680 +#define MTKSDG1_NAND_NFI_H
1682 +/* NAND controller register definition */
1683 +#define MTKSDG1_NFI_CNFG (0x00)
1684 +#define CNFG_AHB BIT(0)
1685 +#define CNFG_READ_EN BIT(1)
1686 +#define CNFG_DMA_BURST_EN BIT(2)
1687 +#define CNFG_BYTE_RW BIT(6)
1688 +#define CNFG_HW_ECC_EN BIT(8)
1689 +#define CNFG_AUTO_FMT_EN BIT(9)
1690 +#define CNFG_OP_IDLE (0 << 12)
1691 +#define CNFG_OP_READ (1 << 12)
1692 +#define CNFG_OP_SRD (2 << 12)
1693 +#define CNFG_OP_PRGM (3 << 12)
1694 +#define CNFG_OP_ERASE (4 << 12)
1695 +#define CNFG_OP_RESET (5 << 12)
1696 +#define CNFG_OP_CUST (6 << 12)
1698 +#define MTKSDG1_NFI_PAGEFMT (0x04)
1699 +#define PAGEFMT_FDM_ECC_SHIFT (12)
1700 +#define PAGEFMT_FDM_SHIFT (8)
1701 +#define PAGEFMT_SPARE_16 (0)
1702 +#define PAGEFMT_SPARE_32 (4)
1703 +#define PAGEFMT_SPARE_SHIFT (4)
1704 +#define PAGEFMT_SEC_SEL_512 BIT(2)
1705 +#define PAGEFMT_512_2K (0)
1706 +#define PAGEFMT_2K_4K (1)
1707 +#define PAGEFMT_4K_8K (2)
1710 +#define MTKSDG1_NFI_CON (0x08)
1711 +#define CON_FIFO_FLUSH BIT(0)
1712 +#define CON_NFI_RST BIT(1)
1713 +#define CON_SRD BIT(4) /* single read */
1714 +#define CON_BRD BIT(8) /* burst read */
1715 +#define CON_BWR BIT(9) /* burst write */
1716 +#define CON_SEC_SHIFT (12)
1718 +/* Timming control register */
1719 +#define MTKSDG1_NFI_ACCCON (0x0C)
1721 +#define MTKSDG1_NFI_INTR_EN (0x10)
1722 +#define INTR_RD_DONE_EN BIT(0)
1723 +#define INTR_WR_DONE_EN BIT(1)
1724 +#define INTR_RST_DONE_EN BIT(2)
1725 +#define INTR_ERS_DONE_EN BIT(3)
1726 +#define INTR_BUSY_RT_EN BIT(4)
1727 +#define INTR_AHB_DONE_EN BIT(6)
1729 +#define MTKSDG1_NFI_INTR_STA (0x14)
1731 +#define MTKSDG1_NFI_CMD (0x20)
1733 +#define MTKSDG1_NFI_ADDRNOB (0x30)
1734 +#define ADDR_ROW_NOB_SHIFT (4)
1736 +#define MTKSDG1_NFI_COLADDR (0x34)
1737 +#define MTKSDG1_NFI_ROWADDR (0x38)
1738 +#define MTKSDG1_NFI_STRDATA (0x40)
1739 +#define MTKSDG1_NFI_CNRNB (0x44)
1740 +#define MTKSDG1_NFI_DATAW (0x50)
1741 +#define MTKSDG1_NFI_DATAR (0x54)
1742 +#define MTKSDG1_NFI_PIO_DIRDY (0x58)
1743 +#define PIO_DI_RDY (0x01)
1746 +#define MTKSDG1_NFI_STA (0x60)
1747 +#define STA_CMD BIT(0)
1748 +#define STA_ADDR BIT(1)
1749 +#define STA_DATAR BIT(2)
1750 +#define STA_DATAW BIT(3)
1751 +#define STA_EMP_PAGE BIT(12)
1753 +#define MTKSDG1_NFI_FIFOSTA (0x64)
1755 +#define MTKSDG1_NFI_ADDRCNTR (0x70)
1756 +#define CNTR_MASK GENMASK(16, 12)
1758 +#define MTKSDG1_NFI_STRADDR (0x80)
1759 +#define MTKSDG1_NFI_BYTELEN (0x84)
1760 +#define MTKSDG1_NFI_CSEL (0x90)
1761 +#define MTKSDG1_NFI_IOCON (0x94)
1763 +/* FDM data for sector: FDM0[L,H] - FDMF[L,H] */
1764 +#define MTKSDG1_NFI_FDM_MAX_SEC (0x10)
1765 +#define MTKSDG1_NFI_FDM_REG_SIZE (8)
1766 +#define MTKSDG1_NFI_FDM0L (0xA0)
1767 +#define MTKSDG1_NFI_FDM0M (0xA4)
1770 +#define MTKSDG1_NFI_FIFODATA0 (0x190)
1771 +#define MTKSDG1_NFI_DEBUG_CON1 (0x220)
1772 +#define MTKSDG1_NFI_MASTER_STA (0x224)
1773 +#define MASTER_STA_MASK (0x0FFF)
1775 +#define MTKSDG1_NFI_RANDOM_CNFG (0x238)
1776 +#define MTKSDG1_NFI_EMPTY_THRESH (0x23C)
1777 +#define MTKSDG1_NFI_NAND_TYPE (0x240)
1778 +#define MTKSDG1_NFI_ACCCON1 (0x244)
1779 +#define MTKSDG1_NFI_DELAY_CTRL (0x248)