Calculate the maximum length of the buffer when writing
accross the page boundary. If the buffer length exceeds
the page boundary, split it. Use this length instead of
comparing the length with the pagesize, because the write
start address can be somewhere in the middle of a page.

Signed-off-by: Alex Michel <alex.mic...@wiedemann-group.com>
---
 drivers/misc/i2c_eeprom.c | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/drivers/misc/i2c_eeprom.c b/drivers/misc/i2c_eeprom.c
index bdd7e018cc..f345e34179 100644
--- a/drivers/misc/i2c_eeprom.c
+++ b/drivers/misc/i2c_eeprom.c
@@ -60,6 +60,17 @@ static int i2c_eeprom_std_read(struct udevice *dev, int 
offset, u
int8_t *buf,                                                                    
            return dm_i2c_read(dev, offset, buf, size);
 }
 
+static int i2c_eeprom_len(unsigned offset, unsigned len, unsigned pagesize)
+{
+       int page_offset =       ((offset) & (pagesize - 1));
+       unsigned maxlen = pagesize - page_offset;
+
+       if (len > maxlen)
+               len = maxlen;
+
+       return len;
+}
+
 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
                                const uint8_t *buf, int size)
 {
@@ -67,7 +78,7 @@ static int i2c_eeprom_std_write(struct udevice *dev, int 
offset,
        int ret;
 
        while (size > 0) {
-               int write_size = min_t(int, size, priv->pagesize);
+               int write_size = i2c_eeprom_len(offset, size, priv->pagesize);
 
                ret = dm_i2c_write(dev, offset, buf, write_size);
                if (ret)
-- 
2.34.1

Reply via email to