spi: dw-g2: add Synopsys DWC SSI v2.02a controller support
Subsystems (2)
cover spi: dw-g2: add Synopsys DWC SSI v2.02a controller support
Add a driver and DT binding for the Synopsys DesignWare SSI Gen2 (DWC_ssi v2.02a) controller.
The controller uses a per-chip-select register map. This first posting covers the MMIO glue, polled spi-mem transfers (short register ops and FIFO indirect reads/writes), SPI-NAND handling, and memory-mapped XIP through spi-mem direct mapping.
The series was exercised on a TI K3 platform with SPI-NOR and a Winbond W25N01JW SPI-NAND.
---
Prakriti Sharma (5):
spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
spi: dw-g2: Add indirect read and write
spi: dw-g2: Add SPI-NAND support
dt-bindings: spi: Document DWC SSI XIP properties
spi: dw-g2: Add XIP support
Siddhant Kumar (1):
dt-bindings: spi: Add Synopsys DWC SSI v2.02a binding
.../bindings/spi/snps,dwc-ssi-2.02a.yaml | 199 ++ MAINTAINERS | 8 + drivers/spi/Kconfig | 21 + drivers/spi/Makefile | 3 + drivers/spi/spi-dw-g2-core.c | 2308 +++++++++++++++++ drivers/spi/spi-dw-g2-mmio.c | 457 ++++ drivers/spi/spi-dw-g2.h | 552 ++++ 7 files changed, 3548 insertions(+) create mode 100644 Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml create mode 100644 drivers/spi/spi-dw-g2-core.c create mode 100644 drivers/spi/spi-dw-g2-mmio.c create mode 100644 drivers/spi/spi-dw-g2.h
Signature · 1 line
2.34.1
PATCH 1/6 dt-bindings: spi: Add Synopsys DWC SSI v2.02a binding
From: Siddhant Kumar <[email protected]>
Add a binding for the Synopsys DesignWare Synchronous Serial Interface (DWC_ssi v2.02a). The controller has a per-chip-select register map and dedicated chip-select control.
--- .../bindings/spi/snps,dwc-ssi-2.02a.yaml | 144 ++++++++++++++++++ MAINTAINERS | 7 + 2 files changed, 151 insertions(+) create mode 100644 Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml new file +144 -0
@@ -0,0 +1,144 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/spi/snps,dwc-ssi-2.02a.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Synopsys DesignWare SSI Gen2 SPI Controller + +maintainers: + - Siddhant Kumar <[email protected]> + - Prakriti Sharma <[email protected]> + +description: | + The Synopsys DesignWare SSI Gen2 (DWC_ssi v2.02a) is an enhanced SPI + controller featuring per-chip-select register banks, dedicated CS control, + and support for Standard/Dual/Quad/Octal SPI modes with DDR capability. + +allOf: + - $ref: spi-controller.yaml# + +properties: + compatible: + const: snps,dwc-ssi-2.02a + + reg: + maxItems: 1 + + interrupts: + description: | + The DWC SSI Gen2 IP exposes per-source interrupt outputs alongside a + combined ssi_intr. SoC integrators may route either the combined line, + the individual lines, or any subset. Provide either a single combined + IRQ, or one entry per declared name in interrupt-names (matching order). + minItems: 1 + maxItems: 4 + + interrupt-names: + description: | + Optional. Naming for each physical IRQ line when the SoC breaks the + controller's interrupt output into multiple wires. Each name is gated + by its corresponding ISR bit in the controller: + "rxf" - RX FIFO threshold (SSI_RXF_INTR, ISR.RXFI) + "txe" - TX FIFO threshold (SSI_TXE_INTR, ISR.TXEI) + "txaf" - TX FIFO almost full (ssi_txaf_intr, ISR.TXAFI) + "wsf" - Write status fail (spi_wr_status_fail_intr, ISR.SPIWSFI) + Omit this property if the SoC combines all sources onto one wire. + minItems: 1 + maxItems: 4 + items: + enum: [rxf, txe, txaf, wsf] + + clocks: + minItems: 1 + items: + - description: SSI controller reference clock + - description: APB interface clock + + clock-names: + minItems: 1 + items: + - const: ssi_clk + - const: pclk + + resets: + maxItems: 1 + + num-cs: + minimum: 1 + maximum: 4 + description: + Number of chip selects supported by the controller. + + dmas: + items: + - description: TX DMA Channel + - description: RX DMA Channel + + dma-names: + items: + - const: tx + - const: rx + + rx-sample-delay-ns: + default: 0 + description: | + Default value of the rx-sample-delay-ns property. + This value will be used if the property is not explicitly defined + for a SPI slave device. + + SPI Rx sample delay offset, unit is nanoseconds. + The delay from the default sample time before the actual sample of the + rxd input signal occurs. The "rx_sample_delay" is an optional feature + of the designware controller, and the upper limit is also subject to + controller configuration. + +patternProperties: + "^.*@[0-9a-f]+$": + type: object + $ref: spi-peripheral-props.yaml + additionalProperties: true + + properties: + reg: + minimum: 0 + maximum: 3 + + rx-sample-delay-ns: + description: + Per-device Rx sample delay override, in nanoseconds. + +unevaluatedProperties: false + +required: + - compatible + - reg + - "#address-cells" + - "#size-cells" + - clocks + +examples: + - | + spi@20100000 { + compatible = "snps,dwc-ssi-2.02a"; + reg = <0x20100000 0x1000>; + #address-cells = <1>; + #size-cells = <0>; + interrupts = <0 42 4>; + clocks = <&spi_clk>; + clock-names = "ssi_clk"; + num-cs = <2>; + dmas = <&dma0 10>, <&dma0 11>; + dma-names = "tx", "rx"; + rx-sample-delay-ns = <3>; + + flash@0 { + compatible = "jedec,spi-nor"; + reg = <0>; + spi-max-frequency = <50000000>; + spi-rx-bus-width = <4>; + spi-tx-bus-width = <4>; + rx-sample-delay-ns = <7>; + }; + }; +...
MAINTAINERS +7 -0
@@ -27304,6 +27304,13 @@ S: Supported F: Documentation/admin-guide/perf/dwc_pcie_pmu.rst F: drivers/perf/dwc_pcie_pmu.c +SYNOPSYS DESIGNWARE SSI GEN2 SPI DRIVER +M: Prakriti Sharma <[email protected]> +M: Siddhant Kumar <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml + SYNOPSYS HSDK RESET CONTROLLER DRIVER M: Eugeniy Paltsev <[email protected]> S: Supported
Signature · 1 line
2.34.1
PATCH 2/6 spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
In reply to Prakriti SharmaAdd a driver for the Synopsys DesignWare SSI Gen2 (DWC_ssi v2.02a) controller. Support probe, the MMIO glue, command-only transfers, and short register operations.
Transfers are polled. The IRQ is claimed, including the optional per-source rxf, txe, txaf, and wsf lines.
--- MAINTAINERS | 1 + drivers/spi/Kconfig | 21 + drivers/spi/Makefile | 3 + drivers/spi/spi-dw-g2-core.c | 896 +++++++++++++++++++++++++++++++++++ drivers/spi/spi-dw-g2-mmio.c | 196 ++++++++ drivers/spi/spi-dw-g2.h | 396 ++++++++++++++++ 6 files changed, 1513 insertions(+) create mode 100644 drivers/spi/spi-dw-g2-core.c create mode 100644 drivers/spi/spi-dw-g2-mmio.c create mode 100644 drivers/spi/spi-dw-g2.h
MAINTAINERS +1 -0
@@ -27310,6 +27310,7 @@ M: Siddhant Kumar <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +F: drivers/spi/spi-dw-g2-*.[ch] SYNOPSYS HSDK RESET CONTROLLER DRIVER M: Eugeniy Paltsev <[email protected]>
drivers/spi/Kconfig +21 -0
@@ -381,6 +381,27 @@ config SPI_DESIGNWARE if SPI_DESIGNWARE +config SPI_DW_SSI_G2 + tristate "Synopsys Designware SSI Gen2 controller" + depends on SPI_DESIGNWARE + help + Synopsys DesignWare SSI Gen2 is a SPI controller that supports + standard, dual, quad, and octal SPI modes. It provides high + performance SPI communication using the spi-mem interface. + Enable this option if you have a device with a DW SSI Gen2 + controller and want to access SPI flash memories. + +config SPI_DW_SSI_G2_MMIO + tristate "Memory-mapped io interface driver for DW SSI Gen2 core" + depends on HAS_IOMEM + depends on SPI_DW_SSI_G2 + help + Memory-mapped I/O interface driver for the Synopsys DesignWare + SSI Gen2 SPI controller. Enable this for platforms that access + the controller registers via memory-mapped I/O. This is the + most common access method for SoC-integrated instances of the + DW SSI Gen2 controller. + config SPI_DW_DMA bool "DMA support for DW SPI controller"
drivers/spi/Makefile +3 -0
@@ -57,6 +57,9 @@ spi-dw-y := spi-dw-core.o spi-dw-$(CONFIG_SPI_DW_DMA) += spi-dw-dma.o obj-$(CONFIG_SPI_DW_MMIO) += spi-dw-mmio.o obj-$(CONFIG_SPI_DW_PCI) += spi-dw-pci.o +obj-$(CONFIG_SPI_DW_SSI_G2) += spi-dw-g2.o +spi-dw-g2-y := spi-dw-g2-core.o +obj-$(CONFIG_SPI_DW_SSI_G2_MMIO) += spi-dw-g2-mmio.o obj-$(CONFIG_SPI_EP93XX) += spi-ep93xx.o obj-$(CONFIG_SPI_FALCON) += spi-falcon.o obj-$(CONFIG_SPI_FSI) += spi-fsi.o
drivers/spi/spi-dw-g2-core.c new file +896 -0
896 lines not on this page · raw message
drivers/spi/spi-dw-g2-mmio.c new file +196 -0
196 lines not on this page · raw message
drivers/spi/spi-dw-g2.h new file +396 -0
396 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
Thank you for your contribution! Sashiko AI review found 13 potential issues to consider: - [High] spi: dw-g2: dw_spi_g2_supports_mem_ops() accepts ops misexecuted by reg_op() - [Medium] spi: dw-g2…
Thank you for your contribution! Sashiko AI review found 13 potential issues to consider:
- [High] spi: dw-g2: dw_spi_g2_supports_mem_ops() accepts ops misexecuted by reg_op()
- [Medium] spi: dw-g2: debugfs init before bus_num allocation and leak on probe error
- [Medium] spi: dw-g2: missing SPI_CPOL and SPI_CPHA setup in dw_spi_g2_build_ctrlr0()
- [Medium] spi: dw-g2: missing spi_controller_put() in dw_spi_g2_mmio_remove()
- [Medium] spi: dw-g2: debugfs leak, UAF, and -1 bus_num in dw_spi_g2_mmio_probe()
- [Medium] spi: dw-g2: devm IRQ outlives reset and dws in probe() and remove()
- [Medium] spi: dw-g2: unwired dw_spi_g2_transfer_one() locks up CPU if called
- [Medium] spi: dw-g2: missing txe, txaf, and wsf IRQ requests in dw_spi_g2_mmio_probe()
- [Medium] spi: dw-g2: unvalidated num-cs and clobbered dws->caps in probe and init
- [Medium] spi: dw-g2: missing linux/bitfield.h include for FIELD_PREP()
- [Medium] spi: dw-g2: unchecked dw_spi_g2_wait_idle() and missing SR_TFE check
- [Low] spi: dw-g2: MAINTAINERS glob misses spi-dw-g2.h and bad kernel-doc markers
- [Low] spi: dw-g2: non-kernel-doc comments using /** in spi-dw-g2-core.c
--
commit 6b1bdfd4c5618c387e6d2d343a2ddf90fcf357ed
Author: Prakriti Sharma <[email protected]>
spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
This commit adds the core and MMIO driver for the Synopsys DesignWare SSI Gen2 (DWC_ssi v2.02a) SPI controller, supporting probe, command-only transfers, and short register operations via polling.
MAINTAINERS M: Siddhant Kumar <[email protected]> · line 27313
Quoted 9 lines
MAINTAINERS +1
-0@@ -27310,6 +27310,7 @@ M: Siddhant Kumar <[email protected]> L: [email protected] S: Maintained F: Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +F: drivers/spi/spi-dw-g2-*.[ch]
[Severity: Low]
Does the hyphen before the wildcard cause get_maintainer.pl to miss drivers/spi/spi-dw-g2.h? Using drivers/spi/spi-dw-g2*.[ch] would match both the header and the .c files.
Quoted 5 lines
drivers/spi/spi-dw-g2-core.c new file +0
-0
[ ... ]
Quoted 6 lines
drivers/spi/spi-dw-g2-core.c new file +6
-0+static void dw_spi_g2_debugfs_init (struct dw_spi_g2 *dws) +{ +char name[32 ]; + +snprintf (name,sizeof (name),"dw_spi_g2.%d" , dws->ctlr->bus_num); +
drivers/spi/spi-dw-g2-core.c line 64
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+ dws->debugfs =debugfs_create_dir (name,NULL ); +if (!dws->debugfs) +return ; + + dws->regset.base = dws->regs; + dws->regset.regs = dw_spi_g2_dbg_regs; + dws->regset.nregs =ARRAY_SIZE (dw_spi_g2_dbg_regs); + +debugfs_create_regset32 ("registers" ,0400 , dws->debugfs, &dws->regset); +}
[Severity: Medium]
Does calling dw_spi_g2_debugfs_init() from dw_spi_g2_init() run before spi_register_controller() assigns a dynamic bus_num?
When a Device Tree node has no spiN alias, dws->ctlr->bus_num is still -1 during dw_spi_g2_init(), so this creates /sys/kernel/debug/dw_spi_g2.-1 and collides if a second controller is present. In addition, debugfs_create_dir() returns an ERR_PTR() when debugfs is disabled or fails, so checking !dws->debugfs does not catch errors (though debugfs_create_regset32() handles an ERR_PTR() parent).
[ ... ]
Quoted 2 lines
drivers/spi/spi-dw-g2-core.c new file +2
-0+static u32 dw_spi_g2_build_ctrlr0 (struct dw_spi_g2 *dws,u8 tmod,u8 frf) +{
[ ... ]
drivers/spi/spi-dw-g2-core.c line 231
Quoted 7 lines
drivers/spi/spi-dw-g2-core.c new file +7
-0+ ctrlr0 |= +FIELD_PREP (DW_SPI_G2_CTRLR0_DFS_MASK ,DW_SPI_G2_DFS_ENCODE (dfs)); + ctrlr0 |=FIELD_PREP (DW_SPI_G2_CTRLR0_TMOD_MASK , tmod); + ctrlr0 |=FIELD_PREP (DW_SPI_G2_CTRLR0_FRF_MASK , frf); + +return ctrlr0; +}
[Severity: Medium]
Should dw_spi_g2_build_ctrlr0() (and dw_spi_g2_update_config()) also set DW_SPI_G2_CTRLR0_SCPOL and DW_SPI_G2_CTRLR0_SCPHA from spi->mode?
dw_spi_g2_init() advertises SPI_CPOL | SPI_CPHA in ctlr->mode_bits, but neither function programs the polarity or phase bits into CTRLR0, forcing all transfers to run in SPI Mode 0 and breaking communication with Mode 1, 2, or 3 devices.
[ ... ]
Quoted 4 lines
drivers/spi/spi-dw-g2-core.c new file +4
-0+static int dw_spi_g2_xfer_fifo (struct dw_spi_g2 *dws, +const u8 *tx_buf,u32 tx_len, +u8 *rx_buf,u32 rx_len) +{
[ ... ]
Quoted 18 lines
drivers/spi/spi-dw-g2-core.c new file +18
-0+if (!rx_buf || !rx_len) { +dw_spi_g2_wait_idle (dws); +return 0 ; + } + +/* RX phase */ + timeout = jiffies +msecs_to_jiffies (1000 ); +while (rx_got < rx_len) { + sr =dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ); +if (sr &DW_SPI_G2_SR_RFNE ) { + rx_buf[rx_got++] =dw_spi_g2_readl (dws->regs, +DW_SPI_G2_DR0 ) & +0xff ; + timeout = jiffies +msecs_to_jiffies (1000 ); + }else if (time_after (jiffies, timeout)) { + dev_err(dws->dev, + "xfer_fifo: RX timeout after %u/%u bytes (SR=0x%08x RXFLR=%u)\n", + rx_got, rx_len,
drivers/spi/spi-dw-g2-core.c line 368
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ), + dw_spi_g2_readl(dws->regs, DW_SPI_G2_RXFLR)); +return -ETIMEDOUT ; + } +cpu_relax (); + } + +dw_spi_g2_wait_idle (dws); +return 0 ; +}
[Severity: Medium]
Can ignoring the return value of dw_spi_g2_wait_idle() here report false success when a transfer times out, and can dw_spi_g2_wait_idle() return before the TX FIFO has started shifting?
dw_spi_g2_wait_idle() in drivers/spi/spi-dw-g2.h only polls !(sr & DW_SPI_G2_SR_BUSY) without checking (sr & DW_SPI_G2_SR_TFE). Because SR_BUSY is driven by the serial shifter in the ssi_clk domain, reading SR immediately after writing DR0 can observe SR_BUSY == 0 while bytes are still queued in the TX FIFO (SR_TFE == 0), causing the caller's dw_spi_g2_transfer_end() to clear SSIENR and abort the transfer early. Also, if dw_spi_g2_wait_idle() times out, dw_spi_g2_xfer_fifo() discards the -ETIMEDOUT error and returns 0.
[ ... ]
Quoted 4 lines
+/**
+ * Command-only operations (WREN, WRDI, Chip Erase, Sector Erase, etc.)
+ * TMOD=1 (TX-Only) — Send opcode + optional address, no data phase
+ */
[ ... ]
Quoted 5 lines
drivers/spi/spi-dw-g2-core.c new file +5
-0+/** +* Indirect read operations +*/ + +/*
drivers/spi/spi-dw-g2-core.c line 551
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+* ======================================================================== +* SPI-MEM interface +* ======================================================================== +*/ + +static bool dw_spi_g2_supports_mem_ops (struct spi_mem *mem, +const struct spi_mem_op *op) +{ +return spi_mem_default_supports_op (mem, op); +}
[Severity: Low]
The two section comments above ("Command-only operations..." and "Indirect read operations") start with /** instead of /*, which triggers scripts/kernel-doc warnings under W=1 builds.
[Severity: High]
Does dw_spi_g2_supports_mem_ops() accept operations that dw_spi_g2_reg_op() and dw_spi_g2_cmd_only() cannot execute as requested?
Because ctlr->mode_bits advertises Dual, Quad, and Octal modes, spi_mem_default_supports_op() accepts multi-wire operations, operations with dummy cycles, and 2-byte opcodes. However, dw_spi_g2_cmd_only_setup() and dw_spi_g2_reg_op_setup() hardcode DW_SPI_G2_FRF_STANDARD (1-1-1 mode) and write 0 to DW_SPI_G2_SPI_CTRLR0, and dw_spi_g2_reg_op() only sends a 1-byte opcode (tx_buf[0] = op->cmd.opcode) and omits op->dummy.nbytes entirely, resulting in malformed flash transactions on the wire. Even after commit "spi: dw-g2: Add SPI-NAND support" and commit "spi: dw-g2:
Add indirect read and write", dw_spi_g2_is_reg_op() routes all transfers up to 256 bytes into dw_spi_g2_reg_op() in 1-1-1 mode with 1-byte opcodes, and TX register/command ops still drop dummy cycles.
[ ... ]
Quoted 20 lines
drivers/spi/spi-dw-g2-core.c new file +20
-0+void dw_spi_g2_update_config (struct dw_spi_g2 *dws,struct spi_device *spi, +struct dw_spi_g2_cfg *cfg) +{ +u32 ctrlr0 =0 ; +u32 div; + + ctrlr0 |=FIELD_PREP (DW_SPI_G2_CTRLR0_DFS_MASK , cfg->dfs -1 ); + ctrlr0 |=FIELD_PREP (DW_SPI_G2_CTRLR0_TMOD_MASK , cfg->tmode); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); + +if (cfg->tmode ==DW_SPI_G2_CTRLR0_TMOD_RO ) +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs), cfg->ndf -1 ); + + div =DIV_ROUND_UP (dws->max_freq, cfg->freq); +if (div &1 ) + div++; +dw_spi_g2_writel (dws->regs,DW_SPI_G2_BAUDR (dws->cs), div); +} +
[ ... ]
Quoted 3 lines
drivers/spi/spi-dw-g2-core.c new file +3
-0+int dw_spi_g2_transfer_one (struct spi_controller *ctlr,struct spi_device *spi, +struct spi_transfer *xfer) +{
[ ... ]
Quoted 28 lines
drivers/spi/spi-dw-g2-core.c new file +28
-0+dw_spi_g2_enable (dws, false); +dw_spi_g2_update_config (dws, spi, &cfg); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR , dws->fifo_len /2 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_RXFTLR , (dws->fifo_len /2 ) -1 ); + +dw_spi_g2_enable (dws, true); + +local_irq_disable (); +/* Polling-based full-duplex transfer */ +while (dws->tx_len || dws->rx_len) { + sr =dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ); + +/* Transmit if FIFO not full and bytes remaining */ +if (dws->tx_len && (sr &DW_SPI_G2_SR_TFNF )) { +u8 val =0 ; + +if (dws->tx) { + val = *(u8 *)dws->tx; + dws->tx +=1 ; + } +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , val); + dws->tx_len--; + } + +/* Receive if FIFO not empty and bytes expected */ +if (dws->rx_len && (sr &DW_SPI_G2_SR_RFNE )) { +u8 val =dw_spi_g2_readl (dws->regs,DW_SPI_G2_DR0 );
drivers/spi/spi-dw-g2-core.c line 846
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+ +if (dws->rx) { + *(u8 *)dws->rx = val; + dws->rx +=1 ; + } + dws->rx_len--; + } +cpu_relax (); + } +local_irq_enable ();
[Severity: Medium]
Should dw_spi_g2_transfer_one() and dw_spi_g2_update_config() be removed since ctlr->transfer_one is never wired up and neither symbol is exported?
If dw_spi_g2_transfer_one() is ever called, it never enables the target CS block or asserts DW_SPI_G2_SER, so the hardware never shifts out the TX FIFO and the unbounded loop under local_irq_disable() will hard-lock the CPU waiting for DW_SPI_G2_SR_RFNE. In addition, dw_spi_g2_update_config() writes per-CS registers without disabling the target CS block and divides by cfg->freq without guarding against 0.
Quoted 6 lines
drivers/spi/spi-dw-g2-mmio.c new file +0
-0@@ -0,0 +1,196 @@
[ ... ]
Quoted 2 lines
drivers/spi/spi-dw-g2-mmio.c new file +2
-0+static int dw_spi_g2_mmio_probe (struct platform_device *pdev) +{
[ ... ]
Quoted 15 lines
drivers/spi/spi-dw-g2-mmio.c new file +15
-0+/* +* IRQ. +* +* The DWC SSI Gen2 IP exposes per-source interrupt outputs +* alongside a combined ssi_intr. SoC integrators may route +* either the combined line, the individual lines, or any +* subset. We accept both DT forms: +* +* - Named-line DT: one entry per output, with interrupt-names +* in {"rxf","txe","txaf","wsf"}. Each line is requested +* and bound to the shared dw_spi_g2_irq() decoder. +* - Single-line DT: combined ssi_intr at index 0, used by +* integrations that OR the sources in HW. +* +* Either way dws->irq holds the "primary" line (rxf when
drivers/spi/spi-dw-g2-mmio.c line 74
Quoted 10 lines
drivers/spi/spi-dw-g2-mmio.c new file +10
-0+* named, index 0 otherwise) so debugfs and PM paths stay +* source-agnostic. +*/ + dws->irq =platform_get_irq_byname_optional (pdev,"rxf" ); +if (dws->irq <0 ) + dws->irq =platform_get_irq (pdev,0 ); +if (dws->irq <0 ) { + ret = dws->irq; + goto err_put; + }
[Severity: Medium]
Where are the "txe", "txaf", and "wsf" interrupt lines requested when named interrupts are used in Device Tree?
Both the commit message and the comment above state that each of {"rxf", "txe", "txaf", "wsf"} is requested and bound to dw_spi_g2_irq(), but dw_spi_g2_mmio_probe() only queries "rxf" (falling back to index 0 even if "rxf" returned -EPROBE_DEFER) and calls devm_request_irq() once for dws->irq.
[ ... ]
Quoted 15 lines
drivers/spi/spi-dw-g2-mmio.c new file +15
-0+reset_control_deassert (dwsmmio->rstc); + +/* Read num-cs from device tree */ + ret =device_property_read_u32 (&pdev->dev,"num-cs" , &num_cs); +if (ret) { +dev_err (&pdev->dev,"Failed to read 'num-cs' property: %d\n" , ret); + goto err_reset; + } + ctlr->num_chipselect = num_cs; + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DEDICATED_CS_EN , +GENMASK (ctlr->num_chipselect -1 ,0 )); +dw_spi_g2_enable (dws, false); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_IMR ,0 );
drivers/spi/spi-dw-g2-mmio.c line 123
Quoted 10 lines
drivers/spi/spi-dw-g2-mmio.c new file +10
-0+/* Clear all interrupts by writing 1s to defined ICR bits */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_ICR ,DW_SPI_G2_ICR_ALL ); + +/* +* Latch the static controller identity once. VERSION_ID and +* HW_CAPABILITIES are read-only and never change at runtime. +*/ + dws->version_id =dw_spi_g2_readl (dws->regs,DW_SPI_G2_VERSION_ID ); + dws->hw_caps =dw_spi_g2_readl (dws->regs,DW_SPI_G2_HW_CAPABILITIES ); + dws->caps = dws->hw_caps;
[Severity: Medium]
Can an omitted or out-of-range num-cs property break probe or trigger undefined shift behavior in GENMASK(ctlr->num_chipselect - 1, 0)?
In Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml, num-cs is optional with a valid range of 1..4, so probe should default num-cs when absent and reject values outside 1..4 before evaluating GENMASK() or indexing per-CS register blocks. Also, the return value of reset_control_deassert() is not checked before accessing MMIO registers, and the writes to DW_SPI_G2_DEDICATED_CS_EN and dws->caps = dws->hw_caps here are immediately overwritten with 0 by dw_spi_g2_init() below.
[ ... ]
Quoted 17 lines
drivers/spi/spi-dw-g2-mmio.c new file +17
-0+/* core init */ + ret =dw_spi_g2_init (dws); +if (ret) + goto err_reset; + + ret =devm_request_irq (&pdev->dev, dws->irq, dw_spi_g2_irq,0 , +dev_name (&pdev->dev), dws); +if (ret) + goto err_reset; + +pm_runtime_enable (&pdev->dev); + + ret =spi_register_controller (ctlr); +if (ret) + goto err_pm; + +dev_info (&pdev->dev,"DW SSI G2 MMIO probed\n" );
drivers/spi/spi-dw-g2-mmio.c line 164
Quoted 10 lines
drivers/spi/spi-dw-g2-mmio.c new file +10
-0+return 0 ; + +err_pm: +pm_runtime_disable (&pdev->dev); +err_reset: +reset_control_assert (dwsmmio->rstc); +err_put: +spi_controller_put (ctlr); +return ret; +}
[Severity: Medium]
Does failing at devm_request_irq() or spi_register_controller() leak the debugfs directory created by dw_spi_g2_init() and leave a dangling &dws->regset pointer after spi_controller_put(ctlr) frees ctlr?
Because err_pm and err_reset do not call dw_spi_g2_deinit(dws), /sys/kernel/debug/dw_spi_g2.<bus_num>/registers remains registered pointing to freed memory, causing a use-after-free if read.
[Severity: Medium]
Can dw_spi_g2_irq() fire after reset_control_assert() and spi_controller_put(ctlr) on the err_pm error path (or after reset_control_assert() in dw_spi_g2_mmio_remove())?
dws->irq is requested with devm_request_irq(), which is not freed until after probe() or remove() returns, while ctlr (containing dws) is allocated with the non-devm spi_alloc_host() and freed immediately at err_put, and dwsmmio->rstc is asserted before devres releases the IRQ.
drivers/spi/spi-dw-g2-mmio.c line 175
Quoted 11 lines
drivers/spi/spi-dw-g2-mmio.c new file +11
-0+ +static void dw_spi_g2_mmio_remove (struct platform_device *pdev) +{ +struct spi_controller *ctlr =platform_get_drvdata (pdev); +struct dw_spi_g2_mmio *dwsmmio =spi_controller_get_devdata (ctlr); + +spi_unregister_controller (ctlr); +dw_spi_g2_deinit (&dwsmmio->dws); +pm_runtime_disable (&pdev->dev); +reset_control_assert (dwsmmio->rstc); +}
[Severity: Medium]
Does dw_spi_g2_mmio_remove() leak ctlr and dwsmmio?
ctlr is allocated with spi_alloc_host() rather than devm_spi_alloc_host(), and spi_unregister_controller() only drops the registration reference from device_add(), so the initial allocation reference is never released without a matching spi_controller_put(ctlr) (or switching probe to devm_spi_alloc_host()).
Quoted 7 lines
drivers/spi/spi-dw-g2.h new file +1
-0@@ -0,0 +1,396 @@ +/* SPDX-License-Identifier: GPL-2.0-only OR MIT */
drivers/spi/spi-dw-g2.h line 11
Quoted 10 lines
drivers/spi/spi-dw-g2.h new file +10
-0+ +#ifndef _SPI_DW_G2_H +#define _SPI_DW_G2_H + +#include <linux/spi/spi.h> +#include <linux/io.h> +#include <linux/device.h> +#include <linux/irqreturn.h> +#include <linux/iopoll.h> +#include <linux/spi/spi-mem.h>
[Severity: Medium]
Does drivers/spi/spi-dw-g2-core.c fail to build at this commit on architectures that do not transitively include linux/bitfield.h?
dw_spi_g2_build_ctrlr0() and dw_spi_g2_update_config() use FIELD_PREP(), but <linux/bitfield.h> is only added to drivers/spi/spi-dw-g2.h later in commit "spi: dw-g2: Add XIP support", breaking bisection on architectures such as x86_64 and riscv.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Mark Brown
maintainerRe: [PATCH 2/6] spi: dw-g2: Add Synopsys DWC SSI v2.02a driver
In reply to Prakriti SharmaQuoted 8 lines
Add a driver for the Synopsys DesignWare SSI Gen2 (
DWC_ssiv2.02a) controller. Support probe, the MMIO glue, command-only transfers, and short register operations.Transfers are polled. The IRQ is claimed, including the optional per-source rxf, txe, txaf, and wsf lines.
+F: drivers/spi/spi-dw-g2-*.[ch]
There's drivers/spi/spi-dw-g2.h too.
Quoted 5 lines
+/*
+ * ========================================================================
+ * DebugFS support - exposes controller registers for debugging
+ * ========================================================================
+ */
If you wanrelatively heavyweight stuff like this consider using regmap; I'd suggest dropping it from the initial submission in any casse.
drivers/spi/spi-dw-g2-core.c line 135
Quoted 9 lines
drivers/spi/spi-dw-g2-core.c new file +9
-0+/* +* Clamp speed to achievable range. If requested speed is below +* hardware minimum, clamp to min_speed +*/ +if (speed < min_speed) { +dev_warn (&spi->dev,"speed %u Hz below minimum %u Hz, clamping\n" , + speed, min_speed); + speed = min_speed; + }
dev_warn_once(), this might happen a lot.
Quoted 15 lines
drivers/spi/spi-dw-g2-core.c new file +15
-0+/** +* dw_spi_g2_xfer_fifo - Low-level FIFO transfer using polling +* @dws: Driver state +* @tx_buf: TX buffer (or NULL if no TX) +* @tx_len: Number of bytes to transmit +* @rx_buf: RX buffer (or NULL if no RX expected) +* @rx_len: Number of bytes to receive +* +* Performs a polled FIFO transfer. Writes tx_len bytes from tx_buf to DR0, +* checking SR.TFNF (TX FIFO not full) before each write. For RX, waits for +* RXFLR to indicate data availability, then reads rx_len bytes from DR0. +* +* Return: 0 on success, -ETIMEDOUT on timeout +*/ +static int dw_spi_g2_xfer_fifo(struct dw_spi_g2 *dws,
drivers/spi/spi-dw-g2-core.c line 324
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+ const u8 *tx_buf, u32 tx_len, + u8 *rx_buf, u32 rx_len) +{ + u32 tx_sent = 0; +u32 rx_got =0 ; +u32 sr; +unsigned long timeout; + +/* TX phase: push bytes checking TFNF */ + timeout = jiffies +msecs_to_jiffies (1000 );
We usually scale our timeouts based on the length and speed of the transfer.
drivers/spi/spi-dw-g2-core.c line 331
Quoted 7 lines
drivers/spi/spi-dw-g2-core.c new file +7
-0+while (tx_sent < tx_len) { + sr =dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ); +if (sr &DW_SPI_G2_SR_TFNF ) { +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , + tx_buf[tx_sent]); + tx_sent++; + timeout = jiffies +msecs_to_jiffies (1000 );
If this timeout is being applied per word written then a second feels like an extravagant timeout.
Quoted 9 lines
drivers/spi/spi-dw-g2-core.c new file +9
-0+/* RX phase */ + timeout = jiffies +msecs_to_jiffies (1000 ); +while (rx_got < rx_len) { + sr =dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ); +if (sr &DW_SPI_G2_SR_RFNE ) { + rx_buf[rx_got++] =dw_spi_g2_readl (dws->regs, +DW_SPI_G2_DR0 ) & +0xff ; + timeout = jiffies +msecs_to_jiffies (1000 );
drivers/spi/spi-dw-g2-core.c line 364
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+ }else if (time_after (jiffies, timeout)) { +dev_err (dws->dev, +"xfer_fifo: RX timeout after %u/%u bytes (SR=0x%08x RXFLR=%u)\n" , + rx_got, rx_len, +dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ), +dw_spi_g2_readl (dws->regs,DW_SPI_G2_RXFLR )); +return -ETIMEDOUT ; + } +cpu_relax (); + }
This looks like it supports only half duplex transfers, is that a hardware limitation or a software one? I'd really expect this to be a transfer_one() callback.
Quoted 4 lines
drivers/spi/spi-dw-g2-core.c new file +4
-0+/** +* dw_spi_g2_fill_tx_buf - Serialize command/address/data into TX buffer +* @buf: Destination buffer +* @op: SPI memory operation
drivers/spi/spi-dw-g2-core.c line 383
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+* @include_data: true to include data bytes, false for command+address only +* +* Builds a byte stream from a spi_mem_op structure: +* [opcode bytes MSB..LSB] [address bytes MSB..LSB] [data bytes (optional)]. +* +* Return: Total number of bytes written to buf +*/ +static u32 dw_spi_g2_fill_tx_buf (u8 *buf,const struct spi_mem_op *op, +bool include_data) +{
This looks like the driver is trying to open code the support for running unaccelerated ops that spi_mem_exec_op() has?
drivers/spi/spi-dw-g2-core.c line 687
Quoted 1 line
drivers/spi/spi-dw-g2-core.c new file +1
-0+dev_info (dws->dev,"DW SSI G2 core init\n" );
Drop this, it's just noise.
Quoted 5 lines
+/*
+ * ========================================================================
+ * Legacy SPItransfer_oneinterface (not actively used with spi-mem)
+ * ========================================================================
+ */
It should be, if only so you can avoid the open coding above.
drivers/spi/spi-dw-g2-core.c line 819
Quoted 7 lines
drivers/spi/spi-dw-g2-core.c new file +6
-0+int dw_spi_g2_transfer_one (struct spi_controller *ctlr,struct spi_device *spi, +struct spi_transfer *xfer) +{ +local_irq_disable (); +/* Polling-based full-duplex transfer */ +while (dws->tx_len || dws->rx_len) {
This needs a *really* good reason to run with interrupts disabled, that's very rude to the rest of the system. Especially to run with interrupts disabled for the whole length of the transfer.
Quoted 8 lines
drivers/spi/spi-dw-g2-core.c new file +8
-0+irqreturn_t dw_spi_g2_irq (int irq,void *dev_id) +{ +struct dw_spi_g2 *dws = dev_id; +u32 isr; + + isr =dw_spi_g2_readl (dws->regs,DW_SPI_G2_ISR ); +if (!isr) +return IRQ_NONE ;
drivers/spi/spi-dw-g2-core.c line 889
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c new file +10
-0+ +/* Check for error conditions */ +if (isr & (DW_SPI_G2_ISR_RXOI |DW_SPI_G2_ISR_RXUI | +DW_SPI_G2_ISR_TXOI |DW_SPI_G2_ISR_TXUI )) { +dev_warn (dws->dev,"Error interrupt: ISR=0x%x\n" , isr); + dws->error = -EIO ; + } + +/* Clear all interrupts by writing 1s to defined ICR bits */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_ICR ,DW_SPI_G2_ICR_ALL );
It would be better to only clear interrupts that the driver handled, this allows the genirq handling for things like stuck interrupts to do the right thing.
drivers/spi/spi-dw-g2-mmio.c line 70
Quoted 3 lines
drivers/spi/spi-dw-g2-mmio.c new file +3
-0+ dws->irq =platform_get_irq_byname_optional (pdev,"rxf" ); +if (dws->irq <0 ) + dws->irq =platform_get_irq (pdev,0 );
The DT binding said that the interrupt line was optional and the driver is polling based so the software seems to support that but this won't handle a missing interrupt gracefully. Either update the binding or the driver. TBH I'd make the interrupt mandatory, this seems to be a relatively high end IP and I'm not sure anyone is going to implement it without interrupts and I'd imagine at least the spi-mem stuff will work more efficiently with interrupts.
drivers/spi/spi-dw-g2-mmio.c line 107
Quoted 6 lines
drivers/spi/spi-dw-g2-mmio.c new file +6
-0+ ret =device_property_read_u32 (&pdev->dev,"num-cs" , &num_cs); +if (ret) { +dev_err (&pdev->dev,"Failed to read 'num-cs' property: %d\n" , ret); + goto err_reset; + } + ctlr->num_chipselect = num_cs;
You should validate num_cs.
drivers/spi/spi-dw-g2-mmio.c line 175
Quoted 10 lines
drivers/spi/spi-dw-g2-mmio.c new file +10
-0+static void dw_spi_g2_mmio_remove (struct platform_device *pdev) +{ +struct spi_controller *ctlr =platform_get_drvdata (pdev); +struct dw_spi_g2_mmio *dwsmmio =spi_controller_get_devdata (ctlr); + +spi_unregister_controller (ctlr); +dw_spi_g2_deinit (&dwsmmio->dws); +pm_runtime_disable (&pdev->dev); +reset_control_assert (dwsmmio->rstc); +}
You're mixing devm and non-devm things here so for example the controller will be deregistered prior to the interrupt handler which will go badly if the interrupt fires in the interim. Also note that the devdata is allocated along with the controller so when you free the controller you should no longer dereference dwsmmio.
drivers/spi/spi-dw-g2.h line 383
Quoted 10 lines
drivers/spi/spi-dw-g2.h new file +10
-0+/* +* Transfer control: enable CS register block, assert CS line, enable SSI. +* Starts the SPI transfer — controller begins clocking data. +*/ +static inline void dw_spi_g2_transfer_begin (struct dw_spi_g2 *dws) +{ +dw_spi_g2_target_enable (dws, dws->cs); +dw_spi_g2_cs_assert (dws, dws->cs); +dw_spi_g2_enable (dws, true); +}
The chip select should be controlled at the start/end of a message, not a transfer (and should just be a set_cs() operation).
Signature · 1 line
Mark Brown <[email protected]>
PATCH 3/6 spi: dw-g2: Add indirect read and write
In reply to Prakriti SharmaAdd the FIFO indirect path used for spi-mem data transfers that are not short register operations.
Reads use receive-only mode. Writes use transmit-only mode and send the command, address, and payload as one continuous FIFO stream.
--- drivers/spi/spi-dw-g2-core.c | 242 ++++++++++++++++++++++++++++++++++- 1 file changed, 236 insertions(+), 6 deletions(-)
drivers/spi/spi-dw-g2-core.c +236 -6
@@ -538,6 +538,237 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)* Indirect read operations */ +/** +* dw_spi_g2_indirect_read_setup - Configure controller for indirect read +* @dws: Driver state +* @op: SPI memory operation +* +* Sets up TMOD=RO with enhanced SPI framing. +* +* Return: 0 on success, negative error code on failure +*/ +static int dw_spi_g2_indirect_read_setup (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_RO , +dw_spi_g2_get_frf (op)); +u32 spictrlr0 =0 ; +u32 dummy_cycles =0 ; +int tt; + +if (op->dummy.nbytes) { +if (!op->dummy.buswidth || op->dummy.buswidth >8 ) { +dev_err (dws->dev,"Invalid dummy buswidth %u\n" , + op->dummy.buswidth); +return -EINVAL ; + } + dummy_cycles = (op->dummy.nbytes *8 ) / op->dummy.buswidth; +if (dummy_cycles >DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX ) { +dev_err (dws->dev,"Dummy cycles %u exceed maximum %u\n" , + dummy_cycles,DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX ); +return -EINVAL ; + } + } + + tt =dw_spi_g2_get_tt (op); +if (tt <0 ) { +dev_err (dws->dev,"Unsupported transfer type: cmd=%u addr=%u data=%u\n" , + op->cmd.buswidth, + op->addr.nbytes ? op->addr.buswidth :0 , + op->data.nbytes ? op->data.buswidth :0 ); +return tt; + } + +dw_spi_g2_controller_reset (dws); +dw_spi_g2_target_disable (dws, dws->cs); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs), + op->data.nbytes -1 ); + + spictrlr0 |=FIELD_PREP (DW_SPI_G2_SPI_CTRLR0_TRANS_TYPE_MASK , tt); + spictrlr0 |=FIELD_PREP (DW_SPI_G2_SPI_CTRLR0_INST_L_MASK , +dw_spi_g2_inst_len (op->cmd.nbytes)); + spictrlr0 |=FIELD_PREP (DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK , +dw_spi_g2_addr_len (op->addr.nbytes)); + spictrlr0 |=FIELD_PREP (DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLE_MASK , + dummy_cycles); + +if (op->cmd.dtr) + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK ; +if (op->addr.dtr || op->data.dtr) + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK ; +if (op->data.dtr) + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK ; + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs), spictrlr0); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_RXFTLR , +min_t (u32, (dws->fifo_len /2 ) -1 , + op->data.nbytes ? op->data.nbytes :1 )); +return 0 ; +} + +/** +* dw_spi_g2_indirect_read - Execute indirect read operation +* @dws: Driver state +* @op: SPI memory operation +* +* Reads data from flash using RO (receive-only) mode. +* Return: 0 on success, negative error code on failure +*/ +static int dw_spi_g2_indirect_read (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 tx_entries; +u8 dummy_buf =0 ; +int ret; + +if (!op->data.nbytes) { +dev_err (dws->dev,"Zero-length read not allowed\n" ); +return -EINVAL ; + } + +if (!op->data.buf.in) { +dev_err (dws->dev,"RX buffer is NULL\n" ); +return -EINVAL ; + } + + ret =dw_spi_g2_indirect_read_setup (dws, op); +if (ret) +return ret; + +dw_spi_g2_target_enable (dws, dws->cs); +dw_spi_g2_enable (dws, true); + + tx_entries =0 ; + +/* Write instruction */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , op->cmd.opcode); + tx_entries++; + +/* Write address as a packed 32-bit value (one FIFO entry) */ +if (op->addr.nbytes) { +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , (u32 )op->addr.val); + tx_entries++; + } + +/* Assert CS - controller begins: sends TX FIFO, then receives */ +dw_spi_g2_cs_assert (dws, dws->cs); + +/* Receive data via polling */ + ret =dw_spi_g2_xfer_fifo (dws, &dummy_buf,0 , + op->data.buf.in, op->data.nbytes); +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); + +return ret; +} + +/** +* Indirect write operations (Page Program) +* Uses TMOD=TO (Transmit-Only) mode +*/ + +/** +* dw_spi_g2_indirect_write_setup - Configure controller for indirect write +* @dws: Driver state +* @op: SPI memory operation +* +* Sets up TMOD=TO (transmit-only), standard SPI mode. +*/ +static void dw_spi_g2_indirect_write_setup (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TO , +DW_SPI_G2_FRF_STANDARD ); + +dw_spi_g2_controller_reset (dws); +dw_spi_g2_target_disable (dws, dws->cs); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_RXFTLR ,0 ); +} + +/** +* dw_spi_g2_indirect_write - Execute indirect write operation +* @dws: Driver state +* @op: SPI memory operation +* +* Writes data to flash using TO (transmit-only) mode. The controller sends +* instruction, address, and data bytes as a continuous stream. +* +* Return: 0 on success, negative error code on failure +*/ +static int dw_spi_g2_indirect_write (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 tx_total = op->cmd.nbytes + op->addr.nbytes + op->data.nbytes; +u8 *tx_buf; +u32 prefill, pos; + +if (!op->data.nbytes) { +dev_err (dws->dev,"Zero-length write not allowed\n" ); +return -EINVAL ; + } + +if (!op->data.buf.out) { +dev_err (dws->dev,"TX buffer is NULL\n" ); +return -EINVAL ; + } + +dw_spi_g2_indirect_write_setup (dws, op); + +/* Allocate and build TX buffer: [cmd][addr][data] */ + tx_buf =kzalloc (tx_total,GFP_KERNEL ); +if (!tx_buf) +return -ENOMEM ; + +dw_spi_g2_fill_tx_buf (tx_buf, op, true); + +/* +* Split enable sequence: +* Enable SSI (without CS), pre-fill TX FIFO, then assert CS. +* FIFO pointers are reset while SSIENR=0, so writes before +* enabling would be discarded. +*/ +dw_spi_g2_target_enable (dws, dws->cs); +dw_spi_g2_enable (dws, true); + +/* Pre-fill FIFO to capacity or total size */ + prefill =min_t (u32, tx_total, dws->fifo_len); +for (pos =0 ; pos < prefill; pos++) +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , tx_buf[pos]); + +/* Assert CS - transfer starts */ +dw_spi_g2_cs_assert (dws, dws->cs); + +/* Refill FIFO with remaining bytes (if any) via polling */ +if (pos < tx_total) { +int ret =dw_spi_g2_xfer_fifo (dws, &tx_buf[pos], + tx_total - pos,NULL ,0 ); +if (ret) { +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); +kfree (tx_buf); +return ret; + } + } + +/* Wait for transfer completion */ +dw_spi_g2_wait_idle (dws); + +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); + +kfree (tx_buf); +return 0 ; +} +/* * ======================================================================== * SPI-MEM interface
@@ -626,12 +857,11 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,if (dw_spi_g2_is_reg_op (op))return dw_spi_g2_reg_op (dws, op); -/* -* Bulk reads and writes are added by the indirect-transfer patch. -* This patch handles commands and short register operations only. -*/ -if (op->data.dir !=SPI_MEM_NO_DATA && !dw_spi_g2_is_reg_op (op)) -return -EOPNOTSUPP ; +if (op->data.dir ==SPI_MEM_DATA_IN ) +return dw_spi_g2_indirect_read (dws, op); + +if (op->data.dir ==SPI_MEM_DATA_OUT ) +return dw_spi_g2_indirect_write (dws, op);return -EOPNOTSUPP ; }
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 3/6] spi: dw-g2: Add indirect read and write
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] spi: dw-g2: multi-line writes sent in 1-1-1 mode in dw_spi_g2_indirect_write() - [High] spi: dw-g2…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] spi: dw-g2: multi-line writes sent in 1-1-1 mode in dw_spi_g2_indirect_write()
- [High] spi: dw-g2: premature TX termination and ignored timeout in dw_spi_g2_indirect_write()
- [Medium] spi: dw-g2: 64-bit address truncated to u32 in dw_spi_g2_indirect_read()
- [Medium] spi: dw-g2: dw_spi_g2_exec_mem_op() routes non-1-1-1 ops to dw_spi_g2_reg_op()
- [Medium] spi: dw-g2: missing CPOL/CPHA setup in dw_spi_g2_build_ctrlr0()
- [Low] spi: dw-g2: unused variable tx_entries in dw_spi_g2_indirect_read()
- [Low] spi: dw-g2: malformed kernel-doc comment above dw_spi_g2_indirect_write_setup()
--
commit 5c69b8ce5159adba9406d4ca9e6fadba79d41ae5
Author: Prakriti Sharma <[email protected]>
spi: dw-g2: Add indirect read and write
This commit adds the FIFO-based indirect read (receive-only) and indirect write (transmit-only) paths for spi-mem data transfers.
Quoted 12 lines
drivers/spi/spi-dw-g2-core.c +4
-0@@ -538,6 +538,237 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)* Indirect read operations */ +/** +* dw_spi_g2_indirect_read_setup - Configure controller for indirect read +* @dws: Driver state +* @op: SPI memory operation
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op) · line 554
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+* +* Sets up TMOD=RO with enhanced SPI framing. +* +* Return: 0 on success, negative error code on failure +*/ +static int dw_spi_g2_indirect_read_setup (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_RO , +dw_spi_g2_get_frf (op));
[Severity: Medium]
Does dw_spi_g2_build_ctrlr0() omit DW_SPI_G2_CTRLR0_SCPOL and DW_SPI_G2_CTRLR0_SCPHA from ctrlr0?
The controller advertises SPI_CPOL | SPI_CPHA in ctlr->mode_bits, but dw_spi_g2_build_ctrlr0() never checks dws->curr_spi->mode, so indirect reads and writes always program CTRLR0 for SPI Mode 0 and fail on flash devices configured for SPI Mode 1, 2, or 3.
[ ... ]
Quoted 6 lines
drivers/spi/spi-dw-g2-core.c +6
-0+static int dw_spi_g2_indirect_read (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 tx_entries; +u8 dummy_buf =0 ; +int ret;
[ ... ]
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op) · line 655
Quoted 11 lines
drivers/spi/spi-dw-g2-core.c +11
-0+ tx_entries =0 ; + +/* Write instruction */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , op->cmd.opcode); + tx_entries++; + +/* Write address as a packed 32-bit value (one FIFO entry) */ +if (op->addr.nbytes) { +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , (u32 )op->addr.val); + tx_entries++; + }
[Severity: Low]tx_entries is incremented here but never read, which triggers a -Wunused-but-set-variable warning under W=1 builds.
[Severity: Medium]
Does casting op->addr.val to u32 and writing a single DR0 entry break reads withop->addr.nbytes > 4?
dw_spi_g2_exec_mem_op() allows addresses up to DW_SPI_G2_ADDR_L_MAX_BITS (60 bits) and dw_spi_g2_indirect_read_setup() programs ADDR_L for the full op->addr.nbytes (and commit "spi: dw-g2: Add XIP and dirmap support" folds dummy bytes into op.addr.nbytes before calling dw_spi_g2_indirect_read()). When ADDR_L > 8 (more than 32 bits), the controller expects two 32-bit address entries in the TX FIFO before starting the receive phase, so writing only one truncated 32-bit word stalls the transfer until dw_spi_g2_xfer_fifo() times out.
[ ... ]
Quoted 4 lines
+/**
+ * Indirect write operations (Page Program)
+ * Uses TMOD=TO (Transmit-Only) mode
+ */
[Severity: Low]
This section comment starts with /** instead of /*, which causes a scripts/kernel-doc warning on W=1 builds.
Quoted 12 lines
drivers/spi/spi-dw-g2-core.c +12
-0+/** +* dw_spi_g2_indirect_write_setup - Configure controller for indirect write +* @dws: Driver state +* @op: SPI memory operation +* +* Sets up TMOD=TO (transmit-only), standard SPI mode. +*/ +static void dw_spi_g2_indirect_write_setup (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TO , +DW_SPI_G2_FRF_STANDARD );
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op) · line 695
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+ +dw_spi_g2_controller_reset (dws); +dw_spi_g2_target_disable (dws, dws->cs); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_RXFTLR ,0 ); +}
[Severity: High]
Can dw_spi_g2_indirect_write_setup() and dw_spi_g2_indirect_write() send multi-line write operations in single-wire 1-1-1 mode?
dw_spi_g2_init() sets SPI_TX_DUAL | SPI_TX_QUAD | SPI_TX_OCTAL in ctlr->mode_bits and dw_spi_g2_supports_mem_ops() delegates directly to spi_mem_default_supports_op(), so spi-mem negotiates multi-wire Page Program operations (such as 1-1-4 or 1-4-4 Quad writes). However, dw_spi_g2_indirect_write_setup() hardcodes DW_SPI_G2_FRF_STANDARD with DW_SPI_G2_SPI_CTRLR0 set to 0, and dw_spi_g2_indirect_write() streams [cmd][addr][data] over IO0 without multi-line framing or dummy cycles, corrupting the data written to flash.
[ ... ]
Quoted 14 lines
drivers/spi/spi-dw-g2-core.c +14
-0+/* Assert CS - transfer starts */ +dw_spi_g2_cs_assert (dws, dws->cs); + +/* Refill FIFO with remaining bytes (if any) via polling */ +if (pos < tx_total) { +int ret =dw_spi_g2_xfer_fifo (dws, &tx_buf[pos], + tx_total - pos,NULL ,0 ); +if (ret) { +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); +kfree (tx_buf); +return ret; + } + }
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op) · line 770
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+ +/* Wait for transfer completion */ +dw_spi_g2_wait_idle (dws); + +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); + +kfree (tx_buf); +return 0 ; +}
[Severity: High]
Can dw_spi_g2_wait_idle() return before the pre-filled TX FIFO has drained, and does ignoring its return value hide transfer timeouts?
drivers/spi/spi-dw-g2.h:dw_spi_g2_wait_idle() {
u32 sr;
return readl_poll_timeout(dws->regs + DW_SPI_G2_SR, sr,
!(sr & DW_SPI_G2_SR_BUSY), 1, DW_SPI_G2_TIMEOUT_US);
}When tx_total <= dws->fifo_len, dw_spi_g2_indirect_write() asserts CS and immediately calls dw_spi_g2_wait_idle(). Because SER is written in the APB clock domain while frames begin shifting on the divided ssi_clk, DW_SPI_G2_SR_BUSY can still read 0 before the first byte is popped from the TX FIFO (while DW_SPI_G2_SR_TFE is 0), causing dw_spi_g2_transfer_end() to disable SSIENR and discard the queued write. In addition, both dw_spi_g2_indirect_write() and dw_spi_g2_xfer_fifo() ignore the -ETIMEDOUT return value of dw_spi_g2_wait_idle(). Commit "spi: dw-g2: Add SPI-NAND support" later routes indirect writes through dw_spi_g2_xfer_fifo(), which still leaves dw_spi_g2_wait_idle() missing a DW_SPI_G2_SR_TFE check and its return value unchecked.
Quoted 8 lines
drivers/spi/spi-dw-g2-core.c +0
-4@@ -626,12 +857,11 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,if (dw_spi_g2_is_reg_op (op))return dw_spi_g2_reg_op (dws, op); -/* -* Bulk reads and writes are added by the indirect-transfer patch. -* This patch handles commands and short register operations only. -*/
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_exec_mem_op(struct spi_mem *mem, · line 867
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +5
-2-if (op->data.dir !=SPI_MEM_NO_DATA && !dw_spi_g2_is_reg_op (op)) -return -EOPNOTSUPP ; +if (op->data.dir ==SPI_MEM_DATA_IN ) +return dw_spi_g2_indirect_read (dws, op); + +if (op->data.dir ==SPI_MEM_DATA_OUT ) +return dw_spi_g2_indirect_write (dws, op);return -EOPNOTSUPP ; }
[Severity: Medium]
Does checking dw_spi_g2_is_reg_op() before the indirect paths misroute short non-1-1-1 operations to dw_spi_g2_reg_op()?
drivers/spi/spi-dw-g2-core.c:dw_spi_g2_is_reg_op() {
return !op->addr.nbytes && op->data.nbytes > 0 &&
op->data.nbytes <= DW_SPI_G2_REG_OP_MAX_DATA;
}dw_spi_g2_is_reg_op() does not check that op->cmd.nbytes == 1, op->dummy.nbytes == 0, all phase buswidths are 1, and DTR is disabled, whereas dw_spi_g2_reg_op() only sends a 1-byte opcode in 1-1-1 standard SPI mode without dummy cycles. Short operations using dummy cycles, 2-byte opcodes, multi-wire buswidths, or DTR are sent through dw_spi_g2_reg_op() instead of dw_spi_g2_indirect_read(), and commit "spi: dw-g2: Add SPI-NAND support" widens dw_spi_g2_is_reg_op() to match addressed operations up to fifo_len bytes with the same 1-1-1 restriction.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
kernel test robot
build botRe: [PATCH 3/6] spi: dw-g2: Add indirect read and write
In reply to Prakriti Sharmakernel test robot noticed the following build warnings:
Hi Prakriti,
kernel test robot noticed the following build warnings:
[auto build test WARNING on broonie-spi/for-next]
[also build test WARNING on robh/for-next linus/master v7.4-rc3 next-20261116] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Prakriti-Sharma/dt-bindings-spi-Add-Synopsys-DWC-SSI-v2-02a-binding/20261119-153508 base: https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next patch link: https://lore.kernel.org/r/20261119100513.433044-4-prakriti%40synopsys.com patch subject: [PATCH 3/6] spi: dw-g2: Add indirect read and write config: m68k-allyesconfig (https://download.01.org/0day-ci/archive/20261120/[email protected]/config) compiler: m68k-linux-gcc (GCC) 16.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261120/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
drivers/spi/spi-dw-g2-core.c: In function 'dw_spi_g2_build_ctrlr0':
drivers/spi/spi-dw-g2-core.c:226:13: error: implicit declaration of function 'FIELD_PREP' [-Wimplicit-function-declaration]
226 | FIELD_PREP(DW_SPI_G2_CTRLR0_DFS_MASK, DW_SPI_G2_DFS_ENCODE(dfs));
| ^~~~~~~~~~
drivers/spi/spi-dw-g2-core.c: In function 'dw_spi_g2_indirect_read':
>> drivers/spi/spi-dw-g2-core.c:624:13: warning: variable 'tx_entries' set but not used [-Wunused-but-set-variable=]
624 | u32 tx_entries;
| ^~~~~~~~~~vim +/tx_entries +624 drivers/spi/spi-dw-g2-core.c
612
613 /**
614 * dw_spi_g2_indirect_read - Execute indirect read operation
615 * @dws: Driver state
616 * @op: SPI memory operation
617 *
618 * Reads data from flash using RO (receive-only) mode.
619 * Return: 0 on success, negative error code on failure
620 */
621 static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,
622 const struct spi_mem_op *op)
623 {
> 624 u32 tx_entries;
625 u8 dummy_buf = 0;
626 int ret;
627
628 if (!op->data.nbytes) {
629 dev_err(dws->dev, "Zero-length read not allowed\n");
630 return -EINVAL;
631 }
632
633 if (!op->data.buf.in) {
634 dev_err(dws->dev, "RX buffer is NULL\n");
635 return -EINVAL;
636 }
637
638 ret = dw_spi_g2_indirect_read_setup(dws, op);
639 if (ret)
640 return ret;
641
642 dw_spi_g2_target_enable(dws, dws->cs);
643 dw_spi_g2_enable(dws, true);
644
645 tx_entries = 0;
646
647 /* Write instruction */
648 dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, op->cmd.opcode);
649 tx_entries++;
650
651 /* Write address as a packed 32-bit value (one FIFO entry) */
652 if (op->addr.nbytes) {
653 dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0, (u32)op->addr.val);
654 tx_entries++;
655 }
656
657 /* Assert CS - controller begins: sends TX FIFO, then receives */
658 dw_spi_g2_cs_assert(dws, dws->cs);
659
660 /* Receive data via polling */
661 ret = dw_spi_g2_xfer_fifo(dws, &dummy_buf, 0,
662 op->data.buf.in, op->data.nbytes);
663 dw_spi_g2_transfer_end(dws);
664 dw_spi_g2_controller_reset(dws);
665
666 return ret;
667 }
668
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH 4/6 spi: dw-g2: Add SPI-NAND support
In reply to Prakriti SharmaHandle SPI-NAND flash in the same spi-mem path as SPI-NOR.
NAND uses a 2-byte address and program-load writes land in the page buffer, so short operations such as JEDEC ID and status reads take the register path. A short delay after chip-select assertion, plus barriers around that write, keeps the ID read ordered on this integration.
Tested with a Winbond W25N01JW SPI-NAND flash.
--- drivers/spi/spi-dw-g2-core.c | 370 +++++++++++++++++++++++++---------- drivers/spi/spi-dw-g2.h | 20 +- 2 files changed, 277 insertions(+), 113 deletions(-)
drivers/spi/spi-dw-g2-core.c +265 -105
@@ -262,9 +262,13 @@ static u32 dw_spi_g2_get_frf(const struct spi_mem_op *op)* dw_spi_g2_get_tt - Determine transfer type from operation bus widths * @op: SPI memory operation * -* The transfer type (TT) field in SPI_CTRLR0 defines which phases use -* multi-line mode. TT0 = standard (1-1-1), TT1 = data multi-line (1-1-x), -* TT2 = addr+data multi-line (1-x-x). TT3 (x-x-x) is not supported. +* TRANS_TYPE in SPI_CTRLR0 / XIP_CTRL controls only the instruction and +* address phases. Data always uses the enhanced mode set by SPI_FRF in +* CTRLR0 (or FRF in XIP_CTRL). +* +* TT0 (0x0): instruction standard, address standard (1-1-x) +* TT1 (0x1): instruction standard, address enhanced (1-x-x) +* TT2 (0x2): instruction enhanced, address enhanced (x-x-x) * * Return: Transfer type value (0-2), or -EOPNOTSUPP for unsupported patterns */
@@ -272,29 +276,37 @@ static int dw_spi_g2_get_tt(const struct spi_mem_op *op) {bool cmd_m = (op->cmd.buswidth >1 );bool addr_m = (op->addr.nbytes && op->addr.buswidth >1 ); -bool data_m = (op->data.nbytes && op->data.buswidth >1 ); -if (!cmd_m && !addr_m && !data_m) -return DW_SPI_G2_TRANS_TYPE_TT0 ;/* 1-1-1 */ -if (!cmd_m && !addr_m && data_m) -return DW_SPI_G2_TRANS_TYPE_TT1 ;/* 1-1-x */ -if (!cmd_m && addr_m && data_m) -return DW_SPI_G2_TRANS_TYPE_TT2 ;/* 1-x-x */ +if (!cmd_m && !addr_m) +return DW_SPI_G2_TRANS_TYPE_TT0 ;/* 1-1-x */ +if (!cmd_m && addr_m) +return DW_SPI_G2_TRANS_TYPE_TT1 ;/* 1-x-x */ +if (cmd_m && addr_m) +return DW_SPI_G2_TRANS_TYPE_TT2 ;/* x-x-x */ return -EOPNOTSUPP ; }/** -* dw_spi_g2_is_reg_op - Check if operation is a register read/write +* dw_spi_g2_is_reg_op - Check if operation fits in TR (transmit-receive) mode +* @dws: Driver state (provides runtime fifo_len) * @op: SPI memory operation * -* Register operations are single-byte commands with no address (e.g., -* RDSR, WRSR, RDID). Data must fit within the fixed-size stack buffer. +* Routes operations through TR mode when total transfer size (cmd + addr + +* dummy + data) fits within the FIFO. This avoids RO mode with SPI_CTRLR0 +* (enhanced SPI framing) which some platforms don't fully support. +* +* Limit is min(fifo_len, compile-time buffer size) to stay within both +* the hardware FIFO and the stack-allocated buffers in reg_op(). */ -static inline bool dw_spi_g2_is_reg_op (const struct spi_mem_op *op) +static inline bool dw_spi_g2_is_reg_op (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) { -return !op->addr.nbytes && op->data.nbytes >0 && - op->data.nbytes <=DW_SPI_G2_REG_OP_MAX_DATA ; +u32 total = op->cmd.nbytes + op->addr.nbytes + + op->dummy.nbytes + op->data.nbytes; +u32 limit =min_t (u32, dws->fifo_len,DW_SPI_G2_REG_OP_BUF_SIZE ); + +return op->data.nbytes >0 && total <= limit; }/**
@@ -483,49 +495,132 @@ static void dw_spi_g2_reg_op_setup(struct dw_spi_g2 *dws,static int dw_spi_g2_reg_op (struct dw_spi_g2 *dws,const struct spi_mem_op *op) { -u8 tx_buf[DW_SPI_G2_REG_OP_MAX_DATA +1 ]; -u32 tx_len; -int ret; - -/* Validate data length fits in stack buffer */ -if (op->data.nbytes >DW_SPI_G2_REG_OP_MAX_DATA ) { -dev_err (dws->dev,"reg_op data length %u exceeds max %u\n" , - op->data.nbytes,DW_SPI_G2_REG_OP_MAX_DATA ); +u8 tx_buf[DW_SPI_G2_REG_OP_BUF_SIZE ]; +u32 tx_len =0 ; +u32 echo_bytes; +int ret =0 ; +u32 i; + +if (op->cmd.nbytes + op->addr.nbytes + op->dummy.nbytes + + op->data.nbytes >sizeof (tx_buf)) { +dev_err (dws->dev,"reg_op transfer too large: cmd=%u addr=%u dummy=%u data=%u\n" , + op->cmd.nbytes, op->addr.nbytes, op->dummy.nbytes, + op->data.nbytes);return -EINVAL ; } +dev_dbg (dws->dev,"reg_op: cmd=0x%02x dir=%d data_len=%u\n" , + op->cmd.opcode, op->data.dir, op->data.nbytes); +dw_spi_g2_reg_op_setup (dws, op); -dw_spi_g2_transfer_begin (dws); +/* Build complete TX buffer: [cmd] [addr] [dummy] [data/zeros] */ + tx_buf[tx_len++] = op->cmd.opcode; + +for (i = op->addr.nbytes; i >0 ; i--) + tx_buf[tx_len++] = (op->addr.val >> ((i -1 ) *8 )) &0xFF ;if (op->data.dir ==SPI_MEM_DATA_OUT ) {if (!op->data.buf.out) {dev_err (dws->dev,"TX buffer is NULL\n" );return -EINVAL ; } -/* TX: [opcode] [data bytes] */ - tx_buf[0 ] = op->cmd.opcode; -memcpy (&tx_buf[1 ], op->data.buf.out, op->data.nbytes); - tx_len =1 + op->data.nbytes; - ret =dw_spi_g2_xfer_fifo (dws, tx_buf, tx_len,NULL ,0 ); +memcpy (&tx_buf[tx_len], op->data.buf.out, op->data.nbytes); + tx_len += op->data.nbytes; }else {if (!op->data.buf.in) {dev_err (dws->dev,"RX buffer is NULL\n" );return -EINVAL ; } -/* -* TR mode: each TX byte produces one RX byte. -* Push opcode + nbytes dummy 0x00s, receive 1+nbytes, -* discard first RX byte (echo of opcode phase). -*/ -u8 rx_tmp[DW_SPI_G2_REG_OP_MAX_DATA +1 ]; - - tx_buf[0 ] = op->cmd.opcode; -memset (&tx_buf[1 ],0x00 , op->data.nbytes); - tx_len =1 + op->data.nbytes; - ret =dw_spi_g2_xfer_fifo (dws, tx_buf, tx_len, rx_tmp, tx_len); -if (!ret) -memcpy (op->data.buf.in, &rx_tmp[1 ], op->data.nbytes); +if (op->dummy.nbytes) { +memset (&tx_buf[tx_len],0x00 , op->dummy.nbytes); + tx_len += op->dummy.nbytes; + } +memset (&tx_buf[tx_len],0x00 , op->data.nbytes); + tx_len += op->data.nbytes; + } + + echo_bytes =1 + op->addr.nbytes + op->dummy.nbytes; + +dev_dbg (dws->dev,"reg_op: tx_len=%u echo=%u\n" , tx_len, echo_bytes); + +dw_spi_g2_target_enable (dws, dws->cs); +dw_spi_g2_enable (dws, true); + +/* +* Write TX data to FIFO with TFNF polling. +* Although tx_len is limited by dw_spi_g2_is_reg_op() to fifo_len, +* we still poll TFNF for safety and to handle any hardware quirks. +*/ +for (i =0 ; i < tx_len; i++) { +u32 timeout =DW_SPI_G2_TIMEOUT_US ; + +/* Wait for TX FIFO to have space */ +while (!(dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ) &DW_SPI_G2_SR_TFNF )) { +if (--timeout ==0 ) { +dev_err (dws->dev,"reg_op: TX FIFO full timeout\n" ); +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); +return -ETIMEDOUT ; + } +udelay (1 ); + } + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , tx_buf[i]); + } + +/* Ensure all FIFO writes complete before CS assertion */ +wmb (); + +dw_spi_g2_cs_assert (dws, dws->cs); + +/* +* CS settling delay for NAND flash: +* +* NAND flash requires a settling period after CS assertion before +* data transactions can begin reliably. This is a hardware timing requirement. +* +* This is specific to NAND flash behavior where CS-to-valid-data timing +* is more stringent than NOR flash. The delay applies to small +* register operations (reg_op path) which are commonly used for: +* - READID (0x9F) +* - GET_FEATURE (0x0F) +* - SET_FEATURE (0x1F) +* - READ_STATUS (0x0F with feature address) +* +* Note: Only applies to NAND (detected via DT "spi-nand" compatible) +* to avoid performance impact on NOR flash. +*/ +if (dws->is_nand) { +/* Force a register read to ensure CS assertion propagates */ + (void )dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ); +usleep_range (10 ,20 ); + } + +dw_spi_g2_wait_idle (dws); + +/* For TR mode reads, harvest RX FIFO after the burst completes */ +if (op->data.dir ==SPI_MEM_DATA_IN ) { +u32 rxflr =dw_spi_g2_readl (dws->regs,DW_SPI_G2_RXFLR ); +u32 rx_got =min_t (u32, rxflr, tx_len); + +/* Ensure RXFLR read completes before reading RX FIFO */ +rmb (); + +for (i =0 ; i < rx_got; i++) { +u8 val =dw_spi_g2_readl (dws->regs, +DW_SPI_G2_DR0 ) &0xFF ; +if (i >= echo_bytes && + (i - echo_bytes) < op->data.nbytes) + ((u8 *)op->data.buf.in)[i - echo_bytes] = val; + } + +if (rx_got < echo_bytes + op->data.nbytes) { +dev_err (dws->dev, +"reg_op RX short: got %u, need %u\n" , + rx_got, echo_bytes + op->data.nbytes); + ret = -EIO ; + } }dw_spi_g2_transfer_end (dws);
@@ -615,14 +710,17 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,* @dws: Driver state * @op: SPI memory operation * -* Reads data from flash using RO (receive-only) mode. +* Reads data from flash using RO (receive-only) mode with enhanced SPI +* framing via SPI_CTRLR0. * Return: 0 on success, negative error code on failure */ static int dw_spi_g2_indirect_read (struct dw_spi_g2 *dws,const struct spi_mem_op *op) { -u32 tx_entries; +struct spi_mem_op chunk_op;u8 dummy_buf =0 ; +u32 total, done =0 ; +u32 tx_entries;int ret;if (!op->data.nbytes) {
@@ -635,35 +733,67 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,return -EINVAL ; } - ret =dw_spi_g2_indirect_read_setup (dws, op); -if (ret) -return ret; + total = op->data.nbytes; +dev_dbg (dws->dev,"indirect_read: cmd=0x%02x addr=0x%llx len=%u\n" , + op->cmd.opcode, op->addr.val, total); -dw_spi_g2_target_enable (dws, dws->cs); -dw_spi_g2_enable (dws, true); +/* +* Split into chunks no larger than dws->fifo_len so the RX FIFO +* never overflows. Each iteration reprograms CTRLR1/NDF and +* re-issues cmd+addr with the column advanced. A single +* >fifo_len request leaves the controller idle (SR.BUSY=0, +* RXFLR=0) after the first FIFO worth of data, causing an +* RX timeout. +*/ + chunk_op = *op; + +while (done < total) { +u32 chunk =min_t (u32, total - done, dws->fifo_len); + + chunk_op.addr.val = op->addr.val + done; + chunk_op.data.nbytes = chunk; + chunk_op.data.buf.in = (u8 *)op->data.buf.in + done; - tx_entries =0 ; + ret =dw_spi_g2_indirect_read_setup (dws, &chunk_op); +if (ret) +return ret; + +dw_spi_g2_target_enable (dws, dws->cs); +dw_spi_g2_enable (dws, true); -/* Write instruction */ -dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , op->cmd.opcode); - tx_entries++; + tx_entries =0 ; -/* Write address as a packed 32-bit value (one FIFO entry) */ -if (op->addr.nbytes) { -dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , (u32 )op->addr.val); +/* Write instruction to DR0 */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , + chunk_op.cmd.opcode); tx_entries++; - } -/* Assert CS - controller begins: sends TX FIFO, then receives */ -dw_spi_g2_cs_assert (dws, dws->cs); +/* +* Write address as a single packed value. +* In enhanced SPI framing (RO mode with SPI_CTRLR0), the +* hardware extracts ADDR_L bits from this write. +*/ +if (chunk_op.addr.nbytes) { +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , + (u32 )chunk_op.addr.val); + tx_entries++; + } -/* Receive data via polling */ - ret =dw_spi_g2_xfer_fifo (dws, &dummy_buf,0 , - op->data.buf.in, op->data.nbytes); -dw_spi_g2_transfer_end (dws); -dw_spi_g2_controller_reset (dws); +/* Assert CS - controller begins: sends TX FIFO, then receives */ +dw_spi_g2_cs_assert (dws, dws->cs); -return ret; +/* Receive data via polling */ + ret =dw_spi_g2_xfer_fifo (dws, &dummy_buf,0 , + chunk_op.data.buf.in, chunk); +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); +if (ret) +return ret; + + done += chunk; + } + +return 0 ; }/**
@@ -676,20 +806,26 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,* @dws: Driver state * @op: SPI memory operation * -* Sets up TMOD=TO (transmit-only), standard SPI mode. +* Sets up TMOD=TO (transmit-only) */ static void dw_spi_g2_indirect_write_setup (struct dw_spi_g2 *dws,const struct spi_mem_op *op) {u32 ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TO ,DW_SPI_G2_FRF_STANDARD ); +u32 spictrlr0 =0 ;dw_spi_g2_controller_reset (dws);dw_spi_g2_target_disable (dws, dws->cs); +if (op->cmd.dtr) + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK ; +if (op->addr.dtr || op->data.dtr) + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK ; +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0);dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); -dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs), spictrlr0);dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR ,0 );dw_spi_g2_writel (dws->regs,DW_SPI_G2_RXFTLR ,0 ); }
@@ -699,8 +835,12 @@ static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,* @dws: Driver state * @op: SPI memory operation * -* Writes data to flash using TO (transmit-only) mode. The controller sends -* instruction, address, and data bytes as a continuous stream. +* Writes data to flash using TMOD=TO (transmit-only). +* NOTE: CS is asserted before SSI enable for NAND compatibility in VDK. +* This non-standard sequence is required because: +* 1. Controller reset in setup clears FIFO (can't prefill while disabled) +* 2. Enabling SSI before CS causes 0x00 transmission from empty FIFO +* 3. NAND Program Load requires data immediately when CS asserts * * Return: 0 on success, negative error code on failure */
@@ -709,7 +849,7 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, {u32 tx_total = op->cmd.nbytes + op->addr.nbytes + op->data.nbytes;u8 *tx_buf; -u32 prefill, pos; +int ret =0 ;if (!op->data.nbytes) {dev_err (dws->dev,"Zero-length write not allowed\n" );
@@ -721,52 +861,36 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,return -EINVAL ; } +dev_dbg (dws->dev,"indirect_write: cmd=0x%02x addr=0x%llx len=%u\n" , + op->cmd.opcode, op->addr.val, op->data.nbytes); +dw_spi_g2_indirect_write_setup (dws, op); -/* Allocate and build TX buffer: [cmd][addr][data] */ tx_buf =kzalloc (tx_total,GFP_KERNEL );if (!tx_buf)return -ENOMEM ;dw_spi_g2_fill_tx_buf (tx_buf, op, true); -/* -* Split enable sequence: -* Enable SSI (without CS), pre-fill TX FIFO, then assert CS. -* FIFO pointers are reset while SSIENR=0, so writes before -* enabling would be discarded. -*/ dw_spi_g2_target_enable (dws, dws->cs); -dw_spi_g2_enable (dws, true); - -/* Pre-fill FIFO to capacity or total size */ - prefill =min_t (u32, tx_total, dws->fifo_len); -for (pos =0 ; pos < prefill; pos++) -dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , tx_buf[pos]); - -/* Assert CS - transfer starts */ dw_spi_g2_cs_assert (dws, dws->cs); +dw_spi_g2_enable (dws, true); -/* Refill FIFO with remaining bytes (if any) via polling */ -if (pos < tx_total) { -int ret =dw_spi_g2_xfer_fifo (dws, &tx_buf[pos], - tx_total - pos,NULL ,0 ); -if (ret) { -dw_spi_g2_transfer_end (dws); -dw_spi_g2_controller_reset (dws); -kfree (tx_buf); -return ret; - } + ret =dw_spi_g2_xfer_fifo (dws, tx_buf, tx_total,NULL ,0 ); +if (ret) { +dev_err (dws->dev, +"indirect_write: xfer_fifo failed: %d\n" , ret); +dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); +kfree (tx_buf); +return ret; } -/* Wait for transfer completion */ -dw_spi_g2_wait_idle (dws); -dw_spi_g2_transfer_end (dws);dw_spi_g2_controller_reset (dws);kfree (tx_buf); -return 0 ; +return ret; }/*
@@ -778,9 +902,46 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,static bool dw_spi_g2_supports_mem_ops (struct spi_mem *mem,const struct spi_mem_op *op) { +struct spi_device *spi = mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); +bool op_is_dtr = + op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr; + +/* +* Detect NAND flash by checking device tree compatible string. +* This is used for VDK timing workarounds. +*/ +if (spi->dev.of_node) { +if (of_device_is_compatible (spi->dev.of_node,"spi-nand" )) + dws->is_nand = true; + } + +if (op_is_dtr) { +/* +* DTR requires enhanced SPI modes (Dual/Quad/Octal). +* Reject standard-width DTR since it has no practical use +* and the hardware doesn't support it meaningfully. +*/ +if (op->data.nbytes && op->data.buswidth ==1 ) +return false; + +/* +* In DTR mode all phases must agree on DTR — reject +* mixed STR/DTR within a single operation. +*/ +if (op->addr.nbytes && op->addr.dtr != op->cmd.dtr) +return false; +if (op->data.nbytes && op->data.dtr != op->cmd.dtr) +return false; + } +return spi_mem_default_supports_op (mem, op); } +static const struct spi_controller_mem_caps dw_spi_g2_mem_caps = { + .dtr = true, +}; +int dw_spi_g2_adjust_mem_op_size (struct spi_mem *mem,struct spi_mem_op *op) {if (op->data.dir ==SPI_MEM_DATA_IN )
@@ -846,15 +1007,13 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem, dws->cs =spi_get_chipselect (spi,0 ); dws->curr_spi = spi; -dev_dbg (dws->dev, -"exec_mem_op: cmd=0x%02x addr_len=%u data_len=%u dir=%d\n" , - op->cmd.opcode, op->addr.nbytes, op->data.nbytes, - op->data.dir); +dev_dbg (dws->dev,"exec_op: cmd=0x%02x addr=0x%llx len=%u dir=%d\n" , + op->cmd.opcode, op->addr.val, op->data.nbytes, op->data.dir);if (op->data.dir ==SPI_MEM_NO_DATA )return dw_spi_g2_cmd_only (dws, op); -if (dw_spi_g2_is_reg_op (op)) +if (dw_spi_g2_is_reg_op (dws, op))return dw_spi_g2_reg_op (dws, op);if (op->data.dir ==SPI_MEM_DATA_IN )
@@ -943,6 +1102,7 @@ int dw_spi_g2_init(struct dw_spi_g2 *dws) ctlr->setup = dw_spi_g2_setup; ctlr->mem_ops = &dw_spi_g2_mem_ops; + ctlr->mem_caps = &dw_spi_g2_mem_caps;dw_spi_g2_debugfs_init (dws);init_completion (&dws->mem_done);
drivers/spi/spi-dw-g2.h +12 -8
@@ -186,13 +186,8 @@ static inline u32 dw_spi_g2_addr_len(unsigned int addr_nbytes)return addr_nbytes ? addr_nbytes *2U :0U ; } -/* -* Maximum chunk size for indirect reads. -* 64KB allows efficient large transfers while staying well within typical -* controller limits. The FIFO (256 bytes) drains continuously during -* the transfer via polling. -*/ -#define DW_G2_MAX_CHUNK 256U +/* Maximum data chunk per spi_mem_op. */ +#define DW_G2_MAX_CHUNK (64 * 1024U)/* Timeout in microseconds for polling operations */ #define DW_SPI_G2_TIMEOUT_US 200000U
@@ -222,7 +217,13 @@ static inline u32 dw_spi_g2_addr_len(unsigned int addr_nbytes)* - etc. * Larger transfers use indirect read/write with FIFO (up to DW_G2_MAX_CHUNK). */ -#define DW_SPI_G2_REG_OP_MAX_DATA 15U +/* +* Maximum total transfer size (cmd + addr + dummy + data) for reg_op +* (TR mode). Must fit within both the TX and RX FIFOs. Each FIFO has +* fifo_len entries (typically 256); we use a compile-time cap here and +* check against the runtime fifo_len in is_reg_op(). +*/ +#define DW_SPI_G2_REG_OP_BUF_SIZE 256Ustruct dw_spi_g2_cfg {u8 tmode;
@@ -259,6 +260,9 @@ struct dw_spi_g2 {struct completion mem_done;int error; +/* Flash type detection for timing workarounds */ +bool is_nand; +#ifdef CONFIG_DEBUG_FS struct dentry *debugfs;struct debugfs_regset32 regset;
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 4/6] spi: dw-g2: Add SPI-NAND support
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [High] spi: dw-g2: dw_spi_g2_is_reg_op() routes multi-IO and DTR ops to 1-1-1 reg_op - [High] spi: dw-g2…
Thank you for your contribution! Sashiko AI review found 7 potential issues to consider:
- [High] spi: dw-g2: dw_spi_g2_is_reg_op() routes multi-IO and DTR ops to 1-1-1 reg_op
- [High] spi: dw-g2: hidden CS-toggling chunks in dw_spi_g2_indirect_read() break NAND
- [High] spi: dw-g2: dw_spi_g2_reg_op() races SR_BUSY after CS assert and drops errors
- [High] spi: dw-g2: broken multi-IO/DTR writes, DTR wait cycles, and op validation
- [Medium] spi: dw-g2: dw_spi_g2_indirect_write() removes FIFO prefill before CS assert
- [Medium] spi: dw-g2: dws->is_nand is mutated unlocked and sticky across chip selects
- [Low] spi: dw-g2: unused write-only variable tx_entries in dw_spi_g2_indirect_read()
--
commit 9669d454b276956e6dd4740dc840ec3191c46b35
Author: Prakriti Sharma <[email protected]>
spi: dw-g2: Add SPI-NAND support
This commit updates the DW SSI G2 spi-mem implementation to handle SPI-NAND devices by routing FIFO-sized operations through TR mode, chunking indirect reads, and adding DTR support and a NAND CS settling delay.
Quoted 60 lines
drivers/spi/spi-dw-g2-core.c +22
-14@@ -262,9 +262,13 @@ static u32 dw_spi_g2_get_frf(const struct spi_mem_op *op)* dw_spi_g2_get_tt - Determine transfer type from operation bus widths * @op: SPI memory operation * -* The transfer type (TT) field in SPI_CTRLR0 defines which phases use -* multi-line mode. TT0 = standard (1-1-1), TT1 = data multi-line (1-1-x), -* TT2 = addr+data multi-line (1-x-x). TT3 (x-x-x) is not supported. +* TRANS_TYPE in SPI_CTRLR0 / XIP_CTRL controls only the instruction and +* address phases. Data always uses the enhanced mode set by SPI_FRF in +* CTRLR0 (or FRF in XIP_CTRL). +* +* TT0 (0x0): instruction standard, address standard (1-1-x) +* TT1 (0x1): instruction standard, address enhanced (1-x-x) +* TT2 (0x2): instruction enhanced, address enhanced (x-x-x) * * Return: Transfer type value (0-2), or -EOPNOTSUPP for unsupported patterns */ @@ -272,29 +276,37 @@ static int dw_spi_g2_get_tt(const struct spi_mem_op *op) {bool cmd_m = (op->cmd.buswidth >1 );bool addr_m = (op->addr.nbytes && op->addr.buswidth >1 ); -bool data_m = (op->data.nbytes && op->data.buswidth >1 ); -if (!cmd_m && !addr_m && !data_m) -return DW_SPI_G2_TRANS_TYPE_TT0 ;/* 1-1-1 */ -if (!cmd_m && !addr_m && data_m) -return DW_SPI_G2_TRANS_TYPE_TT1 ;/* 1-1-x */ -if (!cmd_m && addr_m && data_m) -return DW_SPI_G2_TRANS_TYPE_TT2 ;/* 1-x-x */ +if (!cmd_m && !addr_m) +return DW_SPI_G2_TRANS_TYPE_TT0 ;/* 1-1-x */ +if (!cmd_m && addr_m) +return DW_SPI_G2_TRANS_TYPE_TT1 ;/* 1-x-x */ +if (cmd_m && addr_m) +return DW_SPI_G2_TRANS_TYPE_TT2 ;/* x-x-x */ return -EOPNOTSUPP ; }/** -* dw_spi_g2_is_reg_op - Check if operation is a register read/write +* dw_spi_g2_is_reg_op - Check if operation fits in TR (transmit-receive) mode +* @dws: Driver state (provides runtime fifo_len) * @op: SPI memory operation * -* Register operations are single-byte commands with no address (e.g., -* RDSR, WRSR, RDID). Data must fit within the fixed-size stack buffer. +* Routes operations through TR mode when total transfer size (cmd + addr + +* dummy + data) fits within the FIFO. This avoids RO mode with SPI_CTRLR0 +* (enhanced SPI framing) which some platforms don't fully support. +* +* Limit is min(fifo_len, compile-time buffer size) to stay within both +* the hardware FIFO and the stack-allocated buffers in reg_op(). */ -static inline bool dw_spi_g2_is_reg_op (const struct spi_mem_op *op) +static inline bool dw_spi_g2_is_reg_op(struct dw_spi_g2 *dws,
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_get_tt(const struct spi_mem_op *op) · line 310
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +6
-2+ const struct spi_mem_op *op) { -return !op->addr.nbytes && op->data.nbytes >0 && - op->data.nbytes <=DW_SPI_G2_REG_OP_MAX_DATA ; +u32 total = op->cmd.nbytes + op->addr.nbytes + + op->dummy.nbytes + op->data.nbytes; +u32 limit =min_t (u32, dws->fifo_len,DW_SPI_G2_REG_OP_BUF_SIZE ); + +return op->data.nbytes >0 && total <= limit; }
[Severity: High]
Does dw_spi_g2_is_reg_op() route multi-IO, DTR, and 2-byte-opcode operations up to 256 bytes into the 1-1-1 standard SPI path in dw_spi_g2_reg_op()?
dw_spi_g2_reg_op_setup() hardcodes DW_SPI_G2_FRF_STANDARD and writes 0 to DW_SPI_G2_SPI_CTRLR0, so any Dual, Quad, Octal, or DTR operation whose total length fits in min(dws->fifo_len, 256) (such as a 64-byte SPI-NAND OOB read using 1-1-4 Quad Read From Cache 0x6b) is clocked out on a single data wire in STR mode.
In addition, dw_spi_g2_reg_op() serializes only one byte of op->cmd.opcode and sets echo_bytes = 1 + op->addr.nbytes + op->dummy.nbytes (truncating 2-byte opcodes), drops op->dummy.nbytes on SPI_MEM_DATA_OUT, and clocks dummy bytes as 1-bit data on SPI_MEM_DATA_IN. Conversely, a standard 1-1-1 read larger than dws->fifo_len falls through to dw_spi_g2_indirect_read(), which sets DW_SPI_G2_FRF_STANDARD (where SPI_CTRLR0 enhanced framing is inactive) while writing chunk_op.addr.val as a single DR0 entry.
Quoted 4 lines
drivers/spi/spi-dw-g2-core.c +0
-0@@ -483,49 +495,132 @@ static void dw_spi_g2_reg_op_setup(struct dw_spi_g2 *dws,static int dw_spi_g2_reg_op (struct dw_spi_g2 *dws,const struct spi_mem_op *op) {
[ ... ]
Quoted 45 lines
drivers/spi/spi-dw-g2-core.c +45
-0+/* Ensure all FIFO writes complete before CS assertion */ +wmb (); + +dw_spi_g2_cs_assert (dws, dws->cs); + +/* +* CS settling delay for NAND flash: +* +* NAND flash requires a settling period after CS assertion before +* data transactions can begin reliably. This is a hardware timing requirement. +* +* This is specific to NAND flash behavior where CS-to-valid-data timing +* is more stringent than NOR flash. The delay applies to small +* register operations (reg_op path) which are commonly used for: +* - READID (0x9F) +* - GET_FEATURE (0x0F) +* - SET_FEATURE (0x1F) +* - READ_STATUS (0x0F with feature address) +* +* Note: Only applies to NAND (detected via DT "spi-nand" compatible) +* to avoid performance impact on NOR flash. +*/ +if (dws->is_nand) { +/* Force a register read to ensure CS assertion propagates */ + (void )dw_spi_g2_readl (dws->regs,DW_SPI_G2_SR ); +usleep_range (10 ,20 ); + } + +dw_spi_g2_wait_idle (dws); + +/* For TR mode reads, harvest RX FIFO after the burst completes */ +if (op->data.dir ==SPI_MEM_DATA_IN ) { +u32 rxflr =dw_spi_g2_readl (dws->regs,DW_SPI_G2_RXFLR ); +u32 rx_got =min_t (u32, rxflr, tx_len); + +/* Ensure RXFLR read completes before reading RX FIFO */ +rmb (); + +for (i =0 ; i < rx_got; i++) { +u8 val =dw_spi_g2_readl (dws->regs, +DW_SPI_G2_DR0 ) &0xFF ; +if (i >= echo_bytes && + (i - echo_bytes) < op->data.nbytes) + ((u8 *)op->data.buf.in)[i - echo_bytes] = val; + }
drivers/spi/spi-dw-g2-core.c static void dw_spi_g2_reg_op_setup(struct dw_spi_g2 *dws, · line 626
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +7
-0+ +if (rx_got < echo_bytes + op->data.nbytes) { +dev_err (dws->dev, +"reg_op RX short: got %u, need %u\n" , + rx_got, echo_bytes + op->data.nbytes); + ret = -EIO ; + } }dw_spi_g2_transfer_end (dws);
[Severity: High]
Can dw_spi_g2_wait_idle() return before DW_SPI_G2_SR_BUSY asserts after dw_spi_g2_cs_assert()?
dw_spi_g2_wait_idle() only checks !(sr & DW_SPI_G2_SR_BUSY) without checking DW_SPI_G2_SR_TFE. In DWC SSI hardware, writing SER does not set SR_BUSY until SER synchronizes into the ssi_clk domain. When dws->is_nand is false (such as on SPI-NOR), polling SR immediately after dw_spi_g2_cs_assert() can observe SR_BUSY == 0 before shifting starts:SPI_MEM_DATA_IN then reads RXFLR == 0 and fails with -EIO, while SPI_MEM_DATA_OUT calls dw_spi_g2_transfer_end() and aborts the transfer while the TX FIFO is still full.
Also, dw_spi_g2_wait_idle()'s return value is ignored, so a busy timeout on SPI_MEM_DATA_OUT silently returns 0.
Quoted 74 lines
drivers/spi/spi-dw-g2-core.c +34
-17@@ -615,14 +710,17 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,* @dws: Driver state * @op: SPI memory operation * -* Reads data from flash using RO (receive-only) mode. +* Reads data from flash using RO (receive-only) mode with enhanced SPI +* framing via SPI_CTRLR0. * Return: 0 on success, negative error code on failure */ static int dw_spi_g2_indirect_read (struct dw_spi_g2 *dws,const struct spi_mem_op *op) { -u32 tx_entries; +struct spi_mem_op chunk_op;u8 dummy_buf =0 ; +u32 total, done =0 ; +u32 tx_entries;int ret;if (!op->data.nbytes) {@@ -635,35 +733,67 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,return -EINVAL ; } - ret =dw_spi_g2_indirect_read_setup (dws, op); -if (ret) -return ret; + total = op->data.nbytes; +dev_dbg (dws->dev,"indirect_read: cmd=0x%02x addr=0x%llx len=%u\n" , + op->cmd.opcode, op->addr.val, total); -dw_spi_g2_target_enable (dws, dws->cs); -dw_spi_g2_enable (dws, true); +/* +* Split into chunks no larger than dws->fifo_len so the RX FIFO +* never overflows. Each iteration reprograms CTRLR1/NDF and +* re-issues cmd+addr with the column advanced. A single +* >fifo_len request leaves the controller idle (SR.BUSY=0, +* RXFLR=0) after the first FIFO worth of data, causing an +* RX timeout. +*/ + chunk_op = *op; + +while (done < total) { +u32 chunk =min_t (u32, total - done, dws->fifo_len); + + chunk_op.addr.val = op->addr.val + done; + chunk_op.data.nbytes = chunk; + chunk_op.data.buf.in = (u8 *)op->data.buf.in + done; - tx_entries =0 ; + ret =dw_spi_g2_indirect_read_setup (dws, &chunk_op); +if (ret) +return ret; + +dw_spi_g2_target_enable (dws, dws->cs); +dw_spi_g2_enable (dws, true); -/* Write instruction */ -dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , op->cmd.opcode); - tx_entries++; + tx_entries =0 ; -/* Write address as a packed 32-bit value (one FIFO entry) */ -if (op->addr.nbytes) { -dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , (u32 )op->addr.val); +/* Write instruction to DR0 */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , + chunk_op.cmd.opcode); tx_entries++; - } -/* Assert CS - controller begins: sends TX FIFO, then receives */ -dw_spi_g2_cs_assert (dws, dws->cs);
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws, · line 780
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+/* +* Write address as a single packed value. +* In enhanced SPI framing (RO mode with SPI_CTRLR0), the +* hardware extracts ADDR_L bits from this write. +*/ +if (chunk_op.addr.nbytes) { +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , + (u32 )chunk_op.addr.val); + tx_entries++; + }
[Severity: Low]tx_entries is assigned and incremented here, but never read, which triggers a -Wunused-but-set-variable compiler warning.
Quoted 13 lines
drivers/spi/spi-dw-g2-core.c +5
-6-/* Receive data via polling */ - ret =dw_spi_g2_xfer_fifo (dws, &dummy_buf,0 , - op->data.buf.in, op->data.nbytes); -dw_spi_g2_transfer_end (dws); -dw_spi_g2_controller_reset (dws); +/* Assert CS - controller begins: sends TX FIFO, then receives */ +dw_spi_g2_cs_assert (dws, dws->cs); -return ret; +/* Receive data via polling */ + ret =dw_spi_g2_xfer_fifo (dws, &dummy_buf,0 , + chunk_op.data.buf.in, chunk);
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws, · line 797
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +9
-0+dw_spi_g2_transfer_end (dws); +dw_spi_g2_controller_reset (dws); +if (ret) +return ret; + + done += chunk; + } + +return 0 ; }
[Severity: High]
Won't splitting a single spi_mem_op across multiple CS assertions here while raising DW_G2_MAX_CHUNK to 64 KB break SPI-NAND continuous reads?
In drivers/mtd/nand/spi/core.c, spinand_read_from_cache_op() relies on adjust_op_size() clamping op->data.nbytes to the controller's single-CS transfer limit so it can fall back to buffered page reads when a continuous read cannot complete in one CS assertion. On Winbond W25N01JW, continuous read is enabled by default and uses a 1-byte dummy address (addr.nbytes = 1, addr.val = 0) while streaming from column 0 for as long as CS stays low; toggling CS every dws->fifo_len bytes aborts the continuous stream in the flash and overflows the 1-byte dummy address once done >= 256.
If the controller cannot receive more than dws->fifo_len bytes in one CS assertion, dw_spi_g2_adjust_mem_op_size() should clamp SPI_MEM_DATA_IN to dws->fifo_len so the spi-mem and spinand callers advance the address and manage CS boundaries themselves.
Quoted 29 lines
drivers/spi/spi-dw-g2-core.c +8
-2@@ -676,20 +806,26 @@ static int dw_spi_g2_indirect_read(struct dw_spi_g2 *dws,* @dws: Driver state * @op: SPI memory operation * -* Sets up TMOD=TO (transmit-only), standard SPI mode. +* Sets up TMOD=TO (transmit-only) */ static void dw_spi_g2_indirect_write_setup (struct dw_spi_g2 *dws,const struct spi_mem_op *op) {u32 ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TO ,DW_SPI_G2_FRF_STANDARD ); +u32 spictrlr0 =0 ;dw_spi_g2_controller_reset (dws);dw_spi_g2_target_disable (dws, dws->cs); +if (op->cmd.dtr) + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK ; +if (op->addr.dtr || op->data.dtr) + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK ; +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0);dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); -dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs), spictrlr0);dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR ,0 );dw_spi_g2_writel (dws->regs,DW_SPI_G2_RXFTLR ,0 ); }
[ ... ]
Quoted 35 lines
drivers/spi/spi-dw-g2-core.c +4
-16@@ -721,52 +861,36 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,return -EINVAL ; } +dev_dbg (dws->dev,"indirect_write: cmd=0x%02x addr=0x%llx len=%u\n" , + op->cmd.opcode, op->addr.val, op->data.nbytes); +dw_spi_g2_indirect_write_setup (dws, op); -/* Allocate and build TX buffer: [cmd][addr][data] */ tx_buf =kzalloc (tx_total,GFP_KERNEL );if (!tx_buf)return -ENOMEM ;dw_spi_g2_fill_tx_buf (tx_buf, op, true); -/* -* Split enable sequence: -* Enable SSI (without CS), pre-fill TX FIFO, then assert CS. -* FIFO pointers are reset while SSIENR=0, so writes before -* enabling would be discarded. -*/ dw_spi_g2_target_enable (dws, dws->cs); -dw_spi_g2_enable (dws, true); - -/* Pre-fill FIFO to capacity or total size */ - prefill =min_t (u32, tx_total, dws->fifo_len); -for (pos =0 ; pos < prefill; pos++) -dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , tx_buf[pos]); - -/* Assert CS - transfer starts */ dw_spi_g2_cs_assert (dws, dws->cs); +dw_spi_g2_enable (dws, true); -/* Refill FIFO with remaining bytes (if any) via polling */
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 879
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +1
-9-if (pos < tx_total) { -int ret =dw_spi_g2_xfer_fifo (dws, &tx_buf[pos], - tx_total - pos,NULL ,0 ); -if (ret) { -dw_spi_g2_transfer_end (dws); -dw_spi_g2_controller_reset (dws); -kfree (tx_buf); -return ret; - } + ret =dw_spi_g2_xfer_fifo (dws, tx_buf, tx_total,NULL ,0 );
[Severity: Medium]
Can asserting CS and enabling SSI before pre-filling the TX FIFO cause a TX FIFO underrun that deasserts CS mid-write?
With SER and SSIENR both active, DWC SSI starts shifting as soon as the first byte is written to DR0 in dw_spi_g2_xfer_fifo(), and automatically deasserts CS if the TX FIFO drains empty before the next byte arrives. Because dw_spi_g2_xfer_fifo() runs in preemptible context and reads DW_SPI_G2_SR before each byte write, an interrupt or scheduling delay after the opcode byte can drain the 1-byte FIFO and toggle CS during a page program.
Quoted 7 lines
drivers/spi/spi-dw-g2-core.c +3
-0@@ -778,9 +902,46 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,static bool dw_spi_g2_supports_mem_ops (struct spi_mem *mem,const struct spi_mem_op *op) { +struct spi_device *spi = mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); + bool op_is_dtr =
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 917
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+ op->cmd.dtr || op->addr.dtr || op->dummy.dtr || op->data.dtr; + +/* +* Detect NAND flash by checking device tree compatible string. +* This is used for VDK timing workarounds. +*/ +if (spi->dev.of_node) { +if (of_device_is_compatible (spi->dev.of_node,"spi-nand" )) + dws->is_nand = true; + }
[Severity: Medium]
Isn't dws->is_nand controller-wide state that gets latched permanently across all chip selects and modified without holding any controller lock?
supports_op() is called before spi_mem_access_start() acquires the bus locks, and once any child node matches "spi-nand", dws->is_nand remains true even when talking to a non-NAND device on another chip select.
Quoted 12 lines
drivers/spi/spi-dw-g2-core.c +12
-0+ +if (op_is_dtr) { +/* +* DTR requires enhanced SPI modes (Dual/Quad/Octal). +* Reject standard-width DTR since it has no practical use +* and the hardware doesn't support it meaningfully. +*/ +if (op->data.nbytes && op->data.buswidth ==1 ) +return false; + +/* +* In DTR mode all phases must agree on DTR — reject
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 939
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +8
-0+* mixed STR/DTR within a single operation. +*/ +if (op->addr.nbytes && op->addr.dtr != op->cmd.dtr) +return false; +if (op->data.nbytes && op->data.dtr != op->cmd.dtr) +return false; + } +return spi_mem_default_supports_op (mem, op); }
[Severity: High]
Does dw_spi_g2_supports_mem_ops() advertise multi-IO and DTR write and command-only operations that the driver only transmits in 1-1-1 STR mode?
ctlr->mode_bits includes SPI_TX_DUAL, SPI_TX_QUAD, and SPI_TX_OCTAL, and dw_spi_g2_mem_caps enables .dtr = true, so dw_spi_g2_supports_mem_ops() accepts operations such as Winbond W25N01JW's 1-1-4 Program Load (0x32) or 8D-8D-8D writes. However, dw_spi_g2_indirect_write_setup() and dw_spi_g2_cmd_only_setup() both hardcode DW_SPI_G2_FRF_STANDARD (where the DDR bits written to SPI_CTRLR0 are ignored) and leave INST_L and ADDR_L at 0, sending all bytes over a single wire.
There are two related DTR and buswidth issues in this path:
1. In dw_spi_g2_indirect_read_setup() and dw_spi_g2_exec_mem_op(), dummy_cycles is computed as (op->dummy.nbytes * 8) / op->dummy.buswidth without halving for DTR mode (later partially adjusted in commit "spi: dw-g2: Add XIP support"), programming twice the required wait cycles.
2. dw_spi_g2_supports_mem_ops() does not verify that op->dummy.dtr matches op->cmd.dtr, that cmd/addr buswidth > 1 is rejected when op->data.nbytes == 0, or that multi-line phase buswidths are supported by dw_spi_g2_get_tt() and the single CTRLR0.FRF field (for example, cmd.buswidth > 1 with addr.nbytes == 0 passes supports_op() and then fails in dw_spi_g2_get_tt() with -EOPNOTSUPP).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH 5/6 dt-bindings: spi: Document DWC SSI XIP properties
In reply to Prakriti SharmaDocument the optional XIP memory region and the properties that select execute-in-place, continuous mode, and prefetch.
--- .../bindings/spi/snps,dwc-ssi-2.02a.yaml | 59 ++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-)
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +57 -2
@@ -23,7 +23,18 @@ properties: const: snps,dwc-ssi-2.02a reg: - maxItems: 1 + minItems: 1 + maxItems: 2 + description: | + Controller register base and optional XIP memory region. + - First entry: SSI controller register space + - Second entry (optional): XIP memory-mapped region for direct flash access + + reg-names: + minItems: 1 + items: + - const: ssi_base + - const: xip_mem interrupts: description: |
@@ -93,6 +104,48 @@ properties: of the designware controller, and the upper limit is also subject to controller configuration. + snps,xip-enabled: + type: boolean + description: | + Enable XIP (eXecute-In-Place) mode for direct memory-mapped access to + SPI flash. When enabled, the controller's XIP region is mapped into + CPU address space for zero-copy reads and writes. It requires the + xip_mem entry in reg. + + XIP is used only when this property is present and the module + parameter spi_dw_g2_mmio.xip_mode=1 is set. The default xip_mode=0 + disables XIP. Runtime control is available through sysfs. + + snps,xip-continuous-mode: + type: boolean + description: | + Enable XIP continuous transfer mode. When enabled, the controller omits + the command byte on subsequent sequential reads from the same chip select, + saving 8 SPI clock cycles per access. This improves throughput for code + execution from XIP flash. + + Requirements: + - Flash device must support continuous read mode (e.g., Fast Read Quad + I/O with mode bits) + - Must use consistent command across all accesses + - Not compatible with mixed command types + + The controller automatically re-sends the command when CS is de-asserted + or transfer type changes. + + snps,xip-prefetch-mode: + type: boolean + description: | + Enable XIP hardware prefetch mode. The controller speculatively fetches + the next cache line when the current access completes, anticipating + sequential reads. This reduces latency for instruction fetches during + XIP code execution. + + Best for: Sequential code execution, linear data reads + Not ideal for: Random access patterns, small sporadic reads + + Prefetch is only active when XIP direct mapping is enabled. + patternProperties: "^.*@[0-9a-f]+$": type: object
@@ -121,7 +174,8 @@ examples: - | spi@20100000 { compatible = "snps,dwc-ssi-2.02a"; - reg = <0x20100000 0x1000>; + reg = <0x20100000 0x1000>, <0x60000000 0x10000000>; + reg-names = "ssi_base", "xip_mem"; #address-cells = <1>; #size-cells = <0>; interrupts = <0 42 4>;
@@ -131,6 +185,7 @@ examples: dmas = <&dma0 10>, <&dma0 11>; dma-names = "tx", "rx"; rx-sample-delay-ns = <3>; + snps,xip-enabled; flash@0 { compatible = "jedec,spi-nor";
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] dt-bindings: spi: snps,dwc-ssi-2.02a: XIP properties encode OS policy - [Low] dt-bindings: spi…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] dt-bindings: spi: snps,dwc-ssi-2.02a: XIP properties encode OS policy
- [Low] dt-bindings: spi: snps,dwc-ssi-2.02a: missing reg items and XIP dependencies
--
commit ea2effa98a511e888bf97a068c9c5ac14822f4eb
Author: Prakriti Sharma <[email protected]>
dt-bindings: spi: Document DWC SSI XIP properties
This commit documents an optional XIP memory region and boolean properties for execute-in-place, continuous mode, and prefetch in the Synopsys DWC SSI v2.02a binding.
Quoted 11 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +2
-1@@ -23,7 +23,18 @@ properties: const: snps,dwc-ssi-2.02a reg: - maxItems: 1 + minItems: 1 + maxItems: 2
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml properties: · line 37
Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +10
-0+ description: | + Controller register base and optional XIP memory region. + - First entry: SSI controller register space + - Second entry (optional): XIP memory-mapped region for direct flash access + + reg-names: + minItems: 1 + items: + - const: ssi_base + - const: xip_mem
[Severity: Low]
Should reg use an items list with individual descriptions for each entry (like clocks and dmas in this schema) instead of maxItems: 2 with a bulleted description?
Also, although the snps,xip-enabled description states that it requires the xip_mem entry in reg, the schema has no dependencies or allOf/if/then constraint enforcing minItems: 2 on reg and reg-names when the XIP properties are present, so dt_binding_check and dtbs_check will not catch a missing xip_mem region.
Quoted 35 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +31
-0@@ -93,6 +104,48 @@ properties: of the designware controller, and the upper limit is also subject to controller configuration. + snps,xip-enabled: + type: boolean + description: | + Enable XIP (eXecute-In-Place) mode for direct memory-mapped access to + SPI flash. When enabled, the controller's XIP region is mapped into + CPU address space for zero-copy reads and writes. It requires the + xip_mem entry in reg. + + XIP is used only when this property is present and the module + parameter spi_dw_g2_mmio.xip_mode=1 is set. The default xip_mode=0 + disables XIP. Runtime control is available through sysfs. + + snps,xip-continuous-mode: + type: boolean + description: | + Enable XIP continuous transfer mode. When enabled, the controller omits + the command byte on subsequent sequential reads from the same chip select, + saving 8 SPI clock cycles per access. This improves throughput for code + execution from XIP flash. + + Requirements: + - Flash device must support continuous read mode (e.g., Fast Read Quad + I/O with mode bits) + - Must use consistent command across all accesses + - Not compatible with mixed command types + + The controller automatically re-sends the command when CS is de-asserted + or transfer type changes. + + snps,xip-prefetch-mode: + type: boolean
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml properties: · line 147
Quoted 10 lines
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +10
-0+ description: | + Enable XIP hardware prefetch mode. The controller speculatively fetches + the next cache line when the current access completes, anticipating + sequential reads. This reduces latency for instruction fetches during + XIP code execution. + + Best for: Sequential code execution, linear data reads + Not ideal for: Random access patterns, small sporadic reads + + Prefetch is only active when XIP direct mapping is enabled.
[Severity: Medium]
Do snps,xip-enabled, snps,xip-continuous-mode, and snps,xip-prefetch-mode encode OS and workload policy rather than hardware description?
Whether an XIP memory-mapped window exists is already described by the xip_mem reg entry and the controller's capability registers; snps,xip-enabled acts as a software policy switch and documents Linux-specific module parameters (spi_dw_g2_mmio.xip_mode=1) and sysfs interfaces in the binding. Similarly, snps,xip-continuous-mode depends on the attached flash device and protocol layer rather than a static controller property, and snps,xip-prefetch-mode describes workload tuning (sequential versus random access patterns).
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH 5/6] dt-bindings: spi: Document DWC SSI XIP properties
In reply to Prakriti SharmaQuoted 5 lines · Prakriti Sharma
Document the optional XIP memory region and the properties that select execute-in-place, continuous mode, and prefetch.
Signed-off-by Prakriti Sharma <[email protected]>Signed-off-by Siddhant Kumar <[email protected]>
Please use subject prefixes matching the subsystem. You can get them for example with git log --oneline -- DIRECTORY_OR_FILE on the directory your patch is touching. For bindings, the preferred subjects are explained here:
https://www.kernel.org/doc/html/latest/devicetree/bindings/submitting-patches.html#i-for-patch-submitters
Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml properties: · line 26
Quoted 13 lines
--- .../bindings/spi/snps,dwc-ssi-2.02a.yaml | 59 ++++++++++++++++++- 1 file changed, 57 insertions(+), 2 deletions(-)Documentation/devicetree/bindings/spi/snps,dwc-ssi-2.02a.yaml +0
-1@@ -23,7 +23,18 @@ properties: const: snps,dwc-ssi-2.02a reg: - maxItems: 1
You just added this file, so are you saying that your previous patch is wrong? ABI you just added is incomplete?
Please follow writing bindings doc and submit complete bindings.
Best regards,
Krzysztof
PATCH 6/6 spi: dw-g2: Add XIP support
In reply to Prakriti SharmaAdd memory-mapped execute-in-place through the spi-mem direct-mapping helpers.
NOR page-program writes stay on the indirect FIFO path. XIP issues a separate SPI transaction per access, and page program needs one continuous transaction. Direct mapping can be switched at runtime through debugfs.
--- drivers/spi/spi-dw-g2-core.c | 1074 +++++++++++++++++++++++++++++++++- drivers/spi/spi-dw-g2-mmio.c | 263 ++++++++- drivers/spi/spi-dw-g2.h | 166 +++++- 3 files changed, 1469 insertions(+), 34 deletions(-)
drivers/spi/spi-dw-g2-core.c +1048 -26
@@ -36,8 +36,112 @@ static const struct debugfs_reg32 dw_spi_g2_dbg_regs[] = {DW_SPI_G2_DBG_REG ("IMR" ,DW_SPI_G2_IMR ),DW_SPI_G2_DBG_REG ("ISR" ,DW_SPI_G2_ISR ),DW_SPI_G2_DBG_REG ("DEDICATED_CS" ,DW_SPI_G2_DEDICATED_CS_EN ), +DW_SPI_G2_DBG_REG ("CS0_RX_SAMPLE_DLY" ,DW_SPI_G2_RX_SAMPLE_DELAY (0 )), +DW_SPI_G2_DBG_REG ("CS1_RX_SAMPLE_DLY" ,DW_SPI_G2_RX_SAMPLE_DELAY (1 )), +DW_SPI_G2_DBG_REG ("CS2_RX_SAMPLE_DLY" ,DW_SPI_G2_RX_SAMPLE_DELAY (2 )), +DW_SPI_G2_DBG_REG ("CS3_RX_SAMPLE_DLY" ,DW_SPI_G2_RX_SAMPLE_DELAY (3 )), +DW_SPI_G2_DBG_REG ("CS0_DDR_DRV_EDGE" ,DW_SPI_G2_DDR_DRIVE_EDGE (0 )), +DW_SPI_G2_DBG_REG ("CS1_DDR_DRV_EDGE" ,DW_SPI_G2_DDR_DRIVE_EDGE (1 )), + DW_SPI_G2_DBG_REG("CS2_DDR_DRV_EDGE", DW_SPI_G2_DDR_DRIVE_EDGE(2)), }; +static int xip_mode_get (void *data,u64 *val) +{ +struct dw_spi_g2 *dws = data; + *val = dws->use_xip ?1 :0 ; +return 0 ; +} + +static int xip_mode_set (void *data,u64 val) +{ +struct dw_spi_g2 *dws = data; + +if (val !=0 && val !=1 ) +return -EINVAL ; + +if (val && !dws->xip_mem) { +dev_err (dws->dev,"Cannot enable XIP: no memory-mapped region\n" ); +return -EINVAL ; + } + + dws->use_xip = !!val; +dev_info (dws->dev,"Transfer mode set to %s\n" , + dws->use_xip ?"XIP (direct)" :"indirect (FIFO)" ); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (xip_mode_fops, xip_mode_get, xip_mode_set,"%llu\n" ); + + +static int xip_continuous_mode_get (void *data,u64 *val) +{ +struct dw_spi_g2 *dws = data; + *val = dws->xip_continuous_mode ?1 :0 ; +return 0 ; +} + +static int xip_continuous_mode_set (void *data,u64 val) +{ +struct dw_spi_g2 *dws = data; + +if (val !=0 && val !=1 ) +return -EINVAL ; + +if (val && !dws->xip_mem) { +dev_err (dws->dev, +"Cannot enable XIP continuous mode: no XIP region\n" ); +return -EINVAL ; + } + + dws->xip_continuous_mode = !!val; +dev_info (dws->dev,"XIP continuous transfer mode: %s\n" , + dws->xip_continuous_mode ?"enabled" :"disabled" ); + +if (dws->xip_continuous_mode) +dev_info (dws->dev, +"Requires flash command supporting continuous read (e.g., 0xEB Quad I/O)\n" ); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (xip_continuous_mode_fops, xip_continuous_mode_get, + xip_continuous_mode_set,"%llu\n" ); + +static int xip_prefetch_mode_get (void *data,u64 *val) +{ +struct dw_spi_g2 *dws = data; + *val = dws->xip_prefetch_mode ?1 :0 ; +return 0 ; +} + +static int xip_prefetch_mode_set (void *data,u64 val) +{ +struct dw_spi_g2 *dws = data; + +if (val !=0 && val !=1 ) +return -EINVAL ; + +if (val && !dws->xip_mem) { +dev_err (dws->dev, +"Cannot enable XIP prefetch mode: no XIP region\n" ); +return -EINVAL ; + } + + dws->xip_prefetch_mode = !!val; +dev_info (dws->dev,"XIP prefetch mode: %s\n" , + dws->xip_prefetch_mode ?"enabled" :"disabled" ); + +if (dws->xip_prefetch_mode) +dev_info (dws->dev, +"Requires flash command supporting continuous read (e.g., 0xEB Quad I/O)\n" ); + +return 0 ; +} + +DEFINE_DEBUGFS_ATTRIBUTE (xip_prefetch_mode_fops, xip_prefetch_mode_get, + xip_prefetch_mode_set,"%llu\n" ); +/** * dw_spi_g2_debugfs_init - Create debugfs entries for register inspection * @dws: Driver state
@@ -61,6 +165,11 @@ static void dw_spi_g2_debugfs_init(struct dw_spi_g2 *dws) dws->regset.nregs =ARRAY_SIZE (dw_spi_g2_dbg_regs);debugfs_create_regset32 ("registers" ,0400 , dws->debugfs, &dws->regset); +debugfs_create_file ("xip_mode" ,0644 , dws->debugfs, dws, &xip_mode_fops); +debugfs_create_file ("xip_continuous_mode" ,0644 , dws->debugfs, dws, + &xip_continuous_mode_fops); +debugfs_create_file ("xip_prefetch_mode" ,0644 , dws->debugfs, dws, + &xip_prefetch_mode_fops); }/**
@@ -152,28 +261,61 @@ int dw_spi_g2_setup(struct spi_device *spi)dw_spi_g2_target_disable (dws, dws->cs);dw_spi_g2_writel (dws->regs,DW_SPI_G2_BAUDR (dws->cs), baud); +/* Configure RX sample delay for chip select */ +if (dws->cs <DW_SPI_G2_MAX_CS ) { +u32 rx_dly_ns; + +if (device_property_read_u32 (&spi->dev,"rx-sample-delay-ns" , + &rx_dly_ns)) + rx_dly_ns = dws->def_rx_sample_dly_ns; + + dws->rx_sample_dly[dws->cs] = +DIV_ROUND_CLOSEST (rx_dly_ns, +NSEC_PER_SEC / dws->max_freq); + } +dev_dbg (&spi->dev, -"setup: CS=%u mode=%u bpw=%u speed=%u baud=%u\n" , - dws->cs, spi->mode, spi->bits_per_word, speed, baud); +"setup: CS=%u mode=%u bpw=%u speed=%u baud=%u rx_dly=%u\n" , + dws->cs, spi->mode, spi->bits_per_word, speed, baud, + dws->cs <DW_SPI_G2_MAX_CS ? dws->rx_sample_dly[dws->cs] :0 );return 0 ; } -static void dw_spi_g2_controller_reset (struct dw_spi_g2 *dws) +/** +* dw_spi_g2_set_sampling_mode - Configure RX sampling mode +* @dws: Driver state +* @cs: Chip select +* @mode: Sampling mode to enable +* +* RX_DELAY uses RX_SAMPLE_DELAY. DATA_STROBE uses the flash DQS/RXDS +* input for DTR transfers. Must be called with SSI disabled. +*/ +void dw_spi_g2_set_sampling_mode (struct dw_spi_g2 *dws,u8 cs, +enum dw_spi_g2_sampling_mode mode) { -int drain =0 ; +u32 ctrlr0, spictrlr0; -dw_spi_g2_enable (dws, false); -dw_spi_g2_writel (dws->regs,DW_SPI_G2_SER ,0 ); -/* Clear all interrupts by writing 1s to defined ICR bits */ -dw_spi_g2_writel (dws->regs,DW_SPI_G2_ICR ,DW_SPI_G2_ICR_ALL ); + ctrlr0 =dw_spi_g2_readl (dws->regs,DW_SPI_G2_CTRLR0 (cs)); + spictrlr0 =dw_spi_g2_readl (dws->regs,DW_SPI_G2_SPI_CTRLR0 (cs)); -/* Drain any stale RX data left from previous transfers */ -while (dw_spi_g2_readl (dws->regs,DW_SPI_G2_RXFLR ) >0 && - drain < dws->fifo_len) { - (void )dw_spi_g2_readl (dws->regs,DW_SPI_G2_DR0 ); - drain++; +switch (mode) { +case DW_SPI_G2_SAMPLING_RX_DELAY : +/* Disable CLK_LOOP_EN and SPI_RXDS_EN for basic RX_SAMPLE_DELAY */ + ctrlr0 &= ~DW_SPI_G2_CTRLR0_CLK_LOOP_EN ; + spictrlr0 &= ~DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK ; +break ; + + +case DW_SPI_G2_SAMPLING_DATA_STROBE : +/* Enable SPI_RXDS_EN, disable CLK_LOOP_EN for DQS/strobe mode */ + ctrlr0 &= ~DW_SPI_G2_CTRLR0_CLK_LOOP_EN ; + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK ; +break ; } + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (cs), ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (cs), spictrlr0); }/**
@@ -230,6 +372,30 @@ static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)return ctrlr0; } +static void dw_spi_g2_controller_reset (struct dw_spi_g2 *dws) +{ +int drain =0 ; + +dw_spi_g2_enable (dws, false); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_ICR ,DW_SPI_G2_ICR_ALL ); + +/* Drain any stale RX data left from previous transfers */ +while (dw_spi_g2_readl (dws->regs,DW_SPI_G2_RXFLR ) >0 && + drain < dws->fifo_len) { + (void )dw_spi_g2_readl (dws->regs,DW_SPI_G2_DR0 ); + drain++; + } + +/* Update RX sample delay for current CS if value differs. */ +if (dws->cs <DW_SPI_G2_MAX_CS && + dws->rx_sample_dly[dws->cs] != dws->cur_rx_sample_dly) { +dw_spi_g2_writel (dws->regs,DW_SPI_G2_RX_SAMPLE_DELAY (dws->cs), + dws->rx_sample_dly[dws->cs]); + dws->cur_rx_sample_dly = dws->rx_sample_dly[dws->cs]; + } +} +/** * dw_spi_g2_get_frf - Determine frame format from operation bus widths * @op: SPI memory operation
@@ -456,16 +622,29 @@ static int dw_spi_g2_cmd_only(struct dw_spi_g2 *dws, {u8 buf[8 ];u32 len; +u32 i;int ret; +dev_dbg (dws->dev,"cmd_only: cmd=0x%02x addr=0x%llx\n" , + op->cmd.opcode, op->addr.val); +dw_spi_g2_cmd_only_setup (dws, op); len =dw_spi_g2_fill_tx_buf (buf, op, false); -dw_spi_g2_transfer_begin (dws); - ret =dw_spi_g2_xfer_fifo (dws, buf, len,NULL ,0 ); +dw_spi_g2_target_enable (dws, dws->cs); +dw_spi_g2_enable (dws, true); + +for (i =0 ; i < len; i++) +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DR0 , buf[i]); + +dw_spi_g2_cs_assert (dws, dws->cs); +dw_spi_g2_wait_idle (dws); + ret =0 ; +dw_spi_g2_transfer_end (dws);dw_spi_g2_controller_reset (dws); +return ret; }
@@ -629,6 +808,90 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)return ret; } +/** +* dw_spi_g2_op_freq - Choose the SPI clock frequency for a given op +* @dws: Driver state +* @op: SPI memory operation +* +* Prefer op->max_freq. If it is unset, use a conservative fraction of +* the controller clock rather than dws->max_freq, which can overclock +* a slow flash. +* +* Return: Frequency in Hz to program into BAUDR. +*/ +u32 dw_spi_g2_op_freq (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 f = op->max_freq; + +if (!f) + f = dws->max_freq /8 ; + +return f; +} + +/** +* dw_spi_g2_program_baudr - Program per-CS BAUDR for a given target freq +* @dws: Driver state +* @freq: Target SPI clock frequency in Hz +* +* SCKDV must be even (or 1 for passthrough). Must be called with SSI +* disabled. If @freq is 0, fall back to a conservative fraction of the +* controller clock. No-op if no valid frequency is available or the +* active CS is out of range. +*/ +void dw_spi_g2_program_baudr (struct dw_spi_g2 *dws,u32 freq) +{ +u32 div, baud; + +if (dws->cs >=DW_SPI_G2_MAX_CS ) +return ; + +if (!freq) + freq = dws->max_freq /8 ; +if (!freq) +return ; + + div =DIV_ROUND_UP (dws->clk_rate, freq); +if (div <=1 ) { + baud =1 ;/* Passthrough mode */ + }else { +if (div &1 ) + div++;/* SCKDV must be even */ + baud = div; + } + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_BAUDR (dws->cs), baud); +} + +/** +* dw_spi_g2_update_ddr_drive_edge - Program per-CS DDR transmit drive edge +* @dws: Driver state +* @op: SPI memory operation (must have a DDR/DTR phase) +* +* At BAUDR 1 or 2 the drive edge is set for edge-aligned data so the TX +* delay line can centre the clock; at higher divisors the register returns +* to 0 (edge-aligned with sclk_out, default DDR behaviour). +*/ +static void dw_spi_g2_update_ddr_drive_edge (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 baud; + +if (!op->cmd.dtr && !op->addr.dtr && !op->data.dtr) +return ; + +if (dws->cs >=DW_SPI_G2_MAX_CS ) +return ; + + baud =dw_spi_g2_readl (dws->regs,DW_SPI_G2_BAUDR (dws->cs)) &0xFF ; +if (!baud) + baud =1 ; + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DDR_DRIVE_EDGE (dws->cs), + (baud <=2 ) ?1 :0 ); +} +/** * Indirect read operations */
@@ -658,6 +921,12 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws,return -EINVAL ; } dummy_cycles = (op->dummy.nbytes *8 ) / op->dummy.buswidth; +/* +* In DTR mode two bits are clocked per cycle, so the dummy +* phase needs half as many controller cycles as in SDR mode. +*/ +if (op->data.dtr && dummy_cycles) + dummy_cycles /=2 ;if (dummy_cycles >DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX ) {dev_err (dws->dev,"Dummy cycles %u exceed maximum %u\n" , dummy_cycles,DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX );
@@ -693,11 +962,25 @@ static int dw_spi_g2_indirect_read_setup(struct dw_spi_g2 *dws, spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_DDR_EN_MASK ;if (op->addr.dtr || op->data.dtr) spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK ; -if (op->data.dtr) - spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK ; +/* +* DTR mode with data strobe: set RXDS_EN and enforce mutual +* exclusivity with the RX-delay sampling mode. +*/ +if (op->data.dtr) { + spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK ; +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_DATA_STROBE ); + }else { +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_RX_DELAY ); + }dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs), spictrlr0); +/* Per-op BAUDR: tuned ops use op->max_freq, others cap to safe rate */ +dw_spi_g2_program_baudr (dws,dw_spi_g2_op_freq (dws, op)); +dw_spi_g2_update_ddr_drive_edge (dws, op); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR ,0 ); dw_spi_g2_writel(dws->regs, DW_SPI_G2_RXFTLR, min_t(u32, (dws->fifo_len / 2) - 1,
@@ -823,9 +1106,25 @@ static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,if (op->addr.dtr || op->data.dtr) spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK ; +/* +* For writes, sampling mode is less critical but enforce +* mutual exclusivity for consistency with read path. +*/ +if (op->data.dtr) { +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_DATA_STROBE ); + }else { +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_RX_DELAY ); + }dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0);dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 );dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs), spictrlr0); + +/* Per-op BAUDR: tuned ops use op->max_freq, others cap to safe rate */ +dw_spi_g2_program_baudr (dws,dw_spi_g2_op_freq (dws, op)); +dw_spi_g2_update_ddr_drive_edge (dws, op); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_TXFTLR ,0 );dw_spi_g2_writel (dws->regs,DW_SPI_G2_RXFTLR ,0 ); }
@@ -893,6 +1192,679 @@ static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws,return ret; } +/* +* ======================================================================== +* XIP (Direct Mapping) Support +* ======================================================================== +*/ + +/** +* dw_spi_g2_xip_setup - Configure XIP mode for direct memory access +* @dws: Driver state +* @op: SPI memory operation template +* @is_write: true for write operations, false for read operations +* +* Configures the XIP registers to enable direct memory-mapped access. +* This allows CPU to read/write flash directly via memory bus without +* going through the SPI controller's data registers. +* +* Return: 0 on success, negative error code on failure +*/ +static int dw_spi_g2_xip_setup (struct dw_spi_g2 *dws, +const struct spi_mem_op *op, +bool is_write) +{ +u32 xip_ctrl =0 ; +u32 dummy_cycles =0 ; +int tt; + +if (!dws->xip_mem) { +dev_err (dws->dev,"XIP memory region not mapped\n" ); +return -EINVAL ; + } + + tt =dw_spi_g2_get_tt (op); +if (tt <0 ) { +dev_err (dws->dev,"Unsupported XIP transfer type\n" ); +return tt; + } + +if (op->dummy.nbytes) { +if (!op->dummy.buswidth || op->dummy.buswidth >8 ) +return -EINVAL ; + dummy_cycles = (op->dummy.nbytes *8 ) / op->dummy.buswidth; +/* +* In DTR mode two bits are clocked per cycle, so the dummy +* phase needs half as many controller cycles as in SDR mode. +*/ +if (op->data.dtr && dummy_cycles) + dummy_cycles /=2 ; +if (dummy_cycles >DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX ) +return -EINVAL ; + } + +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + +if (!is_write) { +/* Configure XIP read control */ + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_CTRL_FRF_MASK , +dw_spi_g2_get_frf (op)); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_CTRL_TRANS_TYPE_MASK , tt); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_CTRL_ADDR_L_MASK , +dw_spi_g2_addr_len (op->addr.nbytes)); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_CTRL_INST_L_MASK , +dw_spi_g2_inst_len (op->cmd.nbytes)); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_CTRL_WAIT_CYCLES_MASK , + dummy_cycles); +/* +* DFS_HC (Data Frame Size - Hardware Controlled): let the +* XIP engine derive the frame size from the AHB transaction +* width instead of CTRLR0.DFS. With DFS_HC cleared the +* engine uses CTRLR0.DFS (which is 8, since +* ctlr->bits_per_word_mask = SPI_BPW_MASK(8)) and packs +* each AHB beat MSB-byte-first on the wire, which the +* little-endian AXI wrapper stores byte-reversed in memory +* (per-32-bit-word byte-swap on dirmap reads). Mirrors the +* XIP_WR_CTRL_DFS_HC bit already set on the write path. +*/ + xip_ctrl |=DW_SPI_G2_XIP_CTRL_DFS_HC ; + +if (op->data.dtr) { + xip_ctrl |=DW_SPI_G2_XIP_CTRL_DDR_EN ; + xip_ctrl |=DW_SPI_G2_XIP_CTRL_RXDS_EN ; +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_DATA_STROBE ); + }else { +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_RX_DELAY ); + } +if (op->cmd.dtr) + xip_ctrl |=DW_SPI_G2_XIP_CTRL_INST_DDR_EN ; +if (op->cmd.opcode !=0 ) + xip_ctrl |=DW_SPI_G2_XIP_CTRL_INST_EN ; + +/* Per-op BAUDR: matched ops at op->max_freq, else safe rate */ +dw_spi_g2_program_baudr (dws, +dw_spi_g2_op_freq (dws, op)); +dw_spi_g2_update_ddr_drive_edge (dws, op); + +/* +* XIP Performance Optimizations +* +* Continuous Transfer Mode (CONT_XFER_EN): +* When enabled, the controller omits the command byte on +* subsequent sequential reads from the same CS. +* +* Requirements: +* - Flash must support continuous read mode (e.g., 0xEB Fast +* Read Quad I/O with mode bits) +* - Must use consistent command across all accesses +* - CS must remain asserted between transfers +* +* The controller automatically re-sends the command when: +* - CS is de-asserted (chip select change) +* - Transfer type changes (e.g., switching from quad to single) +* +* Prefetch Mode (PREFETCH_EN): +* The controller speculatively fetches the next cache line +* when the current access completes, anticipating sequential +* reads. This reduces latency for instruction fetches during +* XIP code execution. +* +* Best for: Sequential code execution, linear data reads +* +* Both features are disabled by default for maximum flash +* compatibility and can be enabled via module parameters or +* device tree properties. +*/ +if (dws->xip_continuous_mode && op->cmd.opcode !=0 ) { + xip_ctrl |=DW_SPI_G2_XIP_CTRL_CONT_XFER_EN ; + +/* +* Program XIP_CNT_TIME_OUT +* In continuous mode, this hclk-cycle timeout deselects +* the target if no AHB request arrives within the +* window. Must be written while SSI is disabled. +*/ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_CNT_TIME_OUT , +FIELD_PREP (DW_SPI_G2_XIP_XTOC_MASK , +DW_SPI_G2_XIP_XTOC_DEFAULT )); + } + +if (dws->xip_prefetch_mode) + xip_ctrl |=DW_SPI_G2_XIP_CTRL_PREFETCH_EN ; + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_CTRL (dws->cs), + xip_ctrl); + +if (op->cmd.opcode !=0 ) { +dw_spi_g2_writel (dws->regs, +DW_SPI_G2_XIP_INCR_INST (dws->cs), + op->cmd.opcode); +dw_spi_g2_writel (dws->regs, +DW_SPI_G2_XIP_WRAP_INST (dws->cs), + op->cmd.opcode); + } + }else { +/* Configure XIP write control */ + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_WR_CTRL_FRF_MASK , +dw_spi_g2_get_frf (op)); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_WR_CTRL_TRANS_TYPE_MASK , tt); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_WR_CTRL_ADDR_L_MASK , +dw_spi_g2_addr_len (op->addr.nbytes)); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_WR_CTRL_INST_L_MASK , +dw_spi_g2_inst_len (op->cmd.nbytes)); + xip_ctrl |=FIELD_PREP (DW_SPI_G2_XIP_WR_CTRL_WAIT_CYCLES_MASK , + dummy_cycles); + xip_ctrl |=DW_SPI_G2_XIP_WR_CTRL_DFS_HC ; + +if (op->data.dtr) { + xip_ctrl |=DW_SPI_G2_XIP_WR_CTRL_DDR_EN ; +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_DATA_STROBE ); + }else { +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_RX_DELAY ); + } +if (op->cmd.dtr) + xip_ctrl |=DW_SPI_G2_XIP_WR_CTRL_INST_DDR_EN ; + +/* Per-op BAUDR: matched ops at op->max_freq, else safe rate */ +dw_spi_g2_program_baudr (dws, +dw_spi_g2_op_freq (dws, op)); +dw_spi_g2_update_ddr_drive_edge (dws, op); + +dw_spi_g2_writel (dws->regs, +DW_SPI_G2_XIP_WRITE_CTRL (dws->cs), + xip_ctrl); + +if (op->cmd.opcode !=0 ) { +dw_spi_g2_writel (dws->regs, +DW_SPI_G2_XIP_WRITE_INCR_INST (dws->cs), + op->cmd.opcode); +dw_spi_g2_writel (dws->regs, +DW_SPI_G2_XIP_WRITE_WRAP_INST (dws->cs), + op->cmd.opcode); + } + } + +dw_spi_g2_target_enable (dws, dws->cs); + +dev_dbg (dws->dev,"xip_setup: %s cmd=0x%02x addr_bytes=%u dummy=%u cs=%u\n" , + is_write ?"WRITE" :"READ" , + op->cmd.opcode, op->addr.nbytes, dummy_cycles, dws->cs); + +return 0 ; +} + +/** +* dw_spi_g2_dirmap_create - Create a direct mapping for XIP access +* @desc: Direct mapping descriptor +* +* Sets up XIP mode configuration for the specified operation template. +* The actual memory mapping is expected to be set up by the platform driver. +* +* This configures both CTRLR0 (for data frame size and SPI mode) and the +* XIP-specific registers (for command, address, and transfer type). +* +* Return: 0 on success, negative error code on failure +*/ +int dw_spi_g2_dirmap_create (struct spi_mem_dirmap_desc *desc) +{ +struct spi_device *spi = desc->mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); +const struct spi_mem_op *op = desc->info.op_tmpl; +u32 ctrlr0; +int ret; + +if (!dws->xip_mem) { +dev_dbg (dws->dev,"XIP memory region not available\n" ); +return -EOPNOTSUPP ; + } + +dev_dbg (dws->dev,"dirmap_create: cmd=0x%02x addr_width=%u dir=%s dummy=%u\n" , + op->cmd.opcode, op->addr.nbytes, + op->data.dir ==SPI_MEM_DATA_IN ?"READ" :"WRITE" , + op->dummy.nbytes); + + dws->cs =spi_get_chipselect (spi,0 ); + dws->curr_spi = spi; + +/* +* Configure CTRLR0 with data frame size and SPI mode settings. +* XIP mode uses these settings for all memory-mapped accesses. +*/ +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + + ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TR , +DW_SPI_G2_FRF_STANDARD ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); + +/* Configure XIP registers for read/write operations */ + ret =dw_spi_g2_xip_setup (dws, op, + op->data.dir ==SPI_MEM_DATA_OUT ); +/* +* xip_setup enables the target (sets BIT(cs) in +* DEDICATED_CS_EN) to program per-CS registers. +* Clean it up so stale bits don't leak into +* cmd_only / reg_op operations on other CS lines. +* dirmap_read/write will re-enable when needed. +*/ +dw_spi_g2_target_disable (dws, dws->cs); +return ret; +} + +/** +* dw_spi_g2_dirmap_read - Direct memory-mapped read from flash +* @desc: Direct mapping descriptor +* @offs: Offset within the mapped region +* @len: Number of bytes to read +* @buf: Destination buffer +* +* Performs direct memory-mapped read using XIP mode. The controller +* automatically sends the configured command and address for each access. +* Supports 8/16/32-bit data frame sizes for optimal performance. +* +* Return: Number of bytes read on success, negative error code on failure +*/ +ssize_t dw_spi_g2_dirmap_read (struct spi_mem_dirmap_desc *desc, +u64 offs,size_t len,void *buf) +{ +struct spi_device *spi = desc->mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); +void __iomem *src; +u32 ctrlr0; + + dws->cs =spi_get_chipselect (spi,0 ); + dws->curr_spi = spi; + +if (!dws->use_xip) { +struct spi_mem_op op = *desc->info.op_tmpl; +size_t chunk, done =0 ; +unsigned int dummy_shift =0 ; +int ret; + +/* Clear XIP registers so they don't interfere with RO mode */ +dw_spi_g2_enable (dws, false); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_CTRL (dws->cs),0 ); + +/* +* Fold dummy bytes into the address field. +* +* To handle WAIT_CYCLES in RO mode -- +* the RX FIFO stays empty when WAIT_CYCLES > 0. Work around +* this by absorbing the dummy phase into the address: shift the +* real address left by (dummy_nbytes * 8) bits and zero-pad. +* This makes WAIT_CYCLES=0 while the flash still receives the +* correct number of clock cycles before data. +* +* With TT1 (1-1-x), both address and folded-dummy bits travel +* on the standard single lane, so the flash sees: +* [real addr MSB..LSB] [zero-pad dummy clocks] [data on x lanes] +*/ +if (op.dummy.nbytes) { + dummy_shift = op.dummy.nbytes *8 ; + op.addr.nbytes += op.dummy.nbytes; + op.dummy.nbytes =0 ; + } + +while (done < len) { + chunk =min_t (size_t, len - done, dws->fifo_len); + + op.addr.val = (u64)(offs + done) << dummy_shift; + op.data.nbytes = chunk; + op.data.buf.in = (u8 *)buf + done; + + ret =dw_spi_g2_indirect_read (dws, &op); +if (ret) +return ret; + + done += chunk; + } + +return (ssize_t )len; + } + +if (!dws->xip_mem) +return -EOPNOTSUPP ; + +if (offs + len > dws->xip_len) +return -EINVAL ; + +/* +* Restore full XIP register state. An indirect transfer (from a +* prior mode switch) may have left CTRLR0 in TMOD_RO/TO and +* SPI_CTRLR0 with non-XIP framing, which corrupts XIP accesses. +*/ +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TR , +DW_SPI_G2_FRF_STANDARD ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); +dw_spi_g2_xip_setup (dws, desc->info.op_tmpl, false); + + src = dws->xip_mem + offs; + +/* Enable XIP mode for read */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_SER ,BIT (dws->cs)); +dw_spi_g2_enable (dws, true); + +/* +* PIO read from XIP window. The XIP engine turns each AHB read +* into a (cmd + addr + dummy + data) SPI transaction. +*/ +memcpy_fromio (buf, src, len); + +/* Disable XIP mode and clean up DEDICATED_CS_EN */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_SER ,0 ); +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + +return (ssize_t )len; +} + +/** +* dw_spi_g2_dirmap_write_nor_xip - byte-by-byte NOR XIP write hack +* @dws: driver state +* @desc: dirmap descriptor (XIP write template lives in info.op_tmpl) +* @offs: byte offset inside the flash +* @len: byte count to program +* @buf: data to program +* +* Single-byte XIP write protocol: for every byte we issue WREN +* out-of-band, arm the XIP write engine, do exactly one writeb() into +* the XIP window (the engine emits one cmd+addr+1-data Page Program), +* then poll SR1 until WIP clears. +* +* Functionally correct but ~3 SPI transactions per byte, so dramatically +* slower than the indirect-FIFO path (one cmd+addr+up-to-256 Page Program +* per call). It is selected automatically whenever XIP is enabled on the +* controller (dws->use_xip && dws->xip_mem != NULL), so any NOR write +* issued through dirmap_write() exercises the XIP-write engine end-to-end. +*/ +static ssize_t dw_spi_g2_dirmap_write_nor_xip (struct dw_spi_g2 *dws, +struct spi_mem_dirmap_desc *desc, +u64 offs,size_t len, +const void *buf) +{ +const struct spi_mem_op wren_op =SPI_MEM_OP (SPI_MEM_OP_CMD (DW_SPI_G2_NOR_OP_WREN ,1 ), +SPI_MEM_OP_NO_ADDR , +SPI_MEM_OP_NO_DUMMY , +SPI_MEM_OP_NO_DATA ); +const u8 *src = buf; +void __iomem *dst; +ssize_t result; +u32 ctrlr0; +u8 sr1; +size_t i; +int ret; + +if (!dws->xip_mem) +return -EOPNOTSUPP ; + +if (len > dws->xip_len || offs > dws->xip_len - len) +return -EINVAL ; + + dst = dws->xip_mem + offs; + +dev_dbg (dws->dev, +"dirmap_write_nor_xip: BEGIN offs=0x%llx len=%zu (byte-by-byte WREN/WIP)\n" , + (unsigned long long )offs, len); + +for (i =0 ; i < len; i++) { +const struct spi_mem_op rdsr_op = +SPI_MEM_OP (SPI_MEM_OP_CMD (DW_SPI_G2_NOR_OP_RDSR1 ,1 ), +SPI_MEM_OP_NO_ADDR , +SPI_MEM_OP_NO_DUMMY , +SPI_MEM_OP_DATA_IN (1 , &sr1,1 )); +unsigned long deadline; + +dev_dbg (dws->dev, +"dirmap_write_nor_xip: byte %zu/%zu @ +0x%zx\n" , + i +1 , len, (size_t )offs + i); + +/* WREN: latch the write-enable bit so the next Page Program is accepted. */ + ret =dw_spi_g2_cmd_only (dws, &wren_op); +if (ret) { +dev_err (dws->dev, +"XIP NOR write: WREN failed at byte %zu: %d\n" , + i, ret); + result = ret; + goto out_unwind; + } + +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + + ctrlr0 =dw_spi_g2_build_ctrlr0 (dws, +DW_SPI_G2_CTRLR0_TMOD_TR , +DW_SPI_G2_FRF_STANDARD ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), + ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); +dw_spi_g2_xip_setup (dws, desc->info.op_tmpl, true); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER , +BIT (dws->cs)); +dw_spi_g2_enable (dws, true); + +writeb (src[i], dst + i); + +dw_spi_g2_wait_idle (dws); + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER ,0 ); +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + +/* WIP poll: spin on SR1.WIP until the program completes. */ + sr1 =0 ; + deadline = jiffies + +msecs_to_jiffies (DW_SPI_G2_NOR_WIP_TIMEOUT_MS ); +do { + ret =dw_spi_g2_reg_op (dws, &rdsr_op); +if (ret) { +dev_err (dws->dev, +"XIP NOR write: WIP poll failed at byte %zu: %d\n" , + i, ret); + result = ret; + goto out_unwind; + } +if (!(sr1 &DW_SPI_G2_NOR_SR1_WIP )) +break ; +if (time_after (jiffies, deadline)) { +dev_err (dws->dev, +"XIP NOR write: WIP timeout at byte %zu\n" , + i); + result = -ETIMEDOUT ; + goto out_unwind; + } +udelay (1 ); + }while (1 ); + } + +dev_dbg (dws->dev, +"dirmap_write_nor_xip: END offs=0x%llx len=%zu OK\n" , + (unsigned long long )offs, len); + +return (ssize_t )len; + +out_unwind: +/* +* On any error mid-loop the XIP-write engine may be left armed +* (XIP_WR_SER set, SSI enabled). Tear it down so the next op sees +* a clean controller state regardless of where the failure landed. +*/ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER ,0 ); +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); +return result; +} + +/** +* dw_spi_g2_dirmap_write - Direct memory-mapped write to flash +* @desc: Direct mapping descriptor +* @offs: Offset within the mapped region +* @len: Number of bytes to write +* @buf: Source buffer +* +* Performs direct memory-mapped write using XIP mode. The controller +* automatically sends the configured command and address for each access. +* Supports 8/16/32-bit data frame sizes for optimal performance. +* +* Routing rules: +* - NAND Program Load (addr_nbytes == 2): XIP burst path, pushed as a +* writeb() loop. The on-die page buffer auto-increments the column +* counter on each transaction; the per-byte writeb() pace is required +* because this controller's XIP-write engine only emits the first +* byte of each multi-beat AHB store, so any wider PIO/DMA write +* would corrupt the payload past the first byte of each beat. +* - NOR Page Program (addr_nbytes >= 3) with XIP enabled: byte-by-byte +* XIP-write helper (dw_spi_g2_dirmap_write_nor_xip). The +* XIP_WRITE_CTRL register has no auto-WREN bit, so a bursted XIP +* write to NOR would drop everything past the first transaction +* (write-enable cleared). Doing one WREN + 1-byte XIP store + WIP poll per byte +* is slow but functional. +* - NOR Page Program (addr_nbytes >= 3) with XIP disabled: indirect +* FIFO path. Builds one cmd+addr+up-to-256-data SPI transaction in +* software; correct and dramatically faster than the XIP-write +* helper, used as a fallback when no XIP region is mapped. +* +* Return: Number of bytes written on success, negative error code on failure +*/ +ssize_t dw_spi_g2_dirmap_write (struct spi_mem_dirmap_desc *desc, +u64 offs,size_t len,const void *buf) +{ +struct spi_device *spi = desc->mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); +void __iomem *dst; +u32 ctrlr0; +bool use_indirect = false; + + dws->cs =spi_get_chipselect (spi,0 ); + dws->curr_spi = spi; + +/* +* NOR Page Program (addr_nbytes >= 3) cannot ride the XIP-write +* engine in burst mode -- the engine emits one cmd+addr+data SPI +* transaction per AHB write but the SPI-NOR framework only issues +* WREN once before dirmap_write(), so every transaction past the +* first arrives with write-enable cleared and is silently dropped. +* NAND Program Load (addr_nbytes == 2) is XIP-friendly because the +* on-die page buffer has no per-byte write-enable gating, so it goes +* to the NAND-XIP-BURST path below. +*/ +if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >=3 ) + use_indirect = true; + +/* +* NOR with XIP enabled: route through the byte-by-byte XIP-write +* helper so the XIP-write engine is exercised end-to-end. +*/ +if (use_indirect && dws->use_xip && dws->xip_mem && + desc->info.op_tmpl->addr.nbytes >=3 ) { +dev_info (dws->dev, +"dirmap_write: ROUTE=NOR-XIP cmd=0x%02x offs=0x%llx len=%zu\n" , + desc->info.op_tmpl->cmd.opcode, + (unsigned long long )offs, len); +return dw_spi_g2_dirmap_write_nor_xip (dws, desc, offs, len, + buf); + } + +if (use_indirect) { +struct spi_mem_op op = *desc->info.op_tmpl; +int ret; + +dev_info (dws->dev, +"dirmap_write: ROUTE=NOR-INDIRECT-FIFO cmd=0x%02x offs=0x%llx len=%zu\n" , + op.cmd.opcode, (unsigned long long )offs, len); + +/* +* dirmap_create configured XIP_WRITE_CTRL with opcode, +* address length, etc. These non-zero values can cause +* the controller to misinterpret a plain FIFO (TO-mode) +* write. Zero the XIP write registers so the indirect +* path behaves identically to exec_mem_op. +*/ +dw_spi_g2_enable (dws, false); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER ,0 ); +dw_spi_g2_writel (dws->regs, +DW_SPI_G2_XIP_WRITE_CTRL (dws->cs),0 ); + + op.addr.val = offs; + op.data.nbytes = len; + op.data.buf.out = buf; + + ret =dw_spi_g2_indirect_write (dws, &op); +return ret ? ret : (ssize_t )len; + } + +/* XIP write path — only reached for NAND Program Load */ +if (!dws->xip_mem) +return -EOPNOTSUPP ; + +if (offs + len > dws->xip_len) +return -EINVAL ; + +dev_info (dws->dev, +"dirmap_write: ROUTE=NAND-XIP cmd=0x%02x offs=0x%llx len=%zu\n" , + desc->info.op_tmpl->cmd.opcode, + (unsigned long long )offs, len); + +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TR , +DW_SPI_G2_FRF_STANDARD ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); +dw_spi_g2_xip_setup (dws, desc->info.op_tmpl, true); + + dst = dws->xip_mem + offs; + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER ,BIT (dws->cs)); +dw_spi_g2_enable (dws, true); + +/* +* PIO write into the XIP window. The XIP engine turns every AHB +* write into one (cmd + addr + data) SPI transaction, but on +* this controller only the first byte of each AHB beat is shifted +* out to the flash -- a multi-byte AHB store (writel/writeq, as +* memcpy_toio() emits on arm64 with the MMU on) drops every byte +* past the first. The corruption pattern in flash is unmistakable: +* buf[0..7] = 00 01 02 03 04 05 06 07 +* flash[0..7] = 00 04 ff ff ff ff ff ff <- only +0 and +4 land +* confirming two 32-bit AHB beats per writeq, each beat emitting +* only its low byte. We must therefore push the payload as a +* writeb() loop so every byte is its own AHB beat -> its own SPI +* Program-Load transaction. The on-die NAND page buffer +* auto-increments its column counter on each transaction, so the +* full page lands correctly. +*/ + { +const u8 *src = buf; +size_t i; + +for (i =0 ; i < len; i++) +writeb (src[i], dst + i); + } + +dw_spi_g2_wait_idle (dws); + +/* Disable XIP WR mode and clean up DEDICATED_CS_EN */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER ,0 ); +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + +return (ssize_t )len; +} +/* * ======================================================================== * SPI-MEM interface
@@ -938,8 +1910,20 @@ static bool dw_spi_g2_supports_mem_ops(struct spi_mem *mem,return spi_mem_default_supports_op (mem, op); } +/* +* Controller capabilities advertised to the spi-mem core: +* - .dtr = true: controller can run DTR (octal-DTR / DDR) phases. +* - .per_op_freq = true: dw_spi_g2_program_baudr() reprograms BAUDR +* at the start of every op, so the spi-mem core may set +* op->max_freq below mem->spi->max_speed_hz on a per-op basis +* (e.g. SPI-NAND cache reads capping at ~50-104 MHz, or an +* SFDP-derived per-op cap). Without this flag, spi-mem rejects +* any op where op->max_freq differs from mem->spi->max_speed_hz +* (see spi-mem.c::spi_mem_check_op()). +*/ static const struct spi_controller_mem_caps dw_spi_g2_mem_caps = { .dtr = true, + .per_op_freq = true, };int dw_spi_g2_adjust_mem_op_size (struct spi_mem *mem,struct spi_mem_op *op)
@@ -989,6 +1973,13 @@ static int dw_spi_g2_exec_mem_op(struct spi_mem *mem,return -EINVAL ; } dummy_cycles = (op->dummy.nbytes *8 ) / op->dummy.buswidth; +/* +* In DTR mode two bits are clocked per cycle, so halve the +* cycle count derived from dummy.nbytes before checking the +* hardware limit. +*/ +if (op->data.dtr && dummy_cycles) + dummy_cycles /=2 ;if (dummy_cycles >DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX ) {dev_err (dws->dev,"dummy cycles %u exceed maximum %u\n" , dummy_cycles,DW_SPI_G2_SPI_CTRLR0_WAIT_CYCLES_MAX );
@@ -1029,6 +2020,9 @@ static const struct spi_controller_mem_ops dw_spi_g2_mem_ops = { .supports_op = dw_spi_g2_supports_mem_ops, .adjust_op_size = dw_spi_g2_adjust_mem_op_size, .exec_op = dw_spi_g2_exec_mem_op, + .dirmap_create = dw_spi_g2_dirmap_create, + .dirmap_read = dw_spi_g2_dirmap_read, + .dirmap_write = dw_spi_g2_dirmap_write, };/*
@@ -1075,15 +2069,7 @@ int dw_spi_g2_init(struct dw_spi_g2 *dws)dev_info (dws->dev,"DW SSI G2 core init\n" ); -/* Disable controller and reset to clean state */ -dw_spi_g2_enable (dws, false); -dw_spi_g2_writel (dws->regs,DW_SPI_G2_SER ,0 ); -dw_spi_g2_writel (dws->regs,DW_SPI_G2_DEDICATED_CS_EN ,0 ); - -/* Mask all interrupts — this driver uses polling only */ -dw_spi_g2_writel (dws->regs,DW_SPI_G2_IMR ,0 ); -/* Clear all interrupts by writing 1s to defined ICR bits */ -dw_spi_g2_writel (dws->regs,DW_SPI_G2_ICR ,DW_SPI_G2_ICR_ALL ); +dw_spi_g2_hw_init (dws);/* Hardware capabilities */ dws->fifo_len =dw_spi_g2_detect_fifo_len (dws);
@@ -1126,6 +2112,42 @@ void dw_spi_g2_deinit(struct dw_spi_g2 *dws) }EXPORT_SYMBOL_NS_GPL (dw_spi_g2_deinit,"SPI_DW_G2_CORE" ); +/** +* dw_spi_g2_hw_init - Re-initialize controller hardware after power-up +* @dws: Driver state +* +* Brings the controller back to a known clean state after clocks are +* restored (e.g. resume from suspend). Resets SSI enable, chip selects, +* dedicated CS, interrupt mask, and clears any pending interrupts. +* +* Does NOT reconfigure per-CS transfer registers; those are set up +* on-demand by the first spi-mem operation after resume. +* +* Return: 0 on success +*/ +int dw_spi_g2_hw_init (struct dw_spi_g2 *dws) +{ +u8 cs; + +dw_spi_g2_enable (dws, false); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DEDICATED_CS_EN ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_IMR ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_ICR ,DW_SPI_G2_ICR_ALL ); + +/* +* Set DDR_DRIVE_EDGE for all CS. Value of 1 means data is driven +* on the falling edge of ssi_clk in DDR mode, which aligns the +* output with the sampling window on the flash side. +*/ +for (cs =0 ; cs <DW_SPI_G2_MAX_CS ; cs++) +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DDR_DRIVE_EDGE (cs),1 ); + + +return 0 ; +} +EXPORT_SYMBOL_NS_GPL (dw_spi_g2_hw_init,"SPI_DW_G2_CORE" ); +/* * ======================================================================== * Legacy SPI transfer_one interface (not actively used with spi-mem)
drivers/spi/spi-dw-g2-mmio.c +262 -1
303 lines not on this page · raw message
drivers/spi/spi-dw-g2.h +159 -7
242 lines not on this page · raw message
Signature · 1 line
2.34.1
[email protected]
AI reviewRe: [PATCH 6/6] spi: dw-g2: Add XIP support
Thank you for your contribution! Sashiko AI review found 12 potential issues to consider: - [High] spi: dw-g2: broken NOR XIP write path in dw_spi_g2_dirmap_write() - [High] spi: dw-g2: address…
Thank you for your contribution! Sashiko AI review found 12 potential issues to consider:
- [High] spi: dw-g2: broken NOR XIP write path in dw_spi_g2_dirmap_write()
- [High] spi: dw-g2: address truncation and shift overflow in dw_spi_g2_dirmap_read()
- [High] spi: dw-g2: ignored desc->info.offset and bounds overflow in dirmap ops
- [High] spi: dw-g2: stale XIP_CTRL and XIP_WRITE_CTRL left set for exec_mem_op
- [High] spi: dw-g2: MMIO write after reset and missing put in dw_spi_g2_mmio_remove()
- [Medium] spi: dw-g2: sampling mode bits clobbered after dw_spi_g2_set_sampling_mode()
- [Medium] spi: dw-g2: broken per-CS rx_sample_dly tracking and missing DT default
- [Medium] spi: dw-g2: 8-bit mask on 16-bit BAUDR in dw_spi_g2_update_ddr_drive_edge()
- [Medium] spi: dw-g2: dw_spi_g2_op_freq() ignores max_speed_hz and reg_op skips BAUDR
- [Medium] spi: dw-g2: missing dw_spi_g2_deinit() on probe failure leaks debugfs
- [Medium] spi: dw-g2: missing op validation in dirmap_create and unchecked errors
- [Low] spi: dw-g2: unused DMA struct fields and duplicate undocumented sysfs files
--
commit 31552d5f4b52b9bd42041e49fac8bf977363c65c
Author: Prakriti Sharma <[email protected]>
spi: dw-g2: Add XIP support
This commit adds memory-mapped execute-in-place (XIP) support via the spi-mem direct-mapping interface, along with runtime mode controls, per-op baud rate programming, and RX sampling configuration.
Quoted 4 lines
drivers/spi/spi-dw-g2-core.c +0
-0
[ ... ]
Quoted 16 lines
drivers/spi/spi-dw-g2-core.c +12
-0@@ -152,28 +261,61 @@ int dw_spi_g2_setup(struct spi_device *spi)dw_spi_g2_target_disable (dws, dws->cs);dw_spi_g2_writel (dws->regs,DW_SPI_G2_BAUDR (dws->cs), baud); +/* Configure RX sample delay for chip select */ +if (dws->cs <DW_SPI_G2_MAX_CS ) { +u32 rx_dly_ns; + +if (device_property_read_u32 (&spi->dev,"rx-sample-delay-ns" , + &rx_dly_ns)) + rx_dly_ns = dws->def_rx_sample_dly_ns; + + dws->rx_sample_dly[dws->cs] = +DIV_ROUND_CLOSEST (rx_dly_ns, +NSEC_PER_SEC / dws->max_freq); + }
[ ... ]
Quoted 17 lines
drivers/spi/spi-dw-g2-core.c +13
-0@@ -230,6 +372,30 @@ static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf)return ctrlr0; } +static void dw_spi_g2_controller_reset (struct dw_spi_g2 *dws) +{ +int drain =0 ; + +dw_spi_g2_enable (dws, false); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_ICR ,DW_SPI_G2_ICR_ALL ); + +/* Drain any stale RX data left from previous transfers */ +while (dw_spi_g2_readl (dws->regs,DW_SPI_G2_RXFLR ) >0 && + drain < dws->fifo_len) { + (void )dw_spi_g2_readl (dws->regs,DW_SPI_G2_DR0 ); + drain++;
drivers/spi/spi-dw-g2-core.c static u32 dw_spi_g2_build_ctrlr0(struct dw_spi_g2 *dws, u8 tmod, u8 frf) · line 397
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+ } + +/* Update RX sample delay for current CS if value differs. */ +if (dws->cs <DW_SPI_G2_MAX_CS && + dws->rx_sample_dly[dws->cs] != dws->cur_rx_sample_dly) { +dw_spi_g2_writel (dws->regs,DW_SPI_G2_RX_SAMPLE_DELAY (dws->cs), + dws->rx_sample_dly[dws->cs]); + dws->cur_rx_sample_dly = dws->rx_sample_dly[dws->cs]; + } +}
[Severity: Medium]
Does tracking per-CS DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs) registers with a single scalar dws->cur_rx_sample_dly skip programming other chip selects? If CS0 and CS1 share the same non-zero rx_sample_dly value, the first transfer on CS0 updates dws->cur_rx_sample_dly, causing subsequent transfers on CS1 to see a match and leave DW_SPI_G2_RX_SAMPLE_DELAY(1) unprogrammed.
In addition, dws->def_rx_sample_dly_ns is never read from the controller device node during probe, XIP transfers never call dw_spi_g2_controller_reset() to program DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs), and dw_spi_g2_hw_init() does not reset the cached delay state after a hardware reset. Writing DW_SPI_G2_RX_SAMPLE_DELAY(dws->cs) directly in dw_spi_g2_setup() and restoring it in dw_spi_g2_hw_init() would avoid the stale cache.
[ ... ]
Quoted 15 lines
drivers/spi/spi-dw-g2-core.c +11
-0@@ -629,6 +808,90 @@ static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op)return ret; } +/** +* dw_spi_g2_op_freq - Choose the SPI clock frequency for a given op +* @dws: Driver state +* @op: SPI memory operation +* +* Prefer op->max_freq. If it is unset, use a conservative fraction of +* the controller clock rather than dws->max_freq, which can overclock +* a slow flash. +* +* Return: Frequency in Hz to program into BAUDR. +*/
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op) · line 831
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+u32 dw_spi_g2_op_freq (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 f = op->max_freq; + +if (!f) + f = dws->max_freq /8 ; + +return f; +}
[Severity: Medium]
Can dw_spi_g2_op_freq() ignore the target device's spi-max-frequency? Unlike spi_mem_exec_op(), the spi_mem_dirmap_create(), spi_mem_dirmap_read(), and spi_mem_dirmap_write() paths do not run spi_mem_adjust_op_freq() on desc->info.op_tmpl, so op->max_freq is 0 on standard SPI-NOR and SPI-NAND dirmap templates. Falling back to dws->max_freq / 8 instead of dws->curr_spi->max_speed_hz (and not clamping f to dws->curr_spi->max_speed_hz) can overclock flashes rated below dws->max_freq / 8 or throttle faster flashes.
Also, while dw_spi_g2_mem_caps advertises .per_op_freq = true, neither dw_spi_g2_reg_op_setup() nor dw_spi_g2_cmd_only_setup() calls dw_spi_g2_program_baudr(), so small transfers handled by dw_spi_g2_reg_op() and command-only ops run at whatever BAUDR the previous operation left behind.
Quoted 52 lines
drivers/spi/spi-dw-g2-core.c +52
-0+ +/** +* dw_spi_g2_program_baudr - Program per-CS BAUDR for a given target freq +* @dws: Driver state +* @freq: Target SPI clock frequency in Hz +* +* SCKDV must be even (or 1 for passthrough). Must be called with SSI +* disabled. If @freq is 0, fall back to a conservative fraction of the +* controller clock. No-op if no valid frequency is available or the +* active CS is out of range. +*/ +void dw_spi_g2_program_baudr (struct dw_spi_g2 *dws,u32 freq) +{ +u32 div, baud; + +if (dws->cs >=DW_SPI_G2_MAX_CS ) +return ; + +if (!freq) + freq = dws->max_freq /8 ; +if (!freq) +return ; + + div =DIV_ROUND_UP (dws->clk_rate, freq); +if (div <=1 ) { + baud =1 ;/* Passthrough mode */ + }else { +if (div &1 ) + div++;/* SCKDV must be even */ + baud = div; + } + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_BAUDR (dws->cs), baud); +} + +/** +* dw_spi_g2_update_ddr_drive_edge - Program per-CS DDR transmit drive edge +* @dws: Driver state +* @op: SPI memory operation (must have a DDR/DTR phase) +* +* At BAUDR 1 or 2 the drive edge is set for edge-aligned data so the TX +* delay line can centre the clock; at higher divisors the register returns +* to 0 (edge-aligned with sclk_out, default DDR behaviour). +*/ +static void dw_spi_g2_update_ddr_drive_edge (struct dw_spi_g2 *dws, +const struct spi_mem_op *op) +{ +u32 baud; + +if (!op->cmd.dtr && !op->addr.dtr && !op->data.dtr) +return ; +
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_reg_op(struct dw_spi_g2 *dws, const struct spi_mem_op *op) · line 893
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+if (dws->cs >=DW_SPI_G2_MAX_CS ) +return ; + + baud =dw_spi_g2_readl (dws->regs,DW_SPI_G2_BAUDR (dws->cs)) &0xFF ; +if (!baud) + baud =1 ; + +dw_spi_g2_writel (dws->regs,DW_SPI_G2_DDR_DRIVE_EDGE (dws->cs), + (baud <=2 ) ?1 :0 ); +}
[Severity: Medium]
Should dw_spi_g2_update_ddr_drive_edge() mask DW_SPI_G2_BAUDR(dws->cs) with 0xFFFF instead of 0xFF? Because BAUDR is a 16-bit divisor register, masking with 0xFF truncates any divisor >= 256 to its low byte and misprograms DW_SPI_G2_DDR_DRIVE_EDGE(dws->cs) to 1 whenever the low byte is 0 or 2 (such as divisors 256 or 258).
In addition, dw_spi_g2_program_baudr() does not clamp baud to DW_SPI_G2_MAX_BAUDR (65534) the way dw_spi_g2_setup() does, so a low frequency producing div >= 65535 rounds up to 0x10000 and writes 0 into the 16-bit divisor field.
[ ... ]
Quoted 8 lines
drivers/spi/spi-dw-g2-core.c +4
-0@@ -823,9 +1106,25 @@ static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws,if (op->addr.dtr || op->data.dtr) spictrlr0 |=DW_SPI_G2_SPI_CTRLR0_DDR_EN_MASK ; +/* +* For writes, sampling mode is less critical but enforce +* mutual exclusivity for consistency with read path. +*/
drivers/spi/spi-dw-g2-core.c static void dw_spi_g2_indirect_write_setup(struct dw_spi_g2 *dws, · line 1122
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +7
-0+if (op->data.dtr) { +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_DATA_STROBE ); + }else { +dw_spi_g2_set_sampling_mode (dws, dws->cs, +DW_SPI_G2_SAMPLING_RX_DELAY ); + }dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0);dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 );dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs), spictrlr0);
[Severity: Medium]
Doesn't writing ctrlr0 and spictrlr0 here immediately overwrite the register bits that dw_spi_g2_set_sampling_mode() just modified in hardware? Because local spictrlr0 in dw_spi_g2_indirect_write_setup() never sets DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK, the DW_SPI_G2_SPI_CTRLR0(dws->cs) write clobbers the strobe bit set by dw_spi_g2_set_sampling_mode().
Similarly, in dw_spi_g2_indirect_read_setup(), dw_spi_g2_set_sampling_mode() reads and writes the stale DW_SPI_G2_SPI_CTRLR0(dws->cs) value in hardware right before it is overwritten with local spictrlr0, and in dw_spi_g2_xip_setup() calling dw_spi_g2_set_sampling_mode() sets DW_SPI_G2_SPI_CTRLR0_INST_RXDS_EN_MASK in DW_SPI_G2_SPI_CTRLR0(dws->cs) after the XIP callers explicitly zeroed SPI_CTRLR0.
[ ... ]
Quoted 35 lines
drivers/spi/spi-dw-g2-core.c +35
-0+int dw_spi_g2_dirmap_create (struct spi_mem_dirmap_desc *desc) +{ +struct spi_device *spi = desc->mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); +const struct spi_mem_op *op = desc->info.op_tmpl; +u32 ctrlr0; +int ret; + +if (!dws->xip_mem) { +dev_dbg (dws->dev,"XIP memory region not available\n" ); +return -EOPNOTSUPP ; + } + +dev_dbg (dws->dev,"dirmap_create: cmd=0x%02x addr_width=%u dir=%s dummy=%u\n" , + op->cmd.opcode, op->addr.nbytes, + op->data.dir ==SPI_MEM_DATA_IN ?"READ" :"WRITE" , + op->dummy.nbytes); + + dws->cs =spi_get_chipselect (spi,0 ); + dws->curr_spi = spi; + +/* +* Configure CTRLR0 with data frame size and SPI mode settings. +* XIP mode uses these settings for all memory-mapped accesses. +*/ +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + + ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TR , +DW_SPI_G2_FRF_STANDARD ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); + +/* Configure XIP registers for read/write operations */ + ret =dw_spi_g2_xip_setup (dws, op, + op->data.dir ==SPI_MEM_DATA_OUT );
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1457
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+/* +* xip_setup enables the target (sets BIT(cs) in +* DEDICATED_CS_EN) to program per-CS registers. +* Clean it up so stale bits don't leak into +* cmd_only / reg_op operations on other CS lines. +* dirmap_read/write will re-enable when needed. +*/ +dw_spi_g2_target_disable (dws, dws->cs); +return ret; +}
[Severity: Medium]
Does dw_spi_g2_dirmap_create() validate the operation template before accepting the direct mapping? spi_mem_dirmap_create() calls ctlr->mem_ops->dirmap_create() without calling supports_op first, and neither dw_spi_g2_dirmap_create() nor dw_spi_g2_xip_setup() checks dw_spi_g2_supports_mem_ops(desc->mem, op), dws->cs < DW_SPI_G2_MAX_CS, op->cmd.nbytes <= 2, or op->addr.nbytes * 8U <= DW_SPI_G2_ADDR_L_MAX_BITS.
Also, the return values of dw_spi_g2_xip_setup() (in dw_spi_g2_dirmap_read(), dw_spi_g2_dirmap_write_nor_xip(), and dw_spi_g2_dirmap_write()) and dw_spi_g2_wait_idle() (in dw_spi_g2_cmd_only(), dw_spi_g2_dirmap_write_nor_xip(), and dw_spi_g2_dirmap_write()) are ignored, masking setup errors and controller timeouts as successful transfers.
Quoted 63 lines
drivers/spi/spi-dw-g2-core.c +63
-0+ +/** +* dw_spi_g2_dirmap_read - Direct memory-mapped read from flash +* @desc: Direct mapping descriptor +* @offs: Offset within the mapped region +* @len: Number of bytes to read +* @buf: Destination buffer +* +* Performs direct memory-mapped read using XIP mode. The controller +* automatically sends the configured command and address for each access. +* Supports 8/16/32-bit data frame sizes for optimal performance. +* +* Return: Number of bytes read on success, negative error code on failure +*/ +ssize_t dw_spi_g2_dirmap_read (struct spi_mem_dirmap_desc *desc, +u64 offs,size_t len,void *buf) +{ +struct spi_device *spi = desc->mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); +void __iomem *src; +u32 ctrlr0; + + dws->cs =spi_get_chipselect (spi,0 ); + dws->curr_spi = spi; + +if (!dws->use_xip) { +struct spi_mem_op op = *desc->info.op_tmpl; +size_t chunk, done =0 ; +unsigned int dummy_shift =0 ; +int ret; + +/* Clear XIP registers so they don't interfere with RO mode */ +dw_spi_g2_enable (dws, false); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_WR_SER ,0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_CTRL (dws->cs),0 ); + +/* +* Fold dummy bytes into the address field. +* +* To handle WAIT_CYCLES in RO mode -- +* the RX FIFO stays empty when WAIT_CYCLES > 0. Work around +* this by absorbing the dummy phase into the address: shift the +* real address left by (dummy_nbytes * 8) bits and zero-pad. +* This makes WAIT_CYCLES=0 while the flash still receives the +* correct number of clock cycles before data. +* +* With TT1 (1-1-x), both address and folded-dummy bits travel +* on the standard single lane, so the flash sees: +* [real addr MSB..LSB] [zero-pad dummy clocks] [data on x lanes] +*/ +if (op.dummy.nbytes) { + dummy_shift = op.dummy.nbytes *8 ; + op.addr.nbytes += op.dummy.nbytes; + op.dummy.nbytes =0 ; + } + +while (done < len) { + chunk =min_t (size_t, len - done, dws->fifo_len); + + op.addr.val = (u64)(offs + done) << dummy_shift; + op.data.nbytes = chunk; + op.data.buf.in = (u8 *)buf + done;
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1530
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+ + ret =dw_spi_g2_indirect_read (dws, &op); +if (ret) +return ret; + + done += chunk; + } + +return (ssize_t )len; + }
[Severity: High]
Doesn't folding op.dummy.nbytes into op.addr.nbytes and shifting op.addr.val left by dummy_shift truncate the flash address in dw_spi_g2_indirect_read()? Because dws->use_xip defaults to false while dw_spi_g2_dirmap_create() succeeds whenever dws->xip_mem is present, default dirmap reads take this path and call dw_spi_g2_indirect_read(), which casts chunk_op.addr.val to u32 and writes only a single 32-bit word:
drivers/spi/spi-dw-g2-core.c:dw_spi_g2_indirect_read() {
...
if (chunk_op.addr.nbytes) {
dw_spi_g2_writel(dws->regs, DW_SPI_G2_DR0,
(u32)chunk_op.addr.val);
tx_entries++;
}
...
}Whenever the original op.addr.nbytes + op.dummy.nbytes exceeds 4 bytes (such as a 3-byte or 4-byte SPI-NOR read with dummy bytes, or a 2-byte SPI-NAND 1-4-4 read with 4 dummy bytes), shifting left by dummy_shift pushes the upper address bits (or the entire address when op.dummy.nbytes >= 4) past bit 31 where the u32 cast discards them, reading from the wrong flash offset. When op.dummy.nbytes >= 8 (such as Octal or Octal DTR reads), dummy_shift >= 64 triggers an undefined 64-bit shift and overflows the 4-bit DW_SPI_G2_SPI_CTRLR0_ADDR_L_MASK field in dw_spi_g2_indirect_read_setup(). Moreover, when op.dummy.buswidth > 1 in 1-1-x (TT0) mode, op.dummy.nbytes is sized for the wider dummy buswidth rather than a single address lane, so adding op.dummy.nbytes to op.addr.nbytes sends the wrong number of dummy clock cycles.
Quoted 12 lines
drivers/spi/spi-dw-g2-core.c +12
-0+ +if (!dws->xip_mem) +return -EOPNOTSUPP ; + +if (offs + len > dws->xip_len) +return -EINVAL ; + +/* +* Restore full XIP register state. An indirect transfer (from a +* prior mode switch) may have left CTRLR0 in TMOD_RO/TO and +* SPI_CTRLR0 with non-XIP framing, which corrupts XIP accesses. +*/
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1552
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + ctrlr0 =dw_spi_g2_build_ctrlr0 (dws,DW_SPI_G2_CTRLR0_TMOD_TR , +DW_SPI_G2_FRF_STANDARD ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR0 (dws->cs), ctrlr0); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_SPI_CTRLR0 (dws->cs),0 ); +dw_spi_g2_writel (dws->regs,DW_SPI_G2_CTRLR1 (dws->cs),0 ); +dw_spi_g2_xip_setup (dws, desc->info.op_tmpl, false); + + src = dws->xip_mem + offs;
[Severity: High]
Shouldn't dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write() add desc->info.offset to offs? In the spi-mem dirmap interface, offs is relative to desc->info.offset; for example, spinand_create_dirmap() encodes the plane select bit in info.offset, so ignoring desc->info.offset here (and in the indirect fallback and write paths) causes multi-plane SPI-NAND reads and writes on plane >= 1 to access plane 0 instead.
Also, dw_spi_g2_dirmap_create() never checks that desc->info.offset and desc->info.length fit within dws->xip_len (preventing spi_mem_dirmap_create() from falling back to non-dirmap ops when the flash exceeds the XIP window), and the check offs + len > dws->xip_len in dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write() can wrap around on 64-bit unsigned addition.
Quoted 8 lines
drivers/spi/spi-dw-g2-core.c +8
-0+ +/* Enable XIP mode for read */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_SER ,BIT (dws->cs)); +dw_spi_g2_enable (dws, true); + +/* +* PIO read from XIP window. The XIP engine turns each AHB read +* into a (cmd + addr + dummy + data) SPI transaction.
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1570
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+*/ +memcpy_fromio (buf, src, len); + +/* Disable XIP mode and clean up DEDICATED_CS_EN */ +dw_spi_g2_writel (dws->regs,DW_SPI_G2_XIP_SER ,0 ); +dw_spi_g2_enable (dws, false); +dw_spi_g2_target_disable (dws, dws->cs); + +return (ssize_t )len; +}
[Severity: High]
Does leaving DW_SPI_G2_XIP_CTRL(dws->cs) and DW_SPI_G2_XIP_WRITE_CTRL(dws->cs) non-zero after dw_spi_g2_dirmap_create(), dw_spi_g2_dirmap_read(), and dw_spi_g2_dirmap_write() interfere with subsequent dw_spi_g2_exec_mem_op() FIFO transfers? As noted in the comments in the indirect branches of dw_spi_g2_dirmap_read() and dw_spi_g2_dirmap_write(), non-zero XIP_CTRL and XIP_WRITE_CTRL registers cause the controller to misinterpret FIFO RO/TO-mode operations, yet neither the XIP teardown paths nor the exec_mem_op setup helpers (dw_spi_g2_cmd_only_setup(), dw_spi_g2_reg_op_setup(), dw_spi_g2_indirect_read_setup(), dw_spi_g2_indirect_write_setup()) clear those registers.
[ ... ]
Quoted 28 lines
drivers/spi/spi-dw-g2-core.c +28
-0+ssize_t dw_spi_g2_dirmap_write (struct spi_mem_dirmap_desc *desc, +u64 offs,size_t len,const void *buf) +{ +struct spi_device *spi = desc->mem->spi; +struct dw_spi_g2 *dws =spi_controller_get_devdata (spi->controller); +void __iomem *dst; +u32 ctrlr0; +bool use_indirect = false; + + dws->cs =spi_get_chipselect (spi,0 ); + dws->curr_spi = spi; + +/* +* NOR Page Program (addr_nbytes >= 3) cannot ride the XIP-write +* engine in burst mode -- the engine emits one cmd+addr+data SPI +* transaction per AHB write but the SPI-NOR framework only issues +* WREN once before dirmap_write(), so every transaction past the +* first arrives with write-enable cleared and is silently dropped. +* NAND Program Load (addr_nbytes == 2) is XIP-friendly because the +* on-die page buffer has no per-byte write-enable gating, so it goes +* to the NAND-XIP-BURST path below. +*/ +if (!dws->use_xip || desc->info.op_tmpl->addr.nbytes >=3 ) + use_indirect = true; + +/* +* NOR with XIP enabled: route through the byte-by-byte XIP-write +* helper so the XIP-write engine is exercised end-to-end.
drivers/spi/spi-dw-g2-core.c static int dw_spi_g2_indirect_write(struct dw_spi_g2 *dws, · line 1778
Quoted 10 lines
drivers/spi/spi-dw-g2-core.c +10
-0+*/ +if (use_indirect && dws->use_xip && dws->xip_mem && + desc->info.op_tmpl->addr.nbytes >=3 ) { +dev_info (dws->dev, +"dirmap_write: ROUTE=NOR-XIP cmd=0x%02x offs=0x%llx len=%zu\n" , + desc->info.op_tmpl->cmd.opcode, + (unsigned long long )offs, len); +return dw_spi_g2_dirmap_write_nor_xip (dws, desc, offs, len, + buf); + }
[Severity: High]
Why does dw_spi_g2_dirmap_write() route NOR writes to dw_spi_g2_dirmap_write_nor_xip() when the commit message states that NOR page-program writes stay on the indirect FIFO path?
In dw_spi_g2_dirmap_write_nor_xip(), the controller driver bypasses the spi-nor layer and hardcodes 1-1-1 SDR WREN (0x06) and RDSR1 (0x05) commands along with 1-byte writeb() transactions for every byte:
drivers/spi/spi-dw-g2-core.c:dw_spi_g2_dirmap_write_nor_xip() {
const struct spi_mem_op wren_op = SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_WREN, 1),
SPI_MEM_OP_NO_ADDR,
SPI_MEM_OP_NO_DUMMY,
SPI_MEM_OP_NO_DATA);
...
for (i = 0; i < len; i++) {
const struct spi_mem_op rdsr_op =
SPI_MEM_OP(SPI_MEM_OP_CMD(DW_SPI_G2_NOR_OP_RDSR1, 1),
SPI_MEM_OP_NO_ADDR,
SPI_MEM_OP_NO_DUMMY,
SPI_MEM_OP_DATA_IN(1, &sr1, 1));
...
ret = dw_spi_g2_cmd_only(dws, &wren_op);
}This breaks flashes operating in 4-4-4 QPI, 8-8-8 Octal, or 8D-8D-8D Octal DTR mode (where 1-1-1 SDR 0x06/0x05 commands are invalid and 1-byte page programs violate 2-byte DTR / internal-ECC alignment), and it invokes dw_spi_g2_cmd_only() (a TO-mode FIFO transfer) while DW_SPI_G2_XIP_WRITE_CTRL(dws->cs) is still non-zero.
In addition, all three routing branches in dw_spi_g2_dirmap_write() call dev_info() on every write call, which will flood the kernel log during normal flash writes.
Quoted 4 lines
drivers/spi/spi-dw-g2-mmio.c +0
-0
[ ... ]
Quoted 21 lines
drivers/spi/spi-dw-g2-mmio.c +16
-1@@ -151,7 +386,29 @@ static int dw_spi_g2_mmio_probe(struct platform_device *pdev)if (ret) goto err_pm; -dev_info (&pdev->dev,"DW SSI G2 MMIO probed\n" ); + ret =device_create_file (&pdev->dev, &dev_attr_xip_mode); +if (ret) +dev_warn (&pdev->dev, +"Failed to create xip_mode sysfs attribute: %d\n" , + ret); + +if (dws->xip_mem) { + ret =device_create_file (&pdev->dev, + &dev_attr_xip_continuous_mode); +if (ret) +dev_warn (&pdev->dev, +"Failed to create xip_continuous_mode sysfs: %d\n" , + ret); + + ret =device_create_file (&pdev->dev, + &dev_attr_xip_prefetch_mode);
drivers/spi/spi-dw-g2-mmio.c static int dw_spi_g2_mmio_probe(struct platform_device *pdev) · line 414
Quoted 10 lines
drivers/spi/spi-dw-g2-mmio.c +7
-0+if (ret) +dev_warn (&pdev->dev, +"Failed to create xip_prefetch_mode sysfs: %d\n" , + ret); + } +dev_info (&pdev->dev,"DW SSI G2 MMIO probed (mode=%s)\n" , + dws->use_xip ?"XIP" :"indirect" );return 0 ; err_pm:
[Severity: Medium]
If devm_request_irq() or spi_register_controller() fails after dw_spi_g2_init(dws) has created the debugfs directory and files, doesn't jumping to err_reset or err_pm without calling dw_spi_g2_deinit(dws) leak the debugfs entries? Because spi_controller_put(ctlr) frees ctlr and the embedded dws structure on that error path, the leaked debugfs files retain dangling pointers to dws and dws->regset, causing a use-after-free if they are accessed in debugfs.
drivers/spi/spi-dw-g2-mmio.c static void dw_spi_g2_mmio_remove(struct platform_device *pdev) · line 436
Quoted 13 lines
drivers/spi/spi-dw-g2-mmio.c +4
-0@@ -168,10 +425,14 @@ static void dw_spi_g2_mmio_remove(struct platform_device *pdev)struct spi_controller *ctlr =platform_get_drvdata (pdev);struct dw_spi_g2_mmio *dwsmmio =spi_controller_get_devdata (ctlr); +device_remove_file (&pdev->dev, &dev_attr_xip_mode); +device_remove_file (&pdev->dev, &dev_attr_xip_continuous_mode); +device_remove_file (&pdev->dev, &dev_attr_xip_prefetch_mode);spi_unregister_controller (ctlr);dw_spi_g2_deinit (&dwsmmio->dws);pm_runtime_disable (&pdev->dev);reset_control_assert (dwsmmio->rstc); +dw_spi_g2_enable (&dwsmmio->dws, false); }
[Severity: High]
Doesn't calling dw_spi_g2_enable(&dwsmmio->dws, false) after reset_control_assert(dwsmmio->rstc) write to DW_SPI_G2_SSIENR while the controller is held in hardware reset? On many SoCs, MMIO accesses to an IP block with its reset line asserted trigger a synchronous external abort or bus hang; disabling the controller should happen before asserting reset.
Also, dw_spi_g2_mmio_probe() allocates ctlr with non-managed spi_alloc_host(), and spi_unregister_controller(ctlr) only drops the reference taken by spi_register_controller(), so dw_spi_g2_mmio_remove() still needs to call spi_controller_put(ctlr) to avoid leaking ctlr and dwsmmio.
Quoted 4 lines
drivers/spi/spi-dw-g2.h +0
-0
[ ... ]
Quoted 33 lines
drivers/spi/spi-dw-g2.h +23
-0@@ -260,9 +371,32 @@ struct dw_spi_g2 {struct completion mem_done;int error; +/* XIP (Direct Mapping) support */ +void __iomem *xip_mem; +resource_size_t xip_phys; +size_t xip_len; +bool use_xip; +bool xip_continuous_mode; +bool xip_prefetch_mode; + +/* Per-CS RX sample delay (in SSI clock cycles) */ +u8 rx_sample_dly[DW_SPI_G2_MAX_CS ]; +u8 cur_rx_sample_dly; +u32 def_rx_sample_dly_ns; +/* Flash type detection for timing workarounds */ bool is_nand; + +/* DMA support for FIFO/Indirect operations (External DMA) */ +struct dma_chan *tx_chan; +struct dma_chan *rx_chan; +struct completion tx_dma_complete; +struct completion rx_dma_complete; +bool use_dma_indirect;/* DMA for FIFO operations */ +bool use_dma_read;/* DMA for XIP (placeholder) */ +u32 dma_caps;/* DMA capability flags */ +#ifdef CONFIG_DEBUG_FS struct dentry *debugfs;struct debugfs_regset32 regset;
[ ... ]
Quoted 4 lines
drivers/spi/spi-dw-g2.h +0
-0@@ -319,6 +456,17 @@ int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op);irqreturn_t dw_spi_g2_irq (int irq,void *dev_id);
drivers/spi/spi-dw-g2.h int dw_spi_g2_adjust_mem_op_size(struct spi_mem *mem, struct spi_mem_op *op); · line 468
Quoted 10 lines
drivers/spi/spi-dw-g2.h +10
-0+/* XIP/Direct mapping support */ +int dw_spi_g2_dirmap_create (struct spi_mem_dirmap_desc *desc); +ssize_t dw_spi_g2_dirmap_read (struct spi_mem_dirmap_desc *desc, +u64 offs,size_t len,void *buf); +ssize_t dw_spi_g2_dirmap_write (struct spi_mem_dirmap_desc *desc, +u64 offs,size_t len,const void *buf); + +u32 dw_spi_g2_op_freq (struct dw_spi_g2 *dws, +const struct spi_mem_op *op); +void dw_spi_g2_program_baudr (struct dw_spi_g2 *dws,u32 freq);
[Severity: Low]
These seven DMA fields in struct dw_spi_g2 are unused in this patch, and dw_spi_g2_dirmap_create(), dw_spi_g2_dirmap_read(), dw_spi_g2_dirmap_write(), dw_spi_g2_op_freq(), dw_spi_g2_program_baudr(), and dw_spi_g2_set_sampling_mode() are only used within drivers/spi/spi-dw-g2-core.c and can be static. Also, dw_spi_g2_mmio_probe() creates undocumented sysfs attributes (xip_mode, xip_continuous_mode, xip_prefetch_mode) via device_create_file() after spi_register_controller() that duplicate the debugfs controls mentioned in the commit message.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Mark Brown
maintainerRe: [PATCH 6/6] spi: dw-g2: Add XIP support
In reply to Prakriti Sharmadrivers/spi/spi-dw-g2-core.c static const struct debugfs_reg32 dw_spi_g2_dbg_regs[] = { · line 80
Quoted 4 lines
Add memory-mapped execute-in-place through the spi-mem direct-mapping helpers.
drivers/spi/spi-dw-g2-core.c +1
-0+ *val = dws->xip_continuous_mode ?1 :0 ;
Please write normal conditional statements for improved legibility.
drivers/spi/spi-dw-g2-core.c static const struct debugfs_reg32 dw_spi_g2_dbg_regs[] = { · line 99
Quoted 3 lines
drivers/spi/spi-dw-g2-core.c +3
-0+ dws->xip_continuous_mode = !!val; +dev_info (dws->dev,"XIP continuous transfer mode: %s\n" , + dws->xip_continuous_mode ?"enabled" :"disabled" );
Don't log normal operations at anything above dev_db(), and consider if they should be logged at all.
drivers/spi/spi-dw-g2-core.c static const struct debugfs_reg32 dw_spi_g2_dbg_regs[] = { · line 103
Quoted 3 lines
drivers/spi/spi-dw-g2-core.c +3
-0+if (dws->xip_continuous_mode) +dev_info (dws->dev, +"Requires flash command supporting continuous read (e.g., 0xEB Quad I/O)\n" );
Shouldn't this be an error then?
drivers/spi/spi-dw-g2-mmio.c line 24
Quoted 11 lines
drivers/spi/spi-dw-g2-mmio.c +11
-0+/* +* Module parameter: select transfer mode at load time. +* 1 = XIP (memory-mapped direct access) +* 0 = indirect (default, FIFO-based, uses exec_mem_op path) +* +* Can also be changed at runtime via sysfs: +* /sys/devices/.../xip_mode +*/ +static int xip_mode; +module_param (xip_mode, int,0644 ); +MODULE_PARM_DESC (xip_mode,"Transfer mode: 0=indirect (default), 1=XIP" );
No module parameters please, I see you have other interfaces for configuring this anyway.
drivers/spi/spi-dw-g2.h line 15
Quoted 13 lines
drivers/spi/spi-dw-g2.h +7
-3-#include <linux/spi/spi.h> -#include <linux/io.h> +#include <linux/bitfield.h> +#include <linux/delay.h> #include <linux/device.h> -#include <linux/irqreturn.h> +#include <linux/io.h> #include <linux/iopoll.h> +#include <linux/irqreturn.h> +#include <linux/slab.h> +#include <linux/spi/spi.h> #include <linux/spi/spi-mem.h> +#include <linux/string.h>
You were using a bunch of these headers earlier in the series.
Signature · 1 line
Mark Brown <[email protected]>
kernel test robot
build botRe: [PATCH 6/6] spi: dw-g2: Add XIP support
In reply to Prakriti Sharmakernel test robot noticed the following build warnings:
Hi Prakriti,
kernel test robot noticed the following build warnings:
[auto build test WARNING on broonie-spi/for-next]
[also build test WARNING on robh/for-next linus/master v7.4-rc3 next-20261116] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Prakriti-Sharma/dt-bindings-spi-Add-Synopsys-DWC-SSI-v2-02a-binding/20261119-153508 base: https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next patch link: https://lore.kernel.org/r/20261119100513.433044-7-prakriti%40synopsys.com patch subject: [PATCH 6/6] spi: dw-g2: Add XIP support config: sh-randconfig-1000-20261120 (https://download.01.org/0day-ci/archive/20261120/[email protected]/config) compiler: sh4-linux-gcc (GCC) 14.4.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20261120/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
>> Warning: drivers/spi/spi-dw-g2-core.c:588 Incorrect use of kernel-doc format: * dw_spi_g2_cmd_only_setup - Configure controller for command-only operation >> Warning: drivers/spi/spi-dw-g2-core.c:596 function parameter 'dws' not described in 'dw_spi_g2_cmd_only_setup' >> Warning: drivers/spi/spi-dw-g2-core.c:596 function parameter 'op' not described in 'dw_spi_g2_cmd_only_setup' >> Warning: drivers/spi/spi-dw-g2-core.c:596 expecting prototype for Command(). Prototype was for dw_spi_g2_cmd_only_setup() instead >> Warning: drivers/spi/spi-dw-g2-core.c:895 This comment starts with '/**', but isn't a kernel-doc comment. Refer to Documentation/doc-guide/kernel-doc.rst
* Indirect read operations
Warning: drivers/spi/spi-dw-g2-core.c:1082 This comment starts with '/**', but isn't a kernel-doc comment. Refer to Documentation/doc-guide/kernel-doc.rst
* Indirect write operations (Page Program)
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Mark Brown
maintainerQuoted 2 lines
Add a driver and DT binding for the Synopsys DesignWare SSI Gen2 (
DWC_ssiv2.02a) controller.
I've sent a few comments on the individual patches, and I had a glance over the Sashiko output a bunch of which looks valid. I think the main thing is that there's a bunch of open coding, a lot of this feels not entirely Linuxish.
Signature · 1 line
Mark Brown <[email protected]>