Skip to contentSkip to conversation

spi: Add FSI-attached SPI controller driver

PATCH Superseded Eddie James 4 participants 17 messages Jan 29, 2020 – Feb 10, 2020

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Eddie James

author maintainer
#0 lore raw

PATCH 1/1 spi: Add FSI-attached SPI controller driver

There exists a set of SPI controllers on some POWER processors that may be accessed through the FSI bus. Add a driver to traverse the FSI CFAM engine that can access and drive the SPI controllers. This driver would typically be used by a baseboard management controller (BMC).

---
 MAINTAINERS           |   6 +
 drivers/spi/Kconfig   |   7 +
 drivers/spi/Makefile  |   1 +
 drivers/spi/spi-fsi.c | 547 ++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 561 insertions(+)
 create mode 100644 drivers/spi/spi-fsi.c
MAINTAINERS +6 -0
index fc1e24c..56a50c3 100644
@@ -6862,6 +6862,12 @@ S:	Maintained
 F:	drivers/i2c/busses/i2c-fsi.c
 F:	Documentation/devicetree/bindings/i2c/i2c-fsi.txt
 
+FSI-ATTACHED SPI DRIVER
+M:	Eddie James <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/spi/spi-fsi.c
+
 FSNOTIFY: FILESYSTEM NOTIFICATION INFRASTRUCTURE
 M:	Jan Kara <[email protected]>
 R:	Amir Goldstein <[email protected]>
drivers/spi/Kconfig +7 -0
index d6ed0c3..c6fc9f1 100644
@@ -264,6 +264,13 @@ config SPI_FALCON
 	  has only been tested with m25p80 type chips. The hardware has no
 	  support for other types of SPI peripherals.
 
+config SPI_FSI
+	tristate "FSI SPI driver"
+	depends on FSI
+	help
+	  This enables support for the driver for FSI bus attached SPI
+	  controllers.
+
 config SPI_FSL_LPSPI
 	tristate "Freescale i.MX LPSPI controller"
 	depends on ARCH_MXC || COMPILE_TEST
drivers/spi/Makefile +1 -0
index 9b65ec5..d4ea24e 100644
@@ -40,6 +40,7 @@ spi-dw-midpci-objs			:= spi-dw-pci.o spi-dw-mid.o
 obj-$(CONFIG_SPI_EFM32)			+= spi-efm32.o
 obj-$(CONFIG_SPI_EP93XX)		+= spi-ep93xx.o
 obj-$(CONFIG_SPI_FALCON)		+= spi-falcon.o
+obj-$(CONFIG_SPI_FSI)			+= spi-fsi.o
 obj-$(CONFIG_SPI_FSL_CPM)		+= spi-fsl-cpm.o
 obj-$(CONFIG_SPI_FSL_DSPI)		+= spi-fsl-dspi.o
 obj-$(CONFIG_SPI_FSL_LIB)		+= spi-fsl-lib.o
drivers/spi/spi-fsi.c new file +547 -0

547 lines not on this page · raw message

Signature · 1 line
1.8.3.1
maintainer
#1 lore raw

On Wed, Jan 29, 2020 at 02:08:24PM -0600, Eddie James wrote:

Overall this looks good, some comments below but they're all fairly minor.

drivers/spi/spi-fsi.c line 4

Quoted 6 lines

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

drivers/spi/spi-fsi.c new file +4 -0
@@ -0,0 +1,547 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) IBM Corporation 2020
+ */

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

drivers/spi/spi-fsi.c line 165

Quoted 5 lines
drivers/spi/spi-fsi.c new file +5 -0
+
+static int fsi_spi_data_in(u64 in, u8 *rx, int len)
+{
+	int i;
+	int num_bytes = len > 8 ? 8 : len;

Please write normal conditional statements to improve legibility, the ternery operator isn't really needed here.

drivers/spi/spi-fsi.c line 190

Quoted 5 lines
drivers/spi/spi-fsi.c new file +5 -0
+static int fsi_spi_reset(struct fsi_spi *ctx)
+{
+	int rc;
+
+	dev_info(ctx->dev, "Resetting SPI controller.\n");

This should be lowered to dev_dbg() at most, it's not really adding anything otherwise.

drivers/spi/spi-fsi.c line 526

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

Remove empty functions, if they can safely be empty then it should be possible to omit them.

drivers/spi/spi-fsi.c line 531

Quoted 4 lines
drivers/spi/spi-fsi.c new file +4 -0
+static const struct fsi_device_id fsi_spi_ids[] = {
+	{ FSI_ENGID_SPI, FSI_VERSION_ANY },
+	{ }
+};

This needs a MODULE_DEVICE_TABLE annotation.

author maintainer
#2 lore raw
Quoted 4 lines · Mark Brown

On Wed, Jan 29, 2020 at 02:08:24PM -0600, Eddie James wrote:

Overall this looks good, some comments below but they're all fairly minor.

Thanks for the quick review! I'll fix what you've suggested below.

Thanks,

Eddie

Quoted 38 lines

drivers/spi/spi-fsi.c line 4

Quoted 7 lines

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

drivers/spi/spi-fsi.c new file +4 -0
@@ -0,0 +1,547 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) IBM Corporation 2020
+ */

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

drivers/spi/spi-fsi.c line 165

Quoted 5 lines
drivers/spi/spi-fsi.c new file +5 -0
+
+static int fsi_spi_data_in(u64 in, u8 *rx, int len)
+{
+	int i;
+	int num_bytes = len > 8 ? 8 : len;

Please write normal conditional statements to improve legibility, the ternery operator isn't really needed here.

drivers/spi/spi-fsi.c line 190

Quoted 5 lines
drivers/spi/spi-fsi.c new file +5 -0
+static int fsi_spi_reset(struct fsi_spi *ctx)
+{
+	int rc;
+
+	dev_info(ctx->dev, "Resetting SPI controller.\n");

This should be lowered to dev_dbg() at most, it's not really adding anything otherwise.

drivers/spi/spi-fsi.c line 526

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

Remove empty functions, if they can safely be empty then it should be possible to omit them.

drivers/spi/spi-fsi.c line 531

Quoted 4 lines
drivers/spi/spi-fsi.c new file +4 -0
+static const struct fsi_device_id fsi_spi_ids[] = {
+	{ FSI_ENGID_SPI, FSI_VERSION_ANY },
+	{ }
+};

This needs a MODULE_DEVICE_TABLE annotation.

#3 lore raw
In reply to Eddie James
Quoted 5 lines · Eddie James

There exists a set of SPI controllers on some POWER processors that may be accessed through the FSI bus. Add a driver to traverse the FSI CFAM engine that can access and drive the SPI controllers. This driver would typically be used by a baseboard management controller (BMC).

...

Quoted 4 lines
drivers/spi/spi-fsi.c new file +3 -0
+#include <linux/bitfield.h>
+#include <linux/bits.h>
 
+#include <linux/of.h>

...

drivers/spi/spi-fsi.c line 69

Quoted 2 lines
drivers/spi/spi-fsi.c new file +2 -0
+struct fsi_spi {
+       struct device *dev;

Isn't fsl->dev the same?
Perhaps kernel doc to explain the difference?

Quoted 4 lines
drivers/spi/spi-fsi.c new file +3 -0
+       struct fsi_device *fsi;
 
+       u32 base;
+};

...

drivers/spi/spi-fsi.c line 105

Quoted 7 lines
drivers/spi/spi-fsi.c new file +6 -0
+static int fsi_spi_read_reg(struct fsi_spi *ctx, u32 offset, u64 *value)
+{
+       int rc;
+       __be32 cmd_be;
+       __be32 data_be;
 
+       *value = 0ULL;

Usually the pattern is don't pollute output on error condition. Any reason why you zeroing output beforehand?

Quoted 7 lines
drivers/spi/spi-fsi.c new file +6 -0
+       cmd_be = cpu_to_be32(offset + ctx->base);
+       rc = fsi_device_write(ctx->fsi, FSI2SPI_CMD, &cmd_be, sizeof(cmd_be));
+       if (rc)
+               return rc;
 
+       return 0;
+}

...

Quoted 1 line

+ data_be = cpu_to_be32((value >> 32) & 0xFFFFFFFF);

Redundant & 0xff... part.

Quoted 1 line

+ data_be = cpu_to_be32(value & 0xFFFFFFFF);

Ditto.

You may use upper_32_bits() / lower_32_bits() instead.

...

drivers/spi/spi-fsi.c line 165

Quoted 5 lines
drivers/spi/spi-fsi.c new file +4 -0
+static int fsi_spi_data_in(u64 in, u8 *rx, int len)
+{
+       int i;
 
+       int num_bytes = len > 8 ? 8 : len;

min(len, 8);

drivers/spi/spi-fsi.c line 168

Quoted 2 lines
drivers/spi/spi-fsi.c new file +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

drivers/spi/spi-fsi.c line 174

Quoted 5 lines
drivers/spi/spi-fsi.c new file +4 -0
+       return num_bytes;
+}
 
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Quoted 1 line

+}

...

Quoted 1 line

+ dev_info(ctx->dev, "Resetting SPI controller.\n");

info?! Why?

drivers/spi/spi-fsi.c line 199

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+       rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
+                              SPI_FSI_CLOCK_CFG_RESET2);
+       return rc;

return fsi_spi_write_reg();

...

Quoted 1 line

+ return ((64 - seq->bit) / 8) - 2;

Too many parentheses.

...

Quoted 8 lines
drivers/spi/spi-fsi.c new file +7 -0
+static int fsi_spi_sequence_transfer(struct fsi_spi *ctx,
+                                    struct fsi_spi_sequence *seq,
+                                    struct spi_transfer *transfer)
+{
 
+       int loops = 1;
+       int idx = 0;
+       int rc;

drivers/spi/spi-fsi.c line 232

Quoted 10 lines
drivers/spi/spi-fsi.c new file +9 -0
+       u8 len;
+       u8 rem = 0;
 
+       if (transfer->len > 8) {
+               loops = transfer->len / 8;
+               rem = transfer->len - (loops * 8);
+               len = 8;
+       } else {
+               len = transfer->len;
+       }

len = min(transfer->len, 8);

loops = transfer->len / len;
rem = transfer->len % len;

(I think compiler is clever enough to find out that the division can be avoided)

...and drop assignments in definition block.

I didn't look carefully in the implementation, but I believe there is still room for improvement / optimization.

drivers/spi/spi-fsi.c line 257

Quoted 7 lines
drivers/spi/spi-fsi.c new file +5 -0
+       if (loops > 1) {
 
+               rc = fsi_spi_write_reg(ctx, SPI_FSI_COUNTER_CFG,
+                                      SPI_FSI_COUNTER_CFG_LOOPS(loops - 1));
+               if (rc) {
 
+                       /* Ensure error returns < 0 in this case. */

I didn't get why this case is special? Why not to be consistent with return value?

drivers/spi/spi-fsi.c line 264

Quoted 7 lines
drivers/spi/spi-fsi.c new file +6 -0
+                       if (rc > 0)
+                               rc = -rc;
+
+                       return rc;
+               }
 
+               return loops;

If we return here the amount of loops...

drivers/spi/spi-fsi.c line 96

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+       }
+
+       return 0;

...why here is 0?

I think more consistency is required.

Quoted 1 line

+}

...

drivers/spi/spi-fsi.c line 272

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+static int fsi_spi_transfer_data(struct fsi_spi *ctx,
+                                struct spi_transfer *transfer)
+{

Can you refactor to tx and rx parts?

Quoted 2 lines
(quoted diff) +2 -0
+       return 0;
+}

...

Quoted 4 lines
drivers/spi/spi-fsi.c new file +4 -0
+       do {
+               rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS, &status);
+               if (rc)
+                       return rc;

drivers/spi/spi-fsi.c line 362

Quoted 10 lines
drivers/spi/spi-fsi.c new file +9 -0
+
+               if (status & (SPI_FSI_STATUS_ANY_ERROR |
+                             SPI_FSI_STATUS_TDR_FULL |
+                             SPI_FSI_STATUS_RDR_FULL)) {
+                       rc = fsi_spi_reset(ctx);
+                       if (rc)
+                               return rc;
+
 
+                       continue;

I forgot if this to be infinite loop or if it's going to check previous seq_state value. In any case this code is a bit fishy. Needs comments / refactoring.

Quoted 4 lines
drivers/spi/spi-fsi.c new file +4 -0
+               }
+
+               seq_state = status & SPI_FSI_STATUS_SEQ_STATE;
+       } while (seq_state && (seq_state != SPI_FSI_STATUS_SEQ_STATE_IDLE));

...

drivers/spi/spi-fsi.c line 378

Quoted 8 lines
drivers/spi/spi-fsi.c new file +7 -0
+       if ((clock_cfg & (SPI_FSI_CLOCK_CFG_MM_ENABLE |
+                         SPI_FSI_CLOCK_CFG_ECC_DISABLE |
+                         SPI_FSI_CLOCK_CFG_MODE |
+                         SPI_FSI_CLOCK_CFG_SCK_RECV_DEL |
+                         SPI_FSI_CLOCK_CFG_SCK_DIV)) != wanted_clock_cfg)
 
+               rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
+                                      wanted_clock_cfg);

Missed {} ?

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+
+       return rc;
+}

...

Quoted 1 line

+ rc = fsi_slave_read(fsi->slave, 0x2860, &root_ctrl_8,

What is this magic for?

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+                           sizeof(root_ctrl_8));
+       if (rc)
+               return rc;

...

drivers/spi/spi-fsi.c line 526

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

Why do you need this?

...

drivers/spi/spi-fsi.c line 537

Quoted 6 lines
drivers/spi/spi-fsi.c new file +5 -0
+static struct fsi_driver fsi_spi_driver = {
+       .id_table = fsi_spi_ids,
+       .drv = {
+               .name = "spi-fsi",
 
+               .bus = &fsi_bus_type,

Why is it not in the module_fsi_driver() macro?

Quoted 6 lines
drivers/spi/spi-fsi.c new file +6 -0
+               .probe = fsi_spi_probe,
+               .remove = fsi_spi_remove,
+       },
+};
+
+module_fsi_driver(fsi_spi_driver);
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw
Quoted 12 lines · Andy Shevchenko
Quoted 4 lines · Eddie James

There exists a set of SPI controllers on some POWER processors that may be accessed through the FSI bus. Add a driver to traverse the FSI CFAM engine that can access and drive the SPI controllers. This driver would typically be used by a baseboard management controller (BMC).

...

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+#include <linux/bitfield.h>
+#include <linux/bits.h>
+#include <linux/of.h>

...

Quoted 4 lines

drivers/spi/spi-fsi.c line 69

Quoted 2 lines
drivers/spi/spi-fsi.c new file +2 -0
+struct fsi_spi {
+       struct device *dev;

Isn't fsl->dev the same?
Perhaps kernel doc to explain the difference?

No, it's not the same, as dev here is the SPI controller. I'll add a comment.

Quoted 6 lines
Quoted 4 lines
drivers/spi/spi-fsi.c new file +3 -0
+       struct fsi_device *fsi;
+       u32 base;
+};

...

Quoted 8 lines

drivers/spi/spi-fsi.c line 105

Quoted 6 lines
drivers/spi/spi-fsi.c new file +6 -0
+static int fsi_spi_read_reg(struct fsi_spi *ctx, u32 offset, u64 *value)
+{
+       int rc;
+       __be32 cmd_be;
+       __be32 data_be;
+       *value = 0ULL;

Usually the pattern is don't pollute output on error condition. Any reason why you zeroing output beforehand?

Well otherwise I have to store another 64 bit int and do another assignment at the end. This is an internal function and all the users below know what's happening.

Quoted 9 lines
Quoted 7 lines
drivers/spi/spi-fsi.c new file +6 -0
+       cmd_be = cpu_to_be32(offset + ctx->base);
+       rc = fsi_device_write(ctx->fsi, FSI2SPI_CMD, &cmd_be, sizeof(cmd_be));
+       if (rc)
+               return rc;
+       return 0;
+}

...

Quoted 7 lines
Quoted 1 line

+ data_be = cpu_to_be32((value >> 32) & 0xFFFFFFFF);

Redundant & 0xff... part.

Quoted 1 line

+ data_be = cpu_to_be32(value & 0xFFFFFFFF);

Ditto.

You may use upper_32_bits() / lower_32_bits() instead.

OK, thanks.

Quoted 8 lines

...

drivers/spi/spi-fsi.c line 165

Quoted 4 lines
drivers/spi/spi-fsi.c new file +4 -0
+static int fsi_spi_data_in(u64 in, u8 *rx, int len)
+{
+       int i;
+       int num_bytes = len > 8 ? 8 : len;

min(len, 8);

Sure.

Quoted 6 lines

drivers/spi/spi-fsi.c line 168

Quoted 3 lines
drivers/spi/spi-fsi.c new file +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

No, these are shift in/out operations. The read register will also have previous operations data in them and must be extracted with only the correct number of bytes.

Quoted 16 lines

drivers/spi/spi-fsi.c line 174

Quoted 5 lines
drivers/spi/spi-fsi.c new file +4 -0
+       return num_bytes;
+}
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Quoted 1 line

+}

...

Quoted 1 line

+ dev_info(ctx->dev, "Resetting SPI controller.\n");

info?! Why?

drivers/spi/spi-fsi.c line 199

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+       rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
+                              SPI_FSI_CLOCK_CFG_RESET2);
+       return rc;
Quoted 6 lines

return fsi_spi_write_reg();

...

Quoted 1 line

+ return ((64 - seq->bit) / 8) - 2;

Too many parentheses.

I prefer using 2 extra characters to make it much clearer at a glance.

Quoted 21 lines

...

Quoted 6 lines
drivers/spi/spi-fsi.c new file +6 -0
+static int fsi_spi_sequence_transfer(struct fsi_spi *ctx,
+                                    struct fsi_spi_sequence *seq,
+                                    struct spi_transfer *transfer)
+{
+       int loops = 1;
+       int idx = 0;

drivers/spi/spi-fsi.c line 232

Quoted 10 lines
drivers/spi/spi-fsi.c new file +10 -0
+       int rc;
+       u8 len;
+       u8 rem = 0;
+       if (transfer->len > 8) {
+               loops = transfer->len / 8;
+               rem = transfer->len - (loops * 8);
+               len = 8;
+       } else {
+               len = transfer->len;
+       }

len = min(transfer->len, 8);

Quoted 2 lines

loops = transfer->len / len;
rem = transfer->len % len;

Sure.

Quoted 8 lines

(I think compiler is clever enough to find out that the division can be avoided)

...and drop assignments in definition block.

I didn't look carefully in the implementation, but I believe there is still room for improvement / optimization.

Quoted 7 lines

drivers/spi/spi-fsi.c line 257

Quoted 5 lines
drivers/spi/spi-fsi.c new file +5 -0
+       if (loops > 1) {
+               rc = fsi_spi_write_reg(ctx, SPI_FSI_COUNTER_CFG,
+                                      SPI_FSI_COUNTER_CFG_LOOPS(loops - 1));
+               if (rc) {
+                       /* Ensure error returns < 0 in this case. */

I didn't get why this case is special? Why not to be consistent with return value?

Sure, will fix, this was leftover from some testing.

Quoted 7 lines

drivers/spi/spi-fsi.c line 264

Quoted 7 lines
drivers/spi/spi-fsi.c new file +6 -0
+                       if (rc > 0)
+                               rc = -rc;
+
+                       return rc;
+               }
+               return loops;
Quoted 8 lines

If we return here the amount of loops...

drivers/spi/spi-fsi.c line 96

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+       }
+
+       return 0;

...why here is 0?

I think more consistency is required.

Will refactor.

Quoted 8 lines
Quoted 2 lines

+}

...

drivers/spi/spi-fsi.c line 272

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+static int fsi_spi_transfer_data(struct fsi_spi *ctx,
+                                struct spi_transfer *transfer)
+{

Can you refactor to tx and rx parts?

Why?

Quoted 5 lines
Quoted 3 lines
(quoted diff) +2 -0
+       return 0;
+}

...

Quoted 16 lines

drivers/spi/spi-fsi.c line 362

Quoted 13 lines
drivers/spi/spi-fsi.c new file +13 -0
+       do {
+               rc = fsi_spi_read_reg(ctx, SPI_FSI_STATUS, &status);
+               if (rc)
+                       return rc;
+
+               if (status & (SPI_FSI_STATUS_ANY_ERROR |
+                             SPI_FSI_STATUS_TDR_FULL |
+                             SPI_FSI_STATUS_RDR_FULL)) {
+                       rc = fsi_spi_reset(ctx);
+                       if (rc)
+                               return rc;
+
+                       continue;

I forgot if this to be infinite loop or if it's going to check previous seq_state value. In any case this code is a bit fishy. Needs comments / refactoring.

I'll add a timeout.

Quoted 7 lines
Quoted 5 lines
drivers/spi/spi-fsi.c new file +4 -0
+               }
+
+               seq_state = status & SPI_FSI_STATUS_SEQ_STATE;
+       } while (seq_state && (seq_state != SPI_FSI_STATUS_SEQ_STATE_IDLE));

...

Quoted 8 lines

drivers/spi/spi-fsi.c line 378

Quoted 7 lines
drivers/spi/spi-fsi.c new file +7 -0
+       if ((clock_cfg & (SPI_FSI_CLOCK_CFG_MM_ENABLE |
+                         SPI_FSI_CLOCK_CFG_ECC_DISABLE |
+                         SPI_FSI_CLOCK_CFG_MODE |
+                         SPI_FSI_CLOCK_CFG_SCK_RECV_DEL |
+                         SPI_FSI_CLOCK_CFG_SCK_DIV)) != wanted_clock_cfg)
+               rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
+                                      wanted_clock_cfg);

Missed {} ?

No? It's one line under the if.

Quoted 8 lines
Quoted 4 lines
drivers/spi/spi-fsi.c new file +3 -0
+
+       return rc;
+}

...

Quoted 1 line

+ rc = fsi_slave_read(fsi->slave, 0x2860, &root_ctrl_8,

What is this magic for?

Added comment.

Quoted 11 lines
Quoted 4 lines
drivers/spi/spi-fsi.c new file +3 -0
+                           sizeof(root_ctrl_8));
+       if (rc)
+               return rc;

...

drivers/spi/spi-fsi.c line 526

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

Why do you need this?

Will drop it.

Thanks for the review!

Eddie

Quoted 16 lines

...

drivers/spi/spi-fsi.c line 537

Quoted 5 lines
drivers/spi/spi-fsi.c new file +5 -0
+static struct fsi_driver fsi_spi_driver = {
+       .id_table = fsi_spi_ids,
+       .drv = {
+               .name = "spi-fsi",
+               .bus = &fsi_bus_type,

Why is it not in the module_fsi_driver() macro?

Quoted 6 lines
drivers/spi/spi-fsi.c new file +6 -0
+               .probe = fsi_spi_probe,
+               .remove = fsi_spi_remove,
+       },
+};
+
+module_fsi_driver(fsi_spi_driver);
#5 lore raw
Quoted 2 lines · Eddie James
Quoted 1 line · Andy Shevchenko

On Wed, Jan 29, 2020 at 10:09 PM Eddie James <[email protected]> wrote:

...

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

+ struct device *dev;

Isn't fsl->dev the same?
Perhaps kernel doc to explain the difference?

No, it's not the same, as dev here is the SPI controller. I'll add a comment.

Why to have duplication then?

Quoted 1 line
Quoted 1 line
Quoted 1 line

+ struct fsi_device *fsi;

...

Quoted 10 lines
Quoted 5 lines

drivers/spi/spi-fsi.c line 168

Quoted 2 lines
drivers/spi/spi-fsi.c new file +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

No, these are shift in/out operations. The read register will also have previous operations data in them and must be extracted with only the correct number of bytes.

Why not to call put_unaligned() how the tail in this case (it's 0 or can be easily made to be 0) will affect the result?

Quoted 6 lines
Quoted 6 lines

drivers/spi/spi-fsi.c line 174

Quoted 5 lines
drivers/spi/spi-fsi.c new file +4 -0
+       return num_bytes;
+}
 
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Ditto.

Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

...

Quoted 7 lines
Quoted 4 lines

drivers/spi/spi-fsi.c line 272

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+static int fsi_spi_transfer_data(struct fsi_spi *ctx,
+                                struct spi_transfer *transfer)
+{

Can you refactor to tx and rx parts?

Why?

It's way too long function to read. Indentation level also can improve readability.
That's basically what refactoring is for.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return 0;
+}

...

Quoted 11 lines
Quoted 8 lines

drivers/spi/spi-fsi.c line 378

Quoted 7 lines
drivers/spi/spi-fsi.c new file +7 -0
+       if ((clock_cfg & (SPI_FSI_CLOCK_CFG_MM_ENABLE |
+                         SPI_FSI_CLOCK_CFG_ECC_DISABLE |
+                         SPI_FSI_CLOCK_CFG_MODE |
+                         SPI_FSI_CLOCK_CFG_SCK_RECV_DEL |
+                         SPI_FSI_CLOCK_CFG_SCK_DIV)) != wanted_clock_cfg)
+               rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
+                                      wanted_clock_cfg);

Missed {} ?

No? It's one line under the if.

One statement, but *two* lines.
What does checkpatch.pl tell you about this?

Signature · 2 lines
With Best Regards,
Andy Shevchenko
author maintainer
#6 lore raw
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · Eddie James
Quoted 1 line · Andy Shevchenko

On Wed, Jan 29, 2020 at 10:09 PM Eddie James <[email protected]> wrote:

...

Quoted 7 lines
Quoted 6 lines
Quoted 3 lines
Quoted 1 line

+ struct device *dev;

Isn't fsl->dev the same?
Perhaps kernel doc to explain the difference?

No, it's not the same, as dev here is the SPI controller. I'll add a comment.

Why to have duplication then?

Nothing is being duplicated, the two variables are storing entirely different information, both of which are necessary for each SPI controller that this driver is driving.

Quoted 8 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ struct fsi_device *fsi;

...

Quoted 4 lines
Quoted 4 lines

drivers/spi/spi-fsi.c line 168

Quoted 2 lines
drivers/spi/spi-fsi.c new file +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Quoted 7 lines
Quoted 5 lines
Quoted 1 line

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

No, these are shift in/out operations. The read register will also have previous operations data in them and must be extracted with only the correct number of bytes.

Why not to call put_unaligned() how the tail in this case (it's 0 or can be easily made to be 0) will affect the result?

The shift-in is not the same as any byte-swap or unaligned operation. For however many bytes we've read, we start at that many bytes left-shifted in the register and copy out to our buffer, moving right for each next byte... I don't think there is an existing function for this operation.

Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

drivers/spi/spi-fsi.c line 174

Quoted 5 lines
drivers/spi/spi-fsi.c new file +4 -0
+       return num_bytes;
+}
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Ditto.

I don't understand how this could work for transfers of less than 8 bytes, any put_unaligned would access memory that it doesn't own.

Quoted 9 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+}

...

Quoted 5 lines
Quoted 4 lines

drivers/spi/spi-fsi.c line 272

Quoted 3 lines
drivers/spi/spi-fsi.c new file +3 -0
+static int fsi_spi_transfer_data(struct fsi_spi *ctx,
+                                struct spi_transfer *transfer)
+{

Can you refactor to tx and rx parts?

Quoted 4 lines
Quoted 1 line

Why?

It's way too long function to read. Indentation level also can improve readability.
That's basically what refactoring is for.

The body is 65 lines, I don't think it is too long.

Since the function is used multiple times I think it makes more sense to encapsulate the check for tx/rx in the function rather than split it and have to check each time the functions are used.

Quoted 12 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
(quoted diff) +2 -0
+       return 0;
+}

...

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
drivers/spi/spi-fsi.c new file +7 -0
+       if ((clock_cfg & (SPI_FSI_CLOCK_CFG_MM_ENABLE |
+                         SPI_FSI_CLOCK_CFG_ECC_DISABLE |
+                         SPI_FSI_CLOCK_CFG_MODE |
+                         SPI_FSI_CLOCK_CFG_SCK_RECV_DEL |
+                         SPI_FSI_CLOCK_CFG_SCK_DIV)) != wanted_clock_cfg)
+               rc = fsi_spi_write_reg(ctx, SPI_FSI_CLOCK_CFG,
+                                      wanted_clock_cfg);
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Missed {} ?

No? It's one line under the if.

One statement, but *two* lines.
What does checkpatch.pl tell you about this?

Right.

checkpatch.pl says nothing about this, I think it meets the coding standards as is.

Thanks for the review,

Eddie

Quoted 1 line
#7 lore raw
Quoted 4 lines · Eddie James
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Eddie James
Quoted 1 line · Andy Shevchenko

On Wed, Jan 29, 2020 at 10:09 PM Eddie James <[email protected]> wrote:

...

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Quoted 3 lines
Quoted 1 line

+ struct device *dev;

Isn't fsl->dev the same?
Perhaps kernel doc to explain the difference?

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

No, it's not the same, as dev here is the SPI controller. I'll add a comment.

Why to have duplication then?

Nothing is being duplicated, the two variables are storing entirely different information, both of which are necessary for each SPI controller that this driver is driving.

Oh, I see now, thanks!

...

Quoted 13 lines
Quoted 11 lines
Quoted 9 lines
Quoted 5 lines

drivers/spi/spi-fsi.c line 168

Quoted 2 lines
drivers/spi/spi-fsi.c new file +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

No, these are shift in/out operations. The read register will also have previous operations data in them and must be extracted with only the correct number of bytes.

Why not to call put_unaligned() how the tail in this case (it's 0 or can be easily made to be 0) will affect the result?

Quoted 5 lines

The shift-in is not the same as any byte-swap or unaligned operation. For however many bytes we've read, we start at that many bytes left-shifted in the register and copy out to our buffer, moving right for each next byte... I don't think there is an existing function for this operation.

For me it looks like

  u8 tmp[8];

  put_unaligned_be64(in, tmp);
  memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Quoted 10 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

drivers/spi/spi-fsi.c line 174

Quoted 4 lines
drivers/spi/spi-fsi.c new file +4 -0
+       return num_bytes;
+}
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Ditto.

I don't understand how this could work for transfers of less than 8 bytes, any put_unaligned would access memory that it doesn't own.

Ditto.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#8 lore raw
Quoted 39 lines · Andy Shevchenko
Quoted 4 lines · Eddie James
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Eddie James
Quoted 1 line · Andy Shevchenko

On Wed, Jan 29, 2020 at 10:09 PM Eddie James <[email protected]> wrote:

...

Quoted 10 lines
Quoted 6 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

+ struct device *dev;

Isn't fsl->dev the same?
Perhaps kernel doc to explain the difference?

No, it's not the same, as dev here is the SPI controller. I'll add a comment.

Why to have duplication then?

Nothing is being duplicated, the two variables are storing entirely different information, both of which are necessary for each SPI controller that this driver is driving.

Oh, I see now, thanks!

...

Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

drivers/spi/spi-fsi.c line 168

Quoted 2 lines
drivers/spi/spi-fsi.c new file +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

No, these are shift in/out operations. The read register will also have previous operations data in them and must be extracted with only the correct number of bytes.

Quoted 8 lines
Quoted 2 lines

Why not to call put_unaligned() how the tail in this case (it's 0 or can be easily made to be 0) will affect the result?

The shift-in is not the same as any byte-swap or unaligned operation. For however many bytes we've read, we start at that many bytes left-shifted in the register and copy out to our buffer, moving right for each next byte... I don't think there is an existing function for this operation.

For me it looks like

Quoted 7 lines
   u8 tmp[8];

   put_unaligned_be64(in, tmp);
   memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Thanks,

Eddie

Quoted 13 lines
Quoted 10 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

drivers/spi/spi-fsi.c line 174

Quoted 5 lines
drivers/spi/spi-fsi.c new file +4 -0
+       return num_bytes;
+}
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Ditto.

I don't understand how this could work for transfers of less than 8 bytes, any put_unaligned would access memory that it doesn't own.

Ditto.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

#9 lore raw
Quoted 5 lines · Eddie James
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Eddie James
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Eddie James

On 1/30/20 10:37 AM, Andy Shevchenko wrote:

...

Quoted 27 lines
Quoted 18 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

No, these are shift in/out operations. The read register will also have previous operations data in them and must be extracted with only the correct number of bytes.

Quoted 8 lines
Quoted 2 lines

Why not to call put_unaligned() how the tail in this case (it's 0 or can be easily made to be 0) will affect the result?

The shift-in is not the same as any byte-swap or unaligned operation. For however many bytes we've read, we start at that many bytes left-shifted in the register and copy out to our buffer, moving right for each next byte... I don't think there is an existing function for this operation.

For me it looks like

Quoted 7 lines
   u8 tmp[8];

   put_unaligned_be64(in, tmp);
   memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Quoted 6 lines

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 6 lines
Quoted 5 lines
Quoted 5 lines

Review

Quoted 4 lines
(quoted diff) +4 -0
+       return num_bytes;
+}
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Ditto.

I don't understand how this could work for transfers of less than 8 bytes, any put_unaligned would access memory that it doesn't own.

Ditto.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#10 lore raw
Quoted 33 lines · Andy Shevchenko
Quoted 5 lines · Eddie James
Quoted 4 lines · Andy Shevchenko
Quoted 3 lines · Eddie James
Quoted 2 lines · Andy Shevchenko
Quoted 1 line · Eddie James

On 1/30/20 10:37 AM, Andy Shevchenko wrote:

...

Quoted 25 lines
Quoted 24 lines
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
Quoted 5 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

Isn't it NIH of get_unalinged_be64 / le64 or something similar?

No, these are shift in/out operations. The read register will also have previous operations data in them and must be extracted with only the correct number of bytes.

Quoted 7 lines
Quoted 2 lines

Why not to call put_unaligned() how the tail in this case (it's 0 or can be easily made to be 0) will affect the result?

The shift-in is not the same as any byte-swap or unaligned operation. For however many bytes we've read, we start at that many bytes left-shifted in the register and copy out to our buffer, moving right for each next byte... I don't think there is an existing function for this operation.

For me it looks like

    u8 tmp[8];

    put_unaligned_be64(in, tmp);
    memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Quoted 8 lines
Quoted 6 lines

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

I see now, yes, thanks.

Do you think this is worth a v3? Perhaps put_unaligned is slightly more optimized than the loop but there is more memory copy with that way too.

Eddie

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
Quoted 9 lines
Quoted 7 lines
Quoted 6 lines
Quoted 6 lines

Review

Quoted 5 lines
(quoted diff) +4 -0
+       return num_bytes;
+}
+static int fsi_spi_data_out(u64 *out, const u8 *tx, int len)
+{

Ditto as for above function. (put_unaligned ...)

Ditto.

I don't understand how this could work for transfers of less than 8 bytes, any put_unaligned would access memory that it doesn't own.

Ditto.

Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line
Quoted 1 line

+}

#11 lore raw
Quoted 29 lines · Eddie James
Quoted 28 lines · Andy Shevchenko
Quoted 27 lines · Eddie James
Quoted 12 lines · Andy Shevchenko
Quoted 8 lines · Eddie James
Quoted 7 lines · Andy Shevchenko
Quoted 6 lines · Eddie James
Quoted 3 lines · Andy Shevchenko

Review

Quoted 2 lines
(quoted diff) +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

For me it looks like

Quoted 7 lines
    u8 tmp[8];

    put_unaligned_be64(in, tmp);
    memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Quoted 8 lines
Quoted 2 lines

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

I see now, yes, thanks.

Do you think this is worth a v3? Perhaps put_unaligned is slightly more optimized than the loop but there is more memory copy with that way too.

I already forgot the entire context when this has been called. Can you summarize what the sequence(s) of num_bytes are expected usually.

IIUC if packets small, less than 8 bytes, than num_bytes will be that value. Otherwise it will be something like 8 + 8 + 8 ... + tail. Is it correct assumption?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return num_bytes;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#12 lore raw
Quoted 32 lines · Andy Shevchenko
Quoted 31 lines · Eddie James
Quoted 19 lines · Andy Shevchenko
Quoted 18 lines · Eddie James
Quoted 17 lines · Andy Shevchenko
Quoted 7 lines · Eddie James
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Eddie James
Quoted 3 lines · Andy Shevchenko

Review

Quoted 2 lines
(quoted diff) +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

For me it looks like

     u8 tmp[8];

     put_unaligned_be64(in, tmp);
     memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Quoted 8 lines
Quoted 6 lines

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

I see now, yes, thanks.

Quoted 8 lines
Quoted 2 lines

Do you think this is worth a v3? Perhaps put_unaligned is slightly more optimized than the loop but there is more memory copy with that way too.

I already forgot the entire context when this has been called. Can you summarize what the sequence(s) of num_bytes are expected usually.

IIUC if packets small, less than 8 bytes, than num_bytes will be that value. Otherwise it will be something like 8 + 8 + 8 ... + tail. Is it correct assumption?

Yes, it will typically be 8 + 8 +... remainder. Basically, on any RX, the driver polls for the rx register full. Once full, it will read however much data is left to be transferred. Since we use min(len, 8) then we read 8 usually, until we get to the end.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
(quoted diff) +2 -0
+       return num_bytes;
+}
#13 lore raw
Quoted 43 lines · Eddie James
Quoted 32 lines · Andy Shevchenko
Quoted 31 lines · Eddie James
Quoted 19 lines · Andy Shevchenko
Quoted 18 lines · Eddie James
Quoted 17 lines · Andy Shevchenko
Quoted 7 lines · Eddie James
Quoted 6 lines · Andy Shevchenko
Quoted 5 lines · Eddie James
Quoted 3 lines · Andy Shevchenko

Review

Quoted 2 lines
(quoted diff) +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

For me it looks like

     u8 tmp[8];

     put_unaligned_be64(in, tmp);
     memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Quoted 8 lines
Quoted 6 lines

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

I see now, yes, thanks.

Quoted 8 lines
Quoted 2 lines

Do you think this is worth a v3? Perhaps put_unaligned is slightly more optimized than the loop but there is more memory copy with that way too.

I already forgot the entire context when this has been called. Can you summarize what the sequence(s) of num_bytes are expected usually.

IIUC if packets small, less than 8 bytes, than num_bytes will be that value. Otherwise it will be something like 8 + 8 + 8 ... + tail. Is it correct assumption?

Quoted 4 lines

Yes, it will typically be 8 + 8 +... remainder. Basically, on any RX, the driver polls for the rx register full. Once full, it will read however much data is left to be transferred. Since we use min(len, 8) then we read 8 usually, until we get to the end.

I asked that because we might have a better optimization, i.e, call directly put_unaligned_be64() when we know that length is 8 bytes. For the rest your approach might be simpler. Similar for the TX case.

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
(quoted diff) +2 -0
+       return num_bytes;
+}
Signature · 2 lines
With Best Regards,
Andy Shevchenko
#14 lore raw
Quoted 41 lines · Andy Shevchenko
Quoted 40 lines · Eddie James
Quoted 38 lines · Andy Shevchenko
Quoted 25 lines · Eddie James
Quoted 24 lines · Andy Shevchenko
Quoted 23 lines · Eddie James
Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Eddie James
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Eddie James
Quoted 3 lines · Andy Shevchenko

Review

Quoted 2 lines
(quoted diff) +2 -0
+       for (i = 0; i < num_bytes; ++i)
+               rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

For me it looks like

Quoted 7 lines
      u8 tmp[8];

      put_unaligned_be64(in, tmp);
      memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Quoted 6 lines
Quoted 2 lines

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

I see now, yes, thanks.

Do you think this is worth a v3? Perhaps put_unaligned is slightly more optimized than the loop but there is more memory copy with that way too.

I already forgot the entire context when this has been called. Can you summarize what the sequence(s) of num_bytes are expected usually.

IIUC if packets small, less than 8 bytes, than num_bytes will be that value. Otherwise it will be something like 8 + 8 + 8 ... + tail. Is it correct assumption?

Quoted 7 lines
Quoted 4 lines

Yes, it will typically be 8 + 8 +... remainder. Basically, on any RX, the driver polls for the rx register full. Once full, it will read however much data is left to be transferred. Since we use min(len, 8) then we read 8 usually, until we get to the end.

I asked that because we might have a better optimization, i.e, call directly put_unaligned_be64() when we know that length is 8 bytes. For the rest your approach might be simpler. Similar for the TX case.

I just tried to implement as you suggested but I realized something: The value is already swapped from BE to CPU when the register is read in fsi_spi_read_reg. It happens to work out correctly to use put_unaligned_be64 on a LE CPU to flip the bytes here. But on a BE CPU, this wouldn't be correct I think. Now I don't anticipate this driver running on a BE CPU, but I think it is weird to flip it twice, and better to do it manually here.

What do you think Andy?

Thanks,

Eddie

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
(quoted diff) +2 -0
+       return num_bytes;
+}
#15 lore raw
Quoted 51 lines · Eddie James
Quoted 41 lines · Andy Shevchenko
Quoted 40 lines · Eddie James
Quoted 38 lines · Andy Shevchenko
Quoted 25 lines · Eddie James
Quoted 24 lines · Andy Shevchenko
Quoted 23 lines · Eddie James
Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Eddie James
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Eddie James
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines

+ for (i = 0; i < num_bytes; ++i)
+ rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);

Redundant & 0xffULL part.

For me it looks like

Quoted 7 lines
      u8 tmp[8];

      put_unaligned_be64(in, tmp);
      memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Quoted 6 lines
Quoted 2 lines

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

I see now, yes, thanks.

Do you think this is worth a v3? Perhaps put_unaligned is slightly more optimized than the loop but there is more memory copy with that way too.

I already forgot the entire context when this has been called. Can you summarize what the sequence(s) of num_bytes are expected usually.

IIUC if packets small, less than 8 bytes, than num_bytes will be that value. Otherwise it will be something like 8 + 8 + 8 ... + tail. Is it correct assumption?

Quoted 7 lines
Quoted 4 lines

Yes, it will typically be 8 + 8 +... remainder. Basically, on any RX, the driver polls for the rx register full. Once full, it will read however much data is left to be transferred. Since we use min(len, 8) then we read 8 usually, until we get to the end.

I asked that because we might have a better optimization, i.e, call directly put_unaligned_be64() when we know that length is 8 bytes. For the rest your approach might be simpler. Similar for the TX case.

Quoted 5 lines

I just tried to implement as you suggested but I realized something: The value is already swapped from BE to CPU when the register is read in fsi_spi_read_reg. It happens to work out correctly to use put_unaligned_be64 on a LE CPU to flip the bytes here. But on a BE CPU, this wouldn't be correct I think.

Hmm... Any BE conversion op on BE architecture is no-op. Same for LE on LE.

Quoted 10 lines

Now I don't anticipate this driver
running on a BE CPU, but I think it is weird to flip it twice, and better to do it manually here.

What do you think Andy?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ return num_bytes;
+}

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#16 lore raw
Quoted 50 lines · Andy Shevchenko
Quoted 49 lines · Eddie James
Quoted 47 lines · Andy Shevchenko
Quoted 36 lines · Eddie James
Quoted 35 lines · Andy Shevchenko
Quoted 25 lines · Eddie James
Quoted 24 lines · Andy Shevchenko
Quoted 23 lines · Eddie James
Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Eddie James
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Eddie James
Quoted 3 lines · Andy Shevchenko

+ for (i = 0; i < num_bytes; ++i)
+ rx[i] = (u8)((in >> (8 * ((num_bytes - 1) - i))) & 0xffULL);
Redundant & 0xffULL part.

For me it looks like

Quoted 7 lines
       u8 tmp[8];

       put_unaligned_be64(in, tmp);
       memcpy(rx, tmp, num_bytes);

put_unaligned*() is just a method to unroll the value to the u8 buffer. See, for example, linux/unaligned/be_byteshift.h implementation.

Unforunately it is not the same. put_unaligned_be64 will take the highest 8 bits (0xff00000000000000) and move it into tmp[0]. Then 0x00ff000000000000 into tmp[1], etc. This is only correct for this driver IF my transfer is 8 bytes. If, for example, I transfer 5 bytes, then I need 0x000000ff00000000 into tmp[0], 0x00000000ff000000 into tmp[1], etc. So I think my current implementation is correct.

Quoted 6 lines
Quoted 2 lines

Yes, I missed correction of the start address in memcpy(). Otherwise it's still the same what I was talking about.

I see now, yes, thanks.

Do you think this is worth a v3? Perhaps put_unaligned is slightly more optimized than the loop but there is more memory copy with that way too.

I already forgot the entire context when this has been called. Can you summarize what the sequence(s) of num_bytes are expected usually.

Quoted 7 lines
Quoted 3 lines

IIUC if packets small, less than 8 bytes, than num_bytes will be that value. Otherwise it will be something like 8 + 8 + 8 ... + tail. Is it correct assumption?

Yes, it will typically be 8 + 8 +... remainder. Basically, on any RX, the driver polls for the rx register full. Once full, it will read however much data is left to be transferred. Since we use min(len, 8) then we read 8 usually, until we get to the end.

I asked that because we might have a better optimization, i.e, call directly put_unaligned_be64() when we know that length is 8 bytes. For the rest your approach might be simpler. Similar for the TX case.

Quoted 7 lines
Quoted 5 lines

I just tried to implement as you suggested but I realized something: The value is already swapped from BE to CPU when the register is read in fsi_spi_read_reg. It happens to work out correctly to use put_unaligned_be64 on a LE CPU to flip the bytes here. But on a BE CPU, this wouldn't be correct I think.

Hmm... Any BE conversion op on BE architecture is no-op. Same for LE on LE.

Right. So regardless of architecture, by the time we get to fsi_spi_data_in, the data is in the correct endianness. But on a BE architecture, it would still need to get flipped because that's what the specification indicates. So doing it manually seems correct to me.

Quoted 12 lines
Quoted 10 lines

Now I don't anticipate this driver
running on a BE CPU, but I think it is weird to flip it twice, and better to do it manually here.

What do you think Andy?

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

+ return num_bytes;
+}