Skip to contentSkip to conversation

spi: microchip-core: Code improvements (part 2)

PATCH In review Andy Shevchenko 4 participants 9 messages Nov 28, 2025 – Jan 8 SPI

lore
Share & tools
lore
#0 lore raw

v4 cover spi: microchip-core: Code improvements (part 2)

Here is the second part of the set of refactoring and cleaning it up.

Changelog v4:
- collected tags (Prajna)
- dropped applied patches
- added a new patch 2

v3: <[email protected]>

Changelog v3:
- collected tags (Prajna)
- restored dummy read in TX-only transfers

v2: <[email protected]>

Changelog v2:
- dropped device property agnostic API conversion change (Mark)

Andy Shevchenko (2):
spi: microchip-core: Refactor FIFO read and write handlers
spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

 drivers/spi/spi-microchip-core-spi.c | 33 +++++++++++-----------------
 1 file changed, 13 insertions(+), 20 deletions(-)
Signature · 1 line
2.50.1
#1 lore raw

PATCH v4 1/2 spi: microchip-core: Refactor FIFO read and write handlers

Make both handlers to be shorter and easier to understand. While at it, unify their style.

---
 drivers/spi/spi-microchip-core-spi.c | 31 +++++++++++-----------------
 1 file changed, 12 insertions(+), 19 deletions(-)
drivers/spi/spi-microchip-core-spi.c +12 -19
index 892f066f0074..98bf0e6cd00e 100644
@@ -97,15 +97,12 @@ static inline void mchp_corespi_read_fifo(struct mchp_corespi *spi, u32 fifo_max
 		       MCHP_CORESPI_STATUS_RXFIFO_EMPTY)
 			;
 
+		/* On TX-only transfers always perform a dummy read */
 		data = readb(spi->regs + MCHP_CORESPI_REG_RXDATA);
+		if (spi->rx_buf)
+			*spi->rx_buf++ = data;
 
 		spi->rx_len--;
-		if (!spi->rx_buf)
-			continue;
-
-		*spi->rx_buf = data;
-
-		spi->rx_buf++;
 	}
 }
 
@@ -127,23 +124,19 @@ static void mchp_corespi_disable_ints(struct mchp_corespi *spi)
 
 static inline void mchp_corespi_write_fifo(struct mchp_corespi *spi, u32 fifo_max)
 {
-	int i = 0;
-
-	while ((i < fifo_max) &&
-	       !(readb(spi->regs + MCHP_CORESPI_REG_STAT) &
-		 MCHP_CORESPI_STATUS_TXFIFO_FULL)) {
-		u32 word;
-
-		word = spi->tx_buf ? *spi->tx_buf : 0xaa;
-		writeb(word, spi->regs + MCHP_CORESPI_REG_TXDATA);
+	for (int i = 0; i < fifo_max; i++) {
+		if (readb(spi->regs + MCHP_CORESPI_REG_STAT) &
+		    MCHP_CORESPI_STATUS_TXFIFO_FULL)
+			break;
 
+		/* On RX-only transfers always perform a dummy write */
 		if (spi->tx_buf)
-			spi->tx_buf++;
+			writeb(*spi->tx_buf++, spi->regs + MCHP_CORESPI_REG_TXDATA);
+		else
+			writeb(0xaa, spi->regs + MCHP_CORESPI_REG_TXDATA);
 
-		i++;
+		spi->tx_len--;
 	}
-
-	spi->tx_len -= i;
 }
 
 static void mchp_corespi_set_cs(struct spi_device *spi, bool disable)
Signature · 1 line
2.50.1
#2 lore raw

PATCH v4 2/2 spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

In reply to Andy Shevchenko

Use XOR instead of ANDNOT to simplify the logic. The current approach with (foo & BAR & ~baz) is harder to process than more usual pattern for the comparing misconfiguration using ((foo ^ baz) & BAR) which can be read as "find all different bits between foo and baz that are related to BAR (mask)". Besides that it makes binary code shorter.

Function old new delta
mchp_corespi_setup 103 99 -4

---
 drivers/spi/spi-microchip-core-spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-microchip-core-spi.c +1 -1
index 98bf0e6cd00e..2f4b21ad56a7 100644
@@ -161,7 +161,7 @@ static int mchp_corespi_setup(struct spi_device *spi)
 		return -EOPNOTSUPP;
 	}
 
-	if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
+	if ((spi->mode ^ spi->controller->mode_bits) & SPI_MODE_X_MASK) {
 		dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
 		return -EINVAL;
 	}
Signature · 1 line
2.50.1
#3 lore raw

Re: [PATCH v4 2/2] spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

Quoted 20 lines · Andy Shevchenko

Use XOR instead of ANDNOT to simplify the logic. The current approach with (foo & BAR & ~baz) is harder to process than more usual pattern for the comparing misconfiguration using ((foo ^ baz) & BAR) which can be read as "find all different bits between foo and baz that are related to BAR (mask)". Besides that it makes binary code shorter.

Function old new delta
mchp_corespi_setup 103 99 -4

---
 drivers/spi/spi-microchip-core-spi.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/spi/spi-microchip-core-spi.c +0 -0
index 98bf0e6cd00e..2f4b21ad56a7 100644

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_setup(struct spi_device *spi) · line 164

Quoted 5 lines
drivers/spi/spi-microchip-core-spi.c +1 -1
@@ -161,7 +161,7 @@ static int mchp_corespi_setup(struct spi_device *spi)
                return -EOPNOTSUPP;
        }
 
-       if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
+       if ((spi->mode ^ spi->controller->mode_bits) & SPI_MODE_X_MASK) {

This changes the behavior: if a bit isn't set in spi->mode that is set in mode_bits, it would have been previously accepted, now it's refused. E.g. controller has (SPI_CPOL | SPI_CPHA), device only SPI_CPOL. 0x1 & 0x3 & ~0x3 => 0, vs (0x1 ^ 0x3) & 0x3 => 0x2

If this is the actually intended behavior here, it is a fix and should carry a Fixes tag (the message below implies that).

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_setup(struct spi_device *spi) · line 167

Quoted 3 lines
drivers/spi/spi-microchip-core-spi.c +0 -0
                dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
                return -EINVAL;
        }

Best regards,
Jonas

#4 lore raw

Re: [PATCH v4 2/2] spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

Quoted 2 lines · Jonas Gorski

On Fri, Nov 28, 2025 at 7:56 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 10 lines

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_setup(struct spi_device *spi) · line 164

Quoted 2 lines
drivers/spi/spi-microchip-core-spi.c +1 -1
-       if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
+       if ((spi->mode ^ spi->controller->mode_bits) & SPI_MODE_X_MASK) {

This changes the behavior: if a bit isn't set in spi->mode that is set in mode_bits, it would have been previously accepted, now it's refused. E.g. controller has (SPI_CPOL | SPI_CPHA), device only SPI_CPOL. 0x1 & 0x3 & ~0x3 => 0, vs (0x1 ^ 0x3) & 0x3 => 0x2

If this is the actually intended behavior here, it is a fix and should carry a Fixes tag (the message below implies that).

Yeah, yesterday I was thinking about the same and I was confused by the logic behind. As far as I understood the comments regarding mode provided by DT is that the mode is configured in IP and may not be changed. And you are right about the fix, but let's wait for Microchip to elaborate on the expected behaviour.

Quoted 3 lines
Quoted 3 lines
                dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
                return -EINVAL;
        }

Thanks for the review!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Reviewed-by
#5 lore raw

Re: [PATCH v4 2/2] spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

Quoted 14 lines · Andy Shevchenko
Quoted 2 lines · Jonas Gorski

On Fri, Nov 28, 2025 at 7:56 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 8 lines

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_setup(struct spi_device *spi) · line 164

Quoted 2 lines
drivers/spi/spi-microchip-core-spi.c +1 -1
-       if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
+       if ((spi->mode ^ spi->controller->mode_bits) & SPI_MODE_X_MASK) {

This changes the behavior: if a bit isn't set in spi->mode that is set in mode_bits, it would have been previously accepted, now it's refused. E.g. controller has (SPI_CPOL | SPI_CPHA), device only SPI_CPOL. 0x1 & 0x3 & ~0x3 => 0, vs (0x1 ^ 0x3) & 0x3 => 0x2

Quoted 8 lines
Quoted 2 lines

If this is the actually intended behavior here, it is a fix and should carry a Fixes tag (the message below implies that).

Yeah, yesterday I was thinking about the same and I was confused by the logic behind. As far as I understood the comments regarding mode provided by DT is that the mode is configured in IP and may not be changed. And you are right about the fix, but let's wait for Microchip to elaborate on the expected behaviour.

Prajna is on holiday and I don't have a setup to actually test this on, but I'm 99% sure that you're both right and the original behaviour was wrong. There's a verilog parameter to the IP block that determines which motorola mode it is and a device that's not an exact match won't work. FWIW:

Cheers,
Conor.

Quoted 12 lines
Quoted 4 lines
Quoted 4 lines
                dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
                return -EINVAL;
        }

Thanks for the review!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#6 lore raw

Re: [PATCH v4 2/2] spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

Quoted 4 lines · Conor Dooley
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Jonas Gorski

On Fri, Nov 28, 2025 at 7:56 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 17 lines
Quoted 8 lines
Quoted 8 lines

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_setup(struct spi_device *spi) · line 164

Quoted 2 lines
drivers/spi/spi-microchip-core-spi.c +1 -1
-       if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
+       if ((spi->mode ^ spi->controller->mode_bits) & SPI_MODE_X_MASK) {

This changes the behavior: if a bit isn't set in spi->mode that is set in mode_bits, it would have been previously accepted, now it's refused. E.g. controller has (SPI_CPOL | SPI_CPHA), device only SPI_CPOL. 0x1 & 0x3 & ~0x3 => 0, vs (0x1 ^ 0x3) & 0x3 => 0x2

Quoted 8 lines
Quoted 2 lines

If this is the actually intended behavior here, it is a fix and should carry a Fixes tag (the message below implies that).

Yeah, yesterday I was thinking about the same and I was confused by the logic behind. As far as I understood the comments regarding mode provided by DT is that the mode is configured in IP and may not be changed. And you are right about the fix, but let's wait for Microchip to elaborate on the expected behaviour.

Quoted 4 lines

Prajna is on holiday and I don't have a setup to actually test this on, but I'm 99% sure that you're both right and the original behaviour was wrong. There's a verilog parameter to the IP block that determines which motorola mode it is and a device that's not an exact match won't work.

Okay, let's not hurry up with this and wait for testing results.

Quoted 6 lines

FWIW:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
                dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
                return -EINVAL;
        }

Thanks for the review!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#7 lore raw

Re: [PATCH v4 2/2] spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

Quoted 31 lines · Andy Shevchenko

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

Quoted 4 lines · Conor Dooley
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Jonas Gorski

On Fri, Nov 28, 2025 at 7:56 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 14 lines
Quoted 7 lines
Quoted 7 lines

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_setup(struct spi_device *spi) · line 164

Quoted 2 lines
drivers/spi/spi-microchip-core-spi.c +1 -1
-       if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
+       if ((spi->mode ^ spi->controller->mode_bits) & SPI_MODE_X_MASK) {

This changes the behavior: if a bit isn't set in spi->mode that is set in mode_bits, it would have been previously accepted, now it's refused. E.g. controller has (SPI_CPOL | SPI_CPHA), device only SPI_CPOL. 0x1 & 0x3 & ~0x3 => 0, vs (0x1 ^ 0x3) & 0x3 => 0x2

Quoted 7 lines
Quoted 2 lines

If this is the actually intended behavior here, it is a fix and should carry a Fixes tag (the message below implies that).

Yeah, yesterday I was thinking about the same and I was confused by the logic behind. As far as I understood the comments regarding mode provided by DT is that the mode is configured in IP and may not be changed. And you are right about the fix, but let's wait for Microchip to elaborate on the expected behaviour.

Quoted 4 lines

Prajna is on holiday and I don't have a setup to actually test this on, but I'm 99% sure that you're both right and the original behaviour was wrong. There's a verilog parameter to the IP block that determines which motorola mode it is and a device that's not an exact match won't work.

Okay, let's not hurry up with this and wait for testing results.

Quoted 2 lines

FWIW:

Quoted 10 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
                 dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
                 return -EINVAL;
         }

Thanks for the review!

--
With Best Regards,
Andy Shevchenko

Hi,

I've tested this on my setup and XOR check matches how the controller behaves. The SPI mode is fixed in hardware, so the previous logic was wrong.

#8 lore raw

Re: [PATCH v4 2/2] spi: microchip-core: use XOR instead of ANDNOT to simplify the logic

Quoted 6 lines · Prajna Rajendra Kumar
Quoted 5 lines · Andy Shevchenko
Quoted 4 lines · Conor Dooley
Quoted 3 lines · Andy Shevchenko
Quoted 2 lines · Jonas Gorski

On Fri, Nov 28, 2025 at 7:56 PM Andy Shevchenko
<[email protected]> wrote:

...

Quoted 25 lines
Quoted 25 lines
Quoted 14 lines
Quoted 7 lines
Quoted 7 lines

drivers/spi/spi-microchip-core-spi.c static int mchp_corespi_setup(struct spi_device *spi) · line 164

Quoted 2 lines
drivers/spi/spi-microchip-core-spi.c +1 -1
-       if (spi->mode & SPI_MODE_X_MASK & ~spi->controller->mode_bits) {
+       if ((spi->mode ^ spi->controller->mode_bits) & SPI_MODE_X_MASK) {

This changes the behavior: if a bit isn't set in spi->mode that is set in mode_bits, it would have been previously accepted, now it's refused. E.g. controller has (SPI_CPOL | SPI_CPHA), device only SPI_CPOL. 0x1 & 0x3 & ~0x3 => 0, vs (0x1 ^ 0x3) & 0x3 => 0x2

Quoted 7 lines
Quoted 2 lines

If this is the actually intended behavior here, it is a fix and should carry a Fixes tag (the message below implies that).

Yeah, yesterday I was thinking about the same and I was confused by the logic behind. As far as I understood the comments regarding mode provided by DT is that the mode is configured in IP and may not be changed. And you are right about the fix, but let's wait for Microchip to elaborate on the expected behaviour.

Quoted 4 lines

Prajna is on holiday and I don't have a setup to actually test this on, but I'm 99% sure that you're both right and the original behaviour was wrong. There's a verilog parameter to the IP block that determines which motorola mode it is and a device that's not an exact match won't work.

Okay, let's not hurry up with this and wait for testing results.

Quoted 5 lines

FWIW:

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
                 dev_err(&spi->dev, "incompatible CPOL/CPHA, must match controller's Motorola mode\n");
                 return -EINVAL;
         }
Quoted 7 lines
Quoted 1 line

Thanks for the review!

I've tested this on my setup and XOR check matches how the controller behaves. The SPI mode is fixed in hardware, so the previous logic was wrong.

Thank you! I just sent a new version of this patch with updated tags and commit message.

Signature · 2 lines
With Best Regards,
Andy Shevchenko