Skip to contentSkip to conversation

spi: Add driver to support AMD eSPI controller

PATCH In review Raju Rangoju 2 participants 21 messages Mar 13, 2025 – Apr 2, 2025 SPI

lore
Share & tools
lore
#0 lore raw

cover spi: Add driver to support AMD eSPI controller

The eSPI protocol serves as a communication interface between the main processor and peripheral devices in computer systems. It offers several advantages over the traditional LPC bus, including higher data transfer rates, increased scalability and improved system management capabilities. The eSPI protocol supports multiple channels, each serving a specific purpose in the communication process.

AMD SOCs feature multiple SPI controllers, including a dedicated eSPI controller. This controller is responsible for managing the communication between the main processor and peripheral devices using the eSPI protocol. It provides support for various channels, including Peripheral channel(PC), Virtual Wire(VW), Out-Of-Band(OOB) message, and Flash access channels.

This patch series implements new AMD eSPI driver aims to comprehensive support for the eSPI protocol, including initialization of eSPI controller and slave devices. It also adds functionality for channel-independent eSPI commands and support for I/O and MMIO read/write operations on PC channels.

Raju Rangoju (10):
spi: espi_amd: Add AMD eSPI controller driver support
spi: espi_amd: Add eSPI set config IOCTL command
spi: espi_amd: Add eSPI get config IOCTL command
spi: espi_amd: Add eSPI inband-reset IOCTL command
spi: espi_amd: Add eSPI set IO mode IOCTL command
spi: espi_amd: Add eSPI set channel IOCTL command
spi: espi_amd: Add eSPI set frequency IOCTL command
spi: espi_amd: Add support for IO/MMIO configuration
spi: espi_amd: Add eSPI PC channel IO read/write IOCTL commands
spi: espi_amd: Add eSPI PC channel MMIO read/write IOCTL commands

 MAINTAINERS                  |    6 +
 drivers/spi/Kconfig          |   10 +
 drivers/spi/Makefile         |    2 +
 drivers/spi/espi-amd-core.c  | 1215 ++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd-dev.c   |  584 ++++++++++++++++
 drivers/spi/espi-amd-err.h   |   50 ++
 drivers/spi/espi-amd-slave.h |  109 +++
 drivers/spi/espi-amd.h       |  416 ++++++++++++
 8 files changed, 2392 insertions(+)
 create mode 100644 drivers/spi/espi-amd-core.c
 create mode 100644 drivers/spi/espi-amd-dev.c
 create mode 100644 drivers/spi/espi-amd-err.h
 create mode 100644 drivers/spi/espi-amd-slave.h
 create mode 100644 drivers/spi/espi-amd.h
Signature · 1 line
2.34.1
#1 lore raw

PATCH 1/10 spi: espi_amd: Add AMD eSPI controller driver support

This patch enables core eSPI controller initialization in AMD embedded platforms, supporting communication with eSPI slave devices through the device file entry. Currently, the eSPI controller in AMD embedded platforms has only one chip select line enabled, which is connected to the Embedded Controller (EC).

---
 MAINTAINERS                  |   6 +
 drivers/spi/Kconfig          |  10 +
 drivers/spi/Makefile         |   2 +
 drivers/spi/espi-amd-core.c  | 883 +++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd-dev.c   | 128 +++++
 drivers/spi/espi-amd-err.h   |  50 ++
 drivers/spi/espi-amd-slave.h | 109 +++++
 drivers/spi/espi-amd.h       | 291 ++++++++++++
 8 files changed, 1479 insertions(+)
 create mode 100644 drivers/spi/espi-amd-core.c
 create mode 100644 drivers/spi/espi-amd-dev.c
 create mode 100644 drivers/spi/espi-amd-err.h
 create mode 100644 drivers/spi/espi-amd-slave.h
 create mode 100644 drivers/spi/espi-amd.h
MAINTAINERS +6 -0
index 9f052994b7d4..c102dcce5892 100644
@@ -1059,6 +1059,12 @@ S:	Supported
 F:	drivers/gpu/drm/amd/display/dc/dml/
 F:	drivers/gpu/drm/amd/display/dc/dml2/
 
+AMD ESPI DRIVER
+M:	Raju Rangoju<[email protected]>
+L:	[email protected]
+S:	Supported
+F:	drivers/spi/espi-amd*
+
 AMD FAM15H PROCESSOR POWER MONITORING DRIVER
 M:	Huang Rui <[email protected]>
 L:	[email protected]
drivers/spi/Kconfig +10 -0
index ea8a31032927..72b950f3eccb 100644
@@ -1238,6 +1238,16 @@ config SPI_AMD
 	help
 	  Enables SPI controller driver for AMD SoC.
 
+config SPI_AMD_ESPI
+	tristate "AMD eSPI controller"
+	depends on X86_64 || SPI_MASTER || COMPILE_TEST
+	help
+	  AMD SOCs contains multiple SPI controllers, including a dedicated
+	  eSPI controller. This config enables eSPI controller driver for
+	  AMD SoC. AMD eSPI driver aims to comprehensive support for the
+	  eSPI protocol, including initialization of eSPI controller
+	  and slave devices and eSPI channel specific commands.
+
 #
 # Add new SPI master controllers in alphabetical order above this line
 #
drivers/spi/Makefile +2 -0
index 9db7554c1864..0dec8ad30967 100644
@@ -159,6 +159,8 @@ 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
+obj-$(CONFIG_SPI_AMD_ESPI)		+= espi-amd.o
+espi-amd-objs				:= espi-amd-core.o espi-amd-dev.o
 
 # SPI slave protocol handlers
 obj-$(CONFIG_SPI_SLAVE_TIME)		+= spi-slave-time.o
drivers/spi/espi-amd-core.c new file +883 -0

883 lines not on this page · raw message

drivers/spi/espi-amd-dev.c new file +128 -0

128 lines not on this page · raw message

drivers/spi/espi-amd-err.h new file +50 -0

50 lines not on this page · raw message

drivers/spi/espi-amd-slave.h new file +109 -0

109 lines not on this page · raw message

drivers/spi/espi-amd.h new file +291 -0

291 lines not on this page · raw message

Signature · 1 line
2.34.1
#4 lore raw

PATCH 2/10 spi: espi_amd: Add eSPI set config IOCTL command

In reply to Raju Rangoju

This patch introduces an IOCTL command to set the configuration of the eSPI controller and eSPI slave0. The configuration options include parameters like frequency, channel, and IO mode. The new IOCTL command allow users to dynamically configure the eSPI controller and slave.

---
 drivers/spi/espi-amd-dev.c | 94 ++++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd.h     |  4 ++
 2 files changed, 98 insertions(+)
drivers/spi/espi-amd-dev.c +94 -0
index 4e46c30d3405..5f25ad2b1eef 100644
@@ -28,6 +28,99 @@ static DEFINE_MUTEX(device_list_lock);
 static struct class *amd_espi_dev_class;
 static struct cdev cdev;
 
+static int amd_espi_ioctl_set_conf(struct amd_espi *amd_espi, unsigned long arg)
+{
+	struct config *dev_conf, *config;
+	u32 slave_config;
+	int ret;
+
+	dev_conf = kzalloc(sizeof(*dev_conf), GFP_KERNEL);
+	if (!dev_conf)
+		return -ENOMEM;
+
+	config = kzalloc(sizeof(*config), GFP_KERNEL);
+	if (!config) {
+		kfree(dev_conf);
+		return -ENOMEM;
+	}
+
+	if (copy_from_user(config, (void __user *)arg, sizeof(struct config))) {
+		ret = -EFAULT;
+		goto set_config_free;
+	}
+
+	/* IO mode configuration */
+	if (config->io_mode != IO_MODE_SINGLE && config->io_mode != IO_MODE_DUAL &&
+	    config->io_mode != IO_MODE_QUAD) {
+		dev_err(amd_espi->dev, "Invalid IO mode\n");
+		ret = -EOPNOTSUPP;
+		goto set_config_free;
+	} else {
+		dev_conf->io_mode = config->io_mode;
+	}
+
+	/* Set operating frequency configuration */
+	if (config->op_freq != SLAVE_OP_FREQ_16 && config->op_freq != SLAVE_OP_FREQ_33 &&
+	    config->op_freq != SLAVE_OP_FREQ_66) {
+		dev_err(amd_espi->dev, "Invalid operating frequency\n");
+		ret = -EOPNOTSUPP;
+		goto set_config_free;
+	} else {
+		dev_conf->op_freq = config->op_freq;
+	}
+
+	ret = amd_espi_set_general_conf(amd_espi, dev_conf);
+	if (ret != CB_SUCCESS)
+		goto set_config_free;
+
+	/* Set channel configuration */
+	ret = amd_espi_get_general_config(amd_espi, &slave_config);
+	if (ret != CB_SUCCESS)
+		goto set_config_free;
+
+	if (config->channel_mode == CHANNEL_MODE_PC) {
+		ret = amd_espi_setup_periph_channel(amd_espi, slave_config);
+		if (ret) {
+			dev_err(amd_espi->dev, "Peripheral channel setup failed\n");
+			goto set_config_free;
+		}
+	} else if (config->channel_mode == CHANNEL_MODE_VW) {
+		ret = amd_espi_setup_vw_channel(amd_espi, slave_config);
+		if (ret) {
+			dev_err(amd_espi->dev, "Virtual wire channel setup failed\n");
+			goto set_config_free;
+		}
+	} else {
+		ret = -EOPNOTSUPP;
+	}
+
+set_config_free:
+	kfree(dev_conf);
+	kfree(config);
+	return ret;
+}
+
+static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	struct amd_espi *amd_espi = filp->private_data;
+	int ret = 0;
+
+	/* Check type and command number */
+	if (_IOC_TYPE(cmd) != ESPI_MAGIC_NUMBER)
+		return -EINVAL;
+
+	switch (cmd) {
+	case ESPI_SET_CONFIG:
+		ret = amd_espi_ioctl_set_conf(amd_espi, arg);
+		break;
+	default:
+		dev_err(amd_espi->dev, "ESPI command not found, returning error\n");
+		ret = -EINVAL;
+	}
+
+	return ret;
+}
+
 static int amd_espi_open(struct inode *inode, struct file *filp)
 {
 	struct amd_espi *espi;
@@ -68,6 +161,7 @@ static int amd_espi_release(struct inode *inode, struct file *filp)
 
 static const struct file_operations amd_espi_fops = {
 	.owner = THIS_MODULE,
+	.unlocked_ioctl = amd_espi_ioctl,
 	.open = amd_espi_open,
 	.release = amd_espi_release,
 };
drivers/spi/espi-amd.h +4 -0
index 57b156fb0a05..1de53426059b 100644
@@ -138,6 +138,10 @@
 
 #define ESPI_BASE		((u8 __iomem *)amd_espi->io_remap_addr)
 
+/* IOCTL calls */
+#define ESPI_MAGIC_NUMBER            'i'
+#define ESPI_SET_CONFIG              _IOW(ESPI_MAGIC_NUMBER, 0x1, struct config)
+
 /*
  * enum amd_espi_versions - eSPI controller versions
  * @AMD_ESPI_V1:		AMDI0070 hardware version
Signature · 1 line
2.34.1
#7 lore raw

PATCH 3/10 spi: espi_amd: Add eSPI get config IOCTL command

In reply to Raju Rangoju

This patch introduces an IOCTL command to retrieve the configuration of the eSPI controller and eSPI slave0. The configuration parameters include the frequency, channel, and IO mode.

---
 drivers/spi/espi-amd-dev.c | 69 ++++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd.h     |  1 +
 2 files changed, 70 insertions(+)
drivers/spi/espi-amd-dev.c +69 -0
index 5f25ad2b1eef..79224f4f86fa 100644
@@ -100,6 +100,72 @@ static int amd_espi_ioctl_set_conf(struct amd_espi *amd_espi, unsigned long arg)
 	return ret;
 }
 
+static int amd_espi_ioctl_get_conf(struct amd_espi *amd_espi, unsigned long arg)
+{
+	u32 op_freq, io_mode, slave_conf;
+	struct config *config;
+	int ret;
+
+	config = kzalloc(sizeof(*config), GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;
+
+	ret = amd_espi_get_general_config(amd_espi, &slave_conf);
+	if (ret != CB_SUCCESS)
+		goto get_config_free;
+
+	/* Get IO mode configuration */
+	io_mode = (slave_conf & GENMASK(27, 26)) >> ESPI_SLAVE_IO_MODE_SEL_SHIFT;
+	switch (io_mode) {
+	case IO_MODE_SINGLE:
+	case IO_MODE_DUAL:
+	case IO_MODE_QUAD:
+		config->io_mode = io_mode;
+		break;
+	default:
+		dev_err(amd_espi->dev, "Invalid IO mode\n");
+		ret = -EOPNOTSUPP;
+		goto get_config_free;
+	}
+
+	/* Get operating frequency configuration */
+	op_freq = (slave_conf & GENMASK(22, 20)) >> ESPI_SLAVE_OP_FREQ_SEL_SHIFT;
+	switch (op_freq) {
+	case SLAVE_OP_FREQ_16:
+	case SLAVE_OP_FREQ_33:
+	case SLAVE_OP_FREQ_66:
+		config->op_freq = op_freq;
+		break;
+	default:
+		dev_err(amd_espi->dev, "Invalid operating frequency\n");
+		ret = -EOPNOTSUPP;
+		goto get_config_free;
+	}
+
+	/* Get channel configuration */
+	ret = amd_espi_get_channel_config(amd_espi);
+	config->channel_mode = 0;
+
+	if (ret == -EOPNOTSUPP) {
+		config->channel_mode = CHAN_NOT_ENABLED;
+	} else {
+		if (ret & CHANNEL_MODE_PC)
+			config->channel_mode |= CHANNEL_MODE_PC;
+		if (ret & CHANNEL_MODE_VW)
+			config->channel_mode |= CHANNEL_MODE_VW;
+	}
+
+	if (copy_to_user((void __user *)arg, config, sizeof(struct config))) {
+		ret = -EFAULT;
+		goto get_config_free;
+	}
+	ret = 0;
+
+get_config_free:
+	kfree(config);
+	return ret;
+}
+
 static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
 	struct amd_espi *amd_espi = filp->private_data;
@@ -113,6 +179,9 @@ static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
 	case ESPI_SET_CONFIG:
 		ret = amd_espi_ioctl_set_conf(amd_espi, arg);
 		break;
+	case ESPI_GET_CONFIG:
+		ret = amd_espi_ioctl_get_conf(amd_espi, arg);
+		break;
 	default:
 		dev_err(amd_espi->dev, "ESPI command not found, returning error\n");
 		ret = -EINVAL;
drivers/spi/espi-amd.h +1 -0
index 1de53426059b..bc15f7417c37 100644
@@ -141,6 +141,7 @@
 /* IOCTL calls */
 #define ESPI_MAGIC_NUMBER            'i'
 #define ESPI_SET_CONFIG              _IOW(ESPI_MAGIC_NUMBER, 0x1, struct config)
+#define ESPI_GET_CONFIG              _IOR(ESPI_MAGIC_NUMBER, 0x2, struct config)
 
 /*
  * enum amd_espi_versions - eSPI controller versions
Signature · 1 line
2.34.1
#8 lore raw

PATCH 4/10 spi: espi_amd: Add eSPI inband-reset IOCTL command

In reply to Raju Rangoju

This patch adds support for a channel-independent IOCTL command to dynamically reset the configuration settings of the eSPI controller.

---
 drivers/spi/espi-amd-dev.c | 5 +++++
 drivers/spi/espi-amd.h     | 1 +
 2 files changed, 6 insertions(+)
drivers/spi/espi-amd-dev.c +5 -0
index 79224f4f86fa..ba2becdec8e6 100644
@@ -182,6 +182,11 @@ static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
 	case ESPI_GET_CONFIG:
 		ret = amd_espi_ioctl_get_conf(amd_espi, arg);
 		break;
+	case ESPI_INBAND_RESET:
+		ret = amd_espi_inband_reset(amd_espi);
+		if (ret != CB_SUCCESS)
+			dev_err(amd_espi->dev, "In-band reset failed!\n");
+		break;
 	default:
 		dev_err(amd_espi->dev, "ESPI command not found, returning error\n");
 		ret = -EINVAL;
drivers/spi/espi-amd.h +1 -0
index bc15f7417c37..3c6e9b92c08d 100644
@@ -142,6 +142,7 @@
 #define ESPI_MAGIC_NUMBER            'i'
 #define ESPI_SET_CONFIG              _IOW(ESPI_MAGIC_NUMBER, 0x1, struct config)
 #define ESPI_GET_CONFIG              _IOR(ESPI_MAGIC_NUMBER, 0x2, struct config)
+#define ESPI_INBAND_RESET            _IO(ESPI_MAGIC_NUMBER, 0x3)
 
 /*
  * enum amd_espi_versions - eSPI controller versions
Signature · 1 line
2.34.1
#9 lore raw

PATCH 5/10 spi: espi_amd: Add eSPI set IO mode IOCTL command

In reply to Raju Rangoju

This patch introduces an IOCTL command to allow users to dynamically configure the supported IO modes such as single, dual and quad modes.

---
 drivers/spi/espi-amd-dev.c | 40 ++++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd.h     |  1 +
 2 files changed, 41 insertions(+)
drivers/spi/espi-amd-dev.c +40 -0
index ba2becdec8e6..821cc0486657 100644
@@ -166,6 +166,43 @@ static int amd_espi_ioctl_get_conf(struct amd_espi *amd_espi, unsigned long arg)
 	return ret;
 }
 
+static int amd_espi_ioctl_set_io_mode(struct amd_espi *amd_espi, unsigned long arg)
+{
+	u32 ctrlr_config, slave_config;
+	struct config *config;
+	int ret;
+
+	config = kzalloc(sizeof(*config), GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;
+
+	if (copy_from_user(config, (void __user *)arg, sizeof(struct config))) {
+		ret = -EFAULT;
+		goto set_io_mode_free;
+	}
+
+	ctrlr_config = readl(ESPI_BASE + AMD_ESPI_SLAVE0_CONFIG_REG);
+
+	ret = amd_espi_get_general_config(amd_espi, &slave_config);
+	if (ret != CB_SUCCESS)
+		goto set_io_mode_free;
+
+	ret = amd_espi_set_iomode(amd_espi, &slave_config, &ctrlr_config, config->io_mode);
+	if (ret != CB_SUCCESS)
+		goto set_io_mode_free;
+
+	ret = amd_espi_set_config(amd_espi, slave_config, ESPI_SLAVE_GENERAL_CAPS_CFG);
+	if (ret != CB_SUCCESS) {
+		dev_err(amd_espi->dev, "Set IO mode failed\n");
+		goto set_io_mode_free;
+	}
+	writel(ctrlr_config, (ESPI_BASE + AMD_ESPI_SLAVE0_CONFIG_REG));
+
+set_io_mode_free:
+	kfree(config);
+	return ret;
+}
+
 static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
 	struct amd_espi *amd_espi = filp->private_data;
@@ -187,6 +224,9 @@ static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
 		if (ret != CB_SUCCESS)
 			dev_err(amd_espi->dev, "In-band reset failed!\n");
 		break;
+	case ESPI_SET_IO_MODE:
+		ret = amd_espi_ioctl_set_io_mode(amd_espi, arg);
+		break;
 	default:
 		dev_err(amd_espi->dev, "ESPI command not found, returning error\n");
 		ret = -EINVAL;
drivers/spi/espi-amd.h +1 -0
index 3c6e9b92c08d..f1185f877a05 100644
@@ -143,6 +143,7 @@
 #define ESPI_SET_CONFIG              _IOW(ESPI_MAGIC_NUMBER, 0x1, struct config)
 #define ESPI_GET_CONFIG              _IOR(ESPI_MAGIC_NUMBER, 0x2, struct config)
 #define ESPI_INBAND_RESET            _IO(ESPI_MAGIC_NUMBER, 0x3)
+#define ESPI_SET_IO_MODE             _IOW(ESPI_MAGIC_NUMBER, 0x4, struct config)
 
 /*
  * enum amd_espi_versions - eSPI controller versions
Signature · 1 line
2.34.1
#10 lore raw

PATCH 6/10 spi: espi_amd: Add eSPI set channel IOCTL command

In reply to Raju Rangoju

This patch introduces support for a new IOCTL command to set the channel of eSPI controller. Currently espi_amd driver supports to enable peripheral and virtual wire channels.

---
 drivers/spi/espi-amd-dev.c | 49 ++++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd.h     |  1 +
 2 files changed, 50 insertions(+)
drivers/spi/espi-amd-dev.c +49 -0
index 821cc0486657..c78a9b2b059b 100644
@@ -203,6 +203,52 @@ static int amd_espi_ioctl_set_io_mode(struct amd_espi *amd_espi, unsigned long a
 	return ret;
 }
 
+static int amd_espi_ioctl_set_channel_mode(struct amd_espi *amd_espi, unsigned long arg)
+{
+	struct config *config;
+	u32 slave_config;
+	int ret;
+
+	config = kzalloc(sizeof(*config), GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;
+
+	if (copy_from_user(config, (void __user *)arg, sizeof(struct config))) {
+		ret = -EFAULT;
+		goto set_chan_mode_free;
+	}
+
+	ret = amd_espi_get_general_config(amd_espi, &slave_config);
+	if (ret != CB_SUCCESS) {
+		dev_err(amd_espi->dev, "Get configuration failed\n");
+		goto set_chan_mode_free;
+	}
+
+	switch (config->channel_mode) {
+	case CHANNEL_MODE_PC:
+		ret = amd_espi_setup_periph_channel(amd_espi, slave_config);
+		if (ret != CB_SUCCESS) {
+			dev_err(amd_espi->dev, "Setup peripheral channel failed\n");
+			goto set_chan_mode_free;
+		}
+		break;
+	case CHANNEL_MODE_VW:
+		ret = amd_espi_setup_vw_channel(amd_espi, slave_config);
+		if (ret != CB_SUCCESS) {
+			dev_err(amd_espi->dev, "Setup virtual wire channel failed\n");
+			goto set_chan_mode_free;
+		}
+		break;
+	default:
+		dev_err(amd_espi->dev, "Channel: not supported\n");
+		ret = -EOPNOTSUPP;
+	}
+
+set_chan_mode_free:
+	kfree(config);
+	return ret;
+}
+
 static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
 	struct amd_espi *amd_espi = filp->private_data;
@@ -227,6 +273,9 @@ static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
 	case ESPI_SET_IO_MODE:
 		ret = amd_espi_ioctl_set_io_mode(amd_espi, arg);
 		break;
+	case ESPI_SET_CHAN_MODE:
+		ret = amd_espi_ioctl_set_channel_mode(amd_espi, arg);
+		break;
 	default:
 		dev_err(amd_espi->dev, "ESPI command not found, returning error\n");
 		ret = -EINVAL;
drivers/spi/espi-amd.h +1 -0
index f1185f877a05..3774918d994f 100644
@@ -144,6 +144,7 @@
 #define ESPI_GET_CONFIG              _IOR(ESPI_MAGIC_NUMBER, 0x2, struct config)
 #define ESPI_INBAND_RESET            _IO(ESPI_MAGIC_NUMBER, 0x3)
 #define ESPI_SET_IO_MODE             _IOW(ESPI_MAGIC_NUMBER, 0x4, struct config)
+#define ESPI_SET_CHAN_MODE           _IOW(ESPI_MAGIC_NUMBER, 0x5, struct config)
 
 /*
  * enum amd_espi_versions - eSPI controller versions
Signature · 1 line
2.34.1
#11 lore raw

PATCH 7/10 spi: espi_amd: Add eSPI set frequency IOCTL command

In reply to Raju Rangoju

This patch adds support for a new IOCTL command in the espi_amd driver to set the operating frequency of the eSPI controller. The frequency value can be one of the following supported options: 16MHz, 33MHz or 66MHz.

---
 drivers/spi/espi-amd-dev.c | 40 ++++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd.h     |  1 +
 2 files changed, 41 insertions(+)
drivers/spi/espi-amd-dev.c +40 -0
index c78a9b2b059b..9f1968566980 100644
@@ -249,6 +249,43 @@ static int amd_espi_ioctl_set_channel_mode(struct amd_espi *amd_espi, unsigned l
 	return ret;
 }
 
+static int amd_espi_ioctl_set_freq(struct amd_espi *amd_espi, unsigned long arg)
+{
+	u32 ctrlr_config, slave_config;
+	struct config *config;
+	int ret;
+
+	config = kzalloc(sizeof(*config), GFP_KERNEL);
+	if (!config)
+		return -ENOMEM;
+
+	if (copy_from_user(config, (void __user *)arg, sizeof(struct config))) {
+		ret = -EFAULT;
+		goto set_freq_free;
+	}
+
+	ctrlr_config = readl(ESPI_BASE + AMD_ESPI_SLAVE0_CONFIG_REG);
+
+	ret = amd_espi_get_general_config(amd_espi, &slave_config);
+	if (ret != CB_SUCCESS)
+		goto set_freq_free;
+
+	ret = amd_espi_set_freqmode(amd_espi, &slave_config, &ctrlr_config, config->op_freq);
+	if (ret != CB_SUCCESS)
+		goto set_freq_free;
+
+	ret = amd_espi_set_config(amd_espi, slave_config, ESPI_SLAVE_GENERAL_CAPS_CFG);
+	if (ret != CB_SUCCESS) {
+		dev_err(amd_espi->dev, "Set operating frequency failed\n");
+		goto set_freq_free;
+	}
+	writel(ctrlr_config, (ESPI_BASE + AMD_ESPI_SLAVE0_CONFIG_REG));
+
+set_freq_free:
+	kfree(config);
+	return ret;
+}
+
 static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
 	struct amd_espi *amd_espi = filp->private_data;
@@ -276,6 +313,9 @@ static long amd_espi_ioctl(struct file *filp, unsigned int cmd, unsigned long ar
 	case ESPI_SET_CHAN_MODE:
 		ret = amd_espi_ioctl_set_channel_mode(amd_espi, arg);
 		break;
+	case  ESPI_SET_FREQ:
+		ret = amd_espi_ioctl_set_freq(amd_espi, arg);
+		break;
 	default:
 		dev_err(amd_espi->dev, "ESPI command not found, returning error\n");
 		ret = -EINVAL;
drivers/spi/espi-amd.h +1 -0
index 3774918d994f..e5760db7c1d3 100644
@@ -145,6 +145,7 @@
 #define ESPI_INBAND_RESET            _IO(ESPI_MAGIC_NUMBER, 0x3)
 #define ESPI_SET_IO_MODE             _IOW(ESPI_MAGIC_NUMBER, 0x4, struct config)
 #define ESPI_SET_CHAN_MODE           _IOW(ESPI_MAGIC_NUMBER, 0x5, struct config)
+#define ESPI_SET_FREQ                _IOW(ESPI_MAGIC_NUMBER, 0x6, struct config)
 
 /*
  * enum amd_espi_versions - eSPI controller versions
Signature · 1 line
2.34.1
#12 lore raw

PATCH 8/10 spi: espi_amd: Add support for IO/MMIO configuration

In reply to Raju Rangoju

Add support to configure the eSPI slave0 IO/MMIO address before initiating the peripheral channel IO/MMIO read and write operations. This patch introduces new IOCTLs to enable, disable and read IO/MMIO configurations.

---
 drivers/spi/espi-amd-core.c | 157 ++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd-dev.c  |  53 ++++++++++++
 drivers/spi/espi-amd.h      |  91 +++++++++++++++++++++
 3 files changed, 301 insertions(+)
drivers/spi/espi-amd-core.c +157 -0
index 72a625b8b16d..3704cbd816ae 100644
@@ -553,6 +553,163 @@ int amd_espi_setup_vw_channel(struct amd_espi *amd_espi, u32 slave_caps)
 			CHANNEL_MODE_VW);
 }
 
+void amd_espi_get_io_mmio_decode_info(struct amd_espi *amd_espi,
+				      struct io_mmio_decode_config *config)
+{
+	config->io_mmio_dc_enable = readl(ESPI_BASE + AMD_ESPI_SLAVE0_DECODE_EN_REG);
+	config->range0.val = readl(ESPI_BASE + AMD_ESPI_SLAVE0_IO_BASE_REG0_REG);
+	config->range1.val = readl(ESPI_BASE + AMD_ESPI_SLAVE0_IO_BASE_REG1_REG);
+	config->range2.val = readl(ESPI_BASE + AMD_ESPI_SLAVE0_IO_SIZE_REG);
+	config->mmio_target_range0 = readl(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG0);
+	config->mmio_target_range1 = readl(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG1);
+	config->mmio_target_range2 = readl(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG2);
+	config->mmio_target_range3 = readl(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG3);
+	config->mmio_range4.val = readl(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_SIZE_REG0);
+	config->mmio_range5.val = readl(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_SIZE_REG1);
+}
+
+void amd_espi_set_io_mmio_decode_config(struct amd_espi *amd_espi,
+					struct io_mmio_decode_config *config)
+{
+	struct io_mmio_decode_config io_dc_conf;
+
+	amd_espi_get_io_mmio_decode_info(amd_espi, &io_dc_conf);
+	writel(((~(config->io_mmio_dc_enable) & io_dc_conf.io_mmio_dc_enable) |
+		config->io_mmio_dc_enable), (ESPI_BASE + AMD_ESPI_SLAVE0_DECODE_EN_REG));
+
+	/* IO RANGE-0 configuration */
+	if (config->io_mmio_dc_enable & IO_DECODE_RANGE0) {
+		if (config->range0.base_addr_range0 != io_dc_conf.range0.base_addr_range0) {
+			writel(((io_dc_conf.range0.val & CNTRL_IO_DECODE_ADDR_MASK(0)) |
+				config->range0.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_IO_BASE_REG0_REG));
+			writel(((io_dc_conf.range2.val & CNTRL_IO_DECODE_SIZE_MASK(0)) |
+				config->range2.val), (ESPI_BASE + AMD_ESPI_SLAVE0_IO_SIZE_REG));
+		}
+	}
+
+	/* IO RANGE-1 configuration */
+	if (config->io_mmio_dc_enable & IO_DECODE_RANGE1) {
+		if (config->range0.base_addr_range1 != io_dc_conf.range0.base_addr_range1) {
+			writel(((io_dc_conf.range0.val & CNTRL_IO_DECODE_ADDR_MASK(16)) |
+				config->range0.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_IO_BASE_REG0_REG));
+			writel(((io_dc_conf.range2.val & CNTRL_IO_DECODE_SIZE_MASK(8)) |
+				config->range2.val), (ESPI_BASE + AMD_ESPI_SLAVE0_IO_SIZE_REG));
+		}
+	}
+
+	/* IO RANGE-2 configuration */
+	if (config->io_mmio_dc_enable & IO_DECODE_RANGE2) {
+		if (config->range1.base_addr_range2 != io_dc_conf.range1.base_addr_range2) {
+			writel(((io_dc_conf.range1.val & CNTRL_IO_DECODE_ADDR_MASK(0)) |
+				config->range1.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_IO_BASE_REG1_REG));
+			writel(((io_dc_conf.range2.val & CNTRL_IO_DECODE_SIZE_MASK(16)) |
+				config->range2.val), (ESPI_BASE + AMD_ESPI_SLAVE0_IO_SIZE_REG));
+		}
+	}
+
+	/* IO RANGE-3 configuration */
+	if (config->io_mmio_dc_enable & IO_DECODE_RANGE3) {
+		if (config->range1.base_addr_range3 != io_dc_conf.range1.base_addr_range3) {
+			writel(((io_dc_conf.range1.val & CNTRL_IO_DECODE_ADDR_MASK(16)) |
+				config->range1.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_IO_BASE_REG1_REG));
+			writel(((io_dc_conf.range2.val & CNTRL_IO_DECODE_SIZE_MASK(24)) |
+				config->range2.val), (ESPI_BASE + AMD_ESPI_SLAVE0_IO_SIZE_REG));
+		}
+	}
+
+	/* MMIO RANGE-0 configure */
+	if (config->io_mmio_dc_enable & MMIO_DECODE_RANGE0) {
+		if (config->mmio_target_range0 != io_dc_conf.mmio_target_range0) {
+			writel(config->mmio_target_range0,
+			       (ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG0));
+			writel(((io_dc_conf.mmio_range4.val & CNTRL_MMIO_DECODE_SIZE_MASK(0)) |
+				config->mmio_range4.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_SIZE_REG0));
+		}
+	}
+
+	/* MMIO RANGE-1 configure */
+	if (config->io_mmio_dc_enable & MMIO_DECODE_RANGE1) {
+		if (config->mmio_target_range1 != io_dc_conf.mmio_target_range1) {
+			writel(config->mmio_target_range1,
+			       (ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG1));
+			writel(((io_dc_conf.mmio_range4.val & CNTRL_MMIO_DECODE_SIZE_MASK(16)) |
+				config->mmio_range4.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_SIZE_REG0));
+		}
+	}
+
+	/* MMIO RANGE-2 configure */
+	if (config->io_mmio_dc_enable & MMIO_DECODE_RANGE2) {
+		if (config->mmio_target_range2 != io_dc_conf.mmio_target_range2) {
+			writel(config->mmio_target_range2,
+			       (ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG2));
+			writel(((io_dc_conf.mmio_range5.val & CNTRL_MMIO_DECODE_SIZE_MASK(0)) |
+				config->mmio_range5.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_SIZE_REG1));
+		}
+	}
+
+	/* MMIO RANGE-3 configure */
+	if (config->io_mmio_dc_enable & MMIO_DECODE_RANGE3) {
+		if (config->mmio_target_range3 != io_dc_conf.mmio_target_range3) {
+			writel(config->mmio_target_range3,
+			       (ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_BASE_REG3));
+			writel(((io_dc_conf.mmio_range5.val & CNTRL_MMIO_DECODE_SIZE_MASK(16)) |
+				config->mmio_range5.val),
+				(ESPI_BASE + AMD_ESPI_SLAVE0_MMIO_SIZE_REG1));
+		}
+	}
+}
+
+void amd_espi_disable_io_decode_range(struct amd_espi *amd_espi, u32 io_range)
+{
+	u32 io_mmio_dc_enable = readl(ESPI_BASE + AMD_ESPI_SLAVE0_DECODE_EN_REG);
+
+	switch (io_range) {
+	case 1:
+		if (io_mmio_dc_enable & IO_DECODE_RANGE0)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ IO_DECODE_RANGE0;
+		break;
+	case 2:
+		if (io_mmio_dc_enable & IO_DECODE_RANGE1)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ IO_DECODE_RANGE1;
+		break;
+	case 3:
+		if (io_mmio_dc_enable & IO_DECODE_RANGE2)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ IO_DECODE_RANGE2;
+		break;
+	case 4:
+		if (io_mmio_dc_enable & IO_DECODE_RANGE3)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ IO_DECODE_RANGE3;
+		break;
+	case 5:
+		if (io_mmio_dc_enable & MMIO_DECODE_RANGE0)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ MMIO_DECODE_RANGE0;
+		break;
+	case 6:
+		if (io_mmio_dc_enable & MMIO_DECODE_RANGE1)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ MMIO_DECODE_RANGE1;
+		break;
+	case 7:
+		if (io_mmio_dc_enable & MMIO_DECODE_RANGE2)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ MMIO_DECODE_RANGE2;
+		break;
+	case 8:
+		if (io_mmio_dc_enable & MMIO_DECODE_RANGE3)
+			io_mmio_dc_enable = io_mmio_dc_enable ^ MMIO_DECODE_RANGE3;
+		break;
+	default:
+		break;
+	}
+
+	writel(io_mmio_dc_enable, (ESPI_BASE + AMD_ESPI_SLAVE0_DECODE_EN_REG));
+}
+
 static int amd_espi_get_master_cap(struct amd_espi *amd_espi, struct espi_master *master)
 {
 	u32 master_cap_reg, info;
drivers/spi/espi-amd-dev.c +53 -0

68 lines not on this page · raw message

drivers/spi/espi-amd.h +91 -0

119 lines not on this page · raw message

Signature · 1 line
2.34.1
#15 lore raw

PATCH 9/10 spi: espi_amd: Add eSPI PC channel IO read/write IOCTL commands

In reply to Raju Rangoju

Add new IOCTL commands to perform eSPI peripheral channel(PC) IO read/write operations with peripherals connected to eSPI slave0. The changes include validation of IO configuration before initiating the read/write operations.

---
 drivers/spi/espi-amd-core.c | 95 +++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd-dev.c  | 53 +++++++++++++++++++++
 drivers/spi/espi-amd.h      | 16 +++++++
 3 files changed, 164 insertions(+)
drivers/spi/espi-amd-core.c +95 -0
index 3704cbd816ae..18bfd2ca2316 100644
@@ -114,6 +114,8 @@ static int amd_espi_alloc_cmd_data(struct espi_txcmd *cmd)
 	case GET_CONFIGURATION:
 	case IN_BAND_RESET:
 		size = 1;
+		break;
+	case PERIPHERAL_CHNL:
 	default:
 		break;
 	}
@@ -710,6 +712,99 @@ void amd_espi_disable_io_decode_range(struct amd_espi *amd_espi, u32 io_range)
 	writel(io_mmio_dc_enable, (ESPI_BASE + AMD_ESPI_SLAVE0_DECODE_EN_REG));
 }
 
+static int is_port_address_valid(struct amd_espi *amd_espi, struct periph_io_rw *msg_io)
+{
+	struct io_mmio_decode_config io_conf;
+	u16 port_end_addr = msg_io->port + msg_io->len - 1;
+
+	amd_espi_get_io_mmio_decode_info(amd_espi, &io_conf);
+
+	if (msg_io->port >= io_conf.range0.base_addr_range0 &&
+	    (port_end_addr <= ((u16)io_conf.range0.base_addr_range0 +
+			       io_conf.range2.io_range0_size))) {
+		if (!(io_conf.io_mmio_dc_enable & IO_DECODE_RANGE0)) {
+			dev_err(amd_espi->dev, "IO range0 not enabled for port address: 0x%x\n",
+				msg_io->port);
+			return -EINVAL;
+		}
+	} else if ((msg_io->port >= io_conf.range0.base_addr_range1) &&
+		   (port_end_addr <= ((u16)io_conf.range0.base_addr_range1 +
+				      io_conf.range2.io_range1_size))) {
+		if (!(io_conf.io_mmio_dc_enable & IO_DECODE_RANGE1)) {
+			dev_err(amd_espi->dev, "IO range1 not enabled for port address: 0x%x\n",
+				msg_io->port);
+			return -EINVAL;
+		}
+	} else if ((msg_io->port >= io_conf.range1.base_addr_range2) &&
+		   (port_end_addr  <= ((u16)io_conf.range1.base_addr_range2 +
+				       io_conf.range2.io_range2_size))) {
+		if (!(io_conf.io_mmio_dc_enable & IO_DECODE_RANGE2)) {
+			dev_err(amd_espi->dev, "IO range2 not enabled for port address: 0x%x\n",
+				msg_io->port);
+			return -EINVAL;
+		}
+	} else if ((msg_io->port >= io_conf.range1.base_addr_range3) &&
+		   (port_end_addr <= ((u16)io_conf.range1.base_addr_range3 +
+				      io_conf.range2.io_range3_size))) {
+		if (!(io_conf.io_mmio_dc_enable & IO_DECODE_RANGE3)) {
+			dev_err(amd_espi->dev, "IO range3 not enabled for port address: 0x%x\n",
+				msg_io->port);
+			return -EINVAL;
+		}
+	} else {
+		dev_err(amd_espi->dev, "Port address 0x%x is invalid\n", msg_io->port);
+		return -EINVAL;
+	}
+
+	return CB_SUCCESS;
+}
+
+int amd_espi_periph_io_write(struct amd_espi *amd_espi, struct periph_io_rw *msg_io)
+{
+	if (is_port_address_valid(amd_espi, msg_io) != CB_SUCCESS)
+		return -EINVAL;
+
+	switch (msg_io->len) {
+	case 1:
+		outb(msg_io->data.data_b, msg_io->port);
+		break;
+	case 2:
+		outw(msg_io->data.data_w, msg_io->port);
+		break;
+	case 4:
+		outl(msg_io->data.data_l, msg_io->port);
+		break;
+	default:
+		dev_err(amd_espi->dev, "Length of IO packet is not valid\n");
+		return -EINVAL;
+	}
+
+	return CB_SUCCESS;
+}
+
+int amd_espi_periph_io_read(struct amd_espi *amd_espi, struct periph_io_rw *msg_io)
+{
+	if (is_port_address_valid(amd_espi, msg_io) != CB_SUCCESS)
+		return -EINVAL;
+
+	switch (msg_io->len) {
+	case 1:
+		msg_io->data.data_b = inb(msg_io->port);
+		break;
+	case 2:
+		msg_io->data.data_w = inw(msg_io->port);
+		break;
+	case 4:
+		msg_io->data.data_l = inl(msg_io->port);
+		break;
+	default:
+		dev_err(amd_espi->dev, "Length of IO packet is not valid\n");
+		return -EINVAL;
+	}
+
+	return CB_SUCCESS;
+}
+
 static int amd_espi_get_master_cap(struct amd_espi *amd_espi, struct espi_master *master)
 {
 	u32 master_cap_reg, info;
drivers/spi/espi-amd-dev.c +53 -0

65 lines not on this page · raw message

drivers/spi/espi-amd.h +16 -0
index ae76243786f0..4c67aa9f45a6 100644
@@ -176,6 +176,8 @@
 #define ESPI_GET_IODECODE_CONFIG     _IOWR(ESPI_MAGIC_NUMBER, 0x7, struct io_mmio_decode_config)
 #define ESPI_EN_IODECODE_CONFIG      _IOWR(ESPI_MAGIC_NUMBER, 0x8, struct io_mmio_decode_config)
 #define ESPI_DS_IODECODE_CONFIG      _IOWR(ESPI_MAGIC_NUMBER, 0x9, u32)
+#define ESPI_IO_WRITE                _IOWR(ESPI_MAGIC_NUMBER, 0xa, struct periph_io_rw)
+#define ESPI_IO_READ                 _IOWR(ESPI_MAGIC_NUMBER, 0xb, struct periph_io_rw)
 
 /*
  * enum amd_espi_versions - eSPI controller versions
@@ -368,6 +370,18 @@ struct io_mmio_decode_config {
 	union mmio_target_range5 mmio_range5;
 };
 
+union io_data {
+	u8 data_b;
+	u16 data_w;
+	u32 data_l;
+};
+
+struct periph_io_rw {
+	u8 len;
+	u16 port;
+	union io_data data;
+} __packed;
+
 /* Function prototypes */
 int amd_espi_device_create(struct amd_espi *amd_espi, struct device *dev);
 void amd_espi_device_remove(struct amd_espi *amd_espi);
@@ -388,4 +402,6 @@ void amd_espi_get_io_mmio_decode_info(struct amd_espi *amd_espi,
 void amd_espi_set_io_mmio_decode_config(struct amd_espi *amd_espi,
 					struct io_mmio_decode_config *config);
 void amd_espi_disable_io_decode_range(struct amd_espi *amd_espi, u32 io_range);
+int amd_espi_periph_io_write(struct amd_espi *amd_espi, struct periph_io_rw *message_io);
+int amd_espi_periph_io_read(struct amd_espi *amd_espi, struct periph_io_rw *message_io);
 #endif
Signature · 1 line
2.34.1
#16 lore raw

PATCH 10/10 spi: espi_amd: Add eSPI PC channel MMIO read/write IOCTL commands

In reply to Raju Rangoju

Add new IOCTL commands to perform eSPI peripheral channel(PC) MMIO read/write operations with eSPI slave0. The changes include validation of MMIO address ranges before initiating the read/write operations.

---
 drivers/spi/espi-amd-core.c | 80 +++++++++++++++++++++++++++++++++++++
 drivers/spi/espi-amd-dev.c  | 53 ++++++++++++++++++++++++
 drivers/spi/espi-amd.h      |  9 +++++
 3 files changed, 142 insertions(+)
drivers/spi/espi-amd-core.c +80 -0
index 18bfd2ca2316..31069e86c704 100644
@@ -805,6 +805,86 @@ int amd_espi_periph_io_read(struct amd_espi *amd_espi, struct periph_io_rw *msg_
 	return CB_SUCCESS;
 }
 
+static int is_mmio_address_valid(struct amd_espi *amd_espi,  struct periph_mem_rw *mem_data)
+{
+	struct io_mmio_decode_config io_conf;
+	u32 mmio_end_addr = mem_data->addr + 3;
+
+	amd_espi_get_io_mmio_decode_info(amd_espi, &io_conf);
+	if (mem_data->addr >= io_conf.mmio_target_range0 &&
+	    (mmio_end_addr <= ((u32)io_conf.mmio_target_range0 +
+			       io_conf.mmio_range4.mmio_range0_size))) {
+		if (!(io_conf.io_mmio_dc_enable & MMIO_DECODE_RANGE0)) {
+			dev_err(amd_espi->dev, "MMIO range0 not enabled for address: 0x%x\n",
+				mem_data->addr);
+			return -EINVAL;
+		}
+	} else if ((mem_data->addr >= io_conf.mmio_target_range1) &&
+		   (mmio_end_addr <= ((u32)io_conf.mmio_target_range1 +
+				      io_conf.mmio_range4.mmio_range1_size))) {
+		if (!(io_conf.io_mmio_dc_enable & MMIO_DECODE_RANGE1)) {
+			dev_err(amd_espi->dev, "MMIO range1 not enabled for address: 0x%x\n",
+				mem_data->addr);
+			return -EINVAL;
+		}
+	} else if ((mem_data->addr >= io_conf.mmio_target_range2) &&
+		   (mmio_end_addr <= ((u32)io_conf.mmio_target_range2 +
+				      io_conf.mmio_range5.mmio_range2_size))) {
+		if (!(io_conf.io_mmio_dc_enable & MMIO_DECODE_RANGE2)) {
+			dev_err(amd_espi->dev, "MMIO range2 not enabled for address: 0x%x\n",
+				mem_data->addr);
+			return -EINVAL;
+		}
+	} else if ((mem_data->addr >= io_conf.mmio_target_range3) &&
+		   (mmio_end_addr <= ((u32)io_conf.mmio_target_range3 +
+				      io_conf.mmio_range5.mmio_range3_size))) {
+		if (!(io_conf.io_mmio_dc_enable & MMIO_DECODE_RANGE3)) {
+			dev_err(amd_espi->dev, "MMIO range3 not enabled for address: 0x%x\n",
+				mem_data->addr);
+			return -EINVAL;
+		}
+	} else {
+		dev_err(amd_espi->dev, "Address 0x%x is invalid\n", mem_data->addr);
+		return -EINVAL;
+	}
+
+	return CB_SUCCESS;
+}
+
+int amd_espi_periph_mem_write(struct amd_espi *amd_espi, struct periph_mem_rw *mem_data)
+{
+	void __iomem *mmio_addr;
+
+	if (is_mmio_address_valid(amd_espi, mem_data) != CB_SUCCESS)
+		return -EINVAL;
+
+	mmio_addr = ioremap(mem_data->addr, 4);
+	if (!mmio_addr)
+		return -ENOMEM;
+
+	writel(mem_data->data, mmio_addr);
+	iounmap(mmio_addr);
+
+	return CB_SUCCESS;
+}
+
+int amd_espi_periph_mem_read(struct amd_espi *amd_espi, struct periph_mem_rw *mem_data)
+{
+	void __iomem *mmio_addr;
+
+	if (is_mmio_address_valid(amd_espi, mem_data) != CB_SUCCESS)
+		return -EINVAL;
+
+	mmio_addr = ioremap(mem_data->addr, 4);
+	if (!mmio_addr)
+		return -ENOMEM;
+
+	mem_data->data = readl(mmio_addr);
+	iounmap(mmio_addr);
+
+	return CB_SUCCESS;
+}
+
 static int amd_espi_get_master_cap(struct amd_espi *amd_espi, struct espi_master *master)
 {
 	u32 master_cap_reg, info;
drivers/spi/espi-amd-dev.c +53 -0

65 lines not on this page · raw message

drivers/spi/espi-amd.h +9 -0
index 4c67aa9f45a6..9e084bec8ff5 100644
@@ -178,6 +178,8 @@
 #define ESPI_DS_IODECODE_CONFIG      _IOWR(ESPI_MAGIC_NUMBER, 0x9, u32)
 #define ESPI_IO_WRITE                _IOWR(ESPI_MAGIC_NUMBER, 0xa, struct periph_io_rw)
 #define ESPI_IO_READ                 _IOWR(ESPI_MAGIC_NUMBER, 0xb, struct periph_io_rw)
+#define ESPI_MEM_WRITE               _IOWR(ESPI_MAGIC_NUMBER, 0xc, struct periph_mem_rw)
+#define ESPI_MEM_READ                _IOWR(ESPI_MAGIC_NUMBER, 0xd, struct periph_mem_rw)
 
 /*
  * enum amd_espi_versions - eSPI controller versions
@@ -382,6 +384,11 @@ struct periph_io_rw {
 	union io_data data;
 } __packed;
 
+struct periph_mem_rw {
+	u32 addr;
+	u32 data;
+};
+
 /* Function prototypes */
 int amd_espi_device_create(struct amd_espi *amd_espi, struct device *dev);
 void amd_espi_device_remove(struct amd_espi *amd_espi);
@@ -404,4 +411,6 @@ void amd_espi_set_io_mmio_decode_config(struct amd_espi *amd_espi,
 void amd_espi_disable_io_decode_range(struct amd_espi *amd_espi, u32 io_range);
 int amd_espi_periph_io_write(struct amd_espi *amd_espi, struct periph_io_rw *message_io);
 int amd_espi_periph_io_read(struct amd_espi *amd_espi, struct periph_io_rw *message_io);
+int amd_espi_periph_mem_write(struct amd_espi *amd_espi, struct periph_mem_rw *mem_data);
+int amd_espi_periph_mem_read(struct amd_espi *amd_espi, struct periph_mem_rw *mem_data);
 #endif
Signature · 1 line
2.34.1
maintainer
#2 lore raw

Re: [PATCH 01/10] spi: espi_amd: Add AMD eSPI controller driver support

In reply to Raju Rangoju

drivers/spi/Makefile obj-$(CONFIG_SPI_XTENSA_XTFPGA) += spi-xtensa-xtfpga.o · line 164

Quoted 7 lines · Raju Rangoju
drivers/spi/Makefile +2 -0
@@ -159,6 +159,8 @@ 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
+obj-$(CONFIG_SPI_AMD_ESPI)		+= espi-amd.o
+espi-amd-objs				:= espi-amd-core.o espi-amd-dev.o
 

Please keep these files sorted.

drivers/spi/espi-amd-core.c line 3

Quoted 4 lines
drivers/spi/espi-amd-core.c new file +3 -0
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * AMD eSPI controller driver

Please make the entire comment block a C++ one to make things look more consistent.

drivers/spi/espi-amd-core.c line 6

Quoted 3 lines
drivers/spi/espi-amd-core.c new file +3 -0
+ *
+ * Copyright (c) 2025, Advanced Micro Devices, Inc.
+ * All Rights Reserved.

Are you sure?

drivers/spi/espi-amd-core.c line 60

Quoted 10 lines
drivers/spi/espi-amd-core.c new file +10 -0
+static int amd_espi_check_error_status(struct amd_espi *amd_espi, u32 status)
+{
+	int ret = CB_SUCCESS;
+
+	if (!(status & ESPI_DNCMD_INT)) {
+		ret =  ESPI_DNCMD_INT;
+		dev_err(amd_espi->dev, "eSPI downstream command completion failure\n");
+	} else if (status & ESPI_BUS_ERR_INT) {
+		ret = ESPI_BUS_ERR_INT;
+		dev_err(amd_espi->dev, "%s\n", espi_error_codes[POS_BUS_TIMING]);

Can we really only have one error flagged at once? The whole espi_error_codes thing also seems like unneeded complexity and fagility, this function is the only place they're used and there's nothing ensuring that the defines for indexing into the array have anything to do with the strings in there.

drivers/spi/espi-amd-core.c line 240

Quoted 4 lines
drivers/spi/espi-amd-core.c new file +4 -0
+int amd_espi_set_iomode(struct amd_espi *amd_espi, u32 *slave_config, u32 *ctrlr_config,
+			u8 io_mode)
+{
+	struct espi_master *master = amd_espi->master;

There's a lot of outdated terminology like this in the driver - while sometimes it's unavoidable due to the register map it's better to use more modern terms like controller and device when it's just pure software things.

drivers/spi/espi-amd-core.c line 505

Quoted 7 lines
drivers/spi/espi-amd-core.c new file +7 -0
+	ret = amd_espi_get_config(amd_espi, ESPI_SLAVE_PERIPH_CFG, &slave_config);
+	if (ret != CB_SUCCESS)
+		return ret;
+
+	/* Check if PC is already enabled */
+	if (slave_config & ESPI_SLAVE_CHANNEL_ENABLE)
+		return CB_SUCCESS;

Is there any great reason to use these non-standard CB_ return codes?

drivers/spi/espi-amd-core.c line 779

Quoted 4 lines
drivers/spi/espi-amd-core.c new file +4 -0
+static int amd_espi_suspend(struct device *dev)
+{
+	return 0;
+}

Remove empty functions, if they can safely be empty the functions will be optional.

drivers/spi/espi-amd-core.c line 815

Quoted 5 lines
drivers/spi/espi-amd-core.c new file +5 -0
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -EOPNOTSUPP;
+
+	amd_espi->io_remap_addr = devm_ioremap_resource(dev, res);

dev_platform_get_and_ioremap_resource().

drivers/spi/espi-amd-core.c line 845

Quoted 6 lines
drivers/spi/espi-amd-core.c new file +6 -0
+	amd_espi_control_reg_init(amd_espi);
+	ret = amd_espi_init_slave(amd_espi);
+	if (ret != CB_SUCCESS)
+		goto espidev_free;
+
+	dev_info(dev, "AMD ESPI device initialization completed\n");

This is just noise, remove it.

drivers/spi/espi-amd-core.c line 851

Quoted 6 lines
drivers/spi/espi-amd-core.c new file +6 -0
+
+	return 0;
+
+espidev_free:
+	amd_espi_device_remove(amd_espi);
+	return ret;

This will return your non-standard error codes to generic code.

drivers/spi/espi-amd-core.c line 859

Quoted 6 lines
drivers/spi/espi-amd-core.c new file +6 -0
+static void amd_espi_remove(struct platform_device *pdev)
+{
+	struct amd_espi *amd_espi = platform_get_drvdata(pdev);
+
+	amd_espi_device_remove(amd_espi);
+}

There's no need for this wrapper function, there's exactly one place we can call remove from.

drivers/spi/espi-amd-dev.c line 36

Quoted 6 lines
drivers/spi/espi-amd-dev.c new file +6 -0
+static int amd_espi_open(struct inode *inode, struct file *filp)
+{
+	struct amd_espi *espi;
+	int status = -ENXIO;
+
+	guard(mutex)(&device_list_lock);

Whatever this userspace ABI is for it should be added in a separate patch, most likely it shouldn't be here at all and standard interfaces should be used. Currently it doesn't seem to actually do anything.

maintainer
#5 lore raw

Re: [PATCH 02/10] spi: espi_amd: Add eSPI set config IOCTL command

In reply to Raju Rangoju
Quoted 4 lines · Raju Rangoju

This patch introduces an IOCTL command to set the configuration of the eSPI controller and eSPI slave0. The configuration options include parameters like frequency, channel, and IO mode. The new IOCTL command allow users to dynamically configure the eSPI controller and slave.

Why? We have spidev...

maintainer
#13 lore raw

Re: [PATCH 08/10] spi: espi_amd: Add support for IO/MMIO configuration

In reply to Raju Rangoju
Quoted 4 lines · Raju Rangoju

Add support to configure the eSPI slave0 IO/MMIO address before initiating the peripheral channel IO/MMIO read and write operations. This patch introduces new IOCTLs to enable, disable and read IO/MMIO configurations.

This absolutely does not seem like something that should be exposed to userspace, if there is some reason for this to be runtime configured it needs to be clearly explained.

maintainer
#17 lore raw
In reply to Raju Rangoju
Quoted 6 lines · Raju Rangoju

The eSPI protocol serves as a communication interface between the main processor and peripheral devices in computer systems. It offers several advantages over the traditional LPC bus, including higher data transfer rates, increased scalability and improved system management capabilities. The eSPI protocol supports multiple channels, each serving a specific purpose in the communication process.

I see nothing in this series that registers a SPI controller with the SPI core. You need to use the standard frameworks the kernel offers to provide standard functionality.

#18 lore raw

On 3/17/2025 7:44 PM, Mark Brown wrote:

Hi Mark,

Thanks for reviewing the patch and apologies for delayed response.

Quoted 11 lines · Raju Rangoju
Quoted 6 lines

The eSPI protocol serves as a communication interface between the main processor and peripheral devices in computer systems. It offers several advantages over the traditional LPC bus, including higher data transfer rates, increased scalability and improved system management capabilities. The eSPI protocol supports multiple channels, each serving a specific purpose in the communication process.

I see nothing in this series that registers a SPI controller with the SPI core. You need to use the standard frameworks the kernel offers to provide standard functionality.

The AMD SPI controller hardware has only the chip select line enabled, which is connected to the EC slave in AMD EMB platforms. Currently, there is no support from the slave device to register as an SPI slave device with the SPI framework and provide SPI communication.

For this reason, the AMD eSPI driver is designed to handle device initialization itself and provide a character device file as an interface with user space for dynamic interaction and configurations.

#3 lore raw

Re: [PATCH 01/10] spi: espi_amd: Add AMD eSPI controller driver support

In reply to Mark Brown
Quoted 11 lines · Mark Brown

drivers/spi/Makefile obj-$(CONFIG_SPI_XTENSA_XTFPGA) += spi-xtensa-xtfpga.o · line 164

Quoted 7 lines · Raju Rangoju
drivers/spi/Makefile +2 -0
@@ -159,6 +159,8 @@ 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
+obj-$(CONFIG_SPI_AMD_ESPI)		+= espi-amd.o
+espi-amd-objs				:= espi-amd-core.o espi-amd-dev.o
 

Please keep these files sorted.

Sure Mark, we will address this in V2.

Quoted 8 lines

drivers/spi/espi-amd-core.c line 3

Quoted 5 lines
drivers/spi/espi-amd-core.c new file +3 -0
@@ -0,0 +1,883 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * AMD eSPI controller driver

Please make the entire comment block a C++ one to make things look more consistent.

Will address in V2.

Quoted 7 lines

drivers/spi/espi-amd-core.c line 6

Quoted 4 lines
drivers/spi/espi-amd-core.c new file +3 -0
+ *
+ * Copyright (c) 2025, Advanced Micro Devices, Inc.
+ * All Rights Reserved.

Are you sure?

Yes, we've used the same copyright text in other AMD drivers/files.

Quoted 8 lines
Quoted 8 lines
drivers/spi/espi-amd-core.c new file +8 -0
+static int amd_espi_check_error_status(struct amd_espi *amd_espi, u32 status)
+{
+	int ret = CB_SUCCESS;
+
+	if (!(status & ESPI_DNCMD_INT)) {
+		ret =  ESPI_DNCMD_INT;
+		dev_err(amd_espi->dev, "eSPI downstream command completion failure\n");
+	} else if (status & ESPI_BUS_ERR_INT) {
Quoted 8 lines

drivers/spi/espi-amd-core.c line 60

Quoted 2 lines
drivers/spi/espi-amd-core.c new file +2 -0
+		ret = ESPI_BUS_ERR_INT;
+		dev_err(amd_espi->dev, "%s\n", espi_error_codes[POS_BUS_TIMING]);

Can we really only have one error flagged at once? The whole espi_error_codes thing also seems like unneeded complexity and fagility, this function is the only place they're used and there's nothing ensuring that the defines for indexing into the array have anything to do with the strings in there.

The purpose of maintaining error codes is to inform the user space application about the reason for the command failure. The user space application can then use these error codes to determine the cause of the command failure.

Quoted 10 lines

drivers/spi/espi-amd-core.c line 240

Quoted 5 lines
drivers/spi/espi-amd-core.c new file +4 -0
+int amd_espi_set_iomode(struct amd_espi *amd_espi, u32 *slave_config, u32 *ctrlr_config,
+			u8 io_mode)
+{
+	struct espi_master *master = amd_espi->master;

There's a lot of outdated terminology like this in the driver - while sometimes it's unavoidable due to the register map it's better to use more modern terms like controller and device when it's just pure software things.

Will address in V2.

Quoted 10 lines

drivers/spi/espi-amd-core.c line 505

Quoted 8 lines
drivers/spi/espi-amd-core.c new file +7 -0
+	ret = amd_espi_get_config(amd_espi, ESPI_SLAVE_PERIPH_CFG, &slave_config);
+	if (ret != CB_SUCCESS)
+		return ret;
+
+	/* Check if PC is already enabled */
+	if (slave_config & ESPI_SLAVE_CHANNEL_ENABLE)
+		return CB_SUCCESS;

Is there any great reason to use these non-standard CB_ return codes?

Sure, will address in V2.

Quoted 9 lines

drivers/spi/espi-amd-core.c line 779

Quoted 5 lines
drivers/spi/espi-amd-core.c new file +4 -0
+static int amd_espi_suspend(struct device *dev)
+{
+	return 0;
+}

Remove empty functions, if they can safely be empty the functions will be optional.

Will address in V2.

Quoted 7 lines

drivers/spi/espi-amd-core.c line 815

Quoted 5 lines
drivers/spi/espi-amd-core.c new file +5 -0
+	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
+	if (!res)
+		return -EOPNOTSUPP;
+
+	amd_espi->io_remap_addr = devm_ioremap_resource(dev, res);

dev_platform_get_and_ioremap_resource().

Will address in V2.

Quoted 9 lines

drivers/spi/espi-amd-core.c line 845

Quoted 7 lines
drivers/spi/espi-amd-core.c new file +6 -0
+	amd_espi_control_reg_init(amd_espi);
+	ret = amd_espi_init_slave(amd_espi);
+	if (ret != CB_SUCCESS)
+		goto espidev_free;
+
+	dev_info(dev, "AMD ESPI device initialization completed\n");

This is just noise, remove it.

Sure

Quoted 9 lines

drivers/spi/espi-amd-core.c line 851

Quoted 7 lines
drivers/spi/espi-amd-core.c new file +6 -0
+
+	return 0;
+
+espidev_free:
+	amd_espi_device_remove(amd_espi);
+	return ret;

This will return your non-standard error codes to generic code.

Will address in V2

Quoted 11 lines

drivers/spi/espi-amd-core.c line 859

Quoted 7 lines
drivers/spi/espi-amd-core.c new file +6 -0
+static void amd_espi_remove(struct platform_device *pdev)
+{
+	struct amd_espi *amd_espi = platform_get_drvdata(pdev);
+
+	amd_espi_device_remove(amd_espi);
+}

There's no need for this wrapper function, there's exactly one place we can call remove from.

We have moved all device-related operations to a common file espi-amd-dev.c.

Therefore, the device remove callback is invoked from the driver remove function during un-initialization.

Quoted 10 lines

drivers/spi/espi-amd-dev.c line 36

Quoted 6 lines
drivers/spi/espi-amd-dev.c new file +6 -0
+static int amd_espi_open(struct inode *inode, struct file *filp)
+{
+	struct amd_espi *espi;
+	int status = -ENXIO;
+
+	guard(mutex)(&device_list_lock);

Whatever this userspace ABI is for it should be added in a separate patch, most likely it shouldn't be here at all and standard interfaces should be used. Currently it doesn't seem to actually do anything.

Since we are not registering the driver with spi framework and going with char dev file for user interface, all the dependent functions required including open, close and ioctls for char dev are placed in this file.

#6 lore raw

Re: [PATCH 02/10] spi: espi_amd: Add eSPI set config IOCTL command

In reply to Mark Brown
Quoted 8 lines · Mark Brown
Quoted 4 lines · Raju Rangoju

This patch introduces an IOCTL command to set the configuration of the eSPI controller and eSPI slave0. The configuration options include parameters like frequency, channel, and IO mode. The new IOCTL command allow users to dynamically configure the eSPI controller and slave.

Why? We have spidev...

There is no support from the slave device side to register as an SPI slave device with the SPI framework and provide SPI communication.

For this reason, the AMD eSPI driver can only handle device initialization itself and provide a character device file as an interface to user space.

#14 lore raw

Re: [PATCH 08/10] spi: espi_amd: Add support for IO/MMIO configuration

In reply to Mark Brown
Quoted 10 lines · Mark Brown
Quoted 4 lines · Raju Rangoju

Add support to configure the eSPI slave0 IO/MMIO address before initiating the peripheral channel IO/MMIO read and write operations. This patch introduces new IOCTLs to enable, disable and read IO/MMIO configurations.

This absolutely does not seem like something that should be exposed to userspace, if there is some reason for this to be runtime configured it needs to be clearly explained.

Hi Mark,

Thanks for reviewing this. In subsequent series we are planning to drop the IOCTLS that are exposed to userspace

maintainer
#19 lore raw
In reply to Rangoju, Raju
Quoted 6 lines · Raju
Quoted 3 lines · Mark Brown

I see nothing in this series that registers a SPI controller with the SPI core. You need to use the standard frameworks the kernel offers to provide standard functionality.

Quoted 8 lines

The AMD SPI controller hardware has only the chip select line enabled, which is connected to the EC slave in AMD EMB platforms. Currently, there is no support from the slave device to register as an SPI slave device with the SPI framework and provide SPI communication.

For this reason, the AMD eSPI driver is designed to handle device initialization itself and provide a character device file as an interface with user space for dynamic interaction and configurations.

If you want to ignore the SPI subsystem and just write a driver for your embedded controller then you should put the driver in the subsystem or subsystems for that embedded controller (possibly MFD if it does a bunch of things), not SPI. Even if there is no flexibility you may still want to have the controller side in the SPI subsystem in order to help with reuse with different controller/EC combinations but if you're going that way you need to use the SPI subsystem.

#20 lore raw
Quoted 16 lines · Mark Brown
Quoted 6 lines · Raju
Quoted 3 lines · Mark Brown

I see nothing in this series that registers a SPI controller with the SPI core. You need to use the standard frameworks the kernel offers to provide standard functionality.

Quoted 8 lines

The AMD SPI controller hardware has only the chip select line enabled, which is connected to the EC slave in AMD EMB platforms. Currently, there is no support from the slave device to register as an SPI slave device with the SPI framework and provide SPI communication.

For this reason, the AMD eSPI driver is designed to handle device initialization itself and provide a character device file as an interface with user space for dynamic interaction and configurations.

Quoted 7 lines

If you want to ignore the SPI subsystem and just write a driver for your embedded controller then you should put the driver in the subsystem or subsystems for that embedded controller (possibly MFD if it does a bunch of things), not SPI. Even if there is no flexibility you may still want to have the controller side in the SPI subsystem in order to help with reuse with different controller/EC combinations but if you're going that way you need to use the SPI subsystem.

Thanks for the suggestions Mark. We will rework on this.