From nobody Fri Sep 25 19:19:22 2026 Received: from rtits2.realtek.com.tw (rtits2.realtek.com [211.75.126.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 60A4D5592E7; Wed, 9 Sep 2026 12:57:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=211.75.126.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788958640; cv=none; b=Olgxf3f6fjHhhBRjsSRYp2vZJML9ySPPtQXncZAVFK/BJbHd9rnSZqARkV2GUPYGL7LkCZHM5rrij4eqjPdISQbdL7NV8W4H0kRVRF8XlltJq7QNFfFdVedA4LGwE73BSHq04vW7ryEEfoz6MzECTIxyq0ER3POsBZ4/M+c9/O8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788958640; c=relaxed/simple; bh=pg4Z2aSFmAbUBEghLQTaLGxlX6jySBBp16Cm2/SwMUQ=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=R4+8UQmwrm8nYPvvRE5vOVQNq7SeiPgDCel4DdGR8FKuWQyu6E3ZQIYqpHeMITn9KIe+I8WWSdcqNLVYk1rd3dUmTotaih2Kz5NC8A5O+aBbdV/p6Iong+0J+cA0m3GTQ2jQrz3JbKY2XYmEpc/lHBEe7GCAJD+HuXJLavo96NE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=realtek.com; spf=pass smtp.mailfrom=realtek.com; dkim=pass (2048-bit key) header.d=realtek.com header.i=@realtek.com header.b=E7B9bqT/; arc=none smtp.client-ip=211.75.126.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=realtek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=realtek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=realtek.com header.i=@realtek.com header.b="E7B9bqT/" X-SpamFilter-By: ArmorX SpamTrap 5.80 with qID 689Cv2JJ1624292, This message is accepted by code: ctloc85258 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=realtek.com; s=dkim; t=1788958622; bh=dYT+FrnrCTFCvz3l8ZG/tw+l1LHXjLQmRhnldpmJyb0=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type; b=E7B9bqT/2BmSqzXqaCPD44FNgzox2mIlYxkFqh5bkUPjoiglAnQ5IG0u2wVx79daQ guuFTUBUvXeE4DnqhFugVwKhZYGLD1FedHcfsF7fTiX8GJcXa/JuXS3u9zarTLolf6 Kg1Zh8f5YN/fxSa0Mcsn5SACEoG+BV0/3U3pv8BI0EEFULKHt3Z/v8Iz/FYrj8lJ5u mNY4q29Nny0X4jJNQ6xer49VdAg6+ryTQRit6ytDH/+3YYNbtrIrD/P9WuASDvJr4P MxTR3dkwMkzew/Wmu8+slMwcwvVUfeWGlCwdf4J4pdof/B1Z6cHnN9l7yb0iZWsvQC JHWsLVMamGnOA== Received: from mail.realtek.com (rtkexhmbs02.realtek.com.tw[172.21.6.41]) by rtits2.realtek.com.tw (8.15.2/3.29/5.94) with ESMTPS id 689Cv2JJ1624292 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 9 Sep 2026 20:57:02 +0800 Received: from RTKEXHMBS06.realtek.com.tw (10.21.1.56) by RTKEXHMBS02.realtek.com.tw (172.21.6.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.43; Wed, 9 Sep 2026 20:57:02 +0800 Received: from RTKEXHMBS03.realtek.com.tw (10.21.1.53) by RTKEXHMBS06.realtek.com.tw (10.21.1.56) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.43; Wed, 9 Sep 2026 20:57:02 +0800 Received: from cn1dhc-k02 (172.21.252.101) by RTKEXHMBS03.realtek.com.tw (10.21.1.53) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Wed, 9 Sep 2026 20:57:02 +0800 From: Yu-Chun Lin To: , , , CC: , , , , , , , , , Conor Dooley Subject: [PATCH v4 1/2] dt-bindings: spi: Add Realtek RTD1625 SPI NOR support Date: Wed, 9 Sep 2026 20:57:00 +0800 Message-ID: <20260909125701.273314-2-eleanor.lin@realtek.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260909125701.273314-1-eleanor.lin@realtek.com> References: <20260909125701.273314-1-eleanor.lin@realtek.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jyan Chou Add DT binding schema for Realtek RTD1625 SPI NOR Flash Controller Signed-off-by: Jyan Chou Co-developed-by: Yu-Chun Lin Signed-off-by: Yu-Chun Lin Acked-by: Conor Dooley --- Changes since v2: - Add Conor's acked-by tag. - Add "NOR" in commit message and binding title. --- .../bindings/spi/realtek,rtd1625-nor.yaml | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 Documentation/devicetree/bindings/spi/realtek,rtd1625-n= or.yaml diff --git a/Documentation/devicetree/bindings/spi/realtek,rtd1625-nor.yaml= b/Documentation/devicetree/bindings/spi/realtek,rtd1625-nor.yaml new file mode 100644 index 000000000000..014ddfe0f421 --- /dev/null +++ b/Documentation/devicetree/bindings/spi/realtek,rtd1625-nor.yaml @@ -0,0 +1,65 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/spi/realtek,rtd1625-nor.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: Realtek RTD1625 SPI NOR Flash Controller + +maintainers: + - Jyan Chou + - Yu-Chun Lin + +properties: + compatible: + enum: + - realtek,rtd1625-nor + + reg: + maxItems: 3 + + reg-names: + items: + - const: ctrl + - const: dma + - const: dirmap + + clocks: + maxItems: 1 + + resets: + maxItems: 1 + + "#address-cells": + const: 1 + + "#size-cells": + const: 0 + +required: + - compatible + - reg + - reg-names + - clocks + - resets + - "#address-cells" + - "#size-cells" + +allOf: + - $ref: /schemas/spi/spi-controller.yaml# + +unevaluatedProperties: false + +examples: + - | + spi@14b000 { + compatible =3D "realtek,rtd1625-nor"; + reg =3D <0x14b000 0x50>, <0x14bf00 0x30>, <0x88100000 0x2000000>; + reg-names =3D "ctrl", "dma", "dirmap"; + clocks =3D <&cc 18>; + resets =3D <&cc 26>; + pinctrl-0 =3D <&spi_pins>; + pinctrl-names =3D "default"; + #address-cells =3D <1>; + #size-cells =3D <0>; + }; --=20 2.43.0 From nobody Fri Sep 25 19:19:22 2026 Received: from rtits2.realtek.com.tw (rtits2.realtek.com [211.75.126.72]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 049DE55931F; Wed, 9 Sep 2026 12:57:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=211.75.126.72 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788958639; cv=none; b=i3how8/hEirVZ2bPRTm3BaEfJNdOlAgS0ZJ3HlT7CTYGeGzQoSdtCQZSd1de6cewdL0N2cTJA6qBMkMN9ZwH6ICVXwoJSVppD4fLyv7kWJGdON6GRescCsIjmeNxWWlJrsAem8/dh3aS6H1MdqEk0vq2sD1y9PBeDdoRkJlC1pU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788958639; c=relaxed/simple; bh=rQXxtPGTn+0le1P664FNQhsnU05yMQhSKpgUC/bN+C4=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=bV9QJwHA+9qSPPrKvK1a3vSE/pRJ0/T2/Z0qeWMVRy2geYZ7fTt4q1UTQUhhR2KNMe8frLI1N1pxZRpxV6QAR3iNFm5TJhLWkh4+NqtbYWXvaYM62yrlqTrkmaxasIfVFD1ZyCDdK4HvFun4Di4lPHU+zcIkhv22tChtkedYFzs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=realtek.com; spf=pass smtp.mailfrom=realtek.com; dkim=pass (2048-bit key) header.d=realtek.com header.i=@realtek.com header.b=ITHny0QW; arc=none smtp.client-ip=211.75.126.72 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=realtek.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=realtek.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=realtek.com header.i=@realtek.com header.b="ITHny0QW" X-SpamFilter-By: ArmorX SpamTrap 5.80 with qID 689Cv2JM1624292, This message is accepted by code: ctloc85258 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=realtek.com; s=dkim; t=1788958622; bh=IDkypqrozryxPL5Ml/ir1ioEnOnc/cQkd376ei/8pEc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Content-Type; b=ITHny0QW04OR/dfDULfkudWEusiFrhChNF8hgi/jz8dQdFMe/QVkwVxUlBm7QD4lV VCbDB4S2ITRuinB7D6Th8mq0Tbn8mK+/ieKqhApd0U2Rxw0B3W98DpOQ4zFycR6GsJ IQUHHph1b7h2Wt9TAD9haEv1SX9S90mDUF0wg560XGoPl739yAPGhMykGzQkt4H0uE wJGVc6frBziIM7+rp59CZyM9qokax5n+XLG07qfTYfMwKpWHCWLRC5d1Nov/+xJC2s 3iuHBscg9kmZoWAqxhcTBmOqh7yuE42W/Cyc0fagOIFxEEsLT5xwv/qunJ4O9pyIZv shNSLkKpvXb3w== Received: from mail.realtek.com (rtkexhmbs02.realtek.com.tw[172.21.6.41]) by rtits2.realtek.com.tw (8.15.2/3.29/5.94) with ESMTPS id 689Cv2JM1624292 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Wed, 9 Sep 2026 20:57:02 +0800 Received: from RTKEXHMBS06.realtek.com.tw (10.21.1.56) by RTKEXHMBS02.realtek.com.tw (172.21.6.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.43; Wed, 9 Sep 2026 20:57:03 +0800 Received: from RTKEXHMBS03.realtek.com.tw (10.21.1.53) by RTKEXHMBS06.realtek.com.tw (10.21.1.56) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.43; Wed, 9 Sep 2026 20:57:02 +0800 Received: from cn1dhc-k02 (172.21.252.101) by RTKEXHMBS03.realtek.com.tw (10.21.1.53) with Microsoft SMTP Server id 15.2.2562.49 via Frontend Transport; Wed, 9 Sep 2026 20:57:02 +0800 From: Yu-Chun Lin To: , , , CC: , , , , , , , , Subject: [PATCH v4 2/2] spi: spi-mem: Add Realtek SPI NOR flash controller driver Date: Wed, 9 Sep 2026 20:57:01 +0800 Message-ID: <20260909125701.273314-3-eleanor.lin@realtek.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20260909125701.273314-1-eleanor.lin@realtek.com> References: <20260909125701.273314-1-eleanor.lin@realtek.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Jyan Chou Add a spi-mem driver for the SPI NOR Flash Controller (SFC) found on Realtek DHC SoC. SFC supports Dual I/O. Implement the exec_op API for regular control commands and the dirmap API for hardware-accelerated read/write operations. The controller is described by three register ranges. The first one (ctrl) holds the control registers. The second one (dma) holds the DMA engine registers, used to transfer the payload between the flash and main memory. The third range is the memory aperture (dirmap) that maps the flash contents. Signed-off-by: Jyan Chou Co-developed-by: Yu-Chun Lin Signed-off-by: Yu-Chun Lin --- Changes since v2: - Add Yu-Chun's last name. - Specify "NOR" in the commit message and descriptions to avoid future conflicts with SPI NAND or GSPI. - Make SPDX license and copyright notice headers in C++-style. - Drop driver-internal polling (rtk_spi_read_status()), leaving the status polling and timeout handling to framework. - Track the 4-byte addressing state and properly restore it in rtk_spi_init= () to prevent state loss during runtime PM suspend/resume. - Add hardware cleanup in rtk_spi_dma_transfer() to explicitly trigger a graceful stop of the DMA engine if a timeout occurs. - Check bounds against the direct mapping window (dirmap_size) in both PIO = and DMA transfer paths. - Add reset_control_assert() in the probe error path and .remove(). - Replace RUNTIME_PM_OPS() + SYSTEM_SLEEP_PM_OPS() with DEFINE_RUNTIME_DEV_PM_OPS(). --- MAINTAINERS | 6 + drivers/spi/Kconfig | 10 + drivers/spi/Makefile | 1 + drivers/spi/spi-rtk-nor.c | 623 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 640 insertions(+) create mode 100644 drivers/spi/spi-rtk-nor.c diff --git a/MAINTAINERS b/MAINTAINERS index 34d5c9a0d562..20cd9332f823 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -22754,6 +22754,12 @@ S: Maintained F: Documentation/devicetree/bindings/spi/realtek,rtl9301-snand.yaml F: drivers/spi/spi-realtek-rtl-snand.c =20 +REALTEK SPI-NOR +M: Jyan Chou +M: Yu-Chun Lin +S: Supported +F: drivers/spi/spi-rtk-nor.c + REALTEK SYSTIMER DRIVER M: Hao-Wen Ting S: Maintained diff --git a/drivers/spi/Kconfig b/drivers/spi/Kconfig index 8782514bb89b..d45baa15a601 100644 --- a/drivers/spi/Kconfig +++ b/drivers/spi/Kconfig @@ -957,6 +957,16 @@ config SPI_RSPI help SPI driver for Renesas RSPI and QSPI blocks. =20 +config SPI_RTK_NOR + tristate "Realtek SPI-NOR flash controller" + depends on ARCH_REALTEK || COMPILE_TEST + depends on SPI_MEM + help + This enables support for the SPI NOR Flash Controller found on + Realtek DHC SoCs. The controller supports single and dual I/O + transfers and offloads the payload to an integrated DMA engine. + Say Y or M here if you are building a kernel for a Realtek DHC SoC + config SPI_RZV2H_RSPI tristate "Renesas RZ/V2H RSPI controller" depends on ARCH_RENESAS || COMPILE_TEST diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile index 9fa12498ce8c..68f4d3283e0c 100644 --- a/drivers/spi/Makefile +++ b/drivers/spi/Makefile @@ -131,6 +131,7 @@ obj-$(CONFIG_MACH_REALTEK_RTL) +=3D spi-realtek-rtl.o obj-$(CONFIG_SPI_REALTEK_SNAND) +=3D spi-realtek-rtl-snand.o obj-$(CONFIG_SPI_RPCIF) +=3D spi-rpc-if.o obj-$(CONFIG_SPI_RSPI) +=3D spi-rspi.o +obj-$(CONFIG_SPI_RTK_NOR) +=3D spi-rtk-nor.o obj-$(CONFIG_SPI_RZV2H_RSPI) +=3D spi-rzv2h-rspi.o obj-$(CONFIG_SPI_RZV2M_CSI) +=3D spi-rzv2m-csi.o obj-$(CONFIG_SPI_S3C64XX) +=3D spi-s3c64xx.o diff --git a/drivers/spi/spi-rtk-nor.c b/drivers/spi/spi-rtk-nor.c new file mode 100644 index 000000000000..5ccfa234b6b1 --- /dev/null +++ b/drivers/spi/spi-rtk-nor.c @@ -0,0 +1,623 @@ +// SPDX-License-Identifier: GPL-2.0 +// +// Realtek SPI Nor Flash Controller Driver (SFC) +// +// Copyright (c) 2024-2026 Realtek Semiconductor Corp. + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SFC_OPCODE 0x00 +#define DUAL_MODE_EN BIT(9) + +#define SFC_CTL 0x04 +#define RW_DATAEN BIT(4) +#define ADDR_EN BIT(3) + +#define SFC_SCK 0x08 +#define FDIV_MASK GENMASK(7, 0) + +#define SFC_CE 0x0c +#define DESLT_TIME 0x1a +#define DESLT_TIME_SHIFT 16 +#define PH_CNT 0x13 +#define PH_CNT_SHIFT 8 +#define PL_CNT 0x7 + +#define WT_PROM_DONE BIT(8) + +#define SFC_POS_LATCH 0x14 +#define FALLING_EDGE_EN 0x0 + +#define SFC_WAIT_WR 0x18 +#define SFC_EN_WR 0x1c +#define WT_PROM_EN BIT(8) + +#define SFC_ADR_FOUR_BYTE_EN 0x28 + +#define MD_FDMA_DDR_SADDR 0x0c +#define MD_FDMA_FL_SADDR 0x10 + +#define MD_FDMA_CTRL2 0x14 +#define MAX_XFER_DMA_LEN (BIT(26) | BIT(27)) +#define MAX_XFER_256 BIT(26) +#define DMA_TO_FLASH BIT(25) + +#define MD_FDMA_CTRL1 0x18 +#define DMA_W_EN_START BIT(3) +#define DMA_END BIT(2) +#define DMA_W_EN BIT(1) +#define DMA_START BIT(0) + +#define MD_FDMA_DDR_SADDR1 0x20 + +#define SFC_DMA_TIMEOUT 20000 +#define SFC_DMA_MAX_LEN 0x100 + +#define SFC_CTL_DMYCNT_MASK GENMASK(31, 24) +#define SFC_CTL_DMYCNT_SHIFT 24 + +#define DMA_HIGH_BITS_MASK 0x7 + +#define RTK_SPI_OP_RDSR 0x05 +#define RTK_SPI_OP_WREN 0x06 +#define RTK_SPI_OP_EN4B 0xb7 +#define RTK_SPI_OP_EX4B 0xe9 + +#define SFC_AUTOSUSPEND_TIMEOUT 2000 + +struct rtk_spi_host { + struct device *dev; + struct clk *clk; + struct reset_control *rstc; + void __iomem *regbase; + void __iomem *iobase; + void __iomem *mdbase; + void *buffer; + bool is_4byte; + dma_addr_t dma_buffer; + resource_size_t flash_phys_base; + size_t dirmap_size; +}; + +static u32 rtk_spi_calc_dummy_cycles(const struct spi_mem_op *op) +{ + if (!op->dummy.nbytes) + return 0; + + return (op->dummy.nbytes * 8) / op->dummy.buswidth; +} + +static void rtk_spi_read_mode(struct rtk_spi_host *host, const struct spi_= mem_op *op) +{ + u32 opcode =3D op->cmd.opcode; + u32 dummy_cycles, val; + + if (op->data.buswidth =3D=3D 2) + opcode |=3D DUAL_MODE_EN; + + writel(opcode, host->regbase + SFC_OPCODE); + + val =3D readl(host->regbase + SFC_CTL); + val |=3D RW_DATAEN | ADDR_EN; + + dummy_cycles =3D rtk_spi_calc_dummy_cycles(op); + + val &=3D ~SFC_CTL_DMYCNT_MASK; + val |=3D (dummy_cycles << SFC_CTL_DMYCNT_SHIFT) & SFC_CTL_DMYCNT_MASK; + writel(val, host->regbase + SFC_CTL); + + readl(host->iobase); +} + +static void rtk_spi_write_mode(struct rtk_spi_host *host, + const struct spi_mem_op *op) +{ + u32 opcode =3D op->cmd.opcode; + u32 val; + + writel(opcode, host->regbase + SFC_OPCODE); + + val =3D readl(host->regbase + SFC_CTL); + val |=3D RW_DATAEN | ADDR_EN; + writel(val, host->regbase + SFC_CTL); +} + +static void rtk_spi_enable_auto_write(struct rtk_spi_host *host) +{ + u32 val; + + val =3D WT_PROM_DONE | RTK_SPI_OP_RDSR; + writel(val, host->regbase + SFC_WAIT_WR); + + val =3D WT_PROM_EN | RTK_SPI_OP_WREN; + writel(val, host->regbase + SFC_EN_WR); +} + +static void rtk_spi_disable_auto_write(struct rtk_spi_host *host) +{ + writel(RTK_SPI_OP_RDSR, host->regbase + SFC_WAIT_WR); + writel(RTK_SPI_OP_WREN, host->regbase + SFC_EN_WR); +} + +static int rtk_spi_byte_transfer(struct rtk_spi_host *host, loff_t offset, + size_t len, unsigned char *buf, bool is_read) +{ + if (offset + len > host->dirmap_size) { + dev_err(host->dev, "PIO transfer out of bounds\n"); + return -EINVAL; + } + + if (is_read) + memcpy_fromio(buf, host->iobase + offset, len); + else + memcpy_toio(host->iobase + offset, buf, len); + + return 0; +} + +static void rtk_spi_init(struct rtk_spi_host *host) +{ + u32 val; + + val =3D readl(host->regbase + SFC_SCK); + val &=3D ~FDIV_MASK; + val |=3D (3 << 0) & FDIV_MASK; + writel(val, host->regbase + SFC_SCK); + + val =3D (DESLT_TIME << DESLT_TIME_SHIFT) | (PH_CNT << PH_CNT_SHIFT) | PL_= CNT; + writel(val, host->regbase + SFC_CE); + + writel(FALLING_EDGE_EN, host->regbase + SFC_POS_LATCH); + writel(RTK_SPI_OP_RDSR, host->regbase + SFC_WAIT_WR); + writel(RTK_SPI_OP_WREN, host->regbase + SFC_EN_WR); + + writel(host->is_4byte ? 0x1 : 0x0, host->regbase + SFC_ADR_FOUR_BYTE_EN); +} + +static int rtk_spi_command_read(struct rtk_spi_host *host, const struct sp= i_mem_op *op) +{ + size_t len =3D op->data.nbytes; + loff_t offset =3D op->addr.val; + u8 opcode =3D op->cmd.opcode; + u32 dummy_cycles, val; + + writel(opcode, host->regbase + SFC_OPCODE); + + val =3D readl(host->regbase + SFC_CTL); + val &=3D ~(SFC_CTL_DMYCNT_MASK | ADDR_EN); + val |=3D RW_DATAEN; + + if (op->addr.nbytes > 0) + val |=3D ADDR_EN; + + dummy_cycles =3D rtk_spi_calc_dummy_cycles(op); + val |=3D (dummy_cycles << SFC_CTL_DMYCNT_SHIFT) & SFC_CTL_DMYCNT_MASK; + writel(val, host->regbase + SFC_CTL); + + return rtk_spi_byte_transfer(host, offset, len, op->data.buf.in, true); +} + +static int rtk_spi_do_write_and_cmds(struct rtk_spi_host *host, const stru= ct spi_mem_op *op) +{ + u8 opcode =3D op->cmd.opcode; + u32 ctl_val =3D 0; + + writel(opcode, host->regbase + SFC_OPCODE); + + if (op->data.nbytes > 0) + ctl_val |=3D RW_DATAEN; + if (op->addr.nbytes > 0) + ctl_val |=3D ADDR_EN; + + writel(ctl_val, host->regbase + SFC_CTL); + + if (op->data.nbytes > 0) { + const u8 *buf =3D op->data.buf.out; + + if (op->addr.val + op->data.nbytes > host->dirmap_size) + return -EINVAL; + + for (unsigned int i =3D 0; i < op->data.nbytes; i++) + writeb(buf[i], host->iobase + op->addr.val + i); + } else { + if (op->addr.val >=3D host->dirmap_size) + return -EINVAL; + + readb(host->iobase + op->addr.val); + } + + /* + * Hardware Workaround: + * The controller's auto-mode engine requires SFC_ADR_FOUR_BYTE_EN to be + * explicitly updated when the flash enters or exits 4-byte mode via cont= rol + * commands. + */ + if ((op->addr.nbytes =3D=3D 4 || opcode =3D=3D RTK_SPI_OP_EN4B) && !host-= >is_4byte) { + host->is_4byte =3D true; + writel(0x1, host->regbase + SFC_ADR_FOUR_BYTE_EN); + } else if ((op->addr.nbytes =3D=3D 3 || opcode =3D=3D RTK_SPI_OP_EX4B) &&= host->is_4byte) { + host->is_4byte =3D false; + writel(0x0, host->regbase + SFC_ADR_FOUR_BYTE_EN); + } + + return 0; +} + +static int rtk_spi_dma_transfer(struct rtk_spi_host *host, loff_t offset, + size_t len, bool is_read) +{ + u64 dma_buffer, timeout_us =3D SFC_DMA_TIMEOUT * 100; + u32 flash_phys_addr, val; + int ret; + + if (offset + len > host->dirmap_size) { + dev_err(host->dev, "DMA transfer out of bounds\n"); + return -EINVAL; + } + + writel(DMA_W_EN_START | DMA_W_EN, host->mdbase + MD_FDMA_CTRL1); + + dma_buffer =3D host->dma_buffer; + + /* Setup MD DDR address and flash address */ + writel(lower_32_bits(dma_buffer), host->mdbase + MD_FDMA_DDR_SADDR); + writel(upper_32_bits(dma_buffer) & DMA_HIGH_BITS_MASK, + host->mdbase + MD_FDMA_DDR_SADDR1); + + /* MD_FDMA_FL_SADDR is a 32-bit hardware register */ + flash_phys_addr =3D lower_32_bits(host->flash_phys_base + offset); + writel(flash_phys_addr, host->mdbase + MD_FDMA_FL_SADDR); + + if (is_read) + val =3D MAX_XFER_DMA_LEN | len; + else + val =3D DMA_TO_FLASH | MAX_XFER_256 | len; + + writel(val, host->mdbase + MD_FDMA_CTRL2); + + writel(DMA_W_EN | DMA_START, host->mdbase + MD_FDMA_CTRL1); + udelay(1); + + ret =3D readl_poll_timeout(host->mdbase + MD_FDMA_CTRL1, val, + !(val & DMA_START), 100, timeout_us); + if (ret) { + dev_err(host->dev, "DMA transfer timed out\n"); + val =3D readl(host->mdbase + MD_FDMA_CTRL1); + val |=3D DMA_END | DMA_W_EN_START; + writel(val, host->mdbase + MD_FDMA_CTRL1); + + if (readl_poll_timeout(host->mdbase + MD_FDMA_CTRL1, val, + !(val & DMA_END), 10, 1000)) { + dev_err(host->dev, "Failed to stop DMA gracefully\n"); + } + + return ret; + } + + return 0; +} + +static int rtk_spi_exec_op(struct spi_mem *mem, const struct spi_mem_op *o= p) +{ + struct rtk_spi_host *host =3D spi_controller_get_devdata(mem->spi->contro= ller); + + if (op->data.dir =3D=3D SPI_MEM_DATA_IN) + return rtk_spi_command_read(host, op); + + return rtk_spi_do_write_and_cmds(host, op); +} + +static bool rtk_spi_supports_op(struct spi_mem *mem, + const struct spi_mem_op *op) +{ + if (op->cmd.buswidth !=3D 1) + return false; + + if (op->cmd.dtr || op->addr.dtr || op->data.dtr) + return false; + + if (op->addr.nbytes !=3D 0) { + if (op->addr.buswidth > 1) + return false; + if (op->addr.nbytes < 3 || op->addr.nbytes > 4) + return false; + } + + if (op->dummy.nbytes !=3D 0) { + if (op->dummy.buswidth > 1 || op->dummy.nbytes > 7) + return false; + } + + if (op->data.nbytes !=3D 0 && op->data.buswidth > 2) + return false; + + return spi_mem_default_supports_op(mem, op); +} + +static int rtk_spi_dirmap_create(struct spi_mem_dirmap_desc *desc) +{ + const struct spi_mem_op *op =3D desc->info.op_tmpl; + + if (op->data.dir =3D=3D SPI_MEM_DATA_IN && op->addr.nbytes !=3D 3 && op->= addr.nbytes !=3D 4) + return -EOPNOTSUPP; + + if (op->data.dir !=3D SPI_MEM_DATA_IN && op->data.dir !=3D SPI_MEM_DATA_O= UT) + return -EOPNOTSUPP; + + return 0; +} + +static ssize_t rtk_spi_dirmap_read(struct spi_mem_dirmap_desc *desc, + u64 offs, size_t len, void *buf) +{ + struct rtk_spi_host *host =3D spi_controller_get_devdata(desc->mem->spi->= controller); + const struct spi_mem_op *op =3D desc->info.op_tmpl; + loff_t addr =3D desc->info.offset + offs; + size_t chunk_len; + int ret; + + /* + * Handle unaligned address bytes at the beginning of the read operation. + * The hardware requires 4-byte alignment for DMA transfers. + */ + if (addr & 0x3) { + rtk_spi_read_mode(host, op); + chunk_len =3D min_t(size_t, 4 - (addr & 0x3), len); + ret =3D rtk_spi_byte_transfer(host, addr, chunk_len, buf, true); + if (ret) + return ret; + + return chunk_len; + } + + rtk_spi_read_mode(host, op); + chunk_len =3D min_t(size_t, len, SFC_DMA_MAX_LEN); + ret =3D rtk_spi_dma_transfer(host, addr, chunk_len, true); + if (ret) { + dev_err(host->dev, "DMA read transfer failed: %d\n", ret); + return ret; + } + + memcpy(buf, host->buffer, chunk_len); + + return chunk_len; +} + +static ssize_t rtk_spi_dirmap_write(struct spi_mem_dirmap_desc *desc, + u64 offs, size_t len, const void *buf) +{ + struct rtk_spi_host *host =3D spi_controller_get_devdata(desc->mem->spi->= controller); + const struct spi_mem_op *op =3D desc->info.op_tmpl; + loff_t addr =3D desc->info.offset + offs; + size_t chunk_len; + int ret =3D 0; + + rtk_spi_enable_auto_write(host); + rtk_spi_write_mode(host, op); + + /* + * Handle unaligned address bytes at the beginning of the write operation. + * The hardware requires 4-byte alignment for DMA transfers. + */ + if (addr & 0x3) { + chunk_len =3D min_t(size_t, 4 - (addr & 0x3), len); + ret =3D rtk_spi_byte_transfer(host, addr, chunk_len, (u8 *)buf, false); + + goto out; + } + + chunk_len =3D min_t(size_t, len, SFC_DMA_MAX_LEN); + + memcpy(host->buffer, buf, chunk_len); + + ret =3D rtk_spi_dma_transfer(host, addr, chunk_len, false); + if (ret) + dev_err(host->dev, "DMA write transfer failed: %d\n", ret); + +out: + rtk_spi_disable_auto_write(host); + + return ret < 0 ? ret : chunk_len; +} + +static const struct spi_controller_mem_ops rtk_spi_mem_ops =3D { + .supports_op =3D rtk_spi_supports_op, + .exec_op =3D rtk_spi_exec_op, + .dirmap_create =3D rtk_spi_dirmap_create, + .dirmap_read =3D rtk_spi_dirmap_read, + .dirmap_write =3D rtk_spi_dirmap_write, +}; + +static int rtk_spi_probe(struct platform_device *pdev) +{ + struct device *dev =3D &pdev->dev; + struct spi_controller *ctrl; + struct rtk_spi_host *host; + struct resource *res; + int ret; + + ctrl =3D devm_spi_alloc_host(dev, sizeof(*host)); + if (!ctrl) + return -ENOMEM; + + platform_set_drvdata(pdev, ctrl); + host =3D spi_controller_get_devdata(ctrl); + host->dev =3D dev; + + host->clk =3D devm_clk_get(dev, NULL); + if (IS_ERR(host->clk)) + return PTR_ERR(host->clk); + + ret =3D clk_prepare_enable(host->clk); + if (ret) + return ret; + + host->rstc =3D devm_reset_control_get_optional_exclusive(dev, NULL); + if (IS_ERR(host->rstc)) { + ret =3D PTR_ERR(host->rstc); + goto err_disable_clk; + } + + reset_control_assert(host->rstc); + usleep_range(10, 20); + reset_control_deassert(host->rstc); + + ret =3D dma_set_mask_and_coherent(dev, DMA_BIT_MASK(35)); + if (ret) { + dev_err(dev, "Failed to set dma mask\n"); + goto err_disable_clk; + } + + host->buffer =3D dmam_alloc_coherent(dev, SFC_DMA_MAX_LEN, + &host->dma_buffer, GFP_KERNEL); + if (!host->buffer) { + ret =3D -ENOMEM; + goto err_disable_clk; + } + + host->regbase =3D devm_platform_ioremap_resource_byname(pdev, "ctrl"); + if (IS_ERR(host->regbase)) { + ret =3D PTR_ERR(host->regbase); + goto err_disable_clk; + } + + host->mdbase =3D devm_platform_ioremap_resource_byname(pdev, "dma"); + if (IS_ERR(host->mdbase)) { + ret =3D PTR_ERR(host->mdbase); + goto err_disable_clk; + } + + res =3D platform_get_resource_byname(pdev, IORESOURCE_MEM, "dirmap"); + host->iobase =3D devm_ioremap_resource(dev, res); + if (IS_ERR(host->iobase)) { + ret =3D PTR_ERR(host->iobase); + goto err_disable_clk; + } + + host->dirmap_size =3D resource_size(res); + host->flash_phys_base =3D res->start; + host->is_4byte =3D false; + + ctrl->mode_bits =3D SPI_RX_DUAL | SPI_TX_DUAL; + ctrl->bus_num =3D -1; + ctrl->mem_ops =3D &rtk_spi_mem_ops; + ctrl->num_chipselect =3D 1; + ctrl->auto_runtime_pm =3D true; + + rtk_spi_init(host); + + pm_runtime_set_autosuspend_delay(dev, SFC_AUTOSUSPEND_TIMEOUT); + pm_runtime_use_autosuspend(dev); + pm_runtime_set_active(dev); + pm_runtime_enable(dev); + pm_runtime_get_noresume(dev); + + ret =3D spi_register_controller(ctrl); + if (ret < 0) { + dev_err(dev, "failed to register controller\n"); + goto err_pm_disable; + } + + pm_runtime_put_autosuspend(dev); + + return 0; + +err_pm_disable: + pm_runtime_put_noidle(dev); + pm_runtime_disable(dev); + pm_runtime_set_suspended(dev); + pm_runtime_dont_use_autosuspend(dev); + reset_control_assert(host->rstc); + +err_disable_clk: + clk_disable_unprepare(host->clk); + + return ret; +} + +static void rtk_spi_remove(struct platform_device *pdev) +{ + struct spi_controller *ctrl =3D platform_get_drvdata(pdev); + struct rtk_spi_host *host =3D spi_controller_get_devdata(ctrl); + struct device *dev =3D &pdev->dev; + + spi_unregister_controller(ctrl); + + pm_runtime_get_sync(dev); + pm_runtime_disable(dev); + reset_control_assert(host->rstc); + clk_disable_unprepare(host->clk); + pm_runtime_put_noidle(dev); + pm_runtime_set_suspended(dev); +} + +static const struct of_device_id rtk_spi_dt_ids[] =3D { + { .compatible =3D "realtek,rtd1625-nor" }, + { /* sentinel */ } +}; + +MODULE_DEVICE_TABLE(of, rtk_spi_dt_ids); + +static int rtk_spi_runtime_suspend(struct device *dev) +{ + struct spi_controller *ctlr =3D dev_get_drvdata(dev); + struct rtk_spi_host *host =3D spi_controller_get_devdata(ctlr); + + reset_control_assert(host->rstc); + + clk_disable_unprepare(host->clk); + + return 0; +} + +static int rtk_spi_runtime_resume(struct device *dev) +{ + struct spi_controller *ctlr =3D dev_get_drvdata(dev); + struct rtk_spi_host *host =3D spi_controller_get_devdata(ctlr); + int ret; + + ret =3D clk_prepare_enable(host->clk); + if (ret < 0) { + dev_err(dev, "clk_prepare_enable failed: %d\n", ret); + return ret; + } + + reset_control_assert(host->rstc); + usleep_range(10, 20); + reset_control_deassert(host->rstc); + + rtk_spi_init(host); + + return 0; +} + +static DEFINE_RUNTIME_DEV_PM_OPS(rtk_spi_pm_ops, rtk_spi_runtime_suspend, + rtk_spi_runtime_resume, NULL); + +static struct platform_driver rtk_spi_driver =3D { + .driver =3D { + .name =3D "rtk-spi-nor", + .of_match_table =3D rtk_spi_dt_ids, + .pm =3D pm_ptr(&rtk_spi_pm_ops), + }, + .probe =3D rtk_spi_probe, + .remove =3D rtk_spi_remove, +}; +module_platform_driver(rtk_spi_driver); + +MODULE_DESCRIPTION("Realtek SPI Nor Controller Driver"); +MODULE_AUTHOR("Jyan Chou "); +MODULE_LICENSE("GPL"); --=20 2.43.0