Skip to contentSkip to conversation

Add support for nuvoton ma35 nand controller

PATCH Superseded Hui-Ping Chen 2 participants 5 messages Sep 18, 2024 – Sep 19, 2024

Subsystems (5)

v4 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v4 cover Add support for nuvoton ma35 nand controller

This patch series adds the mtd nand driver for the nuvoton ma35 ARMv8 SoC. It includes DT binding documentation and the ma35 mtd nand driver.

v4:
- Update nuvoton,ma35d1-nand.yaml
- rename 'nuvoton,ma35d1-nand' to 'nuvoton,ma35d1-nand-controller'.
- Update ma35d1 mtd nand driver
- Rewrite the NAND driver using the exec_op API.

v3:
- Update ma35d1 mtd nand driver
- Release IRQ handler.
- Remove unused functions.
- Remove '.owner'.

v2:
- Update nuvoton,ma35d1-nand.yaml
- Adjust the order and remove any unnecessary items.
- Add 'nand-ecc-step-size' and 'nand-ecc-strength' to the required list.
- Update ma35d1 mtd nand driver
- Fix coding style.
- Use 'devm_clk_get' instead of 'of_clk_get'.
- Use 'dev_err_probe' instead of 'dev_err'.
- Remove 'pr_info' and 'of_match_ptr'.
- Remove 'module_init' and 'module_exit'.

Hui-Ping Chen (2):
dt-bindings: mtd: nuvoton,ma35d1-nand: add new bindings
mtd: rawnand: nuvoton: add new driver for the Nuvoton MA35 SoC

 .../bindings/mtd/nuvoton,ma35d1-nand.yaml     |  93 ++
 drivers/mtd/nand/raw/Kconfig                  |   8 +
 drivers/mtd/nand/raw/Makefile                 |   1 +
 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c    | 935 ++++++++++++++++++
 4 files changed, 1037 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml
 create mode 100644 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v4 1/2 dt-bindings: mtd: nuvoton,ma35d1-nand: add new bindings

Add dt-bindings for the Nuvoton MA35 SoC NAND Controller.

---
 .../bindings/mtd/nuvoton,ma35d1-nand.yaml     | 93 +++++++++++++++++++
 1 file changed, 93 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml
Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml new file +93 -0
new file mode 100644
index 000000000000..a8a549644c98
@@ -0,0 +1,93 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mtd/nuvoton,ma35d1-nand.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Nuvoton MA35D1 NAND Flash Interface (NFI) Controller
+
+maintainers:
+  - Hui-Ping Chen <[email protected]>
+
+allOf:
+  - $ref: nand-controller.yaml#
+
+properties:
+  compatible:
+    enum:
+      - nuvoton,ma35d1-nand-controller
+
+  reg:
+    maxItems: 1
+
+  interrupts:
+    maxItems: 1
+
+  clocks:
+    maxItems: 1
+
+patternProperties:
+  "^nand@[a-f0-9]$":
+    type: object
+    $ref: raw-nand-chip.yaml
+    properties:
+      nand-ecc-step-size:
+        enum: [512, 1024]
+
+      nand-ecc-strength:
+        enum: [8, 12, 24]
+
+    required:
+      - nand-ecc-step-size
+      - nand-ecc-strength
+
+    unevaluatedProperties: false
+
+required:
+  - compatible
+  - reg
+  - interrupts
+  - clocks
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    #include <dt-bindings/interrupt-controller/arm-gic.h>
+    #include <dt-bindings/clock/nuvoton,ma35d1-clk.h>
+
+    soc {
+        #address-cells = <2>;
+        #size-cells = <2>;
+
+        nand-controller@401A0000 {
+            compatible = "nuvoton,ma35d1-nand-controller";
+            reg = <0x0 0x401A0000 0x0 0x1000>;
+            interrupts = <GIC_SPI 38 IRQ_TYPE_LEVEL_HIGH>;
+            clocks = <&clk NAND_GATE>;
+            #address-cells = <1>;
+            #size-cells = <0>;
+
+            nand@0 {
+                reg = <0>;
+
+                nand-on-flash-bbt;
+                nand-ecc-step-size = <512>;
+                nand-ecc-strength = <8>;
+
+                partitions {
+                    compatible = "fixed-partitions";
+                    #address-cells = <1>;
+                    #size-cells = <1>;
+
+                    uboot@0 {
+                        label = "nand-uboot";
+                        read-only;
+                        reg = <0x0 0x300000>;
+                    };
+                };
+            };
+        };
+    };
+
+...
Signature · 1 line
2.25.1
#2 lore raw

PATCH v4 2/2 mtd: rawnand: nuvoton: add new driver for the Nuvoton MA35 SoC

In reply to Hui-Ping Chen

Nuvoton MA35 SoCs NAND Flash Interface Controller supports 2kiB, 4kiB and 8kiB page size, and up to 8-bit, 12-bit, and 24-bit hardware ECC calculation circuit to protect data.

---
 drivers/mtd/nand/raw/Kconfig               |   8 +
 drivers/mtd/nand/raw/Makefile              |   1 +
 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c | 935 +++++++++++++++++++++
 3 files changed, 944 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c
drivers/mtd/nand/raw/Kconfig +8 -0
index 614257308516..a95d91e61c42 100644
@@ -448,6 +448,14 @@ config MTD_NAND_RENESAS
 	  Enables support for the NAND controller found on Renesas R-Car
 	  Gen3 and RZ/N1 SoC families.
 
+config MTD_NAND_NUVOTON_MA35
+	tristate "Nuvoton MA35 SoC NAND controller"
+	depends on ARCH_MA35 || COMPILE_TEST
+	depends on OF
+	help
+	  Enables support for the NAND controller found on
+	  the Nuvoton MA35 series SoCs.
+
 comment "Misc"
 
 config MTD_SM_COMMON
drivers/mtd/nand/raw/Makefile +1 -0
index 25120a4afada..b8e1b3af6942 100644
@@ -57,6 +57,7 @@ obj-$(CONFIG_MTD_NAND_INTEL_LGM)	+= intel-nand-controller.o
 obj-$(CONFIG_MTD_NAND_ROCKCHIP)		+= rockchip-nand-controller.o
 obj-$(CONFIG_MTD_NAND_PL35X)		+= pl35x-nand-controller.o
 obj-$(CONFIG_MTD_NAND_RENESAS)		+= renesas-nand-controller.o
+obj-$(CONFIG_MTD_NAND_NUVOTON_MA35)	+= nuvoton_ma35d1_nand.o
 
 nand-objs := nand_base.o nand_legacy.o nand_bbt.o nand_timings.o nand_ids.o
 nand-objs += nand_onfi.o
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +935 -0

935 lines not on this page · raw message

Signature · 1 line
2.25.1
#3 lore raw

Re: [PATCH v4 2/2] mtd: rawnand: nuvoton: add new driver for the Nuvoton MA35 SoC

Hi,

The driver has a few minor whitespace issues, please run through checkpatch.pl to catch them.

Some more things inline.

Quoted 6 lines · Hui-Ping Chen

Nuvoton MA35 SoCs NAND Flash Interface Controller supports 2kiB, 4kiB and 8kiB page size, and up to 8-bit, 12-bit, and 24-bit hardware ECC calculation circuit to protect data.

Quoted 8 lines
---
 drivers/mtd/nand/raw/Kconfig               |   8 +
 drivers/mtd/nand/raw/Makefile              |   1 +
 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c | 935 +++++++++++++++++++++
 3 files changed, 944 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c

+#define SKIP_SPARE_BYTES 4

Unused, please drop.

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 372

Quoted 5 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +5 -0
+static int ma35_nfi_ecc_check(struct nand_chip *chip, unsigned long addr)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	int status, i, j, nchunks = 0;

status should be unsigned.

Quoted 15 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +15 -0
+	int report_err = 0;
+	int err_cnt = 0;
+
+	nchunks = mtd->writesize / chip->ecc.steps;
+	if (nchunks < 4)
+		nchunks = 1;
+	else
+		nchunks /= 4;
+
+	for (j = 0; j < nchunks; j++) {
+		status = readl(nand->regs + MA35_NFI_REG_NANDECCES0 + j*4);
+		if (!status)
+			continue;
+
+		for (i = 0; i < 4; i++) {

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 397

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+			if (!(status & ECC_STATUS_MASK)) {
+				/* No error */
+				status >>= 8;
+				continue;
+
+			} else if ((status & ECC_STATUS_MASK) == 0x01) {
+				/* Correctable error */
+				err_cnt = (status >> 2) & ECC_ERR_CNT_MASK;
+				dev_warn(nand->dev, "nchunks (%d, %d) have %d error!\n",
+					j, i, err_cnt);

Correctable bitflips are expected. Please don't spam the log with it.

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 409

Quoted 12 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +12 -0
+				ma35_nfi_correct(nand, j*4+i, err_cnt, (u8 *)addr);
+				report_err += err_cnt;
+
+			} else {
+				/* uncorrectable error */
+				dev_warn(nand->dev, "uncorrectable error! 0x%4x\n", status);
+				return -1;
+			}
+			status >>= 8;
+		}
+	}
+	return report_err;

There are a few things wrong here. Your chip->ecc.read_page op must return the maximum number of bitflips occured on a subpage while reading a page.

To archieve this I suggest you fix the return value of this function accordingly and call it from chip->ecc.read_page rather than from the interrupt handler.

Nevertheless mtd->ecc_stats.corrected counts the total number of bitflips, so you must handle this counter in this function.

See rk_nfc_read_page_hwecc() as an example of a driver which gets it right.

The background is that we have to rewrite the page once one ECC block hits a critical bitflip limit. A whole page might be fine when the bitflips are evenly distributed across the subpages, but it's not when all bitflips are occur in a single subpage.

Quoted 15 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +15 -0
+static int ma35_nand_do_write(struct nand_chip *chip, const u8 *addr, u32 len)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	dma_addr_t dma_addr;
+	int ret = 0, i;
+	u32 val, reg;
+
+	ma35_nand_target_enable(nand);
+
+	if (len != mtd->writesize) {
+		for (i = 0; i < len; i++)
+			writel(addr[i], nand->regs + MA35_NFI_REG_NANDDATA);
+		ma35_nand_target_disable(nand);
+		return ret;

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 456

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	}
+
+	/* Check the DMA status before enabling the DMA */
+	ret = readl_poll_timeout(nand->regs + MA35_NFI_REG_DMACTL, val,
+				 !(val & DMA_BUSY), 50, HZ/2);
+	if (ret)
+		dev_warn(nand->dev, "dma busy\n");
+
+	/* Reinitial dmac */
+	ma35_nand_dmac_init(nand);

The function name already says it and the comment doesn't offer any additional information. Please drop such comments.

Quoted 8 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +8 -0
+
+	writel(mtd->oobsize, nand->regs + MA35_NFI_REG_NANDRACTL);
+
+	/* setup and start DMA using dma_addr */
+	writel(INT_DMA, nand->regs + MA35_NFI_REG_NANDINTEN);
+	/* To mark this page as dirty. */
+	reg = readl(nand->regs + MA35_NFI_REG_NANDRA0);
+	if (reg & 0xffff0000)

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 474

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+		writel(reg & 0xffff, nand->regs + MA35_NFI_REG_NANDRA0);
+
+	/* Fill dma_addr */
+	dma_addr = dma_map_single(nand->dev, (void *)addr, len, DMA_TO_DEVICE);
+	dma_sync_single_for_device(nand->dev, dma_addr, len, DMA_TO_DEVICE);
+	ret = dma_mapping_error(nand->dev, dma_addr);
+	if (ret) {
+		dev_err(nand->dev, "dma mapping error\n");
+		return -EINVAL;
+	}

Call dma_sync_single_for_device() after you have checked for an error with dma_mapping_error().

That said, I think calling dma_sync_single_for_device() after dma_map_single() is unnecessary.

Quoted 8 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +8 -0
+
+	writel((unsigned long)dma_addr, nand->regs + MA35_NFI_REG_DMASA);
+	writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | DMA_W_EN,
+		nand->regs + MA35_NFI_REG_NANDCTL);
+	ret = wait_for_completion_timeout(&nand->complete, msecs_to_jiffies(1000));
+	if (!ret) {
+		dev_err(nand->dev, "write timeout\n");
+		ret = -ETIMEDOUT;

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 492

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	}
+
+	dma_unmap_single(nand->dev, dma_addr, len, DMA_TO_DEVICE);
+
+	ma35_nand_target_disable(nand);
+
+	return ret;
+}
+
+static int ma35_nand_do_read(struct nand_chip *chip, const u8 *addr, u32 len)

The addr argument shouldn't be const. You are supposed to write to this buffer and you actually do so.

Quoted 5 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +5 -0
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	u8 *ptr = (u8 *)addr;
+	dma_addr_t dma_addr;

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 507

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	int ret = 0, i;
+	u32 val;
+
+	ma35_nand_target_enable(nand);
+
+	if (len != mtd->writesize) {
+		for (i = 0; i < len; i++)
+			*(ptr+i) = (u8)readl(nand->regs + MA35_NFI_REG_NANDDATA);
+		ma35_nand_target_disable(nand);
+		return ret;

Just return 0 here. It's easier to read than having to look up the initialization value.

Quoted 24 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +24 -0
+	}
+
+	/* Check the DMA status before enabling the DMA */
+	ret = readl_poll_timeout(nand->regs + MA35_NFI_REG_DMACTL, val,
+				 !(val & DMA_BUSY), 50, HZ/2);
+	if (ret)
+		dev_warn(nand->dev, "dma busy\n");
+
+	/* Reinitial dmac */
+	ma35_nand_dmac_init(nand);
+
+	writel(mtd->oobsize, nand->regs + MA35_NFI_REG_NANDRACTL);
+
+	/* setup and start DMA using dma_addr */
+	dma_addr = dma_map_single(nand->dev, (void *)addr, len, DMA_FROM_DEVICE);
+	ret = dma_mapping_error(nand->dev, dma_addr);
+	if (ret) {
+		dev_err(nand->dev, "dma mapping error\n");
+		return -EINVAL;
+	}
+	nand->dma_buf = (u8 *)addr;
+	nand->dma_addr = dma_addr;
+
+	writel((unsigned long)dma_addr, nand->regs + MA35_NFI_REG_DMASA);

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 541

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | DMA_R_EN,
+		nand->regs + MA35_NFI_REG_NANDCTL);
+	ret = wait_for_completion_timeout(&nand->complete, msecs_to_jiffies(1000));
+	if (!ret) {
+		dev_err(nand->dev, "read timeout\n");
+		ret = -ETIMEDOUT;
+	}
+
+	dma_sync_single_for_cpu(nand->dev, dma_addr, len, DMA_FROM_DEVICE);
+	dma_unmap_single(nand->dev, dma_addr, len, DMA_FROM_DEVICE);

No need to call dma_sync_single_for_cpu() before dma_unmap_single().

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 553

Quoted 12 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +12 -0
+
+	ma35_nand_target_disable(nand);
+
+	return ret;
+}
+
+
+static int ma35_nand_write_page_hwecc(struct nand_chip *chip, const u8 *buf,
+				      int oob_required, int page)
+{
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	u8 *ecc_calc = chip->ecc.calc_buf;

Make this a void * to get rid of the explicit casting below.

Quoted 22 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +22 -0
+
+	ma35_clear_spare(chip, mtd->oobsize);
+	ma35_write_spare(chip, mtd->oobsize - chip->ecc.total, (u32 *)chip->oob_poi);
+
+	nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
+	nand_prog_page_end_op(chip);
+
+	/* Copy parity code in NANDRA to calc */
+	ma35_read_spare(chip, chip->ecc.total, (u32 *)ecc_calc,
+			mtd->oobsize - chip->ecc.total);
+
+	/* Copy parity code in calc to oob_poi */
+	memcpy((void *)(chip->oob_poi + (mtd->oobsize - chip->ecc.total)),
+		(void *)ecc_calc, chip->ecc.total);
+
+	return 0;
+}
+
+static irqreturn_t ma35_nand_irq(int irq, void *id)
+{
+	struct ma35_nand_info *nand = (struct ma35_nand_info *)id;
+	struct mtd_info *mtd = nand_to_mtd(&nand->chip);

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 631

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	int stat = 0;
+	u32 isr;
+
+	spin_lock(&nand->dma_lock);
+
+	isr = readl(nand->regs + MA35_NFI_REG_NANDINTSTS);
+	if (isr & INT_ECC) {
+		dma_sync_single_for_cpu(nand->dev, nand->dma_addr, mtd->writesize,
+					DMA_FROM_DEVICE);
+		stat = ma35_nfi_ecc_check(&nand->chip, (unsigned long)nand->dma_buf);

nand->dma_buf already is a pointer which you cast to unisgned long here and back to a pointer in ma35_nfi_ecc_check(). ma35_nfi_ecc_check() should take a poiner instead.

Quoted 30 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +30 -0
+		if (stat < 0) {
+			mtd->ecc_stats.failed++;
+			writel(DMA_RST | DMA_EN, nand->regs + MA35_NFI_REG_DMACTL);
+			writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | SWRST,
+				nand->regs + MA35_NFI_REG_NANDCTL);
+		} else if (stat > 0) {
+			mtd->ecc_stats.corrected += stat;   /* Add corrected bit count */
+		}
+		writel(INT_ECC, nand->regs + MA35_NFI_REG_NANDINTSTS);
+	}
+	if (isr & INT_DMA) {
+		writel(INT_DMA, nand->regs + MA35_NFI_REG_NANDINTSTS);
+		complete(&nand->complete);
+	}
+	spin_unlock(&nand->dma_lock);
+
+	return IRQ_HANDLED;
+}
+
+static int ma35_nfc_exec_op(struct nand_chip *chip,
+			  const struct nand_operation *op,
+			  bool check_only)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	u32 i, reg;
+	int ret = 0;
+
+	if (check_only)
+		return 0;
+

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 782

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	ma35_nand_target_enable(nand);
+	reg = readl(nand->regs + MA35_NFI_REG_NANDINTSTS);
+	reg |= INT_RB0;
+	writel(reg, nand->regs + MA35_NFI_REG_NANDINTSTS);
+
+	for (i = 0; i < op->ninstrs; i++) {
+		ret = ma35_nfc_exec_instr(chip, &op->instrs[i]);
+		if (ret)
+			break;
+	}

The way ma35_nand_target_[en|dis]able() is called looks inconsistent.

This function calls ma35_nand_target_enable(), so I would expect that the corresponding ma35_nand_target_disable() should be called here as well.

ma35_nand_do_read() is called from here which has its own call to ma35_nand_target_enable(), but it doesn't call ma35_nand_target_disable() from all of its return pathes.

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 830

Quoted 6 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +6 -0
+
+	ret = devm_request_irq(&pdev->dev, nand->irq, ma35_nand_irq,
+				  IRQF_TRIGGER_HIGH, "ma35d1-nand", nand);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request NAND irq\n");
+		clk_disable_unprepare(nand->clk);

You used devm_clk_get_enabled(), so this will be done automatically.

Quoted 20 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +20 -0
+		return -ENXIO;
+	}
+
+	nand->chip.controller = &nand->controller;
+	platform_set_drvdata(pdev, nand);
+
+	chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA;
+
+	/* set default mode in case dt entry is missing */
+	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
+
+	chip->ecc.write_page = ma35_nand_write_page_hwecc;
+	chip->ecc.read_page  = ma35_nand_read_page_hwecc;
+	chip->ecc.read_oob   = ma35_nand_read_oob_hwecc;
+
+	mtd = nand_to_mtd(chip);
+	mtd->priv = chip;
+	mtd->owner = THIS_MODULE;
+	mtd->dev.parent = &pdev->dev;
+

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 860

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	writel(NAND_EN, nand->regs + MA35_NFI_REG_GCTL);
+
+	ret = nand_scan(chip, 1);
+	if (ret)
+		return ret;
+
+	ret = mtd_device_register(mtd, NULL, 0);
+	if (ret) {
+		nand_cleanup(chip);
+		devm_kfree(&pdev->dev, nand);

Unnecessary free. Drop it.

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 872

Quoted 12 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +12 -0
+		return ret;
+	}
+
+	return ret;
+}
+
+static void ma35_nand_remove(struct platform_device *pdev)
+{
+	struct ma35_nand_info *nand = platform_get_drvdata(pdev);
+	int ret;
+
+	devm_free_irq(&pdev->dev, nand->irq, nand);

devm_ is a mechanism to let resources be freed automatically. There's normally no need to do this manually.

Sascha

Signature · 4 lines
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |
#4 lore raw

Re: [PATCH v4 2/2] mtd: rawnand: nuvoton: add new driver for the Nuvoton MA35 SoC

Dear Sascha,

Thank you for your reply.

Quoted 4 lines · Sascha Hauer

Hi,

The driver has a few minor whitespace issues, please run through checkpatch.pl to catch them.

I will fix it.

Quoted 16 lines

Some more things inline.

Quoted 13 lines · Hui-Ping Chen

Nuvoton MA35 SoCs NAND Flash Interface Controller supports 2kiB, 4kiB and 8kiB page size, and up to 8-bit, 12-bit, and 24-bit hardware ECC calculation circuit to protect data.

---
  drivers/mtd/nand/raw/Kconfig               |   8 +
  drivers/mtd/nand/raw/Makefile              |   1 +
  drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c | 935 +++++++++++++++++++++
  3 files changed, 944 insertions(+)

create mode 100644 drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c

Quoted 2 lines
Quoted 1 line

+#define SKIP_SPARE_BYTES 4

Unused, please drop.

I will remove it.

Quoted 6 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 372

Quoted 5 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +5 -0
+static int ma35_nfi_ecc_check(struct nand_chip *chip, unsigned long addr)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	int status, i, j, nchunks = 0;

status should be unsigned.

Okay, I will modify it.

Quoted 18 lines
Quoted 18 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +18 -0
+	int report_err = 0;
+	int err_cnt = 0;
+
+	nchunks = mtd->writesize / chip->ecc.steps;
+	if (nchunks < 4)
+		nchunks = 1;
+	else
+		nchunks /= 4;
+
+	for (j = 0; j < nchunks; j++) {
+		status = readl(nand->regs + MA35_NFI_REG_NANDECCES0 + j*4);
+		if (!status)
+			continue;
+
+		for (i = 0; i < 4; i++) {
+			if (!(status & ECC_STATUS_MASK)) {
+				/* No error */
+				status >>= 8;
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 397

Quoted 7 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +7 -0
+				continue;
+
+			} else if ((status & ECC_STATUS_MASK) == 0x01) {
+				/* Correctable error */
+				err_cnt = (status >> 2) & ECC_ERR_CNT_MASK;
+				dev_warn(nand->dev, "nchunks (%d, %d) have %d error!\n",
+					j, i, err_cnt);

Correctable bitflips are expected. Please don't spam the log with it.

I will remove it.

Quoted 23 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 409

Quoted 12 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +12 -0
+				ma35_nfi_correct(nand, j*4+i, err_cnt, (u8 *)addr);
+				report_err += err_cnt;
+
+			} else {
+				/* uncorrectable error */
+				dev_warn(nand->dev, "uncorrectable error! 0x%4x\n", status);
+				return -1;
+			}
+			status >>= 8;
+		}
+	}
+	return report_err;

There are a few things wrong here. Your chip->ecc.read_page op must return the maximum number of bitflips occured on a subpage while reading a page.

To archieve this I suggest you fix the return value of this function accordingly and call it from chip->ecc.read_page rather than from the interrupt handler.

Nevertheless mtd->ecc_stats.corrected counts the total number of bitflips, so you must handle this counter in this function.

Quoted 7 lines

See rk_nfc_read_page_hwecc() as an example of a driver which gets it right.

The background is that we have to rewrite the page once one ECC block hits a critical bitflip limit. A whole page might be fine when the bitflips are evenly distributed across the subpages, but it's not when all bitflips are occur in a single subpage.

I will refer to rk_nfc_read_page_hwecc() and then rewrite it.

Quoted 19 lines
Quoted 19 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +19 -0
+static int ma35_nand_do_write(struct nand_chip *chip, const u8 *addr, u32 len)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	dma_addr_t dma_addr;
+	int ret = 0, i;
+	u32 val, reg;
+
+	ma35_nand_target_enable(nand);
+
+	if (len != mtd->writesize) {
+		for (i = 0; i < len; i++)
+			writel(addr[i], nand->regs + MA35_NFI_REG_NANDDATA);
+		ma35_nand_target_disable(nand);
+		return ret;
+	}
+
+	/* Check the DMA status before enabling the DMA */
+	ret = readl_poll_timeout(nand->regs + MA35_NFI_REG_DMACTL, val,
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 456

Quoted 6 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +6 -0
+				 !(val & DMA_BUSY), 50, HZ/2);
+	if (ret)
+		dev_warn(nand->dev, "dma busy\n");
+
+	/* Reinitial dmac */
+	ma35_nand_dmac_init(nand);

The function name already says it and the comment doesn't offer any additional information. Please drop such comments.

I will remove it.

Quoted 21 lines
Quoted 8 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +8 -0
+
+	writel(mtd->oobsize, nand->regs + MA35_NFI_REG_NANDRACTL);
+
+	/* setup and start DMA using dma_addr */
+	writel(INT_DMA, nand->regs + MA35_NFI_REG_NANDINTEN);
+	/* To mark this page as dirty. */
+	reg = readl(nand->regs + MA35_NFI_REG_NANDRA0);
+	if (reg & 0xffff0000)

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 474

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+		writel(reg & 0xffff, nand->regs + MA35_NFI_REG_NANDRA0);
+
+	/* Fill dma_addr */
+	dma_addr = dma_map_single(nand->dev, (void *)addr, len, DMA_TO_DEVICE);
+	dma_sync_single_for_device(nand->dev, dma_addr, len, DMA_TO_DEVICE);
+	ret = dma_mapping_error(nand->dev, dma_addr);
+	if (ret) {
+		dev_err(nand->dev, "dma mapping error\n");
+		return -EINVAL;
+	}

Call dma_sync_single_for_device() after you have checked for an error with dma_mapping_error().

Quoted 2 lines

That said, I think calling dma_sync_single_for_device() after dma_map_single() is unnecessary.

I will modify the calling sequence.

Quoted 12 lines
Quoted 12 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +12 -0
+
+	writel((unsigned long)dma_addr, nand->regs + MA35_NFI_REG_DMASA);
+	writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | DMA_W_EN,
+		nand->regs + MA35_NFI_REG_NANDCTL);
+	ret = wait_for_completion_timeout(&nand->complete, msecs_to_jiffies(1000));
+	if (!ret) {
+		dev_err(nand->dev, "write timeout\n");
+		ret = -ETIMEDOUT;
+	}
+
+	dma_unmap_single(nand->dev, dma_addr, len, DMA_TO_DEVICE);
+
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 492

Quoted 6 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +6 -0
+	ma35_nand_target_disable(nand);
+
+	return ret;
+}
+
+static int ma35_nand_do_read(struct nand_chip *chip, const u8 *addr, u32 len)

The addr argument shouldn't be const. You are supposed to write to this buffer and you actually do so.

I will change this argument to u8 *.

Quoted 9 lines
Quoted 9 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +9 -0
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	u8 *ptr = (u8 *)addr;
+	dma_addr_t dma_addr;
+	int ret = 0, i;
+	u32 val;
+
+	ma35_nand_target_enable(nand);
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 507

Quoted 6 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +6 -0
+
+	if (len != mtd->writesize) {
+		for (i = 0; i < len; i++)
+			*(ptr+i) = (u8)readl(nand->regs + MA35_NFI_REG_NANDDATA);
+		ma35_nand_target_disable(nand);
+		return ret;

Just return 0 here. It's easier to read than having to look up the initialization value.

I will change it to return 0 directly.

Quoted 27 lines
Quoted 27 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +27 -0
+	}
+
+	/* Check the DMA status before enabling the DMA */
+	ret = readl_poll_timeout(nand->regs + MA35_NFI_REG_DMACTL, val,
+				 !(val & DMA_BUSY), 50, HZ/2);
+	if (ret)
+		dev_warn(nand->dev, "dma busy\n");
+
+	/* Reinitial dmac */
+	ma35_nand_dmac_init(nand);
+
+	writel(mtd->oobsize, nand->regs + MA35_NFI_REG_NANDRACTL);
+
+	/* setup and start DMA using dma_addr */
+	dma_addr = dma_map_single(nand->dev, (void *)addr, len, DMA_FROM_DEVICE);
+	ret = dma_mapping_error(nand->dev, dma_addr);
+	if (ret) {
+		dev_err(nand->dev, "dma mapping error\n");
+		return -EINVAL;
+	}
+	nand->dma_buf = (u8 *)addr;
+	nand->dma_addr = dma_addr;
+
+	writel((unsigned long)dma_addr, nand->regs + MA35_NFI_REG_DMASA);
+	writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | DMA_R_EN,
+		nand->regs + MA35_NFI_REG_NANDCTL);
+	ret = wait_for_completion_timeout(&nand->complete, msecs_to_jiffies(1000));
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 541

Quoted 7 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +7 -0
+	if (!ret) {
+		dev_err(nand->dev, "read timeout\n");
+		ret = -ETIMEDOUT;
+	}
+
+	dma_sync_single_for_cpu(nand->dev, dma_addr, len, DMA_FROM_DEVICE);
+	dma_unmap_single(nand->dev, dma_addr, len, DMA_FROM_DEVICE);

No need to call dma_sync_single_for_cpu() before dma_unmap_single().

I will remove it.

Quoted 5 lines
Quoted 5 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +5 -0
+
+	ma35_nand_target_disable(nand);
+
+	return ret;
+}
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 553

Quoted 7 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +7 -0
+
+
+static int ma35_nand_write_page_hwecc(struct nand_chip *chip, const u8 *buf,
+				      int oob_required, int page)
+{
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	u8 *ecc_calc = chip->ecc.calc_buf;

Make this a void * to get rid of the explicit casting below.

I will modify it.

Quoted 27 lines
Quoted 27 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +27 -0
+
+	ma35_clear_spare(chip, mtd->oobsize);
+	ma35_write_spare(chip, mtd->oobsize - chip->ecc.total, (u32 *)chip->oob_poi);
+
+	nand_prog_page_begin_op(chip, page, 0, buf, mtd->writesize);
+	nand_prog_page_end_op(chip);
+
+	/* Copy parity code in NANDRA to calc */
+	ma35_read_spare(chip, chip->ecc.total, (u32 *)ecc_calc,
+			mtd->oobsize - chip->ecc.total);
+
+	/* Copy parity code in calc to oob_poi */
+	memcpy((void *)(chip->oob_poi + (mtd->oobsize - chip->ecc.total)),
+		(void *)ecc_calc, chip->ecc.total);
+
+	return 0;
+}
+
+static irqreturn_t ma35_nand_irq(int irq, void *id)
+{
+	struct ma35_nand_info *nand = (struct ma35_nand_info *)id;
+	struct mtd_info *mtd = nand_to_mtd(&nand->chip);
+	int stat = 0;
+	u32 isr;
+
+	spin_lock(&nand->dma_lock);
+
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 631

Quoted 5 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +5 -0
+	isr = readl(nand->regs + MA35_NFI_REG_NANDINTSTS);
+	if (isr & INT_ECC) {
+		dma_sync_single_for_cpu(nand->dev, nand->dma_addr, mtd->writesize,
+					DMA_FROM_DEVICE);
+		stat = ma35_nfi_ecc_check(&nand->chip, (unsigned long)nand->dma_buf);

nand->dma_buf already is a pointer which you cast to unisgned long here and back to a pointer in ma35_nfi_ecc_check(). ma35_nfi_ecc_check() should take a poiner instead.

I will fix it.

Quoted 42 lines
Quoted 30 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +30 -0
+		if (stat < 0) {
+			mtd->ecc_stats.failed++;
+			writel(DMA_RST | DMA_EN, nand->regs + MA35_NFI_REG_DMACTL);
+			writel(readl(nand->regs + MA35_NFI_REG_NANDCTL) | SWRST,
+				nand->regs + MA35_NFI_REG_NANDCTL);
+		} else if (stat > 0) {
+			mtd->ecc_stats.corrected += stat;   /* Add corrected bit count */
+		}
+		writel(INT_ECC, nand->regs + MA35_NFI_REG_NANDINTSTS);
+	}
+	if (isr & INT_DMA) {
+		writel(INT_DMA, nand->regs + MA35_NFI_REG_NANDINTSTS);
+		complete(&nand->complete);
+	}
+	spin_unlock(&nand->dma_lock);
+
+	return IRQ_HANDLED;
+}
+
+static int ma35_nfc_exec_op(struct nand_chip *chip,
+			  const struct nand_operation *op,
+			  bool check_only)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	u32 i, reg;
+	int ret = 0;
+
+	if (check_only)
+		return 0;
+

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 782

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +10 -0
+	ma35_nand_target_enable(nand);
+	reg = readl(nand->regs + MA35_NFI_REG_NANDINTSTS);
+	reg |= INT_RB0;
+	writel(reg, nand->regs + MA35_NFI_REG_NANDINTSTS);
+
+	for (i = 0; i < op->ninstrs; i++) {
+		ret = ma35_nfc_exec_instr(chip, &op->instrs[i]);
+		if (ret)
+			break;
+	}

The way ma35_nand_target_[en|dis]able() is called looks inconsistent.

Quoted 7 lines

This function calls ma35_nand_target_enable(), so I would expect that the corresponding ma35_nand_target_disable() should be called here as well.

ma35_nand_do_read() is called from here which has its own call to ma35_nand_target_enable(), but it doesn't call ma35_nand_target_disable() from all of its return pathes.

Okay. I will check and fix it.

Quoted 7 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 830

Quoted 6 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +6 -0
+
+	ret = devm_request_irq(&pdev->dev, nand->irq, ma35_nand_irq,
+				  IRQF_TRIGGER_HIGH, "ma35d1-nand", nand);
+	if (ret) {
+		dev_err(&pdev->dev, "failed to request NAND irq\n");
+		clk_disable_unprepare(nand->clk);

You used devm_clk_get_enabled(), so this will be done automatically.

I will remove it.

Quoted 23 lines
Quoted 23 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +23 -0
+		return -ENXIO;
+	}
+
+	nand->chip.controller = &nand->controller;
+	platform_set_drvdata(pdev, nand);
+
+	chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA;
+
+	/* set default mode in case dt entry is missing */
+	chip->ecc.engine_type = NAND_ECC_ENGINE_TYPE_ON_HOST;
+
+	chip->ecc.write_page = ma35_nand_write_page_hwecc;
+	chip->ecc.read_page  = ma35_nand_read_page_hwecc;
+	chip->ecc.read_oob   = ma35_nand_read_oob_hwecc;
+
+	mtd = nand_to_mtd(chip);
+	mtd->priv = chip;
+	mtd->owner = THIS_MODULE;
+	mtd->dev.parent = &pdev->dev;
+
+	writel(NAND_EN, nand->regs + MA35_NFI_REG_GCTL);
+
+	ret = nand_scan(chip, 1);
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 860

Quoted 7 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +7 -0
+	if (ret)
+		return ret;
+
+	ret = mtd_device_register(mtd, NULL, 0);
+	if (ret) {
+		nand_cleanup(chip);
+		devm_kfree(&pdev->dev, nand);

Unnecessary free. Drop it.

I will remove it.

Quoted 6 lines
Quoted 6 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +6 -0
+		return ret;
+	}
+
+	return ret;
+}
+
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c line 872

Quoted 6 lines
drivers/mtd/nand/raw/nuvoton_ma35d1_nand.c new file +6 -0
+static void ma35_nand_remove(struct platform_device *pdev)
+{
+	struct ma35_nand_info *nand = platform_get_drvdata(pdev);
+	int ret;
+
+	devm_free_irq(&pdev->dev, nand->irq, nand);

devm_ is a mechanism to let resources be freed automatically. There's normally no need to do this manually.

Okay. I will check and remove it.

Quoted 1 line

Sascha

Best regards,

Hui-Ping Chen