Skip to contentSkip to conversation

i2c: at91: Read all available bytes at once

PATCH Superseded David Engraf 3 participants 9 messages Apr 18, 2018 – Apr 30, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 i2c: at91: Read all available bytes at once

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

---
 drivers/i2c/busses/i2c-at91.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91.c +6 -2
index bfd1fdff64a9..d01c2b2384bd 100644
@@ -518,8 +518,12 @@ static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
 	 * the RXRDY interrupt first in order to not keep garbage data in the
 	 * Receive Holding Register for the next transfer.
 	 */
-	if (irqstatus & AT91_TWI_RXRDY)
-		at91_twi_read_next_byte(dev);
+	if (irqstatus & AT91_TWI_RXRDY) {
+		/* read all available bytes at once when FIFO is used */
+		do {
+			at91_twi_read_next_byte(dev);
+		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);
+	}
 
 	/*
 	 * When a NACK condition is detected, the I2C controller sets the NACK,
Signature · 1 line
2.14.1
#1 lore raw

Hi David,

Quoted 14 lines · David Engraf

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

---
 drivers/i2c/busses/i2c-at91.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91.c +0 -0
index bfd1fdff64a9..d01c2b2384bd 100644

drivers/i2c/busses/i2c-at91.c static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id) · line 525

Quoted 10 lines
drivers/i2c/busses/i2c-at91.c +5 -2
@@ -518,8 +518,12 @@ static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
 	 * the RXRDY interrupt first in order to not keep garbage data in the
 	 * Receive Holding Register for the next transfer.
 	 */
-	if (irqstatus & AT91_TWI_RXRDY)
-		at91_twi_read_next_byte(dev);
+	if (irqstatus & AT91_TWI_RXRDY) {
+		/* read all available bytes at once when FIFO is used */
+		do {
+			at91_twi_read_next_byte(dev);
+		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);

You can avoid this check by using the RXFL field to know the number of data you can read. Did you try to use it? If yes, did you notice some issues?

Regards

Ludovic

Quoted 7 lines
drivers/i2c/busses/i2c-at91.c +1 -0
+	}
 
 	/*
 	 * When a NACK condition is detected, the I2C controller sets the NACK,
Signature · 1 line
2.14.1
#2 lore raw

Hi Ludovic,

Am 25.04.2018 um 17:08 schrieb Ludovic Desroches:

Quoted 22 lines

Hi David,

Quoted 19 lines · David Engraf

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

---
  drivers/i2c/busses/i2c-at91.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91.c +0 -2
index bfd1fdff64a9..d01c2b2384bd 100644
@@ -518,8 +518,12 @@ static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
  	 * the RXRDY interrupt first in order to not keep garbage data in the
  	 * Receive Holding Register for the next transfer.
  	 */
-	if (irqstatus & AT91_TWI_RXRDY)
-		at91_twi_read_next_byte(dev);
Quoted 8 lines

drivers/i2c/busses/i2c-at91.c static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id) · line 525

Quoted 5 lines
drivers/i2c/busses/i2c-at91.c +5 -0
+	if (irqstatus & AT91_TWI_RXRDY) {
+		/* read all available bytes at once when FIFO is used */
+		do {
+			at91_twi_read_next_byte(dev);
+		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);

You can avoid this check by using the RXFL field to know the number of data you can read. Did you try to use it? If yes, did you notice some issues?

I did a quick test by reading RXFL and it worked as well but I decided to use the more readable solution by polling RXRDY. Also I don't need to check if the FIFO has been enabled.

If you prefer using RXFL I can create a new patch.

Best regards
- David

Quoted 11 lines

Regards

Ludovic

Quoted 7 lines
drivers/i2c/busses/i2c-at91.c +1 -0
+	}
 
  	/*
  	 * When a NACK condition is detected, the I2C controller sets the NACK,
Signature · 1 line
2.14.1
#3 lore raw

Hi David,

Quoted 34 lines · David Engraf

Hi Ludovic,

Am 25.04.2018 um 17:08 schrieb Ludovic Desroches:

Quoted 22 lines

Hi David,

Quoted 19 lines · David Engraf

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

---
  drivers/i2c/busses/i2c-at91.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91.c +0 -2
index bfd1fdff64a9..d01c2b2384bd 100644
@@ -518,8 +518,12 @@ static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
  	 * the RXRDY interrupt first in order to not keep garbage data in the
  	 * Receive Holding Register for the next transfer.
  	 */
-	if (irqstatus & AT91_TWI_RXRDY)
-		at91_twi_read_next_byte(dev);
Quoted 8 lines

drivers/i2c/busses/i2c-at91.c static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id) · line 525

Quoted 5 lines
drivers/i2c/busses/i2c-at91.c +5 -0
+	if (irqstatus & AT91_TWI_RXRDY) {
+		/* read all available bytes at once when FIFO is used */
+		do {
+			at91_twi_read_next_byte(dev);
+		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);

You can avoid this check by using the RXFL field to know the number of data you can read. Did you try to use it? If yes, did you notice some issues?

Quoted 5 lines

I did a quick test by reading RXFL and it worked as well but I decided to use the more readable solution by polling RXRDY. Also I don't need to check if the FIFO has been enabled.

If you prefer using RXFL I can create a new patch.

Honestly, I have no strong opinion about it. As you said you approach is simple and easy to read.

About performances, I assume that both solutions are pretty the same for small number of data. If the number increases, using the RXFL field should give better results.

So I would say, maybe add a note in the commit log or in the code to keep in mind there is this solution to go further.

Otherwise

Regards

Ludovic

Quoted 16 lines

Best regards
- David

Quoted 10 lines

Regards

Ludovic

Quoted 6 lines
drivers/i2c/busses/i2c-at91.c +1 -0
+	}
  	/*
  	 * When a NACK condition is detected, the I2C controller sets the NACK,
Signature · 1 line
2.14.1
#4 lore raw

PATCH v2 1/1 i2c: at91: Read all available bytes at once

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

This patch polls RXRDY and reads all available bytes at once.

---
 drivers/i2c/busses/i2c-at91.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91.c +10 -2
index bfd1fdff64a9..9caee5b79eac 100644
@@ -518,8 +518,16 @@ static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
 	 * the RXRDY interrupt first in order to not keep garbage data in the
 	 * Receive Holding Register for the next transfer.
 	 */
-	if (irqstatus & AT91_TWI_RXRDY)
-		at91_twi_read_next_byte(dev);
+	if (irqstatus & AT91_TWI_RXRDY) {
+		/*
+		 * Read all available bytes at once by polling RXRDY usable w/ and w/o
+		 * FIFO. With FIFO enabled we could also read RXFL and avoid polling
+		 * RXRDY.
+		 */
+		do {
+			at91_twi_read_next_byte(dev);
+		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);
+	}
 
 	/*
 	 * When a NACK condition is detected, the I2C controller sets the NACK,
Signature · 1 line
2.14.1
Acked-by on v2 1/1
#5 lore raw
Quoted 8 lines · David Engraf

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

This patch polls RXRDY and reads all available bytes at once.

Thanks a lot David.

Regards

Ludovic

Quoted 30 lines
---
 drivers/i2c/busses/i2c-at91.c | 12 ++++++++++--
 1 file changed, 10 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91.c +10 -2
index bfd1fdff64a9..9caee5b79eac 100644
@@ -518,8 +518,16 @@ static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
 	 * the RXRDY interrupt first in order to not keep garbage data in the
 	 * Receive Holding Register for the next transfer.
 	 */
-	if (irqstatus & AT91_TWI_RXRDY)
-		at91_twi_read_next_byte(dev);
+	if (irqstatus & AT91_TWI_RXRDY) {
+		/*
+		 * Read all available bytes at once by polling RXRDY usable w/ and w/o
+		 * FIFO. With FIFO enabled we could also read RXFL and avoid polling
+		 * RXRDY.
+		 */
+		do {
+			at91_twi_read_next_byte(dev);
+		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);
+	}
 
 	/*
 	 * When a NACK condition is detected, the I2C controller sets the NACK,
Signature · 1 line
2.14.1
Applied
#6 lore raw
In reply to David Engraf
Quoted 8 lines · David Engraf

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

This patch polls RXRDY and reads all available bytes at once.

checkpatch said twice:

WARNING: line over 80 characters

While I am not super-strict with this limit, it makes sense here IMO. The comment stays readable, and we don't even lose a line.

Fixed it this time for you.

Applied to for-next, thanks!

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180428/2eee6e2b/attachment.sig>

#7 lore raw

Am 28.04.2018 um 14:38 schrieb Wolfram Sang:

Quoted 8 lines · David Engraf
Quoted 7 lines

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

This patch polls RXRDY and reads all available bytes at once.

Quoted 8 lines
Quoted 1 line

checkpatch said twice:

WARNING: line over 80 characters

While I am not super-strict with this limit, it makes sense here IMO. The comment stays readable, and we don't even lose a line.

Sorry for that.

Quoted 1 line

Fixed it this time for you.

Thanks
- David

Quoted 2 lines

Applied to for-next, thanks!

#8 lore raw
In reply to Ludovic Desroches

Hi Ludovic,

Am 25.04.2018 um 17:08 schrieb Ludovic Desroches:

Quoted 22 lines

Hi David,

Quoted 19 lines · David Engraf

With FIFO enabled it is possible to read multiple bytes at once in the interrupt handler as long as RXRDY is set. This may also reduce the number of interrupts.

---
  drivers/i2c/busses/i2c-at91.c | 8 ++++++--
  1 file changed, 6 insertions(+), 2 deletions(-)
drivers/i2c/busses/i2c-at91.c +0 -2
index bfd1fdff64a9..d01c2b2384bd 100644
@@ -518,8 +518,12 @@ static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id)
  	 * the RXRDY interrupt first in order to not keep garbage data in the
  	 * Receive Holding Register for the next transfer.
  	 */
-	if (irqstatus & AT91_TWI_RXRDY)
-		at91_twi_read_next_byte(dev);
Quoted 8 lines

drivers/i2c/busses/i2c-at91.c static irqreturn_t atmel_twi_interrupt(int irq, void *dev_id) · line 525

Quoted 5 lines
drivers/i2c/busses/i2c-at91.c +5 -0
+	if (irqstatus & AT91_TWI_RXRDY) {
+		/* read all available bytes at once when FIFO is used */
+		do {
+			at91_twi_read_next_byte(dev);
+		} while (at91_twi_read(dev, AT91_TWI_SR) & AT91_TWI_RXRDY);

You can avoid this check by using the RXFL field to know the number of data you can read. Did you try to use it? If yes, did you notice some issues?

I did a quick test by reading RXFL and it worked as well but I decided to use the more readable solution by polling RXRDY. Also I don't need to check if the FIFO has been enabled.

If you prefer using RXFL I can create a new patch.

Best regards
- David

Quoted 11 lines

Regards

Ludovic

Quoted 7 lines
drivers/i2c/busses/i2c-at91.c +1 -0
+	}
 
  	/*
  	 * When a NACK condition is detected, the I2C controller sets the NACK,
Signature · 1 line
2.14.1
Signature · 23 lines
Mit freundlichen Gr??en/Best regards,

David Engraf
Product Engineer

SYSGO AG
Office Mainz
Am Pfaffenstein 14 / D-55270 Klein-Winternheim / Germany

Phone: +49-6136-9948-0 / Fax: +49-6136-9948-10
E-mail: david.engraf at sysgo.com
_________________________________________________________________________________

Web: https://www.sysgo.com
Blog: https://www.sysgo.com/blog
Events: https://www.sysgo.com/events
Newsletter: https://www.sysgo.com/newsletter
_________________________________________________________________________________

Handelsregister/Commercial Registry: HRB Mainz 90 HRB 8066
Vorstand/Executive Board: Etienne Butery (CEO), Kai Sablotny (COO)
Aufsichtsratsvorsitzender/Supervisory Board Chairman: Marc Darmon
USt-Id-Nr./VAT-Id-No.: DE 149062328