Skip to contentSkip to conversation

spi: microchip: pci1xxxx: Load SPI driver for SPI endpoint of PCI1XXXX switch

RFC PATCH Superseded Tharun Kumar P 2 participants 7 messages Sep 28, 2022 – Sep 30, 2022 SPI

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover spi: microchip: pci1xxxx: Load SPI driver for SPI endpoint of PCI1XXXX switch

Microchip PCI1XXXX is an unmanaged PCIe3.1a switch for consumer, industrial, and automotive applications. This switch has multiple downstream ports. One of the switch's downstream ports is a multifunction endpoint; one of those functions supports SPI functionality. This series of patches provides the SPI controller driver for the SPI function of the multifunction PCIe endpoint of the switch.

Tharun Kumar P (2):
spi: microchip: pci1xxxx: Add driver for SPI controller of PCI1XXXX
PCIe switch
spi: microchip: pci1xxxx: Add suspend and resume support for PCI1XXXX
SPI driver

 drivers/spi/Kconfig        |   9 +
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-pci1xxxx.c | 453 +++++++++++++++++++++++++++++++++++++
 3 files changed, 463 insertions(+)
 create mode 100644 drivers/spi/spi-pci1xxxx.c
Signature · 1 line
2.25.1
#1 lore raw

RFC PATCH 1/2 spi: microchip: pci1xxxx: Add driver for SPI controller of PCI1XXXX PCIe switch

Microchip pci1xxxx is a PCIe switch with a multi-function endpoint on one of its downstream ports. SPI is one of the functions in the multi-function endpoint. This function has 2 SPI masters, operates at a maximum frequency of 30 MHz and supports 7 client devices per master. This patch adds complete functionality to the SPI function except for suspend and resume.

---
 drivers/spi/Kconfig        |   9 +
 drivers/spi/Makefile       |   1 +
 drivers/spi/spi-pci1xxxx.c | 378 +++++++++++++++++++++++++++++++++++++
 3 files changed, 388 insertions(+)
 create mode 100644 drivers/spi/spi-pci1xxxx.c
drivers/spi/Kconfig +9 -0
index 9987c3f2bd1c..08ca811440f9 100644
@@ -701,6 +701,15 @@ config SPI_ORION
 	  This enables using the SPI master controller on the Orion
 	  and MVEBU chips.
 
+config SPI_PCI1XXXX
+	tristate "PCI1XXXX SPI Bus support"
+	depends on PCI
+	help
+	  Say "yes" to Enable the SPI Bus support for the PCI1xxxx card
+	  This is a PCI to SPI Bus driver
+	  This driver can be built as module. If so, the module will be
+	  called as spi-pci1xxxx.
+
 config SPI_PIC32
 	tristate "Microchip PIC32 series SPI"
 	depends on MACH_PIC32 || COMPILE_TEST
drivers/spi/Makefile +1 -0
index 15d2f3835e45..b75fa3988f96 100644
@@ -93,6 +93,7 @@ obj-$(CONFIG_SPI_OMAP_100K)		+= spi-omap-100k.o
 obj-$(CONFIG_SPI_OMAP24XX)		+= spi-omap2-mcspi.o
 obj-$(CONFIG_SPI_TI_QSPI)		+= spi-ti-qspi.o
 obj-$(CONFIG_SPI_ORION)			+= spi-orion.o
+obj-$(CONFIG_SPI_PCI1XXXX)		+= spi-pci1xxxx.o
 obj-$(CONFIG_SPI_PIC32)			+= spi-pic32.o
 obj-$(CONFIG_SPI_PIC32_SQI)		+= spi-pic32-sqi.o
 obj-$(CONFIG_SPI_PL022)			+= spi-pl022.o
drivers/spi/spi-pci1xxxx.c new file +378 -0

378 lines not on this page · raw message

Signature · 1 line
2.25.1
maintainer
#2 lore raw

Re: [PATCH RFC SPI for-next 1/2] spi: microchip: pci1xxxx: Add driver for SPI controller of PCI1XXXX PCIe switch

drivers/spi/spi-pci1xxxx.c line 8

Quoted 9 lines · Tharun Kumar P
drivers/spi/spi-pci1xxxx.c new file +8 -0
@@ -0,0 +1,378 @@
+// SPDX-License-Identifier: GPL-2.0
+
+/*
+ * PCI1xxxx SPI driver
+ * Copyright (C) 2022 Microchip Technology Inc.
+ * Authors: Tharun Kumar P <[email protected]>
+ *          Kumaravel Thiagarajan <[email protected]>
+ */

Please make the above a single C++ style comment block so things look more intentional.

Quoted 5 lines
drivers/spi/spi-pci1xxxx.c new file +5 -0
+static void pci1xxxx_spi_set_cs(struct spi_device *spi, bool enable)
+{
+	struct pci1xxxx_spi_internal *p = spi_controller_get_devdata(spi->controller);
+	struct pci1xxxx_spi *par = p->parent;
+	u32 regval;

drivers/spi/spi-pci1xxxx.c line 120

Quoted 10 lines
drivers/spi/spi-pci1xxxx.c new file +10 -0
+
+	/* Set the DEV_SEL bits of the SPI_MST_CTL_REG */
+	regval = readl(par->reg_base + SPI_MST_CTL_REG_OFFSET(p->hw_inst));
+	if (enable) {
+		regval &= ~SPI_MST_CTL_DEVSEL_MASK;
+		regval |= (spi->chip_select << 25);
+		writel(regval,
+		       par->reg_base + SPI_MST_CTL_REG_OFFSET(p->hw_inst));
+	}
+}

This does nothing if the chip select is to be disabled, that's clearly not going to work.

drivers/spi/spi-pci1xxxx.c line 209

Quoted 7 lines
drivers/spi/spi-pci1xxxx.c new file +5 -0
+static int pci1xxxx_spi_transfer_one(struct spi_controller *spi_ctlr,
+				     struct spi_device *spi, struct spi_transfer *xfer)
+{
 
+	if (tx_buf) {
 
+			if (rx_buf) {

The driver should set SPI_CONTROLLER_MUST_TX since it needs to transmit data in order to recieve.

drivers/spi/spi-pci1xxxx.c line 255

Quoted 13 lines
drivers/spi/spi-pci1xxxx.c new file +13 -0
+static int pci1xxxx_spi_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
+{
+	u8 hw_inst_cnt, iter, start, only_sec_inst;
+	struct pci1xxxx_spi_internal *spi_sub_ptr;
+	struct device *dev = &pdev->dev;
+	struct pci1xxxx_spi *spi_bus;
+	struct spi_master *spi_host;
+	u32 regval;
+	int ret;
+
+	hw_inst_cnt = ent->driver_data & 0x0f;
+	start = (ent->driver_data & 0xf0) >> 4;
+	(start == 1) ? (only_sec_inst = 1) : (only_sec_inst = 0);

Please write normal conditional statements to improve legibility.

drivers/spi/spi-pci1xxxx.c line 309

Quoted 10 lines
drivers/spi/spi-pci1xxxx.c new file +10 -0
+
+			ret = devm_request_irq(&pdev->dev, spi_sub_ptr->irq,
+					       pci1xxxx_spi_isr, PCI1XXXX_IRQ_FLAGS,
+					       pci_name(pdev), spi_sub_ptr);
+			if (ret < 0) {
+				dev_err(&pdev->dev, "Unable to request irq : %d",
+					spi_sub_ptr->irq);
+				ret = -ENODEV;
+				goto error;
+			}

Are you sure the device is fully set up and ready for interrupts at this point, and that the freeing of the driver will work fine with devm?

drivers/spi/spi-pci1xxxx.c line 336

Quoted 1 line
drivers/spi/spi-pci1xxxx.c new file +1 -0
+		init_completion(&spi_sub_ptr->spi_xfer_done);

I note that the completion that the interrupt handler uses isn't allocated yet for example...

drivers/spi/spi-pci1xxxx.c line 356

Quoted 6 lines
drivers/spi/spi-pci1xxxx.c new file +6 -0
+		/* Initialize Interrupts - SPI_INT */
+		regval = readl(spi_bus->reg_base +
+			       SPI_MST_EVENT_MASK_REG_OFFSET(spi_sub_ptr->hw_inst));
+		regval &= ~SPI_INTR;
+		writel(regval, spi_bus->reg_base +
+		       SPI_MST_EVENT_MASK_REG_OFFSET(spi_sub_ptr->hw_inst));

...and we do some interrupt mask management later.

#3 lore raw

Re: [PATCH RFC SPI for-next 1/2] spi: microchip: pci1xxxx: Add driver for SPI controller of PCI1XXXX PCIe switch

Quoted 5 lines · Mark Brown

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

Please make the above a single C++ style comment block so things look more intentional.

Okay.

Quoted 2 lines

This does nothing if the chip select is to be disabled, that's clearly not going to work.

Okay. I will handle disable case of chip select.

Quoted 2 lines

The driver should set SPI_CONTROLLER_TX since it needs to transmit data inorer to receive

Okay.

Quoted 1 line

Please write normal conditional statements to improve legibility.

Okay.

Quoted 2 lines

Are you sure the device is fully set up and ready for interrupts at this point, and that the freeing of the driver will work fine with devm?

Okay, I will move init_completion and interrupt mask management before devm_request_irq API.

Thanks,
Tharun Kumar P

#4 lore raw

RFC PATCH 2/2 spi: microchip: pci1xxxx: Add suspend and resume support for PCI1XXXX SPI driver

In reply to Tharun Kumar P

Implement suspend, resume callbacks, store config at suspend and restore config at time of resume

---
 drivers/spi/spi-pci1xxxx.c | 75 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 75 insertions(+)
drivers/spi/spi-pci1xxxx.c +75 -0
index 0b6ba8108d19..072549231e11 100644
@@ -58,6 +58,9 @@
 #define SPI_CHIP_SEL_COUNT 7
 #define VENDOR_ID_MCHP 0x1055
 
+#define SPI_SUSPEND_CONFIG 0x101
+#define SPI_RESUME_CONFIG 0x303
+
 struct pci1xxxx_spi_internal {
 	u8 hw_inst;
 	int irq;
@@ -364,10 +367,82 @@ static int pci1xxxx_spi_probe(struct pci_dev *pdev, const struct pci_device_id *
 	return ret;
 }
 
+static void store_restore_config(struct pci1xxxx_spi *spi_ptr,
+				 struct pci1xxxx_spi_internal *spi_sub_ptr,
+				 u8 inst, bool store)
+{
+	u32 regval;
+
+	if (store) {
+		regval = readl(spi_ptr->reg_base +
+			       SPI_MST_CTL_REG_OFFSET(spi_sub_ptr->hw_inst));
+		regval &= SPI_MST_CTL_DEVSEL_MASK;
+		spi_sub_ptr->prev_val.dev_sel = (regval >> 25) & 7;
+		regval = readl(spi_ptr->reg_base +
+			       SPI_PCI_CTRL_REG_OFFSET(spi_sub_ptr->hw_inst));
+		regval &= SPI_MSI_VECTOR_SEL_MASK;
+		spi_sub_ptr->prev_val.msi_vector_sel = (regval >> 4) & 1;
+	} else {
+		regval = readl(spi_ptr->reg_base + SPI_MST_CTL_REG_OFFSET(inst));
+		regval &= ~SPI_MST_CTL_DEVSEL_MASK;
+		regval |= (spi_sub_ptr->prev_val.dev_sel << 25);
+		writel(regval,
+		       spi_ptr->reg_base + SPI_MST_CTL_REG_OFFSET(inst));
+		writel((spi_sub_ptr->prev_val.msi_vector_sel << 4),
+			spi_ptr->reg_base + SPI_PCI_CTRL_REG_OFFSET(inst));
+	}
+}
+
+static int pci1xxxx_spi_resume(struct device *dev)
+{
+	struct pci1xxxx_spi *spi_ptr = dev_get_drvdata(dev);
+	struct pci1xxxx_spi_internal *spi_sub_ptr;
+	u32 regval = SPI_RESUME_CONFIG;
+	u8 iter;
+
+	for (iter = 0; iter < spi_ptr->total_hw_instances; iter++) {
+		spi_sub_ptr = spi_ptr->spi_int[iter];
+		spi_master_resume(spi_sub_ptr->spi_host);
+		writel(regval, spi_ptr->reg_base +
+		       SPI_MST_EVENT_MASK_REG_OFFSET(iter));
+
+		/* Restore config at resume */
+		store_restore_config(spi_ptr, spi_sub_ptr, iter, 0);
+	}
+
+	return 0;
+}
+
+static int pci1xxxx_spi_suspend(struct device *dev)
+{
+	struct pci1xxxx_spi *spi_ptr = dev_get_drvdata(dev);
+	struct pci1xxxx_spi_internal *spi_sub_ptr;
+	u32 reg1 = SPI_SUSPEND_CONFIG;
+	u8 iter;
+
+	for (iter = 0; iter < spi_ptr->total_hw_instances; iter++) {
+		spi_sub_ptr = spi_ptr->spi_int[iter];
+
+		/* Store existing config before suspend */
+		store_restore_config(spi_ptr, spi_sub_ptr, iter, 1);
+		spi_master_suspend(spi_sub_ptr->spi_host);
+		writel(reg1, spi_ptr->reg_base +
+		       SPI_MST_EVENT_MASK_REG_OFFSET(iter));
+	}
+
+	return 0;
+}
+
+static DEFINE_SIMPLE_DEV_PM_OPS(spi_pm_ops, pci1xxxx_spi_suspend,
+				pci1xxxx_spi_resume);
+
 static struct pci_driver pci1xxxx_spi_driver = {
 	.name		= DRV_NAME,
 	.id_table	= pci1xxxx_spi_pci_id_table,
 	.probe		= pci1xxxx_spi_probe,
+	.driver		=	{
+		.pm = pm_sleep_ptr(&spi_pm_ops),
+	},
 };
 
 module_pci_driver(pci1xxxx_spi_driver);
Signature · 1 line
2.25.1
maintainer
#5 lore raw

Re: [PATCH RFC SPI for-next 2/2] spi: microchip: pci1xxxx: Add suspend and resume support for PCI1XXXX SPI driver

drivers/spi/spi-pci1xxxx.c static int pci1xxxx_spi_probe(struct pci_dev *pdev, const struct pci_device_id * · line 431

Quoted 9 lines · Tharun Kumar P
drivers/spi/spi-pci1xxxx.c +9 -0
+	for (iter = 0; iter < spi_ptr->total_hw_instances; iter++) {
+		spi_sub_ptr = spi_ptr->spi_int[iter];
+
+		/* Store existing config before suspend */
+		store_restore_config(spi_ptr, spi_sub_ptr, iter, 1);
+		spi_master_suspend(spi_sub_ptr->spi_host);
+		writel(reg1, spi_ptr->reg_base +
+		       SPI_MST_EVENT_MASK_REG_OFFSET(iter));
+	}

This saves the register configuration before suspending the device, meaning there may be in progress transfers changing the device while the save is going on.

#6 lore raw

Re: [PATCH RFC SPI for-next 2/2] spi: microchip: pci1xxxx: Add suspend and resume support for PCI1XXXX SPI driver

Quoted 6 lines · Mark Brown

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

This saves the register configuration before suspending the device, meaning there may be in progress transfers changing the device while the save is going on

I will add logic to wait for the completion of existing transfer at beginning of suspend callback to handle this.

Thanks,
Tharun Kumar P