Skip to contentSkip to conversation

i2c: slave-eeprom: initialize empty eeprom properly

PATCH In review Patrick Williams 4 participants 14 messages Oct 1, 2019 – Apr 24, 2020

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

PATCH 1/2 i2c: slave-eeprom: initialize empty eeprom properly

The i2c-slave-eeprom driver emulates at24 class eeprom devices, which come initialized with all 1s. Do the same in the software emulation.

---
 drivers/i2c/i2c-slave-eeprom.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/i2c/i2c-slave-eeprom.c +2 -0
index db9763cb4dae..efee56106251 100644
@@ -131,6 +131,8 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
 	if (!eeprom)
 		return -ENOMEM;
 
+	memset(eeprom->buffer, 0xFF, size);
+
 	eeprom->idx_write_cnt = 0;
 	eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
 	eeprom->address_mask = size - 1;
Signature · 1 line
2.17.2 (Apple Git-113)
#1 lore raw

PATCH 2/2 i2c: slave-eeprom: support additional models

Add support for emulating the following EEPROMs:
* 24c01 - 1024 bit
* 24c128 - 128k bit
* 24c256 - 256k bit
* 24c512 - 512k bit

The flag bits in the device id were shifted up 1 bit to make room for saving the 24c512's size. 24c512 uses the full 16-bit address space of a 2-byte addressable EEPROM.

---
 drivers/i2c/i2c-slave-eeprom.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/i2c/i2c-slave-eeprom.c +11 -3
index efee56106251..65419441313b 100644
@@ -37,9 +37,9 @@ struct eeprom_data {
 	u8 buffer[];
 };
 
-#define I2C_SLAVE_BYTELEN GENMASK(15, 0)
-#define I2C_SLAVE_FLAG_ADDR16 BIT(16)
-#define I2C_SLAVE_FLAG_RO BIT(17)
+#define I2C_SLAVE_BYTELEN GENMASK(16, 0)
+#define I2C_SLAVE_FLAG_ADDR16 BIT(17)
+#define I2C_SLAVE_FLAG_RO BIT(18)
 #define I2C_SLAVE_DEVICE_MAGIC(_len, _flags) ((_flags) | (_len))
 
 static int i2c_slave_eeprom_slave_cb(struct i2c_client *client,
@@ -171,12 +171,20 @@ static int i2c_slave_eeprom_remove(struct i2c_client *client)
 }
 
 static const struct i2c_device_id i2c_slave_eeprom_id[] = {
+	{ "slave-24c01", I2C_SLAVE_DEVICE_MAGIC(1024 / 8,  0) },
+	{ "slave-24c01ro", I2C_SLAVE_DEVICE_MAGIC(1024 / 8,  I2C_SLAVE_FLAG_RO) },
 	{ "slave-24c02", I2C_SLAVE_DEVICE_MAGIC(2048 / 8,  0) },
 	{ "slave-24c02ro", I2C_SLAVE_DEVICE_MAGIC(2048 / 8,  I2C_SLAVE_FLAG_RO) },
 	{ "slave-24c32", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16) },
 	{ "slave-24c32ro", I2C_SLAVE_DEVICE_MAGIC(32768 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
 	{ "slave-24c64", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16) },
 	{ "slave-24c64ro", I2C_SLAVE_DEVICE_MAGIC(65536 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
+	{ "slave-24c128", I2C_SLAVE_DEVICE_MAGIC(131072 / 8, I2C_SLAVE_FLAG_ADDR16) },
+	{ "slave-24c128ro", I2C_SLAVE_DEVICE_MAGIC(131072 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
+	{ "slave-24c256", I2C_SLAVE_DEVICE_MAGIC(262144 / 8, I2C_SLAVE_FLAG_ADDR16) },
+	{ "slave-24c256ro", I2C_SLAVE_DEVICE_MAGIC(262144 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
+	{ "slave-24c512", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16) },
+	{ "slave-24c512ro", I2C_SLAVE_DEVICE_MAGIC(524288 / 8, I2C_SLAVE_FLAG_ADDR16 | I2C_SLAVE_FLAG_RO) },
 	{ }
 };
 MODULE_DEVICE_TABLE(i2c, i2c_slave_eeprom_id);
Signature · 1 line
2.17.2 (Apple Git-113)
#2 lore raw

Re: [PATCH 2/2] i2c: slave-eeprom: support additional models

Quoted 11 lines · Patrick Williams

Add support for emulating the following EEPROMs:
* 24c01 - 1024 bit
* 24c128 - 128k bit
* 24c256 - 256k bit
* 24c512 - 512k bit

The flag bits in the device id were shifted up 1 bit to make room for saving the 24c512's size. 24c512 uses the full 16-bit address space of a 2-byte addressable EEPROM.

Do you really need them or is it just nice to have?

I am undecided. I definately don't want all the EEPROM types which exist, but the full 16 bit address range makes sense...

More opinions welcome.

#3 lore raw

Re: [PATCH 2/2] i2c: slave-eeprom: support additional models

Quoted 11 lines · Wolfram Sang
Quoted 10 lines · Patrick Williams

Add support for emulating the following EEPROMs:
* 24c01 - 1024 bit
* 24c128 - 128k bit
* 24c256 - 256k bit
* 24c512 - 512k bit

The flag bits in the device id were shifted up 1 bit to make room for saving the 24c512's size. 24c512 uses the full 16-bit address space of a 2-byte addressable EEPROM.

Quoted 9 lines
Quoted 1 line

Do you really need them or is it just nice to have?

I am undecided. I definately don't want all the EEPROM types which exist, but the full 16 bit address range makes sense...

More opinions welcome.

I don't remember exactly which ones we needed (and I am no longer at Amazon), but it was pretty trivial to add them all to the table so I went ahead and did it. As long as we had one of the 2-byte addressable EEPROMs, anything else necessary could be handleded as a small out-of-tree patch.

Signature · 1 line
Patrick Williams
#4 lore raw
In reply to Patrick Williams

Hi,

I sent in another patch earlier that added support for specifying a file in devicetree to initilize the eeprom from, corresponding to the case of pre-flashed eeprom. Maybe these two patches should be merged so this initialization is only done if no file is specified?

/BA

Quoted 22 lines · Patrick Williams

The i2c-slave-eeprom driver emulates at24 class eeprom devices, which come initialized with all 1s. Do the same in the software emulation.

---
  drivers/i2c/i2c-slave-eeprom.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/i2c/i2c-slave-eeprom.c +2 -0
index db9763cb4dae..efee56106251 100644
@@ -131,6 +131,8 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
  	if (!eeprom)
  		return -ENOMEM;
 
+	memset(eeprom->buffer, 0xFF, size);
+
  	eeprom->idx_write_cnt = 0;
  	eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
  	eeprom->address_mask = size - 1;
#5 lore raw
Quoted 7 lines · Bjorn Ardo

Hi,

I sent in another patch earlier that added support for specifying a file in devicetree to initilize the eeprom from, corresponding to the case of pre-flashed eeprom. Maybe these two patches should be merged so this initialization is only done if no file is specified?

Yes, I agree.

Quoted 24 lines

/BA

Quoted 21 lines · Patrick Williams

The i2c-slave-eeprom driver emulates at24 class eeprom devices, which come initialized with all 1s. Do the same in the software emulation.

---
  drivers/i2c/i2c-slave-eeprom.c | 2 ++
  1 file changed, 2 insertions(+)
drivers/i2c/i2c-slave-eeprom.c +2 -0
index db9763cb4dae..efee56106251 100644
@@ -131,6 +131,8 @@ static int i2c_slave_eeprom_probe(struct i2c_client *client, const struct i2c_de
  	if (!eeprom)
  		return -ENOMEM;
+	memset(eeprom->buffer, 0xFF, size);
+
  	eeprom->idx_write_cnt = 0;
  	eeprom->num_address_bytes = flag_addr16 ? 2 : 1;
  	eeprom->address_mask = size - 1;
#6 lore raw
Quoted 11 lines · Wolfram Sang
Quoted 7 lines · Bjorn Ardo

Hi,

I sent in another patch earlier that added support for specifying a file in devicetree to initilize the eeprom from, corresponding to the case of pre-flashed eeprom. Maybe these two patches should be merged so this initialization is only done if no file is specified?

Yes, I agree.

I've been meaning to get back to this... I'll rebase the commit on the latest tree.

Signature · 1 line
Patrick Williams
#7 lore raw
In reply to Wolfram Sang
Quoted 11 lines · Wolfram Sang
Quoted 7 lines · Bjorn Ardo

Hi,

I sent in another patch earlier that added support for specifying a file in devicetree to initilize the eeprom from, corresponding to the case of pre-flashed eeprom. Maybe these two patches should be merged so this initialization is only done if no file is specified?

Yes, I agree.

It looks like Bjorn's referenced patches are still unmerged also?

Signature · 1 line
Patrick Williams
#8 lore raw
Quoted 10 lines · Patrick Williams
Quoted 9 lines · Wolfram Sang
Quoted 7 lines · Bjorn Ardo

Hi,

I sent in another patch earlier that added support for specifying a file in devicetree to initilize the eeprom from, corresponding to the case of pre-flashed eeprom. Maybe these two patches should be merged so this initialization is only done if no file is specified?

Quoted 4 lines
Quoted 2 lines

Yes, I agree.

It looks like Bjorn's referenced patches are still unmerged also?

Yes, it might be easiest if he merges your patch (with attribution) into the else branch of his fw-load patch.

#9 lore raw
Quoted 10 lines · Wolfram Sang
Quoted 9 lines · Patrick Williams
Quoted 8 lines · Wolfram Sang
Quoted 7 lines · Bjorn Ardo

Hi,

I sent in another patch earlier that added support for specifying a file in devicetree to initilize the eeprom from, corresponding to the case of pre-flashed eeprom. Maybe these two patches should be merged so this initialization is only done if no file is specified?

Quoted 6 lines
Quoted 3 lines
Quoted 2 lines

Yes, I agree.

It looks like Bjorn's referenced patches are still unmerged also?

Yes, it might be easiest if he merges your patch (with attribution) into the else branch of his fw-load patch.

OK, so to summarize, I should update my patch to use device_property_read_string() instead and also init the memory to 0XFF if no file is present. And change name of the function to i2c_slave_init_eeprom_data.

I will look into that and let you know once I'm done.

/BA

#10 lore raw
Quoted 8 lines
Quoted 4 lines

Yes, it might be easiest if he merges your patch (with attribution) into the else branch of his fw-load patch.

OK, so to summarize, I should update my patch to use device_property_read_string() instead and also init the memory to 0XFF if no file is present. And change name of the function to

Or something else went wrong.

Quoted 1 line

i2c_slave_init_eeprom_data.

Yes, that is my idea. You also need to replace checking for an of_node with some equivalent for device properties maybe, but that should be easy to find out.

Quoted 1 line

I will look into that and let you know once I'm done.

Thank you!

#11 lore raw
Quoted 7 lines · Wolfram Sang
Quoted 6 lines
Quoted 3 lines

Yes, it might be easiest if he merges your patch (with attribution) into the else branch of his fw-load patch.

OK, so to summarize, I should update my patch to use device_property_read_string() instead and also init the memory to 0XFF if no file is present. And change name of the function to

Or something else went wrong.

I did like this now: If device_property_read_string() returns a firmware name, I use that, otherwise init to 0xFF. But if it returns a firmware name, and for some reason I get an error when trying to load that firmware I will not default to 0xFF, but rather fail the probe. The logic in that is that if you actively supply a firmware name, you should not silently get 0xFF in your eeprom. Does that sound good?

Quoted 4 lines
Quoted 1 line

i2c_slave_init_eeprom_data.

Yes, that is my idea. You also need to replace checking for an of_node with some equivalent for device properties maybe, but that should be easy to find out.

It appears to me that those kind of checks are done inside device_property_read_string() so I can just remove them and only look at the return value of that function.

Quoted 3 lines
Quoted 1 line

I will look into that and let you know once I'm done.

Thank you!

I have a patch now working on 4.14, will run some tests on it and then try to forward-port to latest kernel och see if it works there as well.

/BA

#12 lore raw
Quoted 7 lines

I did like this now: If device_property_read_string() returns a firmware name, I use that, otherwise init to 0xFF. But if it returns a firmware name, and for some reason I get an error when trying to load that firmware I will not default to 0xFF, but rather fail the probe. The logic in that is that if you actively supply a firmware name, you should not silently get 0xFF in your eeprom. Does that sound good?

Sounds perfect to me.

Quoted 7 lines
Quoted 3 lines

Yes, that is my idea. You also need to replace checking for an of_node with some equivalent for device properties maybe, but that should be easy to find out.

It appears to me that those kind of checks are done inside device_property_read_string() so I can just remove them and only look at the return value of that function.

Even better!

Quoted 2 lines

I have a patch now working on 4.14, will run some tests on it and then try to forward-port to latest kernel och see if it works there as well.

Looking forward to it. I will look at it right away then!

#13 lore raw
Quoted 4 lines · Wolfram Sang
Quoted 2 lines

I have a patch now working on 4.14, will run some tests on it and then try to forward-port to latest kernel och see if it works there as well.

Looking forward to it. I will look at it right away then!

Patch tested on latest kernel and submitted.

/BA