@@ -318,6 +318,7 @@ struct sunxi_nfc_user_data_layout {
* @has_ecc_block_512: If the ECC can handle 512B or only 1024B chunks
* @has_ecc_clk: If the controller needs an ECC clock.
* @has_mbus_clk: If the controller needs a mbus clock.
+ * @needs_bus_reset: Aborted DMA needs an AHB reset to restore data ordering
* @layout: protected user-data layout for the controller
* @reg_io_data: I/O data register
* @reg_ecc_err_cnt: ECC error counter register
@@ -346,6 +347,7 @@ struct sunxi_nfc_caps {
bool has_ecc_block_512;
bool has_ecc_clk;
bool has_mbus_clk;
+ bool needs_bus_reset;
const struct sunxi_nfc_user_data_layout *layout;
unsigned int reg_io_data;
unsigned int reg_ecc_err_cnt;
@@ -389,6 +391,7 @@ struct sunxi_nfc_caps {
* @use_mdma: use an internal MBUS DMA backend
* @mdma_desc: H6-style MBUS DMA descriptor
* @mdma_desc_dma: DMA address of @mdma_desc
+ * @recovery_failed: reject further operations after unsuccessful recovery
* @caps: NAND Controller capabilities
*/
struct sunxi_nfc {
@@ -409,6 +412,7 @@ struct sunxi_nfc {
bool use_mdma;
struct sunxi_nfc_mdma_desc *mdma_desc;
dma_addr_t mdma_desc_dma;
+ bool recovery_failed;
const struct sunxi_nfc_caps *caps;
};
@@ -621,16 +625,50 @@ static void sunxi_nfc_dma_op_cleanup(struct sunxi_nfc *nfc,
struct scatterlist *sg)
{
dma_unmap_sg(nfc->dev, sg, 1, ddir);
+ /* Do not access controller registers after failed recovery. */
+ if (nfc->recovery_failed)
+ return;
+
writel(readl(nfc->regs + NFC_REG_CTL) & ~NFC_RAM_METHOD,
nfc->regs + NFC_REG_CTL);
}
-static void sunxi_nfc_dma_op_abort(struct sunxi_nfc *nfc)
+static int sunxi_nfc_dma_op_abort(struct sunxi_nfc *nfc)
{
- if (nfc->use_mdma)
- sunxi_nfc_rst(nfc);
- else
- dmaengine_terminate_all(nfc->dmac);
+ int ret;
+
+ if (!nfc->caps->needs_bus_reset) {
+ if (nfc->use_mdma)
+ sunxi_nfc_rst(nfc);
+ else
+ dmaengine_terminate_all(nfc->dmac);
+ return 0;
+ }
+
+ /* Drain the handler before resetting its registers. */
+ disable_irq(nfc->irq);
+ writel(0, nfc->regs + NFC_REG_INT);
+ readl(nfc->regs + NFC_REG_INT);
+
+ /* A soft reset alone can leave the first subsequent read displaced. */
+ sunxi_nfc_rst(nfc);
+ ret = reset_control_reset(nfc->reset);
+ if (ret)
+ goto failed;
+
+ writel(0, nfc->regs + NFC_REG_INT);
+ writel(NFC_INT_MASK, nfc->regs + NFC_REG_ST);
+ readl(nfc->regs + NFC_REG_INT);
+ enable_irq(nfc->irq);
+
+ /* The next operation selects the chip and restores its timing and layout. */
+ return 0;
+
+failed:
+ /* Keep the IRQ disabled and reject commands after failed recovery. */
+ nfc->recovery_failed = true;
+ dev_err(nfc->dev, "NAND controller recovery failed: %d\n", ret);
+ return ret;
}
static int sunxi_nfc_select_chip(struct nand_chip *nand, unsigned int cs)
@@ -642,6 +680,9 @@ static int sunxi_nfc_select_chip(struct nand_chip *nand, unsigned int cs)
u32 ctl;
int ret;
+ if (nfc->recovery_failed)
+ return -EIO;
+
if (cs >= sunxi_nand->nsels)
return -EINVAL;
@@ -1464,8 +1505,14 @@ static int sunxi_nfc_hw_ecc_read_batch_dma(struct nand_chip *nand,
nfc->regs + NFC_REG_CMD);
ret = sunxi_nfc_wait_events(nfc, wait, false, 0);
- if (ret)
- sunxi_nfc_dma_op_abort(nfc);
+ if (ret) {
+ int abort_ret = sunxi_nfc_dma_op_abort(nfc);
+
+ if (abort_ret) {
+ sunxi_nfc_dma_op_cleanup(nfc, DMA_FROM_DEVICE, &sg);
+ return abort_ret;
+ }
+ }
sunxi_nfc_randomizer_disable(nand);
sunxi_nfc_hw_ecc_disable(nand);
@@ -1514,7 +1561,7 @@ static int sunxi_nfc_hw_ecc_read_chunks_dma(struct nand_chip *nand, u8 *buf,
return ret;
err_stats:
- /* The caller retries the whole read in PIO mode. */
+ /* A PIO retry must not count completed DMA batches twice. */
mtd->ecc_stats.corrected = corrected;
mtd->ecc_stats.failed = failed;
return ret;
@@ -1648,6 +1695,7 @@ static int sunxi_nfc_hw_ecc_read_page(struct nand_chip *nand, u8 *buf,
static int sunxi_nfc_hw_ecc_read_page_dma(struct nand_chip *nand, u8 *buf,
int oob_required, int page)
{
+ struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
int ret;
ret = nand_read_page_op(nand, page, 0, NULL, 0);
@@ -1656,7 +1704,7 @@ static int sunxi_nfc_hw_ecc_read_page_dma(struct nand_chip *nand, u8 *buf,
ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, oob_required, page, 0,
nand->ecc.steps);
- if (ret >= 0)
+ if (ret >= 0 || nfc->recovery_failed)
return ret;
/* Fallback to PIO mode */
@@ -1677,6 +1725,7 @@ static int sunxi_nfc_hw_ecc_read_subpage_dma(struct nand_chip *nand,
u32 data_offs, u32 readlen,
u8 *buf, int page)
{
+ struct sunxi_nfc *nfc = to_sunxi_nfc(nand->controller);
int first_step = data_offs / nand->ecc.size;
int end_step = DIV_ROUND_UP(data_offs + readlen, nand->ecc.size);
int ret;
@@ -1686,7 +1735,7 @@ static int sunxi_nfc_hw_ecc_read_subpage_dma(struct nand_chip *nand,
return ret;
ret = sunxi_nfc_hw_ecc_read_chunks_dma(nand, buf, false, page, first_step, end_step);
- if (ret >= 0)
+ if (ret >= 0 || nfc->recovery_failed)
return ret;
/* Fallback to PIO mode */
@@ -1828,9 +1877,10 @@ static int sunxi_nfc_hw_ecc_write_page_dma(struct nand_chip *nand,
else
ret = nand_prog_page_begin_op(nand, page, 0, NULL, 0);
if (ret) {
- sunxi_nfc_dma_op_abort(nfc);
+ int abort_ret = sunxi_nfc_dma_op_abort(nfc);
+
sunxi_nfc_dma_op_cleanup(nfc, DMA_TO_DEVICE, &sg);
- return ret;
+ return abort_ret ?: ret;
}
/* exec_op() restores the page's spare base during column changes. */
@@ -1856,8 +1906,14 @@ static int sunxi_nfc_hw_ecc_write_page_dma(struct nand_chip *nand,
nfc->regs + NFC_REG_CMD);
ret = sunxi_nfc_wait_events(nfc, wait, false, 0);
- if (ret)
- sunxi_nfc_dma_op_abort(nfc);
+ if (ret) {
+ int abort_ret = sunxi_nfc_dma_op_abort(nfc);
+
+ if (abort_ret) {
+ sunxi_nfc_dma_op_cleanup(nfc, DMA_TO_DEVICE, &sg);
+ return abort_ret;
+ }
+ }
sunxi_nfc_randomizer_disable(nand);
sunxi_nfc_hw_ecc_disable(nand);
@@ -2794,11 +2850,18 @@ static int sunxi_nfc_probe(struct platform_device *pdev)
}
}
- nfc->reset = devm_reset_control_get_optional_exclusive(dev, "ahb");
+ if (nfc->caps->needs_bus_reset)
+ nfc->reset = devm_reset_control_get_exclusive(dev, "ahb");
+ else
+ nfc->reset = devm_reset_control_get_optional_exclusive(dev, "ahb");
if (IS_ERR(nfc->reset))
- return PTR_ERR(nfc->reset);
+ return dev_err_probe(dev, PTR_ERR(nfc->reset), "failed to get AHB reset\n");
- ret = reset_control_deassert(nfc->reset);
+ /* Exercise the recovery reset operation before accepting DMA transfers. */
+ if (nfc->caps->needs_bus_reset)
+ ret = reset_control_reset(nfc->reset);
+ else
+ ret = reset_control_deassert(nfc->reset);
if (ret) {
dev_err(dev, "reset err %d\n", ret);
return ret;
@@ -2908,6 +2971,7 @@ static const struct sunxi_nfc_caps sunxi_nfc_h616_caps = {
.has_mdma_desc = true,
.has_ecc_clk = true,
.has_mbus_clk = true,
+ .needs_bus_reset = true,
.reg_io_data = NFC_REG_A23_IO_DATA,
.reg_ecc_err_cnt = NFC_REG_H6_ECC_ERR_CNT,
.reg_user_data = NFC_REG_H6_USER_DATA,