linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v3 0/2] spi: Add support for Realtek RTL838x/RTL839x SoC SPI
@ 2021-01-01 13:24 Bert Vermeulen
  2021-01-01 13:24 ` [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller Bert Vermeulen
  2021-01-01 13:24 ` [PATCH v3 2/2] spi: realtek-rtl: Add support for Realtek RTL838x/RTL839x SoC SPI controllers Bert Vermeulen
  0 siblings, 2 replies; 6+ messages in thread
From: Bert Vermeulen @ 2021-01-01 13:24 UTC (permalink / raw)
  To: Mark Brown, Rob Herring, Birger Koblitz, linux-spi, devicetree,
	linux-kernel
  Cc: Bert Vermeulen

v3:
- Added cpu_relax() to busy loop.
- Dropped .remove callback from driver struct.
- Use (variations of) realtek-rtl as prefix.
- Dropped Kconfig entry, and use MACH_REALTEK_RTL setting to build the
  driver, since there's no point booting without the SPI-connected flash.

v2:
- Rewrote from spi-nor driver to regular spi driver, implementing only
  set_cs() and transfer_one(). (Thanks Chuanhong Guo!)


Bert Vermeulen (2):
  dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller
  spi: realtek-rtl: Add support for Realtek RTL838x/RTL839x SoC SPI
    controllers

 .../bindings/spi/realtek,rtl-spi.yaml         |  36 +++
 MAINTAINERS                                   |   6 +
 drivers/spi/Makefile                          |   1 +
 drivers/spi/spi-realtek-rtl.c                 | 205 ++++++++++++++++++
 4 files changed, 248 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/realtek,rtl-spi.yaml
 create mode 100644 drivers/spi/spi-realtek-rtl.c

-- 
2.25.1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller
  2021-01-01 13:24 [PATCH v3 0/2] spi: Add support for Realtek RTL838x/RTL839x SoC SPI Bert Vermeulen
@ 2021-01-01 13:24 ` Bert Vermeulen
  2021-01-04 21:31   ` Mark Brown
  2021-01-01 13:24 ` [PATCH v3 2/2] spi: realtek-rtl: Add support for Realtek RTL838x/RTL839x SoC SPI controllers Bert Vermeulen
  1 sibling, 1 reply; 6+ messages in thread
From: Bert Vermeulen @ 2021-01-01 13:24 UTC (permalink / raw)
  To: Mark Brown, Rob Herring, Birger Koblitz, linux-spi, devicetree,
	linux-kernel
  Cc: Bert Vermeulen

Signed-off-by: Bert Vermeulen <bert@biot.com>
---
 .../bindings/spi/realtek,rtl-spi.yaml         | 36 +++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/spi/realtek,rtl-spi.yaml

diff --git a/Documentation/devicetree/bindings/spi/realtek,rtl-spi.yaml b/Documentation/devicetree/bindings/spi/realtek,rtl-spi.yaml
new file mode 100644
index 000000000000..0d3f8448c9ab
--- /dev/null
+++ b/Documentation/devicetree/bindings/spi/realtek,rtl-spi.yaml
@@ -0,0 +1,36 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/spi/realtek,rtl-spi.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Realtek RTL838x/RTL839x SPI controller
+
+maintainers:
+  - Bert Vermeulen <bert@biot.com>
+  - Birger Koblitz <mail@birger-koblitz.de>
+
+allOf:
+  - $ref: "spi-controller.yaml#"
+
+properties:
+  compatible:
+    const: realtek,rtl-spi
+
+  reg:
+    maxItems: 1
+
+required:
+  - compatible
+  - reg
+
+unevaluatedProperties: false
+
+examples:
+  - |
+    spi: spi@1200 {
+        compatible = "realtek,rtl-spi";
+        reg = <0x1200 0x100>;
+        #address-cells = <1>;
+        #size-cells = <0>;
+    };
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [PATCH v3 2/2] spi: realtek-rtl: Add support for Realtek RTL838x/RTL839x SoC SPI controllers
  2021-01-01 13:24 [PATCH v3 0/2] spi: Add support for Realtek RTL838x/RTL839x SoC SPI Bert Vermeulen
  2021-01-01 13:24 ` [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller Bert Vermeulen
@ 2021-01-01 13:24 ` Bert Vermeulen
  1 sibling, 0 replies; 6+ messages in thread
From: Bert Vermeulen @ 2021-01-01 13:24 UTC (permalink / raw)
  To: Mark Brown, Rob Herring, Birger Koblitz, linux-spi, devicetree,
	linux-kernel
  Cc: Bert Vermeulen

This driver likely also supports earlier (RTL8196) and later (RTL93xx)
SoCs.

The SPI hardware in these SoCs is specifically intended for connecting NOR
bootflash chips, and only used for that in dozens of examined devices.
However boiled down to basics, it's really just a half-duplex SPI
controller.

The hardware appears to have a vestigial second chip-select control, but
it hasn't been seen in the wild and is thus not supported.

Signed-off-by: Bert Vermeulen <bert@biot.com>
---
 MAINTAINERS                   |   6 +
 drivers/spi/Makefile          |   1 +
 drivers/spi/spi-realtek-rtl.c | 205 ++++++++++++++++++++++++++++++++++
 3 files changed, 212 insertions(+)
 create mode 100644 drivers/spi/spi-realtek-rtl.c

diff --git a/MAINTAINERS b/MAINTAINERS
index ad0e34bf8453..c6cb954e5ecf 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -15048,6 +15048,12 @@ F:	Documentation/devicetree/bindings/net/dsa/realtek-smi.txt
 F:	drivers/net/dsa/realtek-smi*
 F:	drivers/net/dsa/rtl83*
 
+REALTEK RTL83XX SPI DRIVER
+M:	Bert Vermeulen <bert@biot.com>
+M:	Birger Koblitz <mail@birger-koblitz.de>
+S:	Maintained
+F:	drivers/spi/spi-realtek-rtl.c
+
 REALTEK WIRELESS DRIVER (rtlwifi family)
 M:	Ping-Ke Shih <pkshih@realtek.com>
 L:	linux-wireless@vger.kernel.org
diff --git a/drivers/spi/Makefile b/drivers/spi/Makefile
index 6fea5821662e..182adef17013 100644
--- a/drivers/spi/Makefile
+++ b/drivers/spi/Makefile
@@ -94,6 +94,7 @@ obj-$(CONFIG_SPI_QCOM_QSPI)		+= spi-qcom-qspi.o
 obj-$(CONFIG_SPI_QUP)			+= spi-qup.o
 obj-$(CONFIG_SPI_ROCKCHIP)		+= spi-rockchip.o
 obj-$(CONFIG_SPI_RB4XX)			+= spi-rb4xx.o
+obj-$(CONFIG_MACH_REALTEK_RTL)		+= spi-realtek-rtl.o
 obj-$(CONFIG_SPI_RPCIF)			+= spi-rpc-if.o
 obj-$(CONFIG_SPI_RSPI)			+= spi-rspi.o
 obj-$(CONFIG_SPI_S3C24XX)		+= spi-s3c24xx-hw.o
diff --git a/drivers/spi/spi-realtek-rtl.c b/drivers/spi/spi-realtek-rtl.c
new file mode 100644
index 000000000000..2ad5ab24e4e1
--- /dev/null
+++ b/drivers/spi/spi-realtek-rtl.c
@@ -0,0 +1,205 @@
+// SPDX-License-Identifier: GPL-2.0-only
+
+#include <linux/module.h>
+#include <linux/platform_device.h>
+#include <linux/mod_devicetable.h>
+#include <linux/spi/spi.h>
+
+struct rtspi {
+	void __iomem *base;
+};
+
+/* SPI Flash Configuration Register */
+#define RTL_SPI_SFCR			0x00
+#define RTL_SPI_SFCR_RBO		BIT(28)
+#define RTL_SPI_SFCR_WBO		BIT(27)
+
+/* SPI Flash Control and Status Register */
+#define RTL_SPI_SFCSR			0x08
+#define RTL_SPI_SFCSR_CSB0		BIT(31)
+#define RTL_SPI_SFCSR_CSB1		BIT(30)
+#define RTL_SPI_SFCSR_RDY		BIT(27)
+#define RTL_SPI_SFCSR_CS		BIT(24)
+#define RTL_SPI_SFCSR_LEN_MASK		~(0x03 << 28)
+#define RTL_SPI_SFCSR_LEN1		(0x00 << 28)
+#define RTL_SPI_SFCSR_LEN4		(0x03 << 28)
+
+/* SPI Flash Data Register */
+#define RTL_SPI_SFDR			0x0c
+
+#define REG(x)		(rtspi->base + x)
+
+
+static void rt_set_cs(struct spi_device *spi, bool active)
+{
+	struct rtspi *rtspi = spi_controller_get_devdata(spi->controller);
+	u32 value;
+
+	/* CS0 bit is active low */
+	value = readl(REG(RTL_SPI_SFCSR));
+	if (active)
+		value |= RTL_SPI_SFCSR_CSB0;
+	else
+		value &= ~RTL_SPI_SFCSR_CSB0;
+	writel(value, REG(RTL_SPI_SFCSR));
+}
+
+static void set_size(struct rtspi *rtspi, int size)
+{
+	u32 value;
+
+	value = readl(REG(RTL_SPI_SFCSR));
+	value &= RTL_SPI_SFCSR_LEN_MASK;
+	if (size == 4)
+		value |= RTL_SPI_SFCSR_LEN4;
+	else if (size == 1)
+		value |= RTL_SPI_SFCSR_LEN1;
+	writel(value, REG(RTL_SPI_SFCSR));
+}
+
+static inline void wait_ready(struct rtspi *rtspi)
+{
+	while (!(readl(REG(RTL_SPI_SFCSR)) & RTL_SPI_SFCSR_RDY))
+		cpu_relax();
+}
+static void send4(struct rtspi *rtspi, const u32 *buf)
+{
+	wait_ready(rtspi);
+	set_size(rtspi, 4);
+	writel(*buf, REG(RTL_SPI_SFDR));
+}
+
+static void send1(struct rtspi *rtspi, const u8 *buf)
+{
+	wait_ready(rtspi);
+	set_size(rtspi, 1);
+	writel(buf[0] << 24, REG(RTL_SPI_SFDR));
+}
+
+static void rcv4(struct rtspi *rtspi, u32 *buf)
+{
+	wait_ready(rtspi);
+	set_size(rtspi, 4);
+	*buf = readl(REG(RTL_SPI_SFDR));
+}
+
+static void rcv1(struct rtspi *rtspi, u8 *buf)
+{
+	wait_ready(rtspi);
+	set_size(rtspi, 1);
+	*buf = readl(REG(RTL_SPI_SFDR)) >> 24;
+}
+
+static int transfer_one(struct spi_controller *ctrl, struct spi_device *spi,
+			struct spi_transfer *xfer)
+{
+	struct rtspi *rtspi = spi_controller_get_devdata(ctrl);
+	void *rx_buf;
+	const void *tx_buf;
+	int cnt;
+
+	tx_buf = xfer->tx_buf;
+	rx_buf = xfer->rx_buf;
+	cnt = xfer->len;
+	if (tx_buf) {
+		while (cnt >= 4) {
+			send4(rtspi, tx_buf);
+			tx_buf += 4;
+			cnt -= 4;
+		}
+		while (cnt) {
+			send1(rtspi, tx_buf);
+			tx_buf++;
+			cnt--;
+		}
+	} else if (rx_buf) {
+		while (cnt >= 4) {
+			rcv4(rtspi, rx_buf);
+			rx_buf += 4;
+			cnt -= 4;
+		}
+		while (cnt) {
+			rcv1(rtspi, rx_buf);
+			rx_buf++;
+			cnt--;
+		}
+	}
+
+	spi_finalize_current_transfer(ctrl);
+
+	return 0;
+}
+
+static void init_hw(struct rtspi *rtspi)
+{
+	u32 value;
+
+	/* Turn on big-endian byte ordering */
+	value = readl(REG(RTL_SPI_SFCR));
+	value |= RTL_SPI_SFCR_RBO | RTL_SPI_SFCR_WBO;
+	writel(value, REG(RTL_SPI_SFCR));
+
+	value = readl(REG(RTL_SPI_SFCSR));
+	/* Permanently disable CS1, since it's never used */
+	value |= RTL_SPI_SFCSR_CSB1;
+	/* Select CS0 for use */
+	value &= RTL_SPI_SFCSR_CS;
+	writel(value, REG(RTL_SPI_SFCSR));
+}
+
+static int realtek_rtl_spi_probe(struct platform_device *pdev)
+{
+	struct spi_controller *ctrl;
+	struct rtspi *rtspi;
+	int err;
+
+	ctrl = devm_spi_alloc_master(&pdev->dev, sizeof(*rtspi));
+	if (!ctrl) {
+		dev_err(&pdev->dev, "Error allocating SPI controller\n");
+		return -ENOMEM;
+	}
+	platform_set_drvdata(pdev, ctrl);
+	rtspi = spi_controller_get_devdata(ctrl);
+
+	rtspi->base = devm_platform_get_and_ioremap_resource(pdev, 0, NULL);
+	if (IS_ERR(rtspi->base)) {
+		dev_err(&pdev->dev, "Could not map SPI register address");
+		return -ENOMEM;
+	}
+
+	init_hw(rtspi);
+
+	ctrl->dev.of_node = pdev->dev.of_node;
+	ctrl->flags = SPI_CONTROLLER_HALF_DUPLEX;
+	ctrl->set_cs = rt_set_cs;
+	ctrl->transfer_one = transfer_one;
+
+	err = devm_spi_register_controller(&pdev->dev, ctrl);
+	if (err) {
+		dev_err(&pdev->dev, "Could not register SPI controller\n");
+		return -ENODEV;
+	}
+
+	return 0;
+}
+
+
+static const struct of_device_id realtek_rtl_spi_of_ids[] = {
+	{ .compatible = "realtek,rtl-spi" },
+	{ /* sentinel */ }
+};
+MODULE_DEVICE_TABLE(of, realtek_rtl_spi_of_ids);
+
+static struct platform_driver realtek_rtl_spi_driver = {
+	.probe = realtek_rtl_spi_probe,
+	.driver = {
+		.name = "realtek-rtl-spi",
+		.of_match_table = realtek_rtl_spi_of_ids,
+	},
+};
+
+module_platform_driver(realtek_rtl_spi_driver);
+
+MODULE_LICENSE("GPL v2");
+MODULE_AUTHOR("Bert Vermeulen <bert@biot.com>");
+MODULE_DESCRIPTION("Realtek RTL SPI driver");
-- 
2.25.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller
  2021-01-01 13:24 ` [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller Bert Vermeulen
@ 2021-01-04 21:31   ` Mark Brown
  2021-01-05  8:43     ` Bert Vermeulen
  0 siblings, 1 reply; 6+ messages in thread
From: Mark Brown @ 2021-01-04 21:31 UTC (permalink / raw)
  To: Bert Vermeulen
  Cc: Rob Herring, Birger Koblitz, linux-spi, devicetree, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 556 bytes --]

On Fri, Jan 01, 2021 at 02:24:31PM +0100, Bert Vermeulen wrote:

> +  compatible:
> +    const: realtek,rtl-spi

This is still just using rtl-spi as the compatible string, please
address the feedback on the previous version.

Please submit patches using subject lines reflecting the style for the
subsystem, this makes it easier for people to identify relevant patches.
Look at what existing commits in the area you're changing are doing and
make sure your subject lines visually resemble what they're doing.
There's no need to resubmit to fix this alone.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller
  2021-01-04 21:31   ` Mark Brown
@ 2021-01-05  8:43     ` Bert Vermeulen
  2021-01-05 13:47       ` Mark Brown
  0 siblings, 1 reply; 6+ messages in thread
From: Bert Vermeulen @ 2021-01-05  8:43 UTC (permalink / raw)
  To: Mark Brown
  Cc: Rob Herring, Birger Koblitz, linux-spi, devicetree, linux-kernel

On 1/4/21 10:31 PM, Mark Brown wrote:
> On Fri, Jan 01, 2021 at 02:24:31PM +0100, Bert Vermeulen wrote:
> 
>> +  compatible:
>> +    const: realtek,rtl-spi
> 
> This is still just using rtl-spi as the compatible string, please
> address the feedback on the previous version.

Mark,

The rtl prefix really is as close as it gets. This is being developed on 
RTL838x and RTL839x, but the driver very likely also works on RTL8196C and 
RTL93xx series. There's no difference to the SPI block in those SoCs.

Do you really want a different per-series compatible when the "IP block" was 
just copy-pasted between series in hardware?


-- 
Bert Vermeulen
bert@biot.com

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller
  2021-01-05  8:43     ` Bert Vermeulen
@ 2021-01-05 13:47       ` Mark Brown
  0 siblings, 0 replies; 6+ messages in thread
From: Mark Brown @ 2021-01-05 13:47 UTC (permalink / raw)
  To: Bert Vermeulen
  Cc: Rob Herring, Birger Koblitz, linux-spi, devicetree, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 583 bytes --]

On Tue, Jan 05, 2021 at 09:43:59AM +0100, Bert Vermeulen wrote:
> On 1/4/21 10:31 PM, Mark Brown wrote:

> > This is still just using rtl-spi as the compatible string, please
> > address the feedback on the previous version.

> The rtl prefix really is as close as it gets. This is being developed on
> RTL838x and RTL839x, but the driver very likely also works on RTL8196C and
> RTL93xx series. There's no difference to the SPI block in those SoCs.

> Do you really want a different per-series compatible when the "IP block" was
> just copy-pasted between series in hardware?

Yes.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 488 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-01-05 13:48 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-01 13:24 [PATCH v3 0/2] spi: Add support for Realtek RTL838x/RTL839x SoC SPI Bert Vermeulen
2021-01-01 13:24 ` [PATCH v3 1/2] dt-bindings: spi: Realtek RTL838x/RTL839x SPI controller Bert Vermeulen
2021-01-04 21:31   ` Mark Brown
2021-01-05  8:43     ` Bert Vermeulen
2021-01-05 13:47       ` Mark Brown
2021-01-01 13:24 ` [PATCH v3 2/2] spi: realtek-rtl: Add support for Realtek RTL838x/RTL839x SoC SPI controllers Bert Vermeulen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).