Skip to contentSkip to conversation

spi: spi-mem: ease checks in dtr_supports_op()

PATCH In review Dhruva Gole 2 participants 5 messages Oct 25, 2022 – Nov 11, 2022 SPI

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 spi: spi-mem: ease checks in dtr_supports_op()

Remove the extra conditions that cause some cases to fail prematurely like if the data number of bytes is odd. The controller can handle odd number of bytes data read in DTR Mode. Don't fail supports op for this condition. This change can also be justified by taking a look at the equivalent code in the linux kernel (v6.0.3), in drivers/spi/spi-mem.c, where such an even number of data bytes check is absent as well. The presence of this even byte check causes supports op failure even if the controller can indeed work in case of odd bytes data reads in DTR (Dual Transfer Rate) mode in xSPI.
There have not been any sort of major bugs in the absence of this particular supports_op check, so it is safe to discard this check from here.

---
v1 of the patch had a relatively shorter commit body that did not sufficiently describe and justify this patch. link to v1:
https://lore.kernel.org/u-boot/[email protected]/

v2 has just updated the commit body while preserving the code changes from earlier v1 patch. This tries to address the changes requested from Jagan Teki.

 drivers/spi/spi-mem.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/spi/spi-mem.c +0 -4
index 8e8995fc537f..eecc13bec90d 100644
@@ -181,10 +181,6 @@ bool spi_mem_dtr_supports_op(struct spi_slave *slave,
 	if (op->dummy.nbytes && op->dummy.buswidth == 8 && op->dummy.nbytes % 2)
 		return false;
 
-	if (op->data.dir != SPI_MEM_NO_DATA &&
-	    op->dummy.buswidth == 8 && op->data.nbytes % 2)
-		return false;
-
 	return spi_mem_check_buswidth(slave, op);
 }
 EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
Signature · 1 line
2.25.1
#1 lore raw

Hi,

A gentle reminder to review the following patch:

spi: spi-mem: ease checks in dtr_supports_op()

https://lore.kernel.org/u-boot/[email protected]/

Kindly merge if it looks okay.

Quoted 40 lines · Dhruva Gole

Remove the extra conditions that cause some cases to fail prematurely like if the data number of bytes is odd. The controller can handle odd number of bytes data read in DTR Mode. Don't fail supports op for this condition. This change can also be justified by taking a look at the equivalent code in the linux kernel (v6.0.3), in drivers/spi/spi-mem.c, where such an even number of data bytes check is absent as well. The presence of this even byte check causes supports op failure even if the controller can indeed work in case of odd bytes data reads in DTR (Dual Transfer Rate) mode in xSPI.
There have not been any sort of major bugs in the absence of this particular supports_op check, so it is safe to discard this check from here.

---
v1 of the patch had a relatively shorter commit body that did not sufficiently describe and justify this patch. link to v1:
https://lore.kernel.org/u-boot/[email protected]/

v2 has just updated the commit body while preserving the code changes from earlier v1 patch. This tries to address the changes requested from Jagan Teki.

  drivers/spi/spi-mem.c | 4 ----
  1 file changed, 4 deletions(-)
drivers/spi/spi-mem.c +0 -4
index 8e8995fc537f..eecc13bec90d 100644
@@ -181,10 +181,6 @@ bool spi_mem_dtr_supports_op(struct spi_slave *slave,
  	if (op->dummy.nbytes && op->dummy.buswidth == 8 && op->dummy.nbytes % 2)
  		return false;
 
-	if (op->data.dir != SPI_MEM_NO_DATA &&
-	    op->dummy.buswidth == 8 && op->data.nbytes % 2)
-		return false;
-
  	return spi_mem_check_buswidth(slave, op);
  }
  EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
Signature · 3 lines
Best regards,
Dhruva Gole
Texas Instruments Incorporated
#2 lore raw
In reply to Dhruva Gole

Hi Dhruva :-)

Quoted 2 lines · Dhruva Gole

Remove the extra conditions that cause some cases to fail prematurely like if the data number of bytes is odd. The controller can handle odd

First, some background on why I added this in the first place. In 1S-1S-1S mode, it takes 8 cycles to send a byte. So regardless of how many bytes you transmit in a transaction, the number of cycles will always be a whole number (in fact, it will be a multiple of 8). Similarly, in 2S-2S-2S mode it takes 4 cycles, in 8S-8S-8S 1 cycle.

But in 8D-8D-8D mode, you transmit a byte in just half a cycle. So the question becomes whether you can ever transmit an odd number of bytes in 8D-8D-8D mode. I have not seen any official document/spec explicitly say this, but to me having half a cycle in the data phase does not make sense. Your transaction should have a whole number of cycles, which means you will always transmit an even number of bytes.

This is why I added this check. Transactions of odd length do not make sense for 8D-8D-8D mode. I have looked at multiple 8D-8D-8D flashes, and all of them forbid 3 byte addressing and odd length data transfers in 8D mode.

Quoted 1 line

number of bytes data read in DTR Mode. Don't fail supports op for this

You should mention which controller. There is more than one out there. And not all of them work the same.

I assume you are talking about the Cadence QSPI controller, since that is what TI chips use. In that case, I have heard from HW folks that it does support odd number of bytes, but when I experimented with it a bit I found out that you can program it to write an odd number of bytes in Octal DTR mode, but it actually ends up writing an extra byte at the end. So it would seem like it does not _actually_ support odd number of bytes.

But that is beside the point anyway IMO. It does not matter much what one specific controller can do. We should stick to what the protocol allows, which does not include odd length transactions.

Quoted 3 lines

condition. This change can also be justified by taking a look at the equivalent code in the linux kernel (v6.0.3), in drivers/spi/spi-mem.c, where such an even number of data bytes check is absent as well.

Sorry, that is not a good justification. Linux code is not the golden source of truth. It can be wrong too. In fact, I'd suggest you add this check there as well.

Quoted 3 lines

The presence of this even byte check causes supports op failure even if the controller can indeed work in case of odd bytes data reads in DTR (Dual Transfer Rate) mode in xSPI.

As I mentioned above, that assertion from TI's HW folks seems incorrect to me. Plus, I am yet to see a flash that can do odd byte reads in Octal DTR mode. Both the flashes TI uses for their boards, Cypress S28 and Micron MT35, do not allow this. Do you have a new flash that does support this feature? If yes, can you please give a link to its datasheet?

Quoted 21 lines

There have not been any sort of major bugs in the absence of this particular supports_op check, so it is safe to discard this check from here.

---
v1 of the patch had a relatively shorter commit body that did not sufficiently describe and justify this patch. link to v1:
https://lore.kernel.org/u-boot/[email protected]/

v2 has just updated the commit body while preserving the code changes from earlier v1 patch. This tries to address the changes requested from Jagan Teki.

 drivers/spi/spi-mem.c | 4 ----
 1 file changed, 4 deletions(-)
drivers/spi/spi-mem.c +0 -0
index 8e8995fc537f..eecc13bec90d 100644

drivers/spi/spi-mem.c bool spi_mem_dtr_supports_op(struct spi_slave *slave, · line 185

Quoted 5 lines
drivers/spi/spi-mem.c +0 -2
@@ -181,10 +181,6 @@ bool spi_mem_dtr_supports_op(struct spi_slave *slave,
 	if (op->dummy.nbytes && op->dummy.buswidth == 8 && op->dummy.nbytes % 2)
 		return false;
 
-	if (op->data.dir != SPI_MEM_NO_DATA &&
-	    op->dummy.buswidth == 8 && op->data.nbytes % 2)

Hmm, this should have been op->data.buswidth instead. That would be a welcome fix indeed :-)

You can also put my explanation for why this exists in a comment here so other people reading this code can also get to know this.

Quoted 8 lines
drivers/spi/spi-mem.c +0 -2
-		return false;
-
 	return spi_mem_check_buswidth(slave, op);
 }
 EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
Signature · 1 line
2.25.1
Signature · 2 lines
Regards,
Pratyush Yadav
#3 lore raw

Hey Pratyush,

Quoted 23 lines · Pratyush Yadav

Hi Dhruva :-)

Quoted 2 lines · Dhruva Gole

Remove the extra conditions that cause some cases to fail prematurely like if the data number of bytes is odd. The controller can handle odd

First, some background on why I added this in the first place. In 1S-1S-1S mode, it takes 8 cycles to send a byte. So regardless of how many bytes you transmit in a transaction, the number of cycles will always be a whole number (in fact, it will be a multiple of 8). Similarly, in 2S-2S-2S mode it takes 4 cycles, in 8S-8S-8S 1 cycle.

But in 8D-8D-8D mode, you transmit a byte in just half a cycle. So the question becomes whether you can ever transmit an odd number of bytes in 8D-8D-8D mode. I have not seen any official document/spec explicitly say this, but to me having half a cycle in the data phase does not make sense. Your transaction should have a whole number of cycles, which means you will always transmit an even number of bytes.

This is why I added this check. Transactions of odd length do not make sense for 8D-8D-8D mode. I have looked at multiple 8D-8D-8D flashes, and all of them forbid 3 byte addressing and odd length data transfers in 8D mode.

Quoted 5 lines
Quoted 1 line

number of bytes data read in DTR Mode. Don't fail supports op for this

You should mention which controller. There is more than one out there. And not all of them work the same.

I assume you are talking about the Cadence QSPI controller, since that

Yes, your assumption is correct. I am talking about the CQSPI Controller.

Quoted 6 lines

is what TI chips use. In that case, I have heard from HW folks that it does support odd number of bytes, but when I experimented with it a bit I found out that you can program it to write an odd number of bytes in Octal DTR mode, but it actually ends up writing an extra byte at the end. So it would seem like it does not _actually_ support odd number of bytes.

I agree, it does not support odd number of byte read writes, what I meant was it does handle it by as you said padding that extra byte at the end. This extra byte can always be disregarded at a later stage.

Quoted 4 lines

But that is beside the point anyway IMO. It does not matter much what one specific controller can do. We should stick to what the protocol allows, which does not include odd length transactions.

True, the protocol ideally can not support odd byte transactions.

Quoted 7 lines
Quoted 4 lines

condition. This change can also be justified by taking a look at the equivalent code in the linux kernel (v6.0.3), in drivers/spi/spi-mem.c, where such an even number of data bytes check is absent as well.

Sorry, that is not a good justification. Linux code is not the golden source of truth. It can be wrong too. In fact, I'd suggest you add this check there as well.

Okay, I was under the impression that U-Boot closely follows linux source and hence thought

this along with the other points that I am making to be a valid justification.

Quoted 7 lines
Quoted 4 lines

The presence of this even byte check causes supports op failure even if the controller can indeed work in case of odd bytes data reads in DTR (Dual Transfer Rate) mode in xSPI.

As I mentioned above, that assertion from TI's HW folks seems incorrect to me. Plus, I am yet to see a flash that can do odd byte reads in Octal DTR mode.

No, a flash does not support an odd bytes read (yet to my knowledge). The controller while finally doing the transaction does infact issue an even byte read.

Quoted 4 lines

Both the flashes TI uses for their boards, Cypress S28 and Micron MT35, do not allow this. Do you have a new flash that does support this feature? If yes, can you please give a link to its datasheet?

Again, how to handle an odd byte read should be taken care of by the controller in my opinion and hence the check feels strict to me to be in spi-mem. The controller can always read an extra byte (or fail op if it doesn't).

Quoted 25 lines
Quoted 25 lines

There have not been any sort of major bugs in the absence of this particular supports_op check, so it is safe to discard this check from here.

---
v1 of the patch had a relatively shorter commit body that did not sufficiently describe and justify this patch. link to v1:
https://lore.kernel.org/u-boot/[email protected]/

v2 has just updated the commit body while preserving the code changes from earlier v1 patch. This tries to address the changes requested from Jagan Teki.

  drivers/spi/spi-mem.c | 4 ----
  1 file changed, 4 deletions(-)
drivers/spi/spi-mem.c +0 -0
index 8e8995fc537f..eecc13bec90d 100644
@@ -181,10 +181,6 @@ bool spi_mem_dtr_supports_op(struct spi_slave *slave,
  	if (op->dummy.nbytes && op->dummy.buswidth == 8 && op->dummy.nbytes % 2)
  		return false;
 
Quoted 4 lines

drivers/spi/spi-mem.c bool spi_mem_dtr_supports_op(struct spi_slave *slave, · line 185

Quoted 2 lines
drivers/spi/spi-mem.c +0 -2
-	if (op->data.dir != SPI_MEM_NO_DATA &&
-	    op->dummy.buswidth == 8 && op->data.nbytes % 2)

Hmm, this should have been op->data.buswidth instead. That would be a welcome fix indeed :-)

Noted,

Quoted 11 lines

You can also put my explanation for why this exists in a comment here so other people reading this code can also get to know this.

Quoted 7 lines
drivers/spi/spi-mem.c +0 -2
-		return false;
-
  	return spi_mem_check_buswidth(slave, op);
  }
  EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
Signature · 1 line
2.25.1

In the end, what I feel is that this is a policy decision that which check should be where.

And it is my opinion that the controller drivers should be given the power to decide

what to do with the odd byte transactions (read extra byte or return ERRORs) whatever

they feel is suitable. I don't think spi-mem should care so much since it is still

at a pretty higher level.

Another idea: Don't fail for odd byte reads/writes (in DTR mode) but rather make the data len even

and then disregard the extra byte later on in spi-mem itself. Let me know if this

sounds better to you rather than simply returning false here.

Signature · 2 lines
Regards,
Dhruva Gole<[email protected]>
#4 lore raw
Quoted 39 lines · Dhruva

Hey Pratyush,

Quoted 23 lines · Pratyush Yadav

Hi Dhruva :-)

Quoted 2 lines · Dhruva Gole

Remove the extra conditions that cause some cases to fail prematurely like if the data number of bytes is odd. The controller can handle odd

First, some background on why I added this in the first place. In 1S-1S-1S mode, it takes 8 cycles to send a byte. So regardless of how many bytes you transmit in a transaction, the number of cycles will always be a whole number (in fact, it will be a multiple of 8). Similarly, in 2S-2S-2S mode it takes 4 cycles, in 8S-8S-8S 1 cycle.

But in 8D-8D-8D mode, you transmit a byte in just half a cycle. So the question becomes whether you can ever transmit an odd number of bytes in 8D-8D-8D mode. I have not seen any official document/spec explicitly say this, but to me having half a cycle in the data phase does not make sense. Your transaction should have a whole number of cycles, which means you will always transmit an even number of bytes.

This is why I added this check. Transactions of odd length do not make sense for 8D-8D-8D mode. I have looked at multiple 8D-8D-8D flashes, and all of them forbid 3 byte addressing and odd length data transfers in 8D mode.

Quoted 5 lines
Quoted 1 line

number of bytes data read in DTR Mode. Don't fail supports op for this

You should mention which controller. There is more than one out there. And not all of them work the same.

I assume you are talking about the Cadence QSPI controller, since that

Yes, your assumption is correct. I am talking about the CQSPI Controller.

Quoted 6 lines

is what TI chips use. In that case, I have heard from HW folks that it does support odd number of bytes, but when I experimented with it a bit I found out that you can program it to write an odd number of bytes in Octal DTR mode, but it actually ends up writing an extra byte at the end. So it would seem like it does not _actually_ support odd number of bytes.

Quoted 3 lines

I agree, it does not support odd number of byte read writes, what I meant was it does handle it by as you said padding that extra byte at the end. This extra byte can always be disregarded at a later stage.

Not always. On writes the extra byte goes to the flash. We do not want to write an undefined value there.

Quoted 12 lines
Quoted 5 lines

But that is beside the point anyway IMO. It does not matter much what one specific controller can do. We should stick to what the protocol allows, which does not include odd length transactions.

True, the protocol ideally can not support odd byte transactions.

Quoted 4 lines
Quoted 4 lines

condition. This change can also be justified by taking a look at the equivalent code in the linux kernel (v6.0.3), in drivers/spi/spi-mem.c, where such an even number of data bytes check is absent as well.

Quoted 7 lines
Quoted 3 lines

Sorry, that is not a good justification. Linux code is not the golden source of truth. It can be wrong too. In fact, I'd suggest you add this check there as well.

Okay, I was under the impression that U-Boot closely follows linux source and hence thought

this along with the other points that I am making to be a valid justification.

It usually does but I still think just saying "Linux does not do it" is not a strong enough justification.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

The presence of this even byte check causes supports op failure even if the controller can indeed work in case of odd bytes data reads in DTR (Dual Transfer Rate) mode in xSPI.

Quoted 7 lines
Quoted 3 lines

As I mentioned above, that assertion from TI's HW folks seems incorrect to me. Plus, I am yet to see a flash that can do odd byte reads in Octal DTR mode.

No, a flash does not support an odd bytes read (yet to my knowledge). The controller while finally doing the transaction does infact issue an even byte read.

Reading an extra byte is not so bad, but writing an extra byte is. That is where we need to be more careful.

Quoted 10 lines
Quoted 5 lines

Both the flashes TI uses for their boards, Cypress S28 and Micron MT35, do not allow this. Do you have a new flash that does support this feature? If yes, can you please give a link to its datasheet?

Again, how to handle an odd byte read should be taken care of by the controller in my opinion and hence the check feels strict to me to be in spi-mem. The controller can always read an extra byte (or fail op if it doesn't).

I think it should be the opposite. SPI MEM should to sanity check the op and only pass an op to the controllers that is actually valid by the protocol. The controller can then do extra checks for things it specifically does not support.

If we do not do this then each controller driver would have to implement this same logic of rejecting these bogus ops and some are bound to get it wrong. And this is just the default supports_op hook, if your controller can roll its own if absolutely necessary. This is supposed to cover the general case.

Quoted 34 lines
Quoted 18 lines
Quoted 18 lines

There have not been any sort of major bugs in the absence of this particular supports_op check, so it is safe to discard this check from here.

---
v1 of the patch had a relatively shorter commit body that did not sufficiently describe and justify this patch. link to v1:
https://lore.kernel.org/u-boot/[email protected]/

v2 has just updated the commit body while preserving the code changes from earlier v1 patch. This tries to address the changes requested from Jagan Teki.

  drivers/spi/spi-mem.c | 4 ----
  1 file changed, 4 deletions(-)
Quoted 11 lines

drivers/spi/spi-mem.c bool spi_mem_dtr_supports_op(struct spi_slave *slave, · line 184

Quoted 9 lines
drivers/spi/spi-mem.c +0 -2
index 8e8995fc537f..eecc13bec90d 100644
@@ -181,10 +181,6 @@ bool spi_mem_dtr_supports_op(struct spi_slave *slave,
  	if (op->dummy.nbytes && op->dummy.buswidth == 8 && op->dummy.nbytes % 2)
  		return false;
-	if (op->data.dir != SPI_MEM_NO_DATA &&
-	    op->dummy.buswidth == 8 && op->data.nbytes % 2)

Hmm, this should have been op->data.buswidth instead. That would be a welcome fix indeed :-)

Noted,

Quoted 4 lines

You can also put my explanation for why this exists in a comment here so other people reading this code can also get to know this.

Quoted 13 lines
Quoted 7 lines
Quoted 7 lines
drivers/spi/spi-mem.c +0 -2
-		return false;
-
  	return spi_mem_check_buswidth(slave, op);
  }
  EXPORT_SYMBOL_GPL(spi_mem_dtr_supports_op);
Signature · 1 line
2.25.1

In the end, what I feel is that this is a policy decision that which check should be where. And it is my opinion that the controller drivers should be given the power to decide what to do with the odd byte transactions (read extra byte or return ERRORs) whatever they feel is suitable. I don't think spi-mem should care so much since it is still at a pretty higher level.

If your controller can do weird stuff then it can roll its own supports_op hook. The default should serve the general case.

But I think we should actually talk about the problem you are trying to solve here. Do you want to allow userspace (or in U-Boot's case, the sf command) to write odd number of bytes? Then in that case I would suggest you look at this [0] series I sent some time ago for Linux. That teaches SPI NOR to pad reads and writes with extra bytes. Does it solve the problem you are looking to solve?

Quoted 4 lines

Another idea: Don't fail for odd byte reads/writes (in DTR mode) but rather make the data len even and then disregard the extra byte later on in spi-mem itself. Let me know if this sounds better to you rather than simply returning false here.

SPI MEM does not have enough information to make the right decision. For example, what value do you pad a write with? The answer can be different depending on if you are writing to a register or the main storage area. Do you read an extra byte at the start or at the end? The answer can depend on the flash size. Only the upper layer like SPI NOR can know this. So I think the logic to pad reads or writes should lie in upper layers.

[0] https://patchwork.ozlabs.org/project/linux-mtd/list/?series=246518&state=*

Signature · 2 lines
Regards,
Pratyush Yadav