Skip to contentSkip to conversation

spi: spi-amd: Add AMD SPI controller driver support

PATCH Superseded Sanjay R Mehta 3 participants 3 messages Apr 12, 2020 – Apr 14, 2020

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 spi: spi-amd: Add AMD SPI controller driver support

This driver supports AMD based SPI Controller for AMD SOCs.This driver supports SPI operations using FIFO mode of transfer.

---
 MAINTAINERS           |   5 +
 drivers/spi/Kconfig   |   6 +
 drivers/spi/Makefile  |   1 +
 drivers/spi/spi-amd.c | 341 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 353 insertions(+)
 create mode 100644 drivers/spi/spi-amd.c
MAINTAINERS +5 -0
index 6158a14..4455b92 100644
@@ -870,6 +870,11 @@ F:	drivers/gpu/drm/amd/include/vi_structs.h
 F:	drivers/gpu/drm/amd/include/v9_structs.h
 F:	include/uapi/linux/kfd_ioctl.h
 
+AMD SPI DRIVER
+M:	Sanjay R Mehta <[email protected]>
+S:	Maintained
+F:	drivers/spi/spi-amd.c
+
 AMD MP2 I2C DRIVER
 M:	Elie Morisse <[email protected]>
 M:	Nehal Shah <[email protected]>
drivers/spi/Kconfig +6 -0
index d6ed0c3..1f4fa9a 100644
@@ -886,6 +886,12 @@ config SPI_ZYNQMP_GQSPI
 	help
 	  Enables Xilinx GQSPI controller driver for Zynq UltraScale+ MPSoC.
 
+config SPI_AMD
+	tristate "AMD SPI controller"
+	depends on SPI_MASTER || COMPILE_TEST
+	help
+	  Enables SPI controller driver for AMD SoC.
+
 #
 # Add new SPI master controllers in alphabetical order above this line
 #
drivers/spi/Makefile +1 -0
index 9b65ec5..ceb7249 100644
@@ -123,6 +123,7 @@ obj-$(CONFIG_SPI_XLP)			+= spi-xlp.o
 obj-$(CONFIG_SPI_XTENSA_XTFPGA)		+= spi-xtensa-xtfpga.o
 obj-$(CONFIG_SPI_ZYNQ_QSPI)		+= spi-zynq-qspi.o
 obj-$(CONFIG_SPI_ZYNQMP_GQSPI)		+= spi-zynqmp-gqspi.o
+obj-$(CONFIG_SPI_AMD)			+= spi-amd.o
 
 # SPI slave protocol handlers
 obj-$(CONFIG_SPI_SLAVE_TIME)		+= spi-slave-time.o
drivers/spi/spi-amd.c new file +341 -0

341 lines not on this page · raw message

Signature · 1 line
2.7.4
maintainer
#1 lore raw

drivers/spi/spi-amd.c line 4

Quoted 6 lines · Sanjay R Mehta

+++ b/drivers/spi/spi-amd.c

drivers/spi/spi-amd.c new file +4 -0
@@ -0,0 +1,341 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+/*
+ * AMD SPI controller driver
+ *

Please make the entire comment a C++ one so things look more intentional.

drivers/spi/spi-amd.c line 15

Quoted 1 line
drivers/spi/spi-amd.c new file +1 -0
+#define DRIVER_NAME		"amd_spi"

This is unused.

drivers/spi/spi-amd.c line 36

Quoted 3 lines
drivers/spi/spi-amd.c new file +3 -0
+/* M_CMD OP codes for SPI */
+#define SPI_XFER_TX		1
+#define SPI_XFER_RX		2

These constants should be namespaced, they're likely to collide with generic additions.

Quoted 9 lines
drivers/spi/spi-amd.c new file +9 -0
+static void amd_spi_execute_opcode(struct spi_master *master)
+{
+	struct amd_spi *amd_spi = spi_master_get_devdata(master);
+	bool spi_busy;
+
+	/* Set ExecuteOpCode bit in the CTRL0 register */
+	amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, AMD_SPI_EXEC_CMD,
+			       AMD_SPI_EXEC_CMD);
+

drivers/spi/spi-amd.c line 145

Quoted 10 lines
drivers/spi/spi-amd.c new file +10 -0
+	/* poll for SPI bus to become idle */
+	spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
+		    AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
+	while (spi_busy) {
+		set_current_state(TASK_INTERRUPTIBLE);
+		schedule();
+		set_current_state(TASK_RUNNING);
+		spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
+			    AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
+	}

This is a weird way to busy wait - usually you'd use a cpu_relax() rather than a schedule(). There's also no timeout here so we could busy wait for ever if something goes wrong.

drivers/spi/spi-amd.c line 154

Quoted 7 lines
drivers/spi/spi-amd.c new file +7 -0
+static int amd_spi_master_setup(struct spi_device *spi)
+{
+	struct spi_master *master = spi->master;
+	struct amd_spi *amd_spi = spi_master_get_devdata(master);
+
+	amd_spi->chip_select = spi->chip_select;
+	amd_spi_select_chip(master);

This looks like it will potentially affect devices other than the current one. setup() may be called while other devices are active it shouldn't do that.

drivers/spi/spi-amd.c line 231

Quoted 12 lines
drivers/spi/spi-amd.c new file +11 -0
+		} else if (m_cmd & SPI_XFER_RX) {
+			/* Store no. of bytes to be received from
+			 * FIFO
+			 */
+			rx_len = xfer->len;
+			buffer = (u8 *)xfer->rx_buf;
 
+		/* Read data from FIFO to receive buffer  */
+		for (i = 0; i < rx_len; i++)
+			buffer[i] = ioread8((u8 __iomem *)amd_spi->io_remap_addr
+					    + AMD_SPI_FIFO_BASE
+					    + tx_len + i);

This will only work for messages with a single receive transfer, if there are multiple transfers then you'll need to store multiple buffers and their lengths.

drivers/spi/spi-amd.c line 255

Quoted 13 lines
drivers/spi/spi-amd.c new file +13 -0
+static int amd_spi_master_transfer(struct spi_master *master,
+				   struct spi_message *msg)
+{
+	struct amd_spi *amd_spi = spi_master_get_devdata(master);
+
+	/*
+	 * Extract spi_transfers from the spi message and
+	 * program the controller.
+	 */
+	amd_spi_fifo_xfer(amd_spi, msg);
+
+	return 0;
+}

This function is completely redundant, just inline amd_spi_fifo_xfer(). It also ignores all errors which isn't great.

drivers/spi/spi-amd.c line 289

Quoted 5 lines
drivers/spi/spi-amd.c new file +5 -0
+	/* Initialize the spi_master fields */
+	master->bus_num = 0;
+	master->num_chipselect = 4;
+	master->mode_bits = 0;
+	master->flags = 0;

This device is single duplex so should flag that.

drivers/spi/spi-amd.c line 297

Quoted 4 lines
drivers/spi/spi-amd.c new file +4 -0
+	err = spi_register_master(master);
+	if (err) {
+		dev_err(dev, "error registering SPI controller\n");
+		goto err_iounmap;

It's best to print the error code to help people debug things.

#2 lore raw
Quoted 101 lines · Mark Brown

drivers/spi/spi-amd.c line 4

Quoted 6 lines · Sanjay R Mehta

+++ b/drivers/spi/spi-amd.c

drivers/spi/spi-amd.c new file +4 -0
@@ -0,0 +1,341 @@
+// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
+/*
+ * AMD SPI controller driver
+ *

Please make the entire comment a C++ one so things look more intentional.

drivers/spi/spi-amd.c line 15

Quoted 1 line
drivers/spi/spi-amd.c new file +1 -0
+#define DRIVER_NAME		"amd_spi"

This is unused.

drivers/spi/spi-amd.c line 36

Quoted 3 lines
drivers/spi/spi-amd.c new file +3 -0
+/* M_CMD OP codes for SPI */
+#define SPI_XFER_TX		1
+#define SPI_XFER_RX		2

These constants should be namespaced, they're likely to collide with generic additions.

Quoted 9 lines
drivers/spi/spi-amd.c new file +9 -0
+static void amd_spi_execute_opcode(struct spi_master *master)
+{
+	struct amd_spi *amd_spi = spi_master_get_devdata(master);
+	bool spi_busy;
+
+	/* Set ExecuteOpCode bit in the CTRL0 register */
+	amd_spi_setclear_reg32(master, AMD_SPI_CTRL0_REG, AMD_SPI_EXEC_CMD,
+			       AMD_SPI_EXEC_CMD);
+

drivers/spi/spi-amd.c line 145

Quoted 10 lines
drivers/spi/spi-amd.c new file +10 -0
+	/* poll for SPI bus to become idle */
+	spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
+		    AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
+	while (spi_busy) {
+		set_current_state(TASK_INTERRUPTIBLE);
+		schedule();
+		set_current_state(TASK_RUNNING);
+		spi_busy = (ioread32((u8 __iomem *)amd_spi->io_remap_addr +
+			    AMD_SPI_CTRL0_REG) & AMD_SPI_BUSY) == AMD_SPI_BUSY;
+	}

This is a weird way to busy wait - usually you'd use a cpu_relax() rather than a schedule(). There's also no timeout here so we could busy wait for ever if something goes wrong.

drivers/spi/spi-amd.c line 154

Quoted 7 lines
drivers/spi/spi-amd.c new file +7 -0
+static int amd_spi_master_setup(struct spi_device *spi)
+{
+	struct spi_master *master = spi->master;
+	struct amd_spi *amd_spi = spi_master_get_devdata(master);
+
+	amd_spi->chip_select = spi->chip_select;
+	amd_spi_select_chip(master);

This looks like it will potentially affect devices other than the current one. setup() may be called while other devices are active it shouldn't do that.

drivers/spi/spi-amd.c line 231

Quoted 12 lines
drivers/spi/spi-amd.c new file +11 -0
+		} else if (m_cmd & SPI_XFER_RX) {
+			/* Store no. of bytes to be received from
+			 * FIFO
+			 */
+			rx_len = xfer->len;
+			buffer = (u8 *)xfer->rx_buf;
 
+		/* Read data from FIFO to receive buffer  */
+		for (i = 0; i < rx_len; i++)
+			buffer[i] = ioread8((u8 __iomem *)amd_spi->io_remap_addr
+					    + AMD_SPI_FIFO_BASE
+					    + tx_len + i);

This will only work for messages with a single receive transfer, if there are multiple transfers then you'll need to store multiple buffers and their lengths.

drivers/spi/spi-amd.c line 255

Quoted 13 lines
drivers/spi/spi-amd.c new file +13 -0
+static int amd_spi_master_transfer(struct spi_master *master,
+				   struct spi_message *msg)
+{
+	struct amd_spi *amd_spi = spi_master_get_devdata(master);
+
+	/*
+	 * Extract spi_transfers from the spi message and
+	 * program the controller.
+	 */
+	amd_spi_fifo_xfer(amd_spi, msg);
+
+	return 0;
+}

This function is completely redundant, just inline amd_spi_fifo_xfer(). It also ignores all errors which isn't great.

drivers/spi/spi-amd.c line 289

Quoted 5 lines
drivers/spi/spi-amd.c new file +5 -0
+	/* Initialize the spi_master fields */
+	master->bus_num = 0;
+	master->num_chipselect = 4;
+	master->mode_bits = 0;
+	master->flags = 0;

This device is single duplex so should flag that.

Quoted 6 lines

drivers/spi/spi-amd.c line 297

Quoted 4 lines
drivers/spi/spi-amd.c new file +4 -0
+	err = spi_register_master(master);
+	if (err) {
+		dev_err(dev, "error registering SPI controller\n");
+		goto err_iounmap;

It's best to print the error code to help people debug things.

Thanks Mark for the feedback. Will make all the suggested changes.

Quoted 1 line