sunxi_nfc_wait_int(nfc, NFC_CMD_INT_FLAG, 0);
 }
 
+static void sunxi_nfc_hw_ecc_enable(struct mtd_info *mtd)
+{
+       struct nand_chip *nand = mtd->priv;
+       struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
+       struct sunxi_nand_hw_ecc *data = nand->ecc.priv;
+       u32 ecc_ctl;
+
+       ecc_ctl = readl(nfc->regs + NFC_REG_ECC_CTL);
+       ecc_ctl &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE |
+                    NFC_ECC_BLOCK_SIZE_MSK);
+       ecc_ctl |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION;
+
+       writel(ecc_ctl, nfc->regs + NFC_REG_ECC_CTL);
+}
+
+static void sunxi_nfc_hw_ecc_disable(struct mtd_info *mtd)
+{
+       struct nand_chip *nand = mtd->priv;
+       struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
+
+       writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
+              nfc->regs + NFC_REG_ECC_CTL);
+}
+
 static int sunxi_nfc_hw_ecc_read_page(struct mtd_info *mtd,
                                      struct nand_chip *chip, uint8_t *buf,
                                      int oob_required, int page)
        struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
        struct nand_ecc_ctrl *ecc = &chip->ecc;
        struct nand_ecclayout *layout = ecc->layout;
-       struct sunxi_nand_hw_ecc *data = ecc->priv;
        unsigned int max_bitflips = 0;
        int offset;
        int ret;
        int i;
        int cnt;
 
-       tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
-       tmp &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE_MSK);
-       tmp |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION;
-
-       writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_enable(mtd);
 
        for (i = 0; i < ecc->steps; i++) {
                if (i)
                }
        }
 
-       tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
-       tmp &= ~NFC_ECC_EN;
-
-       writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_disable(mtd);
 
        return max_bitflips;
 }
        struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
        struct nand_ecc_ctrl *ecc = &chip->ecc;
        struct nand_ecclayout *layout = ecc->layout;
-       struct sunxi_nand_hw_ecc *data = ecc->priv;
        int offset;
        int ret;
        u32 tmp;
        int i;
        int cnt;
 
-       tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
-       tmp &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE_MSK);
-       tmp |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION;
-
-       writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_enable(mtd);
 
        for (i = 0; i < ecc->steps; i++) {
                if (i)
                }
        }
 
-       tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
-       tmp &= ~NFC_ECC_EN;
-
-       writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_disable(mtd);
 
        return 0;
 }
 {
        struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
        struct nand_ecc_ctrl *ecc = &chip->ecc;
-       struct sunxi_nand_hw_ecc *data = ecc->priv;
        unsigned int max_bitflips = 0;
        uint8_t *oob = chip->oob_poi;
        int offset = 0;
        u32 tmp;
        int i;
 
-       tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
-       tmp &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE_MSK);
-       tmp |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION;
-
-       writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_enable(mtd);
 
        for (i = 0; i < ecc->steps; i++) {
                chip->read_buf(mtd, NULL, ecc->size);
                }
        }
 
-       writel(readl(nfc->regs + NFC_REG_ECC_CTL) & ~NFC_ECC_EN,
-              nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_disable(mtd);
 
        return max_bitflips;
 }
 {
        struct sunxi_nfc *nfc = to_sunxi_nfc(chip->controller);
        struct nand_ecc_ctrl *ecc = &chip->ecc;
-       struct sunxi_nand_hw_ecc *data = ecc->priv;
        uint8_t *oob = chip->oob_poi;
        int offset = 0;
        int ret;
        u32 tmp;
        int i;
 
-       tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
-       tmp &= ~(NFC_ECC_MODE_MSK | NFC_ECC_PIPELINE | NFC_ECC_BLOCK_SIZE_MSK);
-       tmp |= NFC_ECC_EN | NFC_ECC_MODE(data->mode) | NFC_ECC_EXCEPTION;
-
-       writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_enable(mtd);
 
        for (i = 0; i < ecc->steps; i++) {
                chip->write_buf(mtd, buf + (i * ecc->size), ecc->size);
                }
        }
 
-       tmp = readl(nfc->regs + NFC_REG_ECC_CTL);
-       tmp &= ~NFC_ECC_EN;
-
-       writel(tmp, nfc->regs + NFC_REG_ECC_CTL);
+       sunxi_nfc_hw_ecc_disable(mtd);
 
        return 0;
 }