Skip to contentSkip to conversation

Add more configuration and regmap support for spi-altera

PATCH In review Xu Yilun 3 participants 17 messages Jun 11, 2020 – Jun 15, 2020 SPI

lore
Share & tools
lore

Xu Yilun

author
#0 lore raw

cover Add more configuration and regmap support for spi-altera

This patchset adds platform_data for spi-altera, to enable more IP configurations, and creating specific spi client devices. It also adds regmap support, to enable the indirect access to this IP.

We have a PCIE based FPGA platform which integrates this IP to communicate with a BMC chip (Intel MAX10) over SPI. The IP is configured as 32bit data width. There is also an indirect access interface in FPGA for host to access the registers of this IP. This patchset enables this use case.

Matthew Gerlach (1):
spi: altera: fix size mismatch on 64 bit processors

Xu Yilun (5):
spi: altera: add 32bit data width transfer support.
spi: altera: add SPI core parameters support via platform data.
spi: altera: add platform data for slave information.
spi: altera: use regmap instead of direct mmio register access
spi: altera: move driver name string to header file

 drivers/spi/Kconfig        |   1 +
 drivers/spi/spi-altera.c   | 161 +++++++++++++++++++++++++++++++++++++--------
 include/linux/spi/altera.h |  37 +++++++++++
 3 files changed, 171 insertions(+), 28 deletions(-)
 create mode 100644 include/linux/spi/altera.h
Signature · 1 line
2.7.4
author
#1 lore raw

PATCH 1/6 spi: altera: add 32bit data width transfer support.

Add support for 32bit width data register, then it supports 32bit data width spi slave device and spi transfers.

---
 drivers/spi/spi-altera.c | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
drivers/spi/spi-altera.c +14 -0
index 41d71ba..d5fa0c5 100644
@@ -86,6 +86,13 @@ static void altera_spi_tx_word(struct altera_spi *hw)
 			txd = (hw->tx[hw->count * 2]
 				| (hw->tx[hw->count * 2 + 1] << 8));
 			break;
+		case 4:
+			txd = (hw->tx[hw->count * 4]
+				| (hw->tx[hw->count * 4 + 1] << 8)
+				| (hw->tx[hw->count * 4 + 2] << 16)
+				| (hw->tx[hw->count * 4 + 3] << 24));
+			break;
+
 		}
 	}
 
@@ -106,6 +113,13 @@ static void altera_spi_rx_word(struct altera_spi *hw)
 			hw->rx[hw->count * 2] = rxd;
 			hw->rx[hw->count * 2 + 1] = rxd >> 8;
 			break;
+		case 4:
+			hw->rx[hw->count * 4] = rxd;
+			hw->rx[hw->count * 4 + 1] = rxd >> 8;
+			hw->rx[hw->count * 4 + 2] = rxd >> 16;
+			hw->rx[hw->count * 4 + 3] = rxd >> 24;
+			break;
+
 		}
 	}
 
Signature · 1 line
2.7.4
author
#2 lore raw

PATCH 2/6 spi: altera: add SPI core parameters support via platform data.

In reply to Xu Yilun

This patch introduced SPI core parameters in platform data, it allows passing these SPI core parameters via platform data.

---
 drivers/spi/spi-altera.c   | 25 ++++++++++++++++++++++---
 include/linux/spi/altera.h | 24 ++++++++++++++++++++++++
 2 files changed, 46 insertions(+), 3 deletions(-)
 create mode 100644 include/linux/spi/altera.h
drivers/spi/spi-altera.c +22 -3
index d5fa0c5..e6e6708 100644
@@ -14,6 +14,7 @@
 #include <linux/errno.h>
 #include <linux/module.h>
 #include <linux/platform_device.h>
+#include <linux/spi/altera.h>
 #include <linux/spi/spi.h>
 #include <linux/io.h>
 #include <linux/of.h>
@@ -40,6 +41,8 @@
 #define ALTERA_SPI_CONTROL_IE_MSK	0x100
 #define ALTERA_SPI_CONTROL_SSO_MSK	0x400
 
+#define ALTERA_SPI_MAX_CS		32
+
 struct altera_spi {
 	void __iomem *base;
 	int irq;
@@ -182,6 +185,7 @@ static irqreturn_t altera_spi_irq(int irq, void *dev)
 
 static int altera_spi_probe(struct platform_device *pdev)
 {
+	struct altera_spi_platform_data *pdata = dev_get_platdata(&pdev->dev);
 	struct altera_spi *hw;
 	struct spi_master *master;
 	int err = -ENODEV;
@@ -192,9 +196,24 @@ static int altera_spi_probe(struct platform_device *pdev)
 
 	/* setup the master state. */
 	master->bus_num = pdev->id;
-	master->num_chipselect = 16;
-	master->mode_bits = SPI_CS_HIGH;
-	master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16);
+
+	if (pdata) {
+		if (pdata->num_chipselect > ALTERA_SPI_MAX_CS) {
+			dev_err(&pdev->dev,
+				"Invalid number of chipselect: %hu\n",
+				pdata->num_chipselect);
+			return -EINVAL;
+		}
+
+		master->num_chipselect = pdata->num_chipselect;
+		master->mode_bits = pdata->mode_bits;
+		master->bits_per_word_mask = pdata->bits_per_word_mask;
+	} else {
+		master->num_chipselect = 16;
+		master->mode_bits = SPI_CS_HIGH;
+		master->bits_per_word_mask = SPI_BPW_RANGE_MASK(1, 16);
+	}
+
 	master->dev.of_node = pdev->dev.of_node;
 	master->transfer_one = altera_spi_txrx;
 	master->set_cs = altera_spi_set_cs;
include/linux/spi/altera.h new file +24 -0
new file mode 100644
index 0000000..344a3fc
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/*
+ * Header File for Altera SPI Driver.
+ */
+#ifndef __LINUX_SPI_ALTERA_H
+#define __LINUX_SPI_ALTERA_H
+
+#include <linux/regmap.h>
+#include <linux/spi/spi.h>
+#include <linux/types.h>
+
+/**
+ * struct altera_spi_platform_data - Platform data of the Altera SPI driver
+ * @mode_bits:		Mode bits of SPI master.
+ * @num_chipselect:	Number of chipselects.
+ * @bits_per_word_mask:	bitmask of supported bits_per_word for transfers.
+ */
+struct altera_spi_platform_data {
+	u16				mode_bits;
+	u16				num_chipselect;
+	u32				bits_per_word_mask;
+};
+
+#endif /* __LINUX_SPI_ALTERA_H */
Signature · 1 line
2.7.4
author
#3 lore raw

PATCH 3/6 spi: altera: add platform data for slave information.

In reply to Xu Yilun

This patch introduces platform data for slave information, it allows spi-altera to add new spi devices once master registration is done.

---
 drivers/spi/spi-altera.c   | 11 +++++++++++
 include/linux/spi/altera.h |  5 +++++
 2 files changed, 16 insertions(+)
drivers/spi/spi-altera.c +11 -0
index e6e6708..aa9d1a2 100644
@@ -189,6 +189,7 @@ static int altera_spi_probe(struct platform_device *pdev)
 	struct altera_spi *hw;
 	struct spi_master *master;
 	int err = -ENODEV;
+	u16 i;
 
 	master = spi_alloc_master(&pdev->dev, sizeof(struct altera_spi));
 	if (!master)
@@ -244,6 +245,16 @@ static int altera_spi_probe(struct platform_device *pdev)
 	err = devm_spi_register_master(&pdev->dev, master);
 	if (err)
 		goto exit;
+
+	if (pdata) {
+		for (i = 0; i < pdata->num_devices; i++) {
+			if (!spi_new_device(master, pdata->devices + i))
+				dev_warn(&pdev->dev,
+					 "unable to create SPI device: %s\n",
+					 pdata->devices[i].modalias);
+		}
+	}
+
 	dev_info(&pdev->dev, "base %p, irq %d\n", hw->base, hw->irq);
 
 	return 0;
include/linux/spi/altera.h +5 -0
index 344a3fc..2d42641 100644
@@ -14,11 +14,16 @@
  * @mode_bits:		Mode bits of SPI master.
  * @num_chipselect:	Number of chipselects.
  * @bits_per_word_mask:	bitmask of supported bits_per_word for transfers.
+ * @num_devices:	Number of devices that shall be added when the driver
+ *			is probed.
+ * @devices:		The devices to add.
  */
 struct altera_spi_platform_data {
 	u16				mode_bits;
 	u16				num_chipselect;
 	u32				bits_per_word_mask;
+	u16				num_devices;
+	struct spi_board_info		*devices;
 };
 
 #endif /* __LINUX_SPI_ALTERA_H */
Signature · 1 line
2.7.4
author
#4 lore raw

PATCH 4/6 spi: altera: use regmap instead of direct mmio register access

In reply to Xu Yilun

This patch adds support for regmap. It allows this driver to be compatible if low layer register access method is changed in some cases.

---
 drivers/spi/Kconfig        |   1 +
 drivers/spi/spi-altera.c   | 103 ++++++++++++++++++++++++++++++++++++---------
 include/linux/spi/altera.h |   6 +++
 3 files changed, 90 insertions(+), 20 deletions(-)
drivers/spi/Kconfig +1 -0
index 8f1f8fc..6d79fc7 100644
@@ -59,6 +59,7 @@ comment "SPI Master Controller Drivers"
 
 config SPI_ALTERA
 	tristate "Altera SPI Controller"
+	select REGMAP_MMIO
 	help
 	  This is the driver for the Altera SPI Controller.
 
drivers/spi/spi-altera.c +83 -20

186 lines not on this page · raw message

include/linux/spi/altera.h +6 -0
index 2d42641..164ab7b 100644
@@ -17,6 +17,10 @@
  * @num_devices:	Number of devices that shall be added when the driver
  *			is probed.
  * @devices:		The devices to add.
+ * @use_parent_regmap:	If true, device uses parent regmap to access its
+ *			registers. Otherwise try map platform mmio resources.
+ * @regoff:		Offset of the device register base in parent regmap.
+ *			This field is ignored when use_parent_regmap == false.
  */
 struct altera_spi_platform_data {
 	u16				mode_bits;
@@ -24,6 +28,8 @@ struct altera_spi_platform_data {
 	u32				bits_per_word_mask;
 	u16				num_devices;
 	struct spi_board_info		*devices;
+	bool				use_parent_regmap;
+	u32				regoff;
 };
 
 #endif /* __LINUX_SPI_ALTERA_H */
Signature · 1 line
2.7.4
maintainer
#5 lore raw

Re: [PATCH 4/6] spi: altera: use regmap instead of direct mmio register access

drivers/spi/spi-altera.c static int altera_spi_probe(struct platform_device *pdev) · line 276

Quoted 7 lines · Xu Yilun
drivers/spi/spi-altera.c +7 -0
+	if (pdata && pdata->use_parent_regmap) {
+		hw->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+		if (!hw->regmap) {
+			dev_err(&pdev->dev, "get regmap failed\n");
+			goto exit;
+		}
+		hw->base = pdata->regoff;

This seems very confused - there's some abstraction problem here. This looks like the driver should know about whatever is causing it to use the parent regmap, it shouldn't just be "use the parent regmap" directly since that is too much of an implementation detail. This new feature also seems like a separate change which should be in a separate patch, the changelog only talked about converting to use regmap which I'd have expected to be a straight 1:1 replacement of non-regmap stuff with regmap stuff.

author
#6 lore raw

Re: [PATCH 4/6] spi: altera: use regmap instead of direct mmio register access

Quoted 14 lines · Mark Brown

drivers/spi/spi-altera.c static int altera_spi_probe(struct platform_device *pdev) · line 276

Quoted 7 lines · Xu Yilun
drivers/spi/spi-altera.c +7 -0
+	if (pdata && pdata->use_parent_regmap) {
+		hw->regmap = dev_get_regmap(pdev->dev.parent, NULL);
+		if (!hw->regmap) {
+			dev_err(&pdev->dev, "get regmap failed\n");
+			goto exit;
+		}
+		hw->base = pdata->regoff;

This seems very confused - there's some abstraction problem here. This looks like the driver should know about whatever is causing it to use the parent regmap, it shouldn't just be "use the parent regmap" directly since that is too much of an implementation detail. This new feature

Our usecase is that, we have the PCIE card which integrates this spi-altera soft IP. So It seems reasonable we reuse this driver. But the IP registers are not directly accessed by MMIO. It is accessed via an indirect access interfaces, SW need to operate on the indirect access interfaces to RW the spi-altera registers. like the following:

+------+   +------------+   +------------+
| PCIE |---| indirect   |---| spi-altera |
+------+   | access     |   +------------+
           | interfaces |
           +------------+
           | SPI params |
           +------------+

So we think of creating regmap to abstract the actually register accessing detail. The parent device driver creates the regmap of indirect access, and it creates the spi-altera platform device as child. Spi-altera driver could just get the regmap from parent, don't have to care about the indirect access detail.

It seems your concern is how to gracefully let spi-altera driver get the regmap. or not using it. Since our platform doesn't enable device tree support, seems the only way to talk to platform device is the platform_data.

Do you have any suggestion on that?

I think the driver may need to figure out the role of the device in system, whether it is a subdev of other device (like MFD? Many mfd subdev driver will get parent regmap by default), or it is an independent mmio device. But I'm not sure how to do it in right way.

Quoted 4 lines

also seems like a separate change which should be in a separate patch, the changelog only talked about converting to use regmap which I'd have expected to be a straight 1:1 replacement of non-regmap stuff with regmap stuff.

Understood. I should make a patch to do 1:1 replacement of regmap first, then a second patch to handle the parent regmap.

maintainer
#7 lore raw

Re: [PATCH 4/6] spi: altera: use regmap instead of direct mmio register access

Quoted 5 lines · Xu Yilun

So we think of creating regmap to abstract the actually register accessing detail. The parent device driver creates the regmap of indirect access, and it creates the spi-altera platform device as child. Spi-altera driver could just get the regmap from parent, don't have to care about the indirect access detail.

To be clear there's absolutely no problem with the end result, my concern is the way that we're getting there.

Quoted 4 lines

It seems your concern is how to gracefully let spi-altera driver get the regmap. or not using it. Since our platform doesn't enable device tree support, seems the only way to talk to platform device is the platform_data.

No, the problem is with how that platform data is structured. Based on what you're saying I'd suggest adding another device ID for this - you can use the id_table field in struct platform_driver to have more than one ID like you can have more than one ACPI ID or OF compatible. That would mirror how this would be handled if things were enumerated through firmware.

Quoted 4 lines

I think the driver may need to figure out the role of the device in system, whether it is a subdev of other device (like MFD? Many mfd subdev driver will get parent regmap by default), or it is an independent mmio device. But I'm not sure how to do it in right way.

Yes, it sounds like this card is a MFD.

author
#8 lore raw

Re: [PATCH 4/6] spi: altera: use regmap instead of direct mmio register access

Quoted 16 lines · Mark Brown
Quoted 5 lines · Xu Yilun

So we think of creating regmap to abstract the actually register accessing detail. The parent device driver creates the regmap of indirect access, and it creates the spi-altera platform device as child. Spi-altera driver could just get the regmap from parent, don't have to care about the indirect access detail.

To be clear there's absolutely no problem with the end result, my concern is the way that we're getting there.

Quoted 4 lines

It seems your concern is how to gracefully let spi-altera driver get the regmap. or not using it. Since our platform doesn't enable device tree support, seems the only way to talk to platform device is the platform_data.

Quoted 6 lines

No, the problem is with how that platform data is structured. Based on what you're saying I'd suggest adding another device ID for this - you can use the id_table field in struct platform_driver to have more than one ID like you can have more than one ACPI ID or OF compatible. That would mirror how this would be handled if things were enumerated through firmware.

Thanks for your quick response. It's very clear and makes sense. I'll change it.

Quoted 7 lines
Quoted 5 lines

I think the driver may need to figure out the role of the device in system, whether it is a subdev of other device (like MFD? Many mfd subdev driver will get parent regmap by default), or it is an independent mmio device. But I'm not sure how to do it in right way.

Yes, it sounds like this card is a MFD.

author
#9 lore raw

PATCH 5/6 spi: altera: move driver name string to header file

In reply to Xu Yilun

This allows other driver to reuse the name string for spi-altera platform device creation.

---
 drivers/spi/spi-altera.c   | 6 ++----
 include/linux/spi/altera.h | 2 ++
 2 files changed, 4 insertions(+), 4 deletions(-)
drivers/spi/spi-altera.c +2 -4
index 8d47c57..2c12c7a 100644
@@ -19,8 +19,6 @@
 #include <linux/io.h>
 #include <linux/of.h>
 
-#define DRV_NAME "spi_altera"
-
 #define ALTERA_SPI_RXDATA	0
 #define ALTERA_SPI_TXDATA	4
 #define ALTERA_SPI_STATUS	8
@@ -338,7 +336,7 @@ MODULE_DEVICE_TABLE(of, altera_spi_match);
 static struct platform_driver altera_spi_driver = {
 	.probe = altera_spi_probe,
 	.driver = {
-		.name = DRV_NAME,
+		.name = ALTERA_SPI_DRV_NAME,
 		.pm = NULL,
 		.of_match_table = of_match_ptr(altera_spi_match),
 	},
@@ -348,4 +346,4 @@ module_platform_driver(altera_spi_driver);
 MODULE_DESCRIPTION("Altera SPI driver");
 MODULE_AUTHOR("Thomas Chou <[email protected]>");
 MODULE_LICENSE("GPL");
-MODULE_ALIAS("platform:" DRV_NAME);
+MODULE_ALIAS("platform:" ALTERA_SPI_DRV_NAME);
include/linux/spi/altera.h +2 -0
index 164ab7b..6539371 100644
@@ -9,6 +9,8 @@
 #include <linux/spi/spi.h>
 #include <linux/types.h>
 
+#define ALTERA_SPI_DRV_NAME	"spi-altera"
+
 /**
  * struct altera_spi_platform_data - Platform data of the Altera SPI driver
  * @mode_bits:		Mode bits of SPI master.
Signature · 1 line
2.7.4
maintainer
#10 lore raw

Re: [PATCH 5/6] spi: altera: move driver name string to header file

Quoted 2 lines · Xu Yilun

This allows other driver to reuse the name string for spi-altera platform device creation.

This is a very unusual thing to do, normally we just have the users use the strong directly. It feels like if we are going to change this idiom we should do so globally but that seems like far more trouble thanit's worth.

author
#11 lore raw

Re: [PATCH 5/6] spi: altera: move driver name string to header file

Quoted 8 lines · Mark Brown
Quoted 2 lines · Xu Yilun

This allows other driver to reuse the name string for spi-altera platform device creation.

This is a very unusual thing to do, normally we just have the users use the strong directly. It feels like if we are going to change this idiom we should do so globally but that seems like far more trouble thanit's worth.

Sure, I'll discard this patch and just use string for spi-altera device creation.

Thanks,
Yilun.

author
#12 lore raw

PATCH 6/6 spi: altera: fix size mismatch on 64 bit processors

In reply to Xu Yilun

From: Matthew Gerlach <[email protected]>

The spi-altera driver was originally written with a 32 bit processor, where sizeof(unsigned long) is 4. On a 64 bit processor sizeof(unsigned long) is 8. Change the structure member to u32 to match the actual size of the control register.

---
 drivers/spi/spi-altera.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-altera.c +1 -1
index 2c12c7a..468fbd5 100644
@@ -46,7 +46,7 @@ struct altera_spi {
 	int len;
 	int count;
 	int bytes_per_word;
-	unsigned long imr;
+	u32 imr;
 
 	/* data buffers */
 	const unsigned char *tx;
Signature · 1 line
2.7.4
maintainer
#13 lore raw

Re: [PATCH 6/6] spi: altera: fix size mismatch on 64 bit processors

Quoted 10 lines · Xu Yilun

From: Matthew Gerlach <[email protected]>

The spi-altera driver was originally written with a 32 bit processor, where sizeof(unsigned long) is 4. On a 64 bit processor sizeof(unsigned long) is 8. Change the structure member to u32 to match the actual size of the control register.

---

You've not provided a Signed-off-by for this, I can't do anything with it. For details on what Signed-off-by means and why it's important please see Documentation/process/submitting-patches.rst.

author
#14 lore raw

Re: [PATCH 6/6] spi: altera: fix size mismatch on 64 bit processors

Quoted 9 lines · Mark Brown
Quoted 8 lines · Xu Yilun

From: Matthew Gerlach <[email protected]>

The spi-altera driver was originally written with a 32 bit processor, where sizeof(unsigned long) is 4. On a 64 bit processor sizeof(unsigned long) is 8. Change the structure member to u32 to match the actual size of the control register.

Quoted 6 lines
Quoted 2 lines

---

You've not provided a Signed-off-by for this, I can't do anything with it. For details on what Signed-off-by means and why it's important please see Documentation/process/submitting-patches.rst.

Thanks for your explanation. I'll add my Signed-off-by.

Reviewed-by on the v1 series
#15 lore raw
In reply to Xu Yilun

This patchset looks good to me.

Thanks,

Tom

Quoted 25 lines · Xu Yilun

This patchset adds platform_data for spi-altera, to enable more IP configurations, and creating specific spi client devices. It also adds regmap support, to enable the indirect access to this IP.

We have a PCIE based FPGA platform which integrates this IP to communicate with a BMC chip (Intel MAX10) over SPI. The IP is configured as 32bit data width. There is also an indirect access interface in FPGA for host to access the registers of this IP. This patchset enables this use case.

Matthew Gerlach (1):
spi: altera: fix size mismatch on 64 bit processors

Xu Yilun (5):
spi: altera: add 32bit data width transfer support.
spi: altera: add SPI core parameters support via platform data.
spi: altera: add platform data for slave information.
spi: altera: use regmap instead of direct mmio register access
spi: altera: move driver name string to header file

 drivers/spi/Kconfig        |   1 +
 drivers/spi/spi-altera.c   | 161 +++++++++++++++++++++++++++++++++++++--------
 include/linux/spi/altera.h |  37 +++++++++++
 3 files changed, 171 insertions(+), 28 deletions(-)
 create mode 100644 include/linux/spi/altera.h
maintainer
#16 lore raw
In reply to Xu Yilun
Quoted 10 lines · Xu Yilun

This patchset adds platform_data for spi-altera, to enable more IP configurations, and creating specific spi client devices. It also adds regmap support, to enable the indirect access to this IP.

We have a PCIE based FPGA platform which integrates this IP to communicate with a BMC chip (Intel MAX10) over SPI. The IP is configured as 32bit data width. There is also an indirect access interface in FPGA for host to access the registers of this IP. This patchset enables this use case.

[...]

Applied to

https://git.kernel.org/pub/scm/linux/kernel/git/broonie/spi.git for-next

Thanks!

[1/3] spi: altera: add 32bit data width transfer support.

      commit: 3011d314751535782508a86bbd8de415ea99909f
[2/3] spi: altera: add SPI core parameters support via platform data.
      commit: 8e04187c1bc7953f6dfad3400c58b1b0b0ad767b
[3/3] spi: altera: add platform data for slave information.
      commit: 1fccd182a4694a848f2d6f3b1820d6fc71d9c99d

All being well this means that it will be integrated into the linux-next tree (usually sometime in the next 24 hours) and sent to Linus during the next merge window (or sooner if it is a bug fix), however if problems are discovered then the patch may be dropped or reverted.

You may get further e-mails resulting from automated or manual testing and review of the tree, please engage with people reporting problems and send followup patches addressing any issues that are reported if needed.

If any updates are required or you are submitting further changes they should be sent as incremental updates against current git, existing patches will not be replaced.

Please add any relevant lists and maintainers to the CCs when replying to this mail.

Thanks,
Mark