[PATCH v11 19/26] mtd: rawnand: sunxi: propagate chip clock setup errors

From: James Hilliard

Date: Fri Nov 20 2026 - 20:13:12 EST


If clk_set_rate() fails during chip selection, the next NAND command
can run with timings calculated for a clock rate that was never set.
The driver also caches the requested rate, so selecting the chip again
does not retry the change.

Return the clock error before programming the timings or updating the
cache. Propagate it through exec_op() and the DMA page writer so
neither starts a command at the wrong rate.

Fixes: 1fef62c1423b ("mtd: nand: add sunxi NAND flash controller support")
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/nand/raw/sunxi_nand.c | 32 ++++++++++++++++++++++----------
1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/drivers/mtd/nand/raw/sunxi_nand.c b/drivers/mtd/nand/raw/sunxi_nand.c
index ee68ef9bba33..37cc023db74e 100644
--- a/drivers/mtd/nand/raw/sunxi_nand.c
+++ b/drivers/mtd/nand/raw/sunxi_nand.c
@@ -628,16 +628,25 @@ static void sunxi_nfc_dma_op_abort(struct sunxi_nfc *nfc)
dmaengine_terminate_all(nfc->dmac);
}

-static void sunxi_nfc_select_chip(struct nand_chip *nand, unsigned int cs)
+static int sunxi_nfc_select_chip(struct nand_chip *nand, unsigned int cs)
{
struct mtd_info *mtd = nand_to_mtd(nand);
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
struct sunxi_nfc *nfc = to_sunxi_nfc(sunxi_nand->nand.controller);
struct sunxi_nand_chip_sel *sel;
u32 ctl;
+ int ret;

if (cs >= sunxi_nand->nsels)
- return;
+ return -EINVAL;
+
+ if (nfc->clk_rate != sunxi_nand->clk_rate) {
+ ret = clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
+ if (ret)
+ return ret;
+
+ nfc->clk_rate = sunxi_nand->clk_rate;
+ }

ctl = readl(nfc->regs + NFC_REG_CTL) &
~(NFC_PAGE_SHIFT_MSK | NFC_CE_SEL_MSK | NFC_RB_SEL_MSK | NFC_EN);
@@ -649,14 +658,11 @@ static void sunxi_nfc_select_chip(struct nand_chip *nand, unsigned int cs)

writel(mtd->writesize, nfc->regs + NFC_REG_SPARE_AREA(nfc));

- if (nfc->clk_rate != sunxi_nand->clk_rate) {
- clk_set_rate(nfc->mod_clk, sunxi_nand->clk_rate);
- nfc->clk_rate = sunxi_nand->clk_rate;
- }
-
writel(sunxi_nand->timing_ctl, nfc->regs + NFC_REG_TIMING_CTL);
writel(sunxi_nand->timing_cfg, nfc->regs + NFC_REG_TIMING_CFG);
writel(ctl, nfc->regs + NFC_REG_CTL);
+
+ return 0;
}

static int sunxi_nfc_buf_chunk_size(struct sunxi_nfc *nfc, int len)
@@ -1778,7 +1784,9 @@ static int sunxi_nfc_hw_ecc_write_page_dma(struct nand_chip *nand,
u32 wait;
int first_step, batch_steps, ret, i;

- sunxi_nfc_select_chip(nand, nand->cur_cs);
+ ret = sunxi_nfc_select_chip(nand, nand->cur_cs);
+ if (ret)
+ return ret;

for (first_step = 0; first_step < ecc->steps; first_step += batch_steps) {
unsigned int reg_index = 0;
@@ -2519,9 +2527,13 @@ static int sunxi_nfc_exec_op(struct nand_chip *nand,
{
struct sunxi_nand_chip *sunxi_nand = to_sunxi_nand(nand);
const struct nand_op_parser *parser;
+ int ret;

- if (!check_only)
- sunxi_nfc_select_chip(nand, op->cs);
+ if (!check_only) {
+ ret = sunxi_nfc_select_chip(nand, op->cs);
+ if (ret)
+ return ret;
+ }

if (sunxi_nand->sels[op->cs].rb >= 0)
parser = &sunxi_nfc_op_parser;

--
2.53.0