Skip to contentSkip to conversation

at24: Support SMBus read/write of 16-bit devices

PATCH Superseded Aaron Sierra 2 participants 3 messages Nov 10, 2015 1 message not in the archive

Subsystems (2)

v4 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH v4 1/1 at24: Support SMBus read/write of 16-bit devices

Previously, the at24 driver would bail out in the case of a 16-bit addressable EEPROM attached to an SMBus controller. This is because SMBus block reads and writes don't map to I2C multi-byte reads and writes when the offset portion is 2 bytes.

Instead of bailing out, this patch settles for functioning with single byte read SMBus cycles. Writes can be block or single-byte, depending on SMBus controller features.

Read access is not without some risk. Multiple SMBus cycles are required to read even one byte. If the SMBus has multiple masters and one accesses this EEPROM between the dummy address write and the subsequent current-address-read cycle(s), this driver will receive data from the wrong address.

Functionality has been tested with the following devices:

AT24CM01 attached to Intel ISCH SMBus
AT24C512 attached to Intel I801 SMBus

Read performance:
3.6 KB/s with 32-byte* access

*limited to 32-bytes by I2C_SMBUS_BLOCK_MAX.

Write performance:
248 B/s with 1-byte page (default)
3.9 KB/s with 128-byte* page (via platform data)

*limited to 31-bytes by I2C_SMBUS_BLOCK_MAX - 1.

---
v2 - Account for changes related to introduction of

      i2c_smbus_read_i2c_block_data_or_emulated()

v3 - Consolidate three patches into one
- Expand comments regarding SMBus multi-master read risks.
- Rely on current-address-read for improved read performance (i.e. one dummy address write followed by multiple individual byte reads). This improves performance from 1.4 KiB/s to 3.6 KiB/s.
- Use struct at24_data's writebuf instead of kzalloc-ing
- Only limit write_max by 1-byte when accessing a 16-bit device with block writes instead of attempting to preserve a power-of-two.
- Style fixes (indentation, parentheses, unnecessary masking, etc.)
v4 - Address 16-bit safety in Kconfig
- Set "count" to zero later in at24_smbus_read_block_data()
- Fix over-80-columns issues in at24_eeprom_read()
- Fix write_max off-by-one in at24_probe()
- Check SMBus functionality needed for 16-bit device reads
- Homogenize indentation of SMBus functionality checks for SMBus write

 drivers/misc/eeprom/Kconfig |   5 +-
 drivers/misc/eeprom/at24.c  | 129 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 119 insertions(+), 15 deletions(-)
drivers/misc/eeprom/Kconfig +4 -1
index 04f2e1f..cc0f86c 100644
@@ -22,7 +22,10 @@ config EEPROM_AT24
 
 	  If you use this with an SMBus adapter instead of an I2C adapter,
 	  full functionality is not available.  Only smaller devices are
-	  supported (24c16 and below, max 4 kByte).
+	  supported via block reads (24c16 and below, max 4 kByte).
+	  Larger devices that use 16-bit addresses will only work with
+	  individual byte reads, which is very slow in general and is unsafe
+	  in multi-master SMBus topologies.
 
 	  This driver can also be built as a module.  If so, the module
 	  will be called at24.
drivers/misc/eeprom/at24.c +115 -14

173 lines not on this page · raw message

Signature · 1 line
1.9.1
Reviewed-by on v4 1/1
#2 lore raw

Hi Aaron,

Quoted 52 lines · Aaron Sierra

Previously, the at24 driver would bail out in the case of a 16-bit addressable EEPROM attached to an SMBus controller. This is because SMBus block reads and writes don't map to I2C multi-byte reads and writes when the offset portion is 2 bytes.

Instead of bailing out, this patch settles for functioning with single byte read SMBus cycles. Writes can be block or single-byte, depending on SMBus controller features.

Read access is not without some risk. Multiple SMBus cycles are required to read even one byte. If the SMBus has multiple masters and one accesses this EEPROM between the dummy address write and the subsequent current-address-read cycle(s), this driver will receive data from the wrong address.

Functionality has been tested with the following devices:

AT24CM01 attached to Intel ISCH SMBus
AT24C512 attached to Intel I801 SMBus

Read performance:
3.6 KB/s with 32-byte* access

*limited to 32-bytes by I2C_SMBUS_BLOCK_MAX.

Write performance:
248 B/s with 1-byte page (default)
3.9 KB/s with 128-byte* page (via platform data)

*limited to 31-bytes by I2C_SMBUS_BLOCK_MAX - 1.

---
v2 - Account for changes related to introduction of

      i2c_smbus_read_i2c_block_data_or_emulated()

v3 - Consolidate three patches into one
- Expand comments regarding SMBus multi-master read risks.
- Rely on current-address-read for improved read performance (i.e. one dummy address write followed by multiple individual byte reads). This improves performance from 1.4 KiB/s to 3.6 KiB/s.
- Use struct at24_data's writebuf instead of kzalloc-ing
- Only limit write_max by 1-byte when accessing a 16-bit device with block writes instead of attempting to preserve a power-of-two.
- Style fixes (indentation, parentheses, unnecessary masking, etc.)
v4 - Address 16-bit safety in Kconfig
- Set "count" to zero later in at24_smbus_read_block_data()
- Fix over-80-columns issues in at24_eeprom_read()
- Fix write_max off-by-one in at24_probe()
- Check SMBus functionality needed for 16-bit device reads
- Homogenize indentation of SMBus functionality checks for SMBus write

Quoted 3 lines
 drivers/misc/eeprom/Kconfig |   5 +-
 drivers/misc/eeprom/at24.c  | 129 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 119 insertions(+), 15 deletions(-)

This is a significant addition of code so feel free to add your name at the top of at24.c.

We're almost there:

Quoted 7 lines
drivers/misc/eeprom/at24.c +0 -0
index 5d7c090..3dfd2ed 100644
(...)
@@ -527,10 +608,19 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 

drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 614

Quoted 10 lines
drivers/misc/eeprom/at24.c +4 -4
 	/* Use I2C operations unless we're stuck with SMBus extensions. */
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
-		if (chip.flags & AT24_FLAG_ADDR16)
-			return -EPFNOSUPPORT;
-
-		if (i2c_check_functionality(client->adapter,
+		if ((chip.flags & AT24_FLAG_ADDR16) &&
+		    i2c_check_functionality(client->adapter,
+				I2C_FUNC_SMBUS_READ_BYTE_DATA |
+				I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {

It is I2C_FUNC_SMBUS_READ_BYTE that you use, not
I2C_FUNC_SMBUS_READ_BYTE_DATA.

Quoted 14 lines
drivers/misc/eeprom/at24.c +9 -0
+			/*
+			 * We need SMBUS_WRITE_BYTE_DATA and
+			 * SMBUS_READ_BYTE_DATA to implement byte reads for
+			 * 16-bit address devices. This will be slow, but
+			 * better than nothing (e.g. read @ 3.6 KiB/s). It is
+			 * also unsafe in a multi-master topology.
+			 */
+			use_smbus = I2C_SMBUS_BYTE_DATA;
+		} else if (i2c_check_functionality(client->adapter,
 				I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
 			use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
 		} else if (i2c_check_functionality(client->adapter,
(...)

drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 703

Quoted 8 lines
drivers/misc/eeprom/at24.c +3 -2
@@ -598,8 +698,9 @@ static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id)
 
 			if (write_max > io_limit)
 				write_max = io_limit;
-			if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
-				write_max = I2C_SMBUS_BLOCK_MAX;
+			if (use_smbus && write_max >= I2C_SMBUS_BLOCK_MAX)
+				write_max = I2C_SMBUS_BLOCK_MAX -
+					!!(chip.flags & AT24_FLAG_ADDR16);

Beh. OK, it works, I will admit it's even kind of clever, but it also looks fragile and confusing to some degree. What is wrong with just spelling out the condition explicitly?

			unsigned smbus_limit = (chip.flags & AT24_FLAG_ADDR16) ?
					       I2C_SMBUS_BLOCK_MAX - 1 :
					       I2C_SMBUS_BLOCK_MAX;

			if (use_smbus && write_max > smbus_limit)
				write_max = smbus_limit;

This might not even be slower, and IMHO it is easier to understand.

drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 706

Quoted 3 lines
drivers/misc/eeprom/at24.c +0 -0
 			at24->write_max = write_max;
 
 			/* buffer (data + address at the beginning) */

I have no objection to this patch being merged into the upstream kernel, but ultimately this is Wolfram's call.

Signature · 2 lines
Jean Delvare
SUSE L3 Support
#3 lore raw

----- Original Message -----

Quoted 76 lines

From: "Jean Delvare" <[email protected]>
Sent: Tuesday, November 10, 2015 3:39:20 AM

Hi Aaron,

Quoted 52 lines · Aaron Sierra

Previously, the at24 driver would bail out in the case of a 16-bit addressable EEPROM attached to an SMBus controller. This is because SMBus block reads and writes don't map to I2C multi-byte reads and writes when the offset portion is 2 bytes.

Instead of bailing out, this patch settles for functioning with single byte read SMBus cycles. Writes can be block or single-byte, depending on SMBus controller features.

Read access is not without some risk. Multiple SMBus cycles are required to read even one byte. If the SMBus has multiple masters and one accesses this EEPROM between the dummy address write and the subsequent current-address-read cycle(s), this driver will receive data from the wrong address.

Functionality has been tested with the following devices:

AT24CM01 attached to Intel ISCH SMBus
AT24C512 attached to Intel I801 SMBus

Read performance:
3.6 KB/s with 32-byte* access

*limited to 32-bytes by I2C_SMBUS_BLOCK_MAX.

Write performance:
248 B/s with 1-byte page (default)
3.9 KB/s with 128-byte* page (via platform data)

*limited to 31-bytes by I2C_SMBUS_BLOCK_MAX - 1.

---
v2 - Account for changes related to introduction of

      i2c_smbus_read_i2c_block_data_or_emulated()

v3 - Consolidate three patches into one
- Expand comments regarding SMBus multi-master read risks.
- Rely on current-address-read for improved read performance (i.e. one dummy address write followed by multiple individual byte reads). This improves performance from 1.4 KiB/s to 3.6 KiB/s.
- Use struct at24_data's writebuf instead of kzalloc-ing
- Only limit write_max by 1-byte when accessing a 16-bit device with block writes instead of attempting to preserve a power-of-two.
- Style fixes (indentation, parentheses, unnecessary masking, etc.)
v4 - Address 16-bit safety in Kconfig
- Set "count" to zero later in at24_smbus_read_block_data()
- Fix over-80-columns issues in at24_eeprom_read()
- Fix write_max off-by-one in at24_probe()
- Check SMBus functionality needed for 16-bit device reads
- Homogenize indentation of SMBus functionality checks for SMBus write

Quoted 4 lines
 drivers/misc/eeprom/Kconfig |   5 +-
 drivers/misc/eeprom/at24.c  | 129
 +++++++++++++++++++++++++++++++++++++++-----
 2 files changed, 119 insertions(+), 15 deletions(-)

This is a significant addition of code so feel free to add your name at the top of at24.c.

We're almost there:

Quoted 8 lines
drivers/misc/eeprom/at24.c +0 -0
index 5d7c090..3dfd2ed 100644
(...)
@@ -527,10 +608,19 @@ static int at24_probe(struct i2c_client *client,
 const struct i2c_device_id *id)
 
Quoted 13 lines

drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const struct i2c_device_id *id) · line 614

Quoted 10 lines
drivers/misc/eeprom/at24.c +4 -4
 	/* Use I2C operations unless we're stuck with SMBus extensions. */
 	if (!i2c_check_functionality(client->adapter, I2C_FUNC_I2C)) {
-		if (chip.flags & AT24_FLAG_ADDR16)
-			return -EPFNOSUPPORT;
-
-		if (i2c_check_functionality(client->adapter,
+		if ((chip.flags & AT24_FLAG_ADDR16) &&
+		    i2c_check_functionality(client->adapter,
+				I2C_FUNC_SMBUS_READ_BYTE_DATA |
+				I2C_FUNC_SMBUS_WRITE_BYTE_DATA)) {

It is I2C_FUNC_SMBUS_READ_BYTE that you use, not
I2C_FUNC_SMBUS_READ_BYTE_DATA.

Sorry about that.

Quoted 28 lines
Quoted 14 lines
drivers/misc/eeprom/at24.c +9 -0
+			/*
+			 * We need SMBUS_WRITE_BYTE_DATA and
+			 * SMBUS_READ_BYTE_DATA to implement byte reads for
+			 * 16-bit address devices. This will be slow, but
+			 * better than nothing (e.g. read @ 3.6 KiB/s). It is
+			 * also unsafe in a multi-master topology.
+			 */
+			use_smbus = I2C_SMBUS_BYTE_DATA;
+		} else if (i2c_check_functionality(client->adapter,
 				I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
 			use_smbus = I2C_SMBUS_I2C_BLOCK_DATA;
drivers/misc/eeprom/at24.c +0 -0
 		} else if (i2c_check_functionality(client->adapter,
(...)

drivers/misc/eeprom/at24.c static int at24_probe(struct i2c_client *client, const · line 704

Quoted 9 lines
drivers/misc/eeprom/at24.c +3 -2
@@ -598,8 +698,9 @@ static int at24_probe(struct i2c_client *client, const
 struct i2c_device_id *id)
 
 			if (write_max > io_limit)
 				write_max = io_limit;
-			if (use_smbus && write_max > I2C_SMBUS_BLOCK_MAX)
-				write_max = I2C_SMBUS_BLOCK_MAX;
+			if (use_smbus && write_max >= I2C_SMBUS_BLOCK_MAX)
+				write_max = I2C_SMBUS_BLOCK_MAX -
+					!!(chip.flags & AT24_FLAG_ADDR16);

Beh. OK, it works, I will admit it's even kind of clever, but it also looks fragile and confusing to some degree. What is wrong with just spelling out the condition explicitly?

Quoted 8 lines
			unsigned smbus_limit = (chip.flags & AT24_FLAG_ADDR16) ?
					       I2C_SMBUS_BLOCK_MAX - 1 :
					       I2C_SMBUS_BLOCK_MAX;

			if (use_smbus && write_max > smbus_limit)
				write_max = smbus_limit;

This might not even be slower, and IMHO it is easier to understand.

There's nothing wrong with your suggestion. My original intent was to change the test as little as necessary. I see the improved clarity of your suggestion.

-Aaron S.