* [PATCH v8 0/4] Add Cortina Systems CS7542 NAND controller support
@ 2026-12-04 14:03 Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 1/4] mtd: rawnand: Honor controller raw page callbacks in ECC_NONE Fil Dunsky via B4 Relay
` (3 more replies)
0 siblings, 4 replies; 10+ messages in thread
From: Fil Dunsky via B4 Relay @ 2026-12-04 14:03 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Philipp Zabel
Cc: linux-mtd, devicetree, linux-kernel
Add a programmed-I/O driver for the Cortina CS7542 NAND controller.
The controller accepts complete command, address and data phases. Use
the NAND core operation parser to validate and issue those phases.
The binding describes both chip selects, both bus widths and the
Hamming and BCH engines. The driver supports one 8-bit target,
on-host Hamming with the existing Almond+ layout, software ECC and
raw data. It initializes the reset and NAND timing at probe.
On Almond+, the reviewed driver identified the 512 MiB Spansion
S34ML04G1 and read matching U-Boot and kernel samples from RAM. It
returned to the installed OpenWrt with all partitions read-only.
I also used one isolated spare physical eraseblock per ECC mode on
Almond+. Hardware Hamming, software Hamming and ECC_NONE each passed
page writes and reads, erase verification, auto OOB and raw data/OOB.
The Hamming modes corrected an injected single-bit change. ECC_NONE
returned the altered byte without correction. Each test restored the
original UBI erase-counter header and left the rest of the block
erased, then returned to OpenWrt with the five UBI volumes intact.
---
Changes in v8:
- Use the NAND operation parser and transfer data directly to and from
the caller's buffers.
- Reject unsupported transfers before programming the controller.
- Reset the controller and program its default asynchronous timing.
- Keep controller-supplied raw page callbacks in the NAND core for
ECC_NONE. Disable on-host ECC for software ECC and NONE. Keep the geometry
checks and the full-page write constraint local to on-host Hamming.
- Use the standard MTD name and remove informational probe messages.
- Link to v7: https://patch.msgid.link/20261202-b4-cs7542-nand-v7-0-81d3ed341c77@gmail.com
Changes in v7:
- Keep the 100 ms PIO transfer budget independent of NAND WAITRDY.
- Use time-based tight polling for generic PIO completion.
- Reproduce the v6 regression and check full parameter and page reads,
readiness errors and a stuck request in mocked MMIO; compile the
affected ARM object for Linux and OpenWrt.
- Share the 100 ms transfer budget across Hamming page data and OOB.
Keep separate ECC and ready/busy waits, and the per-word stall limit.
- Treat a cleared request bit on the final word as completion. Cover
expiry at the boundary, a delayed observation of completion and a
final request still pending at the deadline in the MMIO model.
- Before the final-word change, the driver in an OpenWrt RAM kernel
on Almond+ matched ECC data and raw/generic OOB for 128 pages across all four
banks, with all MTD partitions read-only. The watchdog returned to
OpenWrt. The Hamming write and timeout faults were tested in MMIO
mocks with GCC and Clang.
- Link to v6: https://patch.msgid.link/20261201-b4-cs7542-nand-v6-0-e6b0d712745b@gmail.com
Changes in v6:
- Bound each PIO transfer with one deadline and preserve the cached
read position when rejecting a change-column request.
- Link to v5: https://patch.msgid.link/20261127-b4-cs7542-nand-v5-0-45b1a92bd13d@gmail.com
Changes in v5:
- Select the available NAND child, including CS1 when CS0 is disabled.
- Reject ECC_NONE; retain software ECC and monolithic raw transfers.
- Check the changed attach and probe paths with actual-source tests.
- Link to v4: https://patch.msgid.link/20261126-b4-cs7542-nand-v4-0-38e488f6f8b1@gmail.com
Changes in v4:
- Address Miquel Raynal's binding and driver review on v2.
- Describe controller capabilities rather than only the initial driver.
- Add the AXI clock, flash IRQ, optional reset and data aperture binding.
- Separate supports_op() and remove the NAND opcode whitelist.
- Use core page helpers for hardware ECC; allow software ECC/raw transfers.
- Support either chip select and reject multiple connected targets explicitly.
- Preserve bus timing through clock rate exclusivity; do not pulse flash reset.
- Clarify the vendor EXTADDR convention; retain the existing Hamming layout.
- Link to v3: https://patch.msgid.link/20261125-b4-cs7542-nand-v3-0-0b6d141c102e@gmail.com
---
Fil Dunsky (4):
mtd: rawnand: Honor controller raw page callbacks in ECC_NONE
dt-bindings: mtd: Add Cortina CS7542 NAND controller
mtd: rawnand: Add Cortina CS7542 controller support
MAINTAINERS: Add Cortina CS7542 NAND controller entry
.../bindings/mtd/cortina,cs7542-nand.yaml | 89 +++
MAINTAINERS | 7 +
drivers/mtd/nand/raw/Kconfig | 11 +
drivers/mtd/nand/raw/Makefile | 1 +
drivers/mtd/nand/raw/cs752x_nand.c | 756 +++++++++++++++++++++
drivers/mtd/nand/raw/nand_base.c | 10 +-
6 files changed, 870 insertions(+), 4 deletions(-)
---
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
change-id: 20261123-b4-cs7542-nand-87997e300079
Best regards,
--
Fil Dunsky <filipp.dunsky@gmail.com>
^ permalink raw reply [flat|nested] 10+ messages in thread
* [PATCH v8 1/4] mtd: rawnand: Honor controller raw page callbacks in ECC_NONE
2026-12-04 14:03 [PATCH v8 0/4] Add Cortina Systems CS7542 NAND controller support Fil Dunsky via B4 Relay
@ 2026-12-04 14:03 ` Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller Fil Dunsky via B4 Relay
` (2 subsequent siblings)
3 siblings, 0 replies; 10+ messages in thread
From: Fil Dunsky via B4 Relay @ 2026-12-04 14:03 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Philipp Zabel
Cc: linux-mtd, devicetree, linux-kernel
From: Fil Dunsky <filipp.dunsky@gmail.com>
The ECC_NONE path installs the generic raw page helpers unconditionally.
These helpers split main and OOB accesses and split the program
transaction. Controllers which require a single transaction provide
monolithic raw callbacks, but the ECC_NONE path discards them.
Keep the controller callbacks when supplied and install the normal
generic fallbacks otherwise. Use those same raw callbacks for page I/O
when ECC is disabled. Preserve the existing OOB helpers and geometry.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
drivers/mtd/nand/raw/nand_base.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/drivers/mtd/nand/raw/nand_base.c b/drivers/mtd/nand/raw/nand_base.c
index fe8c6dcc9..200d507b6 100644
--- a/drivers/mtd/nand/raw/nand_base.c
+++ b/drivers/mtd/nand/raw/nand_base.c
@@ -6301,11 +6301,13 @@ static int nand_scan_tail(struct nand_chip *chip)
case NAND_ECC_ENGINE_TYPE_NONE:
pr_warn("NAND_ECC_ENGINE_TYPE_NONE selected by board driver. This is not recommended!\n");
- ecc->read_page = nand_read_page_raw;
- ecc->write_page = nand_write_page_raw;
+ if (!ecc->read_page_raw)
+ ecc->read_page_raw = nand_read_page_raw;
+ if (!ecc->write_page_raw)
+ ecc->write_page_raw = nand_write_page_raw;
+ ecc->read_page = ecc->read_page_raw;
+ ecc->write_page = ecc->write_page_raw;
ecc->read_oob = nand_read_oob_std;
- ecc->read_page_raw = nand_read_page_raw;
- ecc->write_page_raw = nand_write_page_raw;
ecc->write_oob = nand_write_oob_std;
ecc->size = mtd->writesize;
ecc->bytes = 0;
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller
2026-12-04 14:03 [PATCH v8 0/4] Add Cortina Systems CS7542 NAND controller support Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 1/4] mtd: rawnand: Honor controller raw page callbacks in ECC_NONE Fil Dunsky via B4 Relay
@ 2026-12-04 14:03 ` Fil Dunsky via B4 Relay
2026-12-04 14:12 ` Philipp Zabel
2026-12-04 14:38 ` sashiko-bot
2026-12-04 14:03 ` [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 4/4] MAINTAINERS: Add Cortina CS7542 NAND controller entry Fil Dunsky via B4 Relay
3 siblings, 2 replies; 10+ messages in thread
From: Fil Dunsky via B4 Relay @ 2026-12-04 14:03 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Philipp Zabel
Cc: linux-mtd, devicetree, linux-kernel
From: Fil Dunsky <filipp.dunsky@gmail.com>
Describe the flash controller registers, optional banked data aperture,
AXI bus clock, flash interrupt and shared flash reset. The controller
supports two chip selects, 8/16-bit devices, Hamming with 256-byte or
512-byte steps, and BCH.
Inherit the standard NAND chip ECC properties. The example selects the
256-byte Hamming layout used on Almond+.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
---
.../bindings/mtd/cortina,cs7542-nand.yaml | 89 ++++++++++++++++++++++
1 file changed, 89 insertions(+)
diff --git a/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml b/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml
new file mode 100644
index 000000000..3bc5b1411
--- /dev/null
+++ b/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml
@@ -0,0 +1,89 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/mtd/cortina,cs7542-nand.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Cortina Systems CS7542 NAND controller
+
+maintainers:
+ - Fil Dunsky <filipp.dunsky@gmail.com>
+
+description:
+ The flash controller has two NAND chip selects and supports 8-bit and
+ 16-bit devices, programmed I/O, hardware Hamming ECC with 256-byte or
+ 512-byte steps, and hardware BCH ECC. It is on the AXI peripheral bus.
+ The data aperture selects a bank through the controller registers.
+ The flash controller also has access to the shared SSP DMA engine;
+ the programmed-I/O interface does not require that DMA resource.
+
+allOf:
+ - $ref: nand-controller.yaml#
+
+properties:
+ compatible:
+ const: cortina,cs7542-nand
+
+ reg:
+ minItems: 1
+ items:
+ - description: Flash controller registers
+ - description: Banked NAND data aperture
+
+ reg-names:
+ minItems: 1
+ items:
+ - const: regs
+ - const: data
+
+ clocks:
+ maxItems: 1
+ description: AXI bus clock
+
+ interrupts:
+ maxItems: 1
+ description: Flash controller interrupt
+
+ resets:
+ maxItems: 1
+ description: Shared flash controller reset
+
+patternProperties:
+ "^nand@[01]$":
+ type: object
+ $ref: raw-nand-chip.yaml#
+ properties:
+ reg:
+ minimum: 0
+ maximum: 1
+ unevaluatedProperties: false
+
+required:
+ - compatible
+ - reg
+ - clocks
+ - interrupts
+
+unevaluatedProperties: false
+
+examples:
+ - |
+ #include <dt-bindings/interrupt-controller/arm-gic.h>
+
+ nand-controller@f0050000 {
+ compatible = "cortina,cs7542-nand";
+ reg = <0xf0050000 0x300>, <0xe0000000 0x08000000>;
+ reg-names = "regs", "data";
+ clocks = <&axi_clk>;
+ interrupts = <GIC_SPI 18 IRQ_TYPE_LEVEL_HIGH>;
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ nand@0 {
+ reg = <0>;
+ nand-ecc-mode = "hw";
+ nand-ecc-algo = "hamming";
+ nand-ecc-step-size = <256>;
+ nand-ecc-strength = <1>;
+ };
+ };
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support
2026-12-04 14:03 [PATCH v8 0/4] Add Cortina Systems CS7542 NAND controller support Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 1/4] mtd: rawnand: Honor controller raw page callbacks in ECC_NONE Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller Fil Dunsky via B4 Relay
@ 2026-12-04 14:03 ` Fil Dunsky via B4 Relay
2026-12-04 14:12 ` Philipp Zabel
2026-12-04 15:05 ` sashiko-bot
2026-12-04 14:03 ` [PATCH v8 4/4] MAINTAINERS: Add Cortina CS7542 NAND controller entry Fil Dunsky via B4 Relay
3 siblings, 2 replies; 10+ messages in thread
From: Fil Dunsky via B4 Relay @ 2026-12-04 14:03 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Philipp Zabel
Cc: linux-mtd, devicetree, linux-kernel
From: Fil Dunsky <filipp.dunsky@gmail.com>
Add programmed-I/O support for the CS7542 NAND controller. Use the
NAND operation parser to validate complete instructions and populate
the native command, address and data registers. Transfer the requested
bytes directly between the controller and the NAND core's buffer.
Reset the controller at probe and program its default asynchronous bus
timing. Keep the AXI clock rate fixed while the timing is in use.
Poll the controller and the NAND R/B pin with distinct timeouts.
Keep the existing 256-byte Hamming step and OOB layout used by Almond+.
Disable the on-host Hamming and BCH engines for software ECC and for
raw NAND operations. The initial driver supports one 8-bit target on
either of the two hardware chip selects.
The flash controller is fixed. An open MTD device pins its module, but
sysfs unbind bypasses that reference and can release the live
controller state. Suppress manual bind/unbind until the MTD devices
can be detached safely.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
drivers/mtd/nand/raw/Kconfig | 11 +
drivers/mtd/nand/raw/Makefile | 1 +
drivers/mtd/nand/raw/cs752x_nand.c | 756 +++++++++++++++++++++++++++++++++++++
3 files changed, 768 insertions(+)
diff --git a/drivers/mtd/nand/raw/Kconfig b/drivers/mtd/nand/raw/Kconfig
index 1f4053e53..d4796a7f1 100644
--- a/drivers/mtd/nand/raw/Kconfig
+++ b/drivers/mtd/nand/raw/Kconfig
@@ -12,6 +12,17 @@ if MTD_RAW_NAND
comment "Raw/parallel NAND flash controllers"
+config MTD_NAND_CS752X
+ tristate "Cortina Systems CS7542 NAND controller"
+ depends on OF && HAS_IOMEM
+ help
+ Support the CS7542 controller with programmed I/O, software ECC, and
+ hardware Hamming ECC compatible with existing Almond+ firmware.
+ Transfers use bounded polling and the native command sequencer.
+
+ To compile this driver as a module, choose M here: the module
+ will be called cs752x_nand.
+
config MTD_NAND_DENALI
tristate
diff --git a/drivers/mtd/nand/raw/Makefile b/drivers/mtd/nand/raw/Makefile
index 619760138..3562f8940 100644
--- a/drivers/mtd/nand/raw/Makefile
+++ b/drivers/mtd/nand/raw/Makefile
@@ -71,3 +71,4 @@ nand-objs += nand_micron.o
nand-objs += nand_sandisk.o
nand-objs += nand_samsung.o
nand-objs += nand_toshiba.o
+obj-$(CONFIG_MTD_NAND_CS752X) += cs752x_nand.o
diff --git a/drivers/mtd/nand/raw/cs752x_nand.c b/drivers/mtd/nand/raw/cs752x_nand.c
new file mode 100644
index 000000000..690067b9b
--- /dev/null
+++ b/drivers/mtd/nand/raw/cs752x_nand.c
@@ -0,0 +1,756 @@
+// SPDX-License-Identifier: GPL-2.0
+#include <linux/kernel.h>
+#include <linux/slab.h>
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/platform_device.h>
+#include <linux/reset.h>
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/rawnand.h>
+#include <linux/mtd/partitions.h>
+#include <linux/bitops.h>
+#include <linux/err.h>
+#include <linux/unaligned.h>
+#include <linux/io.h>
+#include <linux/iopoll.h>
+#include <linux/clk.h>
+#include <linux/delay.h>
+#include <linux/jiffies.h>
+#include <linux/ktime.h>
+#include <linux/of.h>
+
+#define CS75XX_PIO_TIMEOUT_MS 100
+#define CS75XX_NAND_RESET_TIMING 0x00041703
+
+#define FLASH_STATUS 0x08
+#define FLASH_NF_ACCESS 0x28
+#define FLASH_NF_COUNT 0x2c
+#define FLASH_NF_COMMAND 0x30
+#define FLASH_NF_ADDRESS_1 0x34
+#define FLASH_NF_ADDRESS_2 0x38
+#define FLASH_NF_DATA 0x3c
+#define FLASH_NF_TIMING 0x40
+#define FLASH_NF_ECC_STATUS 0x44
+#define FLASH_NF_ECC_CONTROL 0x48
+#define FLASH_NF_ECC_OOB 0x4c
+#define FLASH_NF_ECC_GEN0 0x50
+#define FLASH_FLASH_ACCESS_START 0xa4
+#define FLASH_NF_ECC_RESET 0xa8
+#define FLASH_FLASH_INTERRUPT 0xac
+#define FLASH_NF_BCH_CONTROL 0xd0
+
+#define NF_ACCESS_EXTADDR(x) ((x) & 0xff)
+#define NF_ACCESS_WIDTH8 (0x0 << 10)
+#define NF_ACCESS_WIDTH32 (0x2 << 10)
+#define NF_ACCESS_CE_ALT BIT(15)
+
+#define NF_CNT_CMD(x) ((x) & 0x3)
+#define NF_CNT_ADDR(x) (((x) & 0x7) << 4)
+#define NF_CNT_DATA(x) (((x) & 0x3fff) << 8)
+#define NF_CNT_OOB(x) (((x) & 0x3ffU) << 22)
+#define NCNT_EMPTY_OOB 0x3ff
+#define NCNT_EMPTY_DATA 0x3fff
+#define NCNT_EMPTY_ADDR 0x7
+#define NCNT_EMPTY_CMD 0x3
+
+#define FLASH_START_REGREQ BIT(0)
+#define FLASH_START_CMD(x) (((x) & 0x3) << 12)
+#define FLASH_RD 0x2
+#define FLASH_WT 0x3
+
+#define ECC_RESET_ECC_CLR BIT(0)
+#define ECC_RESET_FIFO_CLR BIT(1)
+#define ECC_RESET_NF_RESET BIT(2)
+
+#define FLASH_INT_REGIRQ BIT(0)
+
+#define ECC_CTL_CODESEL(x) (((x) & 0xf) << 4)
+#define ECC_CTL_EN BIT(8)
+
+#define ECC_STS_STATUS(v) ((v) & 0x3)
+#define ECC_STS_ERRBIT(v) (((v) >> 3) & 0xf)
+#define ECC_STS_ERRBYTE(v) (((v) >> 7) & 0x1ff)
+#define ECC_STS_DONE BIT(31)
+#define ECC_NO_ERR 0x0
+#define ECC_1BIT_DATA_ERR 0x1
+#define ECC_1BIT_ECC_ERR 0x2
+#define ECC_UNCORRECTABLE 0x3
+
+#define FLASH_STATUS_NSTATE_MASK (0x0f << 8)
+
+struct cs752x_nand_host {
+ struct nand_controller controller;
+ struct nand_chip chip;
+ struct device *dev;
+
+ void __iomem *iobase_fl;
+ u8 cs;
+ bool use_hwecc;
+
+ u8 *ecc_calc;
+ u8 *ecc_code;
+};
+
+/* The controller sequences complete transactions; do not split CMD/ADDR. */
+struct cs752x_nand_op {
+ u32 command;
+ u64 address;
+ unsigned int ncmds;
+ unsigned int naddrs;
+ const struct nand_op_instr *data;
+ unsigned int waitrdy_timeout_ms;
+ unsigned int delay_ns;
+ bool wait_ready;
+};
+
+static inline struct cs752x_nand_host *to_host(struct nand_chip *chip)
+{
+ return container_of(chip, struct cs752x_nand_host, chip);
+}
+
+static inline u32 fl_readl(struct cs752x_nand_host *h, u32 reg)
+{
+ return readl(h->iobase_fl + reg);
+}
+
+static inline void fl_writel(struct cs752x_nand_host *h, u32 reg, u32 val)
+{
+ writel(val, h->iobase_fl + reg);
+}
+
+static int check_flash_ctrl_status(struct cs752x_nand_host *h)
+{
+ u32 val;
+ int ret;
+
+ ret = readl_poll_timeout(h->iobase_fl + FLASH_STATUS, val,
+ !(val & FLASH_STATUS_NSTATE_MASK), 1, 1000000);
+ if (ret)
+ dev_err(h->dev, "FLASH_STATUS busy: 0x%x\n", val);
+ return ret;
+}
+
+static int wait_reg(struct cs752x_nand_host *h, u32 reg, u32 bit, const char *what)
+{
+ u32 val;
+ int ret;
+
+ ret = readl_poll_timeout(h->iobase_fl + reg, val, val & bit, 1, 2000000);
+ if (ret)
+ dev_err(h->dev, "%s timeout\n", what);
+ return ret;
+}
+
+static void reset_ecc_bch_registers(struct cs752x_nand_host *h)
+{
+ fl_writel(h, FLASH_NF_ECC_RESET, ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR);
+ fl_writel(h, FLASH_FLASH_INTERRUPT, FLASH_INT_REGIRQ);
+ fl_writel(h, FLASH_NF_ECC_RESET, ECC_RESET_ECC_CLR);
+ fl_writel(h, FLASH_NF_BCH_CONTROL, 0);
+ fl_writel(h, FLASH_NF_ECC_CONTROL, 0);
+}
+
+static int cs752x_page_op_begin(struct cs752x_nand_host *h)
+{
+ int ret = check_flash_ctrl_status(h);
+
+ if (ret) {
+ fl_writel(h, FLASH_NF_ECC_RESET,
+ ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
+ return ret;
+ }
+ reset_ecc_bch_registers(h);
+ return 0;
+}
+
+static int cs752x_nand_words(struct cs752x_nand_host *h, u8 *buf,
+ unsigned int len, bool write, u64 deadline)
+{
+ u32 value;
+ unsigned int i;
+ int ret;
+
+ if (len % sizeof(u32))
+ return -EINVAL;
+ for (i = 0; i < len; i += sizeof(u32)) {
+ if (ktime_get_ns() >= deadline)
+ return -ETIMEDOUT;
+ if (write)
+ fl_writel(h, FLASH_NF_DATA, get_unaligned_le32(buf + i));
+ fl_writel(h, FLASH_FLASH_ACCESS_START,
+ FLASH_START_REGREQ | FLASH_START_CMD(write ? FLASH_WT : FLASH_RD));
+ ret = readl_poll_timeout_atomic(h->iobase_fl + FLASH_FLASH_ACCESS_START,
+ value, !(value & FLASH_START_REGREQ) ||
+ ktime_get_ns() >= deadline, 1, 10000);
+ if (ret || (value & FLASH_START_REGREQ))
+ return -ETIMEDOUT;
+ if (!write)
+ put_unaligned_le32(fl_readl(h, FLASH_NF_DATA), buf + i);
+ }
+ return 0;
+}
+
+static int fill_hamming_oob_data(struct cs752x_nand_host *h)
+{
+ struct nand_chip *chip = &h->chip;
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ u8 *ecc_calc = h->ecc_calc;
+ int eccsteps = chip->ecc.steps, eccbytes = chip->ecc.bytes;
+ int i, j, ret;
+
+ ret = wait_reg(h, FLASH_NF_ECC_STATUS, ECC_STS_DONE, "ecc gen");
+ if (ret)
+ return ret;
+ fl_writel(h, FLASH_NF_ECC_CONTROL,
+ fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_EN);
+
+ for (i = 0, j = 0; eccsteps; eccsteps--, i++, j += eccbytes) {
+ u32 v = fl_readl(h, FLASH_NF_ECC_GEN0 + 4 * i);
+
+ ecc_calc[j] = v & 0xff;
+ ecc_calc[j + 1] = (v >> 8) & 0xff;
+ ecc_calc[j + 2] = (v >> 16) & 0xff;
+ }
+ return mtd_ooblayout_set_eccbytes(mtd, ecc_calc, chip->oob_poi, 0,
+ chip->ecc.total);
+}
+
+static int hamming_correct(struct cs752x_nand_host *h, u8 *buf, int page)
+{
+ struct nand_chip *chip = &h->chip;
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ u8 *ecc_code = h->ecc_code;
+ int eccsteps = chip->ecc.steps, eccbytes = chip->ecc.bytes;
+ int eccsize = chip->ecc.size;
+ int i, max_bitflips = 0, ret;
+ u8 *p = buf;
+
+ ret = wait_reg(h, FLASH_NF_ECC_STATUS, ECC_STS_DONE, "ecc gen");
+ if (ret)
+ return ret;
+ fl_writel(h, FLASH_NF_ECC_CONTROL,
+ fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_EN);
+
+ ret = mtd_ooblayout_get_eccbytes(mtd, ecc_code, chip->oob_poi, 0, chip->ecc.total);
+ if (ret)
+ return ret;
+
+ for (i = 0; eccsteps; eccsteps--, i += eccbytes, p += eccsize) {
+ u32 sts, ctl;
+ int step = i / eccbytes;
+
+ fl_writel(h, FLASH_NF_ECC_OOB,
+ ecc_code[i] | (ecc_code[i + 1] << 8) | (ecc_code[i + 2] << 16));
+ ctl = fl_readl(h, FLASH_NF_ECC_CONTROL) & ~ECC_CTL_CODESEL(0xf);
+ fl_writel(h, FLASH_NF_ECC_CONTROL, ctl | ECC_CTL_CODESEL(step));
+
+ sts = fl_readl(h, FLASH_NF_ECC_STATUS);
+ switch (ECC_STS_STATUS(sts)) {
+ case ECC_NO_ERR:
+ break;
+ case ECC_1BIT_DATA_ERR:
+ if (ECC_STS_ERRBYTE(sts) >= eccsize || ECC_STS_ERRBIT(sts) >= 8) {
+ mtd->ecc_stats.failed++;
+ break;
+ }
+ p[ECC_STS_ERRBYTE(sts)] ^= BIT(ECC_STS_ERRBIT(sts));
+ mtd->ecc_stats.corrected++;
+ max_bitflips = max(max_bitflips, 1);
+ dev_dbg(h->dev, "page %d step %d: 1-bit data error corrected\n",
+ page, step);
+ break;
+ case ECC_1BIT_ECC_ERR:
+ mtd->ecc_stats.corrected++;
+ max_bitflips = max(max_bitflips, 1);
+ dev_dbg(h->dev, "page %d step %d: 1-bit ecc error\n", page, step);
+ break;
+ case ECC_UNCORRECTABLE:
+ mtd->ecc_stats.failed++;
+ dev_dbg(h->dev, "page %d step %d: uncorrectable\n", page, step);
+ break;
+ }
+ }
+ return max_bitflips;
+}
+
+static int cs752x_nand_wait_ready(struct cs752x_nand_host *h,
+ unsigned int timeout_ms)
+{
+ const struct nand_interface_config *conf;
+ u32 value;
+
+ /* R/B can remain high for tWB after a command has been issued. */
+ conf = nand_get_interface_config(&h->chip);
+ ndelay(NAND_COMMON_TIMING_NS(conf, tWB_max));
+
+ /* nflash_rbIn_d is the sampled NAND R/B input, not the FSM state. */
+ return readl_poll_timeout(h->iobase_fl + FLASH_STATUS, value,
+ value & BIT(26), 1, (u64)timeout_ms * 1000);
+}
+
+static int cs752x_nand_hwecc_op(struct cs752x_nand_host *h,
+ const struct cs752x_nand_op *p)
+{
+ struct nand_chip *chip = &h->chip;
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ bool write = p->data->type == NAND_OP_DATA_OUT_INSTR;
+ u64 deadline;
+ u32 count, access;
+ int ret;
+
+ ret = cs752x_page_op_begin(h);
+ if (ret)
+ return ret;
+ fl_writel(h, FLASH_NF_ECC_CONTROL, ECC_CTL_EN);
+ count = NF_CNT_DATA(mtd->writesize - 1) | NF_CNT_OOB(mtd->oobsize - 1) |
+ NF_CNT_ADDR(p->naddrs - 1) | NF_CNT_CMD(p->ncmds - 1);
+ access = NF_ACCESS_WIDTH32 |
+ NF_ACCESS_EXTADDR(((p->address >> 16) << chip->page_shift) >> 27);
+ if (h->cs)
+ access |= NF_ACCESS_CE_ALT;
+ fl_writel(h, FLASH_NF_COUNT, count);
+ fl_writel(h, FLASH_NF_COMMAND, p->command);
+ fl_writel(h, FLASH_NF_ADDRESS_1, lower_32_bits(p->address));
+ fl_writel(h, FLASH_NF_ADDRESS_2, upper_32_bits(p->address));
+ fl_writel(h, FLASH_NF_ACCESS, access);
+ /* Data and OOB share one transfer budget. */
+ deadline = ktime_get_ns() + CS75XX_PIO_TIMEOUT_MS * (u64)NSEC_PER_MSEC;
+ ret = cs752x_nand_words(h, write ? (u8 *)p->data->ctx.data.buf.out :
+ p->data->ctx.data.buf.in,
+ mtd->writesize, write, deadline);
+ if (!ret && write) {
+ u64 ecc_start = ktime_get_ns();
+
+ ret = fill_hamming_oob_data(h);
+ /* ECC completion has its own timeout. */
+ deadline += ktime_get_ns() - ecc_start;
+ }
+ if (!ret)
+ ret = cs752x_nand_words(h, chip->oob_poi, mtd->oobsize, write, deadline);
+ if (!ret && p->wait_ready)
+ ret = cs752x_nand_wait_ready(h, p->waitrdy_timeout_ms);
+ if (p->delay_ns)
+ ndelay(p->delay_ns);
+ if (ret)
+ fl_writel(h, FLASH_NF_ECC_RESET,
+ ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
+ /* Read ECC registers remain live until the caller corrects the data. */
+ return ret;
+}
+
+static int cs752x_nand_pio(struct cs752x_nand_host *h,
+ const struct cs752x_nand_op *p, u8 *buf,
+ unsigned int len, bool write)
+{
+ u64 deadline;
+ u32 value;
+ unsigned int i, step = write && len ? sizeof(u32) : 1;
+ int ret;
+
+ ret = check_flash_ctrl_status(h);
+ if (ret)
+ return ret;
+ reset_ecc_bch_registers(h);
+ fl_writel(h, FLASH_NF_COUNT,
+ NF_CNT_OOB(NCNT_EMPTY_OOB) |
+ NF_CNT_DATA(len ? len - 1 : NCNT_EMPTY_DATA) |
+ NF_CNT_ADDR(p->naddrs ? p->naddrs - 1 : NCNT_EMPTY_ADDR) |
+ NF_CNT_CMD(p->ncmds ? p->ncmds - 1 : NCNT_EMPTY_CMD));
+ fl_writel(h, FLASH_NF_COMMAND, p->command);
+ fl_writel(h, FLASH_NF_ADDRESS_1, lower_32_bits(p->address));
+ fl_writel(h, FLASH_NF_ADDRESS_2, upper_32_bits(p->address));
+ /*
+ * Explicit row cycles are in ADDRESS_1/_2. Match the vendor erase
+ * and OOB register-PIO paths, which leave aperture EXTADDR zero.
+ */
+ fl_writel(h, FLASH_NF_ACCESS,
+ (write && len ? NF_ACCESS_WIDTH32 : NF_ACCESS_WIDTH8) |
+ (h->cs ? NF_ACCESS_CE_ALT : 0));
+
+ /* The PIO transfer has one budget, independent of the NAND R/B wait. */
+ deadline = ktime_get_ns() + CS75XX_PIO_TIMEOUT_MS * (u64)NSEC_PER_MSEC;
+ for (i = 0; i < max(len, 1U); i += step) {
+ u64 now = ktime_get_ns();
+
+ if (now >= deadline) {
+ ret = -ETIMEDOUT;
+ goto out;
+ }
+ if (write && len)
+ fl_writel(h, FLASH_NF_DATA, get_unaligned_le32(buf + i));
+ fl_writel(h, FLASH_FLASH_ACCESS_START,
+ FLASH_START_REGREQ | FLASH_START_CMD(write ? FLASH_WT : FLASH_RD));
+ /* Poll without sleeping and measure the remaining wall-clock time. */
+ ret = readl_poll_timeout(h->iobase_fl + FLASH_FLASH_ACCESS_START,
+ value, !(value & FLASH_START_REGREQ), 0,
+ DIV_ROUND_UP_ULL(deadline - now, NSEC_PER_USEC));
+ if (ret)
+ goto out;
+ if (!write && len)
+ buf[i] = fl_readl(h, FLASH_NF_DATA) >> ((i % 4) * 8);
+ }
+ ret = p->wait_ready ? cs752x_nand_wait_ready(h, p->waitrdy_timeout_ms) : 0;
+ if (p->delay_ns)
+ ndelay(p->delay_ns);
+out:
+ fl_writel(h, FLASH_NF_ECC_RESET,
+ ECC_RESET_ECC_CLR | ECC_RESET_FIFO_CLR | ECC_RESET_NF_RESET);
+ return ret;
+}
+
+static int cs752x_nand_exec_subop(struct nand_chip *chip,
+ const struct nand_subop *subop)
+{
+ struct cs752x_nand_host *h = to_host(chip);
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ struct cs752x_nand_op p = {};
+ unsigned int len = 0, i;
+ u8 *buf = NULL;
+ bool write = false;
+ int ret;
+
+ for (i = 0; i < subop->ninstrs; i++) {
+ const struct nand_op_instr *instr = &subop->instrs[i];
+ unsigned int off, j;
+
+ p.delay_ns = max(p.delay_ns, instr->delay_ns);
+ switch (instr->type) {
+ case NAND_OP_CMD_INSTR:
+ p.command |= (u32)instr->ctx.cmd.opcode << (p.ncmds * 8);
+ p.ncmds++;
+ break;
+ case NAND_OP_ADDR_INSTR:
+ off = nand_subop_get_addr_start_off(subop, i);
+ p.naddrs = nand_subop_get_num_addr_cyc(subop, i);
+ for (j = 0; j < p.naddrs; j++)
+ p.address |= (u64)instr->ctx.addr.addrs[off + j] << (j * 8);
+ break;
+ case NAND_OP_DATA_IN_INSTR:
+ case NAND_OP_DATA_OUT_INSTR:
+ p.data = instr;
+ write = instr->type == NAND_OP_DATA_OUT_INSTR;
+ off = nand_subop_get_data_start_off(subop, i);
+ len = nand_subop_get_data_len(subop, i);
+ buf = write ? (u8 *)instr->ctx.data.buf.out + off :
+ instr->ctx.data.buf.in + off;
+ break;
+ case NAND_OP_WAITRDY_INSTR:
+ p.wait_ready = true;
+ p.waitrdy_timeout_ms = instr->ctx.waitrdy.timeout_ms;
+ break;
+ }
+ }
+
+ if (!p.ncmds && !p.data) {
+ ret = cs752x_nand_wait_ready(h, p.waitrdy_timeout_ms);
+ if (p.delay_ns)
+ ndelay(p.delay_ns);
+ return ret;
+ }
+
+ if (h->use_hwecc && p.data && p.ncmds == 2 && p.naddrs >= 4 &&
+ !(p.address & 0xffff) && len == mtd->writesize)
+ return cs752x_nand_hwecc_op(h, &p);
+
+ /* The register engine starts a reset command in write mode. */
+ if (!p.data && p.ncmds == 1 && p.command == NAND_CMD_RESET)
+ write = true;
+ return cs752x_nand_pio(h, &p, buf, len, write);
+}
+
+static const struct nand_op_parser_pattern cs752x_nand_patterns[] = {
+ NAND_OP_PARSER_PATTERN(cs752x_nand_exec_subop,
+ NAND_OP_PARSER_PAT_CMD_ELEM(false),
+ NAND_OP_PARSER_PAT_CMD_ELEM(true),
+ NAND_OP_PARSER_PAT_ADDR_ELEM(false, 5),
+ NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, NCNT_EMPTY_DATA),
+ NAND_OP_PARSER_PAT_CMD_ELEM(true),
+ NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
+ NAND_OP_PARSER_PATTERN(cs752x_nand_exec_subop,
+ NAND_OP_PARSER_PAT_CMD_ELEM(false),
+ NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
+ NAND_OP_PARSER_PAT_CMD_ELEM(true),
+ NAND_OP_PARSER_PAT_WAITRDY_ELEM(true),
+ NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, NCNT_EMPTY_DATA)),
+ NAND_OP_PARSER_PATTERN(cs752x_nand_exec_subop,
+ NAND_OP_PARSER_PAT_CMD_ELEM(false),
+ NAND_OP_PARSER_PAT_ADDR_ELEM(true, 5),
+ NAND_OP_PARSER_PAT_CMD_ELEM(true),
+ NAND_OP_PARSER_PAT_WAITRDY_ELEM(true)),
+ NAND_OP_PARSER_PATTERN(cs752x_nand_exec_subop,
+ NAND_OP_PARSER_PAT_DATA_IN_ELEM(false, NCNT_EMPTY_DATA)),
+ NAND_OP_PARSER_PATTERN(cs752x_nand_exec_subop,
+ NAND_OP_PARSER_PAT_DATA_OUT_ELEM(false, NCNT_EMPTY_DATA)),
+ NAND_OP_PARSER_PATTERN(cs752x_nand_exec_subop,
+ NAND_OP_PARSER_PAT_WAITRDY_ELEM(false)),
+};
+
+static const struct nand_op_parser cs752x_nand_op_parser = {
+ .patterns = cs752x_nand_patterns,
+ .npatterns = ARRAY_SIZE(cs752x_nand_patterns),
+};
+
+static int cs752x_nand_exec_op(struct nand_chip *chip,
+ const struct nand_operation *op, bool check_only)
+{
+ unsigned int i;
+ int ret;
+
+ if (op->cs || !op->ninstrs)
+ return -EOPNOTSUPP;
+
+ for (i = 0; i < op->ninstrs; i++) {
+ const struct nand_op_instr *instr = &op->instrs[i];
+
+ switch (instr->type) {
+ case NAND_OP_ADDR_INSTR:
+ if (!instr->ctx.addr.naddrs || instr->ctx.addr.naddrs > 5)
+ return -EOPNOTSUPP;
+ break;
+ case NAND_OP_DATA_OUT_INSTR:
+ if (instr->ctx.data.len % sizeof(u32))
+ return -EOPNOTSUPP;
+ fallthrough;
+ case NAND_OP_DATA_IN_INSTR:
+ if (!instr->ctx.data.len ||
+ instr->ctx.data.len > NCNT_EMPTY_DATA)
+ return -EOPNOTSUPP;
+ break;
+ case NAND_OP_WAITRDY_INSTR:
+ if (!instr->ctx.waitrdy.timeout_ms)
+ return -EOPNOTSUPP;
+ break;
+ case NAND_OP_CMD_INSTR:
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+ }
+
+ ret = nand_op_parser_exec_op(chip, &cs752x_nand_op_parser, op, true);
+ if (ret || check_only)
+ return ret;
+
+ return nand_op_parser_exec_op(chip, &cs752x_nand_op_parser, op, false);
+}
+
+static int cs752x_nand_read_page_hwecc(struct nand_chip *chip, u8 *buf,
+ int oob_required, int page)
+{
+ struct cs752x_nand_host *h = to_host(chip);
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ int ret;
+
+ h->use_hwecc = true;
+ ret = nand_read_page_op(chip, page, 0, buf, mtd->writesize);
+ h->use_hwecc = false;
+ if (ret)
+ return ret;
+ return hamming_correct(h, buf, page);
+}
+
+static int cs752x_nand_write_page_hwecc(struct nand_chip *chip, const u8 *buf,
+ int oob_required, int page)
+{
+ struct cs752x_nand_host *h = to_host(chip);
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ int ret;
+
+ h->use_hwecc = true;
+ ret = nand_prog_page_op(chip, page, 0, buf, mtd->writesize);
+ h->use_hwecc = false;
+ return ret;
+}
+
+static int cs752x_nand_attach_chip(struct nand_chip *chip)
+{
+ struct mtd_info *mtd = nand_to_mtd(chip);
+ struct cs752x_nand_host *h = to_host(chip);
+ int ecc_total, ret;
+
+ /* The data port transfers four bytes per register write on an x8 bus. */
+ if (chip->options & NAND_BUSWIDTH_16 ||
+ mtd->writesize + mtd->oobsize > NCNT_EMPTY_DATA ||
+ mtd->writesize % sizeof(u32) || mtd->oobsize % sizeof(u32))
+ return -EOPNOTSUPP;
+
+ chip->ecc.read_page_raw = nand_monolithic_read_page_raw;
+ chip->ecc.write_page_raw = nand_monolithic_write_page_raw;
+
+ switch (chip->ecc.engine_type) {
+ case NAND_ECC_ENGINE_TYPE_NONE:
+ case NAND_ECC_ENGINE_TYPE_SOFT:
+ ret = check_flash_ctrl_status(h);
+ if (ret)
+ return ret;
+ reset_ecc_bch_registers(h);
+ return 0;
+ case NAND_ECC_ENGINE_TYPE_ON_HOST:
+ break;
+ default:
+ return -EOPNOTSUPP;
+ }
+
+ /*
+ * The hardware also has Hamming-512 and BCH engines. This driver
+ * implements the Hamming-256 format used by the existing flash.
+ */
+ if ((mtd->writesize != SZ_2K && mtd->writesize != SZ_4K) ||
+ (mtd->oobsize != 64 && mtd->oobsize != 128) ||
+ (chip->ecc.placement != NAND_ECC_PLACEMENT_UNKNOWN &&
+ chip->ecc.placement != NAND_ECC_PLACEMENT_OOB) ||
+ (chip->ecc.algo != NAND_ECC_ALGO_UNKNOWN &&
+ chip->ecc.algo != NAND_ECC_ALGO_HAMMING) ||
+ (chip->ecc.size && chip->ecc.size != 256) ||
+ (chip->ecc.strength && chip->ecc.strength != 1))
+ return -EINVAL;
+
+ ecc_total = mtd->writesize / 256 * 3;
+ /* The standard Hamming layout reserves the last 24 or 48 OOB bytes. */
+ if (ecc_total > (mtd->oobsize == 64 ? 24 : 48))
+ return -EINVAL;
+ chip->options |= NAND_NO_SUBPAGE_WRITE;
+ chip->ecc.placement = NAND_ECC_PLACEMENT_OOB;
+ chip->ecc.algo = NAND_ECC_ALGO_HAMMING;
+ chip->ecc.size = 256;
+ chip->ecc.bytes = 3;
+ chip->ecc.strength = 1;
+
+ h->ecc_calc = devm_kzalloc(h->dev, mtd->oobsize, GFP_KERNEL);
+ h->ecc_code = devm_kzalloc(h->dev, mtd->oobsize, GFP_KERNEL);
+ if (!h->ecc_calc || !h->ecc_code)
+ return -ENOMEM;
+
+ mtd_set_ooblayout(mtd, nand_get_large_page_hamming_ooblayout());
+
+ chip->ecc.read_page = cs752x_nand_read_page_hwecc;
+ chip->ecc.write_page = cs752x_nand_write_page_hwecc;
+
+ return 0;
+}
+
+static const struct nand_controller_ops cs752x_nand_controller_ops = {
+ .attach_chip = cs752x_nand_attach_chip,
+ .exec_op = cs752x_nand_exec_op,
+};
+
+static int cs752x_nand_probe(struct platform_device *pdev)
+{
+ struct device *dev = &pdev->dev;
+ struct cs752x_nand_host *h;
+ struct nand_chip *chip;
+ struct mtd_info *mtd;
+ struct device_node *flash;
+ struct reset_control *reset;
+ struct clk *clk;
+ u32 cs;
+ int err;
+
+ h = devm_kzalloc(dev, sizeof(*h), GFP_KERNEL);
+ if (!h)
+ return -ENOMEM;
+ h->dev = dev;
+
+ clk = devm_clk_get_enabled(dev, NULL);
+ if (IS_ERR(clk))
+ return dev_err_probe(dev, PTR_ERR(clk), "AXI clock\n");
+
+ err = devm_clk_rate_exclusive_get(dev, clk);
+ if (err)
+ return dev_err_probe(dev, err, "hold the NAND clock rate\n");
+
+ reset = devm_reset_control_get_optional_exclusive(dev, NULL);
+ if (IS_ERR(reset))
+ return dev_err_probe(dev, PTR_ERR(reset), "flash reset\n");
+
+ if (reset) {
+ err = reset_control_assert(reset);
+ if (err)
+ return dev_err_probe(dev, err, "assert flash reset\n");
+
+ udelay(2);
+ err = reset_control_deassert(reset);
+ if (err)
+ return dev_err_probe(dev, err, "release flash reset\n");
+ }
+
+ h->iobase_fl = devm_platform_ioremap_resource(pdev, 0);
+ if (IS_ERR(h->iobase_fl))
+ return PTR_ERR(h->iobase_fl);
+
+ err = check_flash_ctrl_status(h);
+ if (err)
+ return err;
+
+ /* Use the controller's default asynchronous bus timing. */
+ fl_writel(h, FLASH_NF_TIMING, CS75XX_NAND_RESET_TIMING);
+
+ nand_controller_init(&h->controller);
+ h->controller.ops = &cs752x_nand_controller_ops;
+
+ chip = &h->chip;
+ mtd = nand_to_mtd(chip);
+ chip->controller = &h->controller;
+ if (of_get_available_child_count(dev->of_node) != 1)
+ return dev_err_probe(dev, -EOPNOTSUPP,
+ "one connected NAND target is supported\n");
+ flash = of_get_next_available_child(dev->of_node, NULL);
+ if (!flash || !of_node_name_eq(flash, "nand")) {
+ of_node_put(flash);
+ return dev_err_probe(dev, -ENODEV, "missing NAND chip node\n");
+ }
+ err = of_property_read_u32(flash, "reg", &cs);
+ if (err || cs > 1) {
+ of_node_put(flash);
+ return dev_err_probe(dev, -EINVAL, "invalid chip select\n");
+ }
+ h->cs = cs;
+ nand_set_flash_node(chip, flash);
+ of_node_put(flash);
+ nand_set_controller_data(chip, h);
+ mtd->dev.parent = dev;
+
+ err = nand_scan(chip, 1);
+ if (err)
+ return dev_err_probe(dev, err, "NAND scan failed\n");
+
+ err = mtd_device_register(mtd, NULL, 0);
+ if (err) {
+ nand_cleanup(chip);
+ return err;
+ }
+
+ platform_set_drvdata(pdev, h);
+ return 0;
+}
+
+static void cs752x_nand_remove(struct platform_device *pdev)
+{
+ struct cs752x_nand_host *h = platform_get_drvdata(pdev);
+ struct nand_chip *chip = &h->chip;
+
+ WARN_ON(mtd_device_unregister(nand_to_mtd(chip)));
+ nand_cleanup(chip);
+}
+
+static const struct of_device_id cs752x_nand_dt_ids[] = {
+ { .compatible = "cortina,cs7542-nand" },
+ {}
+};
+MODULE_DEVICE_TABLE(of, cs752x_nand_dt_ids);
+
+static struct platform_driver cs752x_nand_driver = {
+ .driver = {
+ .name = "cs752x_nand",
+ .of_match_table = cs752x_nand_dt_ids,
+ /* Fixed flash has no safe manual detach with active MTD users. */
+ .suppress_bind_attrs = true,
+ },
+ .probe = cs752x_nand_probe,
+ .remove = cs752x_nand_remove,
+};
+module_platform_driver(cs752x_nand_driver);
+
+MODULE_LICENSE("GPL");
+MODULE_DESCRIPTION("NAND flash driver for Cortina CS7542");
+MODULE_ALIAS("platform:cs752x_nand");
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* [PATCH v8 4/4] MAINTAINERS: Add Cortina CS7542 NAND controller entry
2026-12-04 14:03 [PATCH v8 0/4] Add Cortina Systems CS7542 NAND controller support Fil Dunsky via B4 Relay
` (2 preceding siblings ...)
2026-12-04 14:03 ` [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support Fil Dunsky via B4 Relay
@ 2026-12-04 14:03 ` Fil Dunsky via B4 Relay
3 siblings, 0 replies; 10+ messages in thread
From: Fil Dunsky via B4 Relay @ 2026-12-04 14:03 UTC (permalink / raw)
To: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, Philipp Zabel
Cc: linux-mtd, devicetree, linux-kernel
From: Fil Dunsky <filipp.dunsky@gmail.com>
List the binding and driver under the author maintaining the Almond+
platform support, together with the appropriate subsystem mailing list.
Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
---
MAINTAINERS | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/MAINTAINERS b/MAINTAINERS
index 44540a1eb..2524c5d59 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -6918,6 +6918,13 @@ S: Maintained
F: Documentation/hwmon/corsair-psu.rst
F: drivers/hwmon/corsair-psu.c
+CORTINA SYSTEMS CS7542 NAND CONTROLLER
+M: Fil Dunsky <filipp.dunsky@gmail.com>
+L: linux-mtd@lists.infradead.org
+S: Maintained
+F: Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml
+F: drivers/mtd/nand/raw/cs752x_nand.c
+
COUNTER SUBSYSTEM
M: William Breathitt Gray <wbg@kernel.org>
L: linux-iio@vger.kernel.org
--
2.53.0
^ permalink raw reply related [flat|nested] 10+ messages in thread
* Re: [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller
2026-12-04 14:03 ` [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller Fil Dunsky via B4 Relay
@ 2026-12-04 14:12 ` Philipp Zabel
2026-12-04 14:38 ` sashiko-bot
1 sibling, 0 replies; 10+ messages in thread
From: Philipp Zabel @ 2026-12-04 14:12 UTC (permalink / raw)
To: filipp.dunsky, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: linux-mtd, devicetree, linux-kernel
On Fr, 2026-12-04 at 17:03 +0300, Fil Dunsky via B4 Relay wrote:
> From: Fil Dunsky <filipp.dunsky@gmail.com>
>
> Describe the flash controller registers, optional banked data aperture,
> AXI bus clock, flash interrupt and shared flash reset. The controller
> supports two chip selects, 8/16-bit devices, Hamming with 256-byte or
> 512-byte steps, and BCH.
>
> Inherit the standard NAND chip ECC properties. The example selects the
> 256-byte Hamming layout used on Almond+.
>
> Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
> Reviewed-by: Rob Herring (Arm) <robh@kernel.org>
> ---
> .../bindings/mtd/cortina,cs7542-nand.yaml | 89 ++++++++++++++++++++++
> 1 file changed, 89 insertions(+)
>
> diff --git a/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml b/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml
> new file mode 100644
> index 000000000..3bc5b1411
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml
> @@ -0,0 +1,89 @@
[...]
> +
> + resets:
> + maxItems: 1
> + description: Shared flash controller reset
How is the reset shared?
regards
Philipp
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support
2026-12-04 14:03 ` [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support Fil Dunsky via B4 Relay
@ 2026-12-04 14:12 ` Philipp Zabel
2026-12-04 15:37 ` Fil Dunsky
2026-12-04 15:05 ` sashiko-bot
1 sibling, 1 reply; 10+ messages in thread
From: Philipp Zabel @ 2026-12-04 14:12 UTC (permalink / raw)
To: filipp.dunsky, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Rob Herring, Krzysztof Kozlowski,
Conor Dooley
Cc: linux-mtd, devicetree, linux-kernel
On Fr, 2026-12-04 at 17:03 +0300, Fil Dunsky via B4 Relay wrote:
> From: Fil Dunsky <filipp.dunsky@gmail.com>
>
> Add programmed-I/O support for the CS7542 NAND controller. Use the
> NAND operation parser to validate complete instructions and populate
> the native command, address and data registers. Transfer the requested
> bytes directly between the controller and the NAND core's buffer.
>
> Reset the controller at probe and program its default asynchronous bus
> timing. Keep the AXI clock rate fixed while the timing is in use.
> Poll the controller and the NAND R/B pin with distinct timeouts.
>
> Keep the existing 256-byte Hamming step and OOB layout used by Almond+.
> Disable the on-host Hamming and BCH engines for software ECC and for
> raw NAND operations. The initial driver supports one 8-bit target on
> either of the two hardware chip selects.
>
> The flash controller is fixed. An open MTD device pins its module, but
> sysfs unbind bypasses that reference and can release the live
> controller state. Suppress manual bind/unbind until the MTD devices
> can be detached safely.
>
> Signed-off-by: Fil Dunsky <filipp.dunsky@gmail.com>
> ---
> drivers/mtd/nand/raw/Kconfig | 11 +
> drivers/mtd/nand/raw/Makefile | 1 +
> drivers/mtd/nand/raw/cs752x_nand.c | 756 +++++++++++++++++++++++++++++++++++++
> 3 files changed, 768 insertions(+)
>
[...]
> diff --git a/drivers/mtd/nand/raw/cs752x_nand.c b/drivers/mtd/nand/raw/cs752x_nand.c
> new file mode 100644
> index 000000000..690067b9b
> --- /dev/null
> +++ b/drivers/mtd/nand/raw/cs752x_nand.c
> @@ -0,0 +1,756 @@
[...]
> +static int cs752x_nand_probe(struct platform_device *pdev)
> +{
[...]
> + reset = devm_reset_control_get_optional_exclusive(dev, NULL);
The dt-binding calls this reset "shared", but here we request exclusive
access, which is correct?
> + if (IS_ERR(reset))
> + return dev_err_probe(dev, PTR_ERR(reset), "flash reset\n");
> +
> + if (reset) {
> + err = reset_control_assert(reset);
> + if (err)
> + return dev_err_probe(dev, err, "assert flash reset\n");
> +
> + udelay(2);
> + err = reset_control_deassert(reset);
> + if (err)
> + return dev_err_probe(dev, err, "release flash reset\n");
> + }
regards
Philipp
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller
2026-12-04 14:03 ` [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller Fil Dunsky via B4 Relay
2026-12-04 14:12 ` Philipp Zabel
@ 2026-12-04 14:38 ` sashiko-bot
1 sibling, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-04 14:38 UTC (permalink / raw)
To: Fil Dunsky, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Philipp Zabel
Cc: linux-mtd, devicetree
Thank you for your contribution! Sashiko AI review found 2 potential
issues to consider:
- [Low] dt-bindings: mtd: narrow nand@[01] pattern lets nand@2..f bypass
schema checks
- [Low] dt-bindings: mtd: new cortina,cs7542-nand example uses
deprecated nand-ecc-mode
--
commit c5a8c999ee36c043d6790c4db87262ce31e97a61
Author: Fil Dunsky <filipp.dunsky@gmail.com>
dt-bindings: mtd: Add Cortina CS7542 NAND controller
This commit adds the Device Tree binding schema for the Cortina Systems
CS7542 NAND flash controller, describing its registers, optional data
aperture, clock, interrupt, shared reset, and two chip-select child nodes.
> diff --git a/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml b/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml
> new file mode 100644
> index 0000000000000..3bc5b141133d6
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml
> @@ -0,0 +1,89 @@
[ ... ]
> +allOf:
> + - $ref: nand-controller.yaml#
[ ... ]
> +patternProperties:
> + "^nand@[01]$":
> + type: object
> + $ref: raw-nand-chip.yaml#
> + properties:
> + reg:
> + minimum: 0
> + maximum: 1
> + unevaluatedProperties: false
[Severity: Low]
Can child nodes named nand@2 through nand@f bypass the reg maximum and child
unevaluatedProperties: false check here?
Because this schema inherits nand-controller.yaml#, its ^nand@[a-f0-9]$
pattern evaluates nand@2..f against raw-nand-chip.yaml#, which sets no reg
upper bound and allows additionalProperties. Root unevaluatedProperties:
false then accepts out-of-range chip selects and unknown child properties.
Matching ^nand@[a-f0-9]$ here (or setting ^nand@[2-9a-f]$: false) would
enforce the two-chip-select limit and child property checks across all NAND
child nodes.
[ ... ]
> + nand@0 {
> + reg = <0>;
> + nand-ecc-mode = "hw";
> + nand-ecc-algo = "hamming";
> + nand-ecc-step-size = <256>;
> + nand-ecc-strength = <1>;
> + };
[Severity: Low]
Should the example avoid the deprecated nand-ecc-mode property?
raw-nand-property.yaml marks nand-ecc-mode as deprecated. New bindings can
use nand-ecc-engine instead, or omit it since the raw NAND core already
defaults to on-host ECC.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-b4-cs7542-nand-v8-0-bb068644f71f@gmail.com?part=2
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support
2026-12-04 14:03 ` [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support Fil Dunsky via B4 Relay
2026-12-04 14:12 ` Philipp Zabel
@ 2026-12-04 15:05 ` sashiko-bot
1 sibling, 0 replies; 10+ messages in thread
From: sashiko-bot @ 2026-12-04 15:05 UTC (permalink / raw)
To: Fil Dunsky, Miquel Raynal, Richard Weinberger,
Vignesh Raghavendra, Rob Herring, Krzysztof Kozlowski,
Conor Dooley, Philipp Zabel
Cc: linux-mtd, devicetree
Thank you for your contribution! Sashiko AI review found 6 potential
issues to consider:
- [High] mtd: rawnand: cs752x_nand_probe() takes and pulses shared reset
exclusively
- [Medium] mtd: rawnand: missing FSM reset on timeout in
cs752x_nand_pio() and ECC
- [Medium] mtd: rawnand: sleeping readl_poll_timeout() in
mtd_panic_write() context
- [Medium] mtd: rawnand: cs752x: per-word udelay(1) in
cs752x_nand_words()
- [Medium] mtd: rawnand: cs752x: zero-slack usleep_range(1, 1) hrtimer
storms
- [Low] mtd: rawnand: cs752x_nand_exec_op() rejects op->cs on check_only
probes
--
commit 65996877e86929ea6a1fd88c61cb2bb3794c71d1
Author: Fil Dunsky <filipp.dunsky@gmail.com>
mtd: rawnand: Add Cortina CS7542 controller support
This commit adds a raw NAND controller driver for the Cortina CS7542
using programmed I/O, software ECC, and 256-byte hardware Hamming ECC.
> diff --git a/drivers/mtd/nand/raw/cs752x_nand.c b/drivers/mtd/nand/raw/cs752x_nand.c
> new file mode 100644
> index 0000000000000..690067b9bafa7
> --- /dev/null
> +++ b/drivers/mtd/nand/raw/cs752x_nand.c
> @@ -0,0 +1,756 @@
[ ... ]
> +static int check_flash_ctrl_status(struct cs752x_nand_host *h)
> +{
> + u32 val;
> + int ret;
> +
> + ret = readl_poll_timeout(h->iobase_fl + FLASH_STATUS, val,
> + !(val & FLASH_STATUS_NSTATE_MASK), 1, 1000000);
> + if (ret)
> + dev_err(h->dev, "FLASH_STATUS busy: 0x%x\n", val);
> + return ret;
> +}
> +
> +static int wait_reg(struct cs752x_nand_host *h, u32 reg, u32 bit, const char *what)
> +{
> + u32 val;
> + int ret;
> +
> + ret = readl_poll_timeout(h->iobase_fl + reg, val, val & bit, 1, 2000000);
> + if (ret)
> + dev_err(h->dev, "%s timeout\n", what);
> + return ret;
> +}
[Severity: Medium]
Can check_flash_ctrl_status(), wait_reg(), and cs752x_nand_wait_ready()
sleep in atomic context during mtd_panic_write()?
nand_scan_tail() sets mtd->_panic_write = panic_nand_write, which reaches
these helpers via nand_do_write_ops() during a panic dump (e.g. mtdoops).
With sleep_us = 1, readl_poll_timeout() invokes might_sleep_if() and
usleep_range(), risking scheduling-while-atomic warnings or a hang.
Checking mtd->oops_panic_write to use readl_poll_timeout_atomic() (or
clearing mtd->_panic_write if panic writes are not supported) avoids this.
[ ... ]
> + fl_writel(h, FLASH_FLASH_ACCESS_START,
> + FLASH_START_REGREQ | FLASH_START_CMD(write ? FLASH_WT : FLASH_RD));
> + ret = readl_poll_timeout_atomic(h->iobase_fl + FLASH_FLASH_ACCESS_START,
> + value, !(value & FLASH_START_REGREQ) ||
> + ktime_get_ns() >= deadline, 1, 10000);
[Severity: Medium]
Does passing delay_us = 1 to readl_poll_timeout_atomic() force a udelay(1)
busy-wait on every 4-byte word in cs752x_nand_words()?
Four 8-bit NAND bus cycles take longer than one MMIO turnaround, so the
first read usually sees FLASH_START_REGREQ set and stalls in udelay(1),
adding over 1 ms of busy-waiting per 4KB+128B page. Tight-polling with
delay_us = 0, as in cs752x_nand_pio(), avoids this per-word stall.
[ ... ]
> +static int cs752x_nand_wait_ready(struct cs752x_nand_host *h,
> + unsigned int timeout_ms)
> +{
> + const struct nand_interface_config *conf;
> + u32 value;
> +
> + /* R/B can remain high for tWB after a command has been issued. */
> + conf = nand_get_interface_config(&h->chip);
> + ndelay(NAND_COMMON_TIMING_NS(conf, tWB_max));
> +
> + /* nflash_rbIn_d is the sampled NAND R/B input, not the FSM state. */
> + return readl_poll_timeout(h->iobase_fl + FLASH_STATUS, value,
> + value & BIT(26), 1, (u64)timeout_ms * 1000);
> +}
[Severity: Medium]
Will passing sleep_us = 1 to readl_poll_timeout() here (and in
check_flash_ctrl_status() and wait_reg()) cause hrtimer interrupt storms?
poll_timeout_us() converts sleep_us = 1 to usleep_range(1, 1), which arms
a zero-slack hrtimer 1 us ahead on every iteration. Across a multi-ms
block erase or multi-hundred-us page program, that fires hundreds to
thousands of back-to-back timer interrupts and context switches.
Using a larger sleep_us (such as 20 us, giving usleep_range(6, 20) with
slack) reduces timer overhead while waiting on R/B.
[ ... ]
> +static int cs752x_nand_pio(struct cs752x_nand_host *h,
> + const struct cs752x_nand_op *p, u8 *buf,
> + unsigned int len, bool write)
> +{
> + u64 deadline;
> + u32 value;
> + unsigned int i, step = write && len ? sizeof(u32) : 1;
> + int ret;
> +
> + ret = check_flash_ctrl_status(h);
> + if (ret)
> + return ret;
> + reset_ecc_bch_registers(h);
[Severity: Medium]
Should cs752x_nand_pio() use cs752x_page_op_begin() so a busy timeout
resets the controller FSM?
Unlike cs752x_page_op_begin(), returning directly here skips writing
ECC_RESET_NF_RESET to FLASH_NF_ECC_RESET. If the FSM wedges busy (for
example in software-ECC mode where cs752x_page_op_begin() is never
called), subsequent PIO and nand_reset_op() calls fail with -ETIMEDOUT.
Similarly, if wait_reg() times out in hamming_correct(), it returns early
without clearing ECC_CTL_EN or asserting FLASH_NF_ECC_RESET, leaving the
ECC engine active after cs752x_nand_hwecc_op().
[ ... ]
> +static int cs752x_nand_exec_op(struct nand_chip *chip,
> + const struct nand_operation *op, bool check_only)
> +{
> + unsigned int i;
> + int ret;
> +
> + if (op->cs || !op->ninstrs)
> + return -EOPNOTSUPP;
[Severity: Low]
Does rejecting non-zero op->cs unconditionally break nand_check_op()
probes when no target is selected?
nand_scan_tail() deselects the target (chip->cur_cs = -1) before
rawnand_late_check_supported_ops() probes continuous cached reads via
nand_check_op(). Checking op->cs only when !check_only avoids silently
disabling continuous reads on read-cache-capable chips under software ECC.
[ ... ]
> + reset = devm_reset_control_get_optional_exclusive(dev, NULL);
> + if (IS_ERR(reset))
> + return dev_err_probe(dev, PTR_ERR(reset), "flash reset\n");
> +
> + if (reset) {
> + err = reset_control_assert(reset);
> + if (err)
> + return dev_err_probe(dev, err, "assert flash reset\n");
> +
> + udelay(2);
> + err = reset_control_deassert(reset);
> + if (err)
> + return dev_err_probe(dev, err, "release flash reset\n");
> + }
[Severity: High]
Can cs752x_nand_probe() request and pulse the flash controller reset
exclusively when the DT binding documents it as shared?
Documentation/devicetree/bindings/mtd/cortina,cs7542-nand.yaml defines
resets as "Shared flash controller reset". When another driver shares the
line, __reset_control_get_internal() triggers WARN_ON() and returns
-EBUSY; if this driver probes first, it blocks and resets active siblings.
Asserting the reset before validating the NAND child node also pulses the
shared line even on an invalid DT configuration. Requesting a shared
reset (e.g. devm_reset_control_get_optional_shared()) matches the binding.
--
Sashiko AI review · https://sashiko.dev/#/patchset/20261204-b4-cs7542-nand-v8-0-bb068644f71f@gmail.com?part=3
^ permalink raw reply [flat|nested] 10+ messages in thread
* Re: [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support
2026-12-04 14:12 ` Philipp Zabel
@ 2026-12-04 15:37 ` Fil Dunsky
0 siblings, 0 replies; 10+ messages in thread
From: Fil Dunsky @ 2026-12-04 15:37 UTC (permalink / raw)
To: Philipp Zabel
Cc: Miquel Raynal, Richard Weinberger, Vignesh Raghavendra,
Rob Herring, Krzysztof Kozlowski, Conor Dooley, linux-mtd,
devicetree, linux-kernel
Hi Philipp,
> The dt-binding calls this reset "shared", but here we request exclusive
> access, which is correct?
The binding is right. Global reset bit 14 covers the common flash
interface for NAND, parallel NOR and serial flash. The Almond+ DT only
has a NAND consumer today, but that does not make the reset exclusive.
The next revision takes a shared, deasserted reset reference and keeps it
for the life of the driver. At probe it resets only the local NAND state
machine through the NAND ECC reset register. It validates the NAND child
before obtaining the shared reset and describes the affected interfaces
in the binding.
The candidate booted on Almond+ and read 128 pages across four banks,
including the same raw OOB bytes as the installed driver.
Thanks,
Fil
^ permalink raw reply [flat|nested] 10+ messages in thread
end of thread, other threads:[~2026-12-04 15:38 UTC | newest]
Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 14:03 [PATCH v8 0/4] Add Cortina Systems CS7542 NAND controller support Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 1/4] mtd: rawnand: Honor controller raw page callbacks in ECC_NONE Fil Dunsky via B4 Relay
2026-12-04 14:03 ` [PATCH v8 2/4] dt-bindings: mtd: Add Cortina CS7542 NAND controller Fil Dunsky via B4 Relay
2026-12-04 14:12 ` Philipp Zabel
2026-12-04 14:38 ` sashiko-bot
2026-12-04 14:03 ` [PATCH v8 3/4] mtd: rawnand: Add Cortina CS7542 controller support Fil Dunsky via B4 Relay
2026-12-04 14:12 ` Philipp Zabel
2026-12-04 15:37 ` Fil Dunsky
2026-12-04 15:05 ` sashiko-bot
2026-12-04 14:03 ` [PATCH v8 4/4] MAINTAINERS: Add Cortina CS7542 NAND controller entry Fil Dunsky via B4 Relay
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox