Skip to contentSkip to conversation

Add support for nuvoton ma35 nand controller

PATCH Superseded Hui-Ping Chen 3 participants 6 messages Oct 23, 2024 – Nov 13, 2024

Subsystems (4)

v8 of 11 Superseded Latest is v11 →

lore
Share & tools
lore
#0 lore raw

v8 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.

v8:
- Update ma35d1 mtd nand driver
- Rename to nuvoton-ma35d1-nand-controller.c.
- Use switch case instead of if else.
- Move some parameters to be set during initialization.
- Fix the ecc.read_page return value issue.
- Add enable/disable ECC engine before and after reading/writing the page.
- Return IRQ_NONE if (isr & INT_DMA) == 0.
- Move the HW ECC related settings to ON_HOST.
- Move hw_init() to probe.

v7:
- Update nuvoton,ma35d1-nand.yaml
- Remove required 'nand-ecc-step-size' and 'nand-ecc-strength'.
- Add 'reg' for chip select.
- Update ma35d1 mtd nand driver
- Update space and comments style.
- Add chip select setting from DT.
- Add switch case which supports various ECC configurations.
- Set reset before NAND controller enable.

v6:
- Update ma35d1 mtd nand driver
- Remove extra blank lines and add comments.

v5:
- Update ma35d1 mtd nand driver
- Remove unnecessary definitions and comments.
- Modified DMA API call sequence.
- Move the ECC check out of the interrupt handler. Check it after reading a page.

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     |  95 ++
 drivers/mtd/nand/raw/Kconfig                  |   8 +
 drivers/mtd/nand/raw/Makefile                 |   1 +
 .../nand/raw/nuvoton-ma35d1-nand-controller.c | 932 ++++++++++++++++++
 4 files changed, 1036 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml
 create mode 100644 drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c
Signature · 1 line
2.25.1
#1 lore raw

PATCH v8 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     | 95 +++++++++++++++++++
 1 file changed, 95 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml
Documentation/devicetree/bindings/mtd/nuvoton,ma35d1-nand.yaml new file +95 -0
new file mode 100644
index 000000000000..0b651450a8f1
@@ -0,0 +1,95 @@
+# 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:
+      reg:
+        minimum: 0
+        maximum: 1
+
+      nand-ecc-step-size:
+        enum: [512, 1024]
+
+      nand-ecc-strength:
+        enum: [8, 12, 24]
+
+    required:
+      - reg
+
+    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
#3 lore raw

PATCH v8 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 +
 .../nand/raw/nuvoton-ma35d1-nand-controller.c | 932 ++++++++++++++++++
 3 files changed, 941 insertions(+)
 create mode 100644 drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c
drivers/mtd/nand/raw/Kconfig +8 -0
index d0aaccf72d78..e3677bcaf035 100644
@@ -454,6 +454,14 @@ config MTD_NAND_TS72XX
 	help
 	  Enables support for NAND controller on ts72xx SBCs.
 
+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 d0b0e6b83568..99e79c448847 100644
@@ -58,6 +58,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-controller.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-controller.c new file +932 -0

932 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#4 lore raw

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

In reply to Hui-Ping Chen

Hello,

Quoted 5 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +5 -0
+static int ma35_nand_attach_chip(struct nand_chip *chip)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct ma35_nand_chip *nvtnand = to_ma35_nand(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 650

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +10 -0
+	struct device *dev = mtd->dev.parent;
+	u32 reg;
+
+	if (chip->options & NAND_BUSWIDTH_16) {
+		dev_err(dev, "16 bits bus width not supported");
+		return -EINVAL;
+	}
+
+	nvtnand->nchunks = mtd->writesize / chip->ecc.steps;
+	nvtnand->nchunks = (nvtnand->nchunks < 4) ? 1 : nvtnand->nchunks / 4;

This second division looks broken. Also, you probably don't want to do that outside of the ON_HOST situation. Finally, you should probably update chip->ecc.steps and chip->ecc.size to your final choice.

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 662

Quoted 12 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +12 -0
+
+	reg = readl(nand->regs + MA35_NFI_REG_NANDCTL) & (~PSIZE_MASK);
+	if (mtd->writesize == 2048)
+		writel(reg | PSIZE_2K, nand->regs + MA35_NFI_REG_NANDCTL);
+	else if (mtd->writesize == 4096)
+		writel(reg | PSIZE_4K, nand->regs + MA35_NFI_REG_NANDCTL);
+	else if (mtd->writesize == 8192)
+		writel(reg | PSIZE_8K, nand->regs + MA35_NFI_REG_NANDCTL);
+
+	switch (chip->ecc.engine_type) {
+	case NAND_ECC_ENGINE_TYPE_ON_HOST:
+		chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA;

What is the reason for refusing subpage writes? This is not something you can do later, so unless there is a good reason, please do not set this flag.

Quoted 15 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +15 -0
+		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;
+		return ma35_nand_hwecc_init(chip, nand);
+	case NAND_ECC_ENGINE_TYPE_NONE:
+	case NAND_ECC_ENGINE_TYPE_SOFT:
+	case NAND_ECC_ENGINE_TYPE_ON_DIE:
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+

...

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 755

Quoted 12 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +12 -0
+static int ma35_nand_chip_init(struct device *dev, struct ma35_nand_info *nand,
+				 struct device_node *np)
+{
+	struct ma35_nand_chip *nvtnand;
+	struct nand_chip *chip;
+	struct mtd_info *mtd;
+	int nsels;
+	u32 tmp;
+	int ret;
+	int i;
+
+	if (!of_get_property(np, "reg", &nsels))

Please convert to device_property_ helpers. And remove the of include once you no longer need it.

Quoted 34 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +34 -0
+		return -ENODEV;
+
+	nsels /= sizeof(u32);
+	if (!nsels || nsels > MA35_MAX_NSELS) {
+		dev_err(dev, "invalid reg property size %d\n", nsels);
+		return -EINVAL;
+	}
+
+	nvtnand = devm_kzalloc(dev, struct_size(nvtnand, sels, nsels),
+			      GFP_KERNEL);
+	if (!nvtnand)
+		return -ENOMEM;
+
+	nvtnand->nsels = nsels;
+	for (i = 0; i < nsels; i++) {
+		ret = of_property_read_u32_index(np, "reg", i, &tmp);
+		if (ret) {
+			dev_err(dev, "reg property failure : %d\n", ret);
+			return ret;
+		}
+
+		if (tmp >= MA35_MAX_NSELS) {
+			dev_err(dev, "invalid CS: %u\n", tmp);
+			return -EINVAL;
+		}
+
+		if (test_and_set_bit(tmp, &nand->assigned_cs)) {
+			dev_err(dev, "CS %u already assigned\n", tmp);
+			return -EINVAL;
+		}
+
+		nvtnand->sels[i] = tmp;
+	}
+

...

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 807

Quoted 4 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +4 -0
+
+	ret = mtd_device_register(mtd, NULL, 0);
+	if (ret) {
+		dev_err(dev, "MTD parse partition error\n");

probably useless error message?

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 815

Quoted 8 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +8 -0
+		nand_cleanup(chip);
+		return ret;
+	}
+
+	list_add_tail(&nvtnand->node, &nand->chips);
+
+	return 0;
+}

I believe next iteration should be the one, I'm rather happy with the overall look.

Thanks,
Miquèl

#5 lore raw

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

Dear Miquel,

Thank you for your reply.

Quoted 12 lines · Miquel Raynal

Hello,

Quoted 10 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +10 -0
+static int ma35_nand_attach_chip(struct nand_chip *chip)
+{
+	struct ma35_nand_info *nand = nand_get_controller_data(chip);
+	struct ma35_nand_chip *nvtnand = to_ma35_nand(chip);
+	struct mtd_info *mtd = nand_to_mtd(chip);
+	struct device *dev = mtd->dev.parent;
+	u32 reg;
+
+	if (chip->options & NAND_BUSWIDTH_16) {
+		dev_err(dev, "16 bits bus width not supported");
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 650

Quoted 5 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +5 -0
+		return -EINVAL;
+	}
+
+	nvtnand->nchunks = mtd->writesize / chip->ecc.steps;
+	nvtnand->nchunks = (nvtnand->nchunks < 4) ? 1 : nvtnand->nchunks / 4;

This second division looks broken. Also, you probably don't want to do that outside of the ON_HOST situation. Finally, you should probably update chip->ecc.steps and chip->ecc.size to your final choice.

Regarding these two lines, I will remove them and instead set them in ma35_nand_hwecc_init().

However, since the nchunks is actually used to get the ECC status register count,

I will rename nchunks to eccstatus and perform the calculation using ecc.steps.

Quoted 7 lines
Quoted 7 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +7 -0
+
+	reg = readl(nand->regs + MA35_NFI_REG_NANDCTL) & (~PSIZE_MASK);
+	if (mtd->writesize == 2048)
+		writel(reg | PSIZE_2K, nand->regs + MA35_NFI_REG_NANDCTL);
+	else if (mtd->writesize == 4096)
+		writel(reg | PSIZE_4K, nand->regs + MA35_NFI_REG_NANDCTL);
+	else if (mtd->writesize == 8192)
Quoted 8 lines

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 662

Quoted 5 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +5 -0
+		writel(reg | PSIZE_8K, nand->regs + MA35_NFI_REG_NANDCTL);
+
+	switch (chip->ecc.engine_type) {
+	case NAND_ECC_ENGINE_TYPE_ON_HOST:
+		chip->options |= NAND_NO_SUBPAGE_WRITE | NAND_USES_DMA;

What is the reason for refusing subpage writes? This is not something you can do later, so unless there is a good reason, please do not set this flag.

I will remove the NAND_NO_SUBPAGE_WRITE flag.

Quoted 17 lines
Quoted 15 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +15 -0
+		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;
+		return ma35_nand_hwecc_init(chip, nand);
+	case NAND_ECC_ENGINE_TYPE_NONE:
+	case NAND_ECC_ENGINE_TYPE_SOFT:
+	case NAND_ECC_ENGINE_TYPE_ON_DIE:
+		break;
+	default:
+		return -EINVAL;
+	}
+
+	return 0;
+}
+

...

Quoted 14 lines

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 755

Quoted 12 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +12 -0
+static int ma35_nand_chip_init(struct device *dev, struct ma35_nand_info *nand,
+				 struct device_node *np)
+{
+	struct ma35_nand_chip *nvtnand;
+	struct nand_chip *chip;
+	struct mtd_info *mtd;
+	int nsels;
+	u32 tmp;
+	int ret;
+	int i;
+
+	if (!of_get_property(np, "reg", &nsels))

Please convert to device_property_ helpers. And remove the of include once you no longer need it.

I will use device_property_read_u32 instead.

Quoted 36 lines
Quoted 34 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +34 -0
+		return -ENODEV;
+
+	nsels /= sizeof(u32);
+	if (!nsels || nsels > MA35_MAX_NSELS) {
+		dev_err(dev, "invalid reg property size %d\n", nsels);
+		return -EINVAL;
+	}
+
+	nvtnand = devm_kzalloc(dev, struct_size(nvtnand, sels, nsels),
+			      GFP_KERNEL);
+	if (!nvtnand)
+		return -ENOMEM;
+
+	nvtnand->nsels = nsels;
+	for (i = 0; i < nsels; i++) {
+		ret = of_property_read_u32_index(np, "reg", i, &tmp);
+		if (ret) {
+			dev_err(dev, "reg property failure : %d\n", ret);
+			return ret;
+		}
+
+		if (tmp >= MA35_MAX_NSELS) {
+			dev_err(dev, "invalid CS: %u\n", tmp);
+			return -EINVAL;
+		}
+
+		if (test_and_set_bit(tmp, &nand->assigned_cs)) {
+			dev_err(dev, "CS %u already assigned\n", tmp);
+			return -EINVAL;
+		}
+
+		nvtnand->sels[i] = tmp;
+	}
+

...

Quoted 5 lines

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 807

Quoted 4 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +4 -0
+
+	ret = mtd_device_register(mtd, NULL, 0);
+	if (ret) {
+		dev_err(dev, "MTD parse partition error\n");

probably useless error message?

I will remove this error message.

Quoted 10 lines

drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c line 815

Quoted 8 lines
drivers/mtd/nand/raw/nuvoton-ma35d1-nand-controller.c new file +8 -0
+		nand_cleanup(chip);
+		return ret;
+	}
+
+	list_add_tail(&nvtnand->node, &nand->chips);
+
+	return 0;
+}

I believe next iteration should be the one, I'm rather happy with the overall look.

Hope so, thank you!

Quoted 2 lines

Thanks,
Miquèl

Best regards,

Hui-Ping Chen