Skip to contentSkip to conversation

misc: i2c_eeprom: Backport commits for proper pagesize and partition support

PATCH New Tan Siewert 1 participant 13 messages Jan 20, 2025

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

cover misc: i2c_eeprom: Backport commits for proper pagesize and partition support

This series adds proper support for pagesizes and partitions on EEPROMs connected via an I2C bus. All commits were backported from upstream tag v2021.04.

The original issue arised during debugging a platform that gets its MAC address from an Atmel 24c128 EEPROM instead of the hardware, which always failed [1].

With the patches applied, the EEPROM can be read properly.

Before:

ast# i2c dev 7
ast# i2c md 0x57 0x3f80 6
3f80: ff ff ff ff ff ff ......

After:

ast# i2c bus
Bus 3: i2c-bus@100
Bus 7: i2c-bus@300 (active 7)

    57: eeprom@57, offset len 2, flags 0

ast# i2c md 0x57 0x3f80 6
3f80: 78 24 35 bb cd 01 x$5...

[1] https://discord.com/channels/775381525260664832/775694683589574659/1329524655752745044

Baruch Siach (3):
misc: i2c_eeprom: support DT pagesize property
misc: i2c_eeprom: add eeprom write support
misc: i2c_eeprom: verify that the chip is functional at probe()

Eugen Hristev (1):
misc: i2c_eeprom: implement different probe test eeprom offset

Masahiro Yamada (2):
misc: i2c_eeprom: remove pagewidth field from i2c_eeprom
misc: i2c_eeprom: store pagesize instead of pagewidth in
i2c_eeprom_drv_data

Michal Simek (1):
i2c: eeprom: Use reg property instead of offset and size

Robert Beckett (4):
i2c: add support for offset overflow in to address
misc: i2c_eeprom: add fixed partitions support
misc: i2c_eeprom: add size query
misc: i2c_eeprom: set offset len and chip addr offset mask

Simon Glass (1):
common: Move old EEPROM functions into a new header

 arch/sandbox/dts/test.dts                     |  13 +
 board/atmel/common/mac_eeprom.c               |   1 +
 board/compulab/common/eeprom.c                |   1 +
 board/corscience/tricorder/tricorder-eeprom.c |   1 +
 board/dhelectronics/dh_imx6/dh_imx6.c         |   1 +
 board/freescale/mpc8323erdb/mpc8323erdb.c     |   1 +
 board/kosagi/novena/novena.c                  |   1 +
 board/phytec/phycore_rk3288/phycore-rk3288.c  |   1 +
 board/rockchip/tinker_rk3288/tinker-rk3288.c  |   1 +
 board/samtec/vining_fpga/socfpga.c            |   1 +
 board/ti/am43xx/board.c                       |   1 +
 board/ti/ks2_evm/board_k2g.c                  |   1 +
 cmd/eeprom.c                                  |   1 +
 configs/sandbox_defconfig                     |   1 +
 drivers/i2c/i2c-uclass.c                      |  34 +-
 drivers/misc/i2c_eeprom.c                     | 316 ++++++++++++++++--
 drivers/mtd/mw_eeprom.c                       |   1 +
 drivers/rtc/rv3029.c                          |   1 +
 env/eeprom.c                                  |   1 +
 include/common.h                              |  15 -
 include/eeprom.h                              |  24 ++
 include/i2c.h                                 |  33 ++
 include/i2c_eeprom.h                          |  14 +-
 test/dm/bootcount.c                           |   8 +
 24 files changed, 430 insertions(+), 43 deletions(-)
 create mode 100644 include/eeprom.h
Signature · 1 line
2.47.0
#1 lore raw

PATCH 1/12 i2c: add support for offset overflow in to address

From: Robert Beckett <[email protected]>

Some devices (2 wire eeproms for example) use some bits from the chip address to represent the high bits of the offset instead of or as well as using multiple bytes for the offset, effectively stealing chip addresses on the bus.

Add a chip offset mask that can be set for any i2c chip which gets filled with the offset overflow during offset setup.

(cherry picked from commit 85968522b928e19a27aa79f0cb186c80b8b82e47)
---
 drivers/i2c/i2c-uclass.c | 34 +++++++++++++++++++++++++++-------
 include/i2c.h            | 33 +++++++++++++++++++++++++++++++++
 2 files changed, 60 insertions(+), 7 deletions(-)
drivers/i2c/i2c-uclass.c +27 -7
index 49e23a0a4bf..8c9abca175a 100644
@@ -52,16 +52,19 @@ void i2c_dump_msgs(struct i2c_msg *msg, int nmsgs)
 static int i2c_setup_offset(struct dm_i2c_chip *chip, uint offset,
 			    uint8_t offset_buf[], struct i2c_msg *msg)
 {
-	int offset_len;
+	int offset_len = chip->offset_len;
 
 	msg->addr = chip->chip_addr;
+	if (chip->chip_addr_offset_mask)
+		msg->addr |= (offset >> (8 * offset_len)) &
+			chip->chip_addr_offset_mask;
 	msg->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
 	msg->len = chip->offset_len;
 	msg->buf = offset_buf;
-	if (!chip->offset_len)
+	if (!offset_len)
 		return -EADDRNOTAVAIL;
-	assert(chip->offset_len <= I2C_MAX_OFFSET_LEN);
-	offset_len = chip->offset_len;
+	assert(offset_len <= I2C_MAX_OFFSET_LEN);
+
 	while (offset_len--)
 		*offset_buf++ = offset >> (8 * offset_len);
 
@@ -83,7 +86,7 @@ static int i2c_read_bytewise(struct udevice *dev, uint offset,
 		if (i2c_setup_offset(chip, offset + i, offset_buf, msg))
 			return -EINVAL;
 		ptr = msg + 1;
-		ptr->addr = chip->chip_addr;
+		ptr->addr = msg->addr;
 		ptr->flags = msg->flags | I2C_M_RD;
 		ptr->len = 1;
 		ptr->buf = &buffer[i];
@@ -139,7 +142,7 @@ int dm_i2c_read(struct udevice *dev, uint offset, uint8_t *buffer, int len)
 		ptr++;
 
 	if (len) {
-		ptr->addr = chip->chip_addr;
+		ptr->addr = msg->addr;
 		ptr->flags = chip->flags & DM_I2C_CHIP_10BIT ? I2C_M_TEN : 0;
 		ptr->flags |= I2C_M_RD;
 		ptr->len = len;
@@ -323,7 +326,8 @@ int i2c_get_chip(struct udevice *bus, uint chip_addr, uint offset_len,
 		struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
 		int ret;
 
-		if (chip->chip_addr == chip_addr) {
+		if (chip->chip_addr == (chip_addr &
+					~chip->chip_addr_offset_mask)) {
 			ret = device_probe(dev);
 			debug("found, ret=%d\n", ret);
 			if (ret)
@@ -465,6 +469,22 @@ int i2c_get_chip_offset_len(struct udevice *dev)
 	return chip->offset_len;
 }
 
+int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask)
+{
+	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
+
+	chip->chip_addr_offset_mask = mask;
+
+	return 0;
+}
+
+uint i2c_get_chip_addr_offset_mask(struct udevice *dev)
+{
+	struct dm_i2c_chip *chip = dev_get_parent_platdata(dev);
+
+	return chip->chip_addr_offset_mask;
+}
+
 #ifdef CONFIG_DM_GPIO
 static void i2c_gpio_set_pin(struct gpio_desc *pin, int bit)
 {
include/i2c.h +33 -0
index ccffc195527..b4a46c9823b 100644
@@ -45,12 +45,26 @@ struct udevice;
  *		represent up to 256 bytes. A value larger than 1 may be
  *		needed for larger devices.
  * @flags:	Flags for this chip (dm_i2c_chip_flags)
+ * @chip_addr_offset_mask: Mask of offset bits within chip_addr. Used for
+ *			   devices which steal addresses as part of offset.
+ *			   If offset_len is zero, then the offset is encoded
+ *			   completely within the chip address itself.
+ *			   e.g. a devce with chip address of 0x2c with 512
+ *			   registers might use the bottom bit of the address
+ *			   to indicate which half of the address space is being
+ *			   accessed while still only using 1 byte offset.
+ *			   This means it will respond to  chip address 0x2c and
+ *			   0x2d.
+ *			   A real world example is the Atmel AT24C04. It's
+ *			   datasheet explains it's usage of this addressing
+ *			   mode.
  * @emul: Emulator for this chip address (only used for emulation)
  */
 struct dm_i2c_chip {
 	uint chip_addr;
 	uint offset_len;
 	uint flags;
+	uint chip_addr_offset_mask;
 #ifdef CONFIG_SANDBOX
 	struct udevice *emul;
 	bool test_mode;
@@ -259,6 +273,25 @@ int i2c_set_chip_offset_len(struct udevice *dev, uint offset_len);
  */
 int i2c_get_chip_offset_len(struct udevice *dev);
 
+/**
+ * i2c_set_chip_addr_offset_mask() - set mask of address bits usable by offset
+ *
+ * Some devices listen on multiple chip addresses to achieve larger offsets
+ * than their single or multiple byte offsets would allow for. You can use this
+ * function to set the bits that are valid to be used for offset overflow.
+ *
+ * @mask: The mask to be used for high offset bits within address
+ * @return 0 if OK, other -ve value on error
+ */
+int i2c_set_chip_addr_offset_mask(struct udevice *dev, uint mask);
+
+/*
+ * i2c_get_chip_addr_offset_mask() - get mask of address bits usable by offset
+ *
+ * @return current chip addr offset mask
+ */
+uint i2c_get_chip_addr_offset_mask(struct udevice *dev);
+
 /**
  * i2c_deblock() - recover a bus that is in an unknown state
  *
Signature · 1 line
2.47.0
#2 lore raw

PATCH 2/12 misc: i2c_eeprom: support DT pagesize property

In reply to Tan Siewert

From: Baruch Siach <[email protected]>

Read the page size from DT when available.

(cherry picked from commit a29034d1e6d1c1535055f455b5af5906a7edbc04)
---
 drivers/misc/i2c_eeprom.c | 6 ++++++
 1 file changed, 6 insertions(+)
drivers/misc/i2c_eeprom.c +6 -0
index 29ad87c1d7b..ce2cad44d8d 100644
@@ -50,6 +50,12 @@ static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
 {
 	struct i2c_eeprom *priv = dev_get_priv(dev);
 	u64 data = dev_get_driver_data(dev);
+	u32 pagesize;
+
+	if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
+		priv->pagesize = pagesize;
+		return 0;
+	}
 
 	/* 6 bit -> page size of up to 2^63 (should be sufficient) */
 	priv->pagewidth = data & 0x3F;
Signature · 1 line
2.47.0
#3 lore raw

PATCH 3/12 misc: i2c_eeprom: add eeprom write support

In reply to Tan Siewert

From: Baruch Siach <[email protected]>

Write up to page size in each i2c transfer.

(cherry picked from commit 84c80c63d53bc8a7779b1e7e7084ee3b2d20e768)
---
 drivers/misc/i2c_eeprom.c | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)
drivers/misc/i2c_eeprom.c +19 -1
index ce2cad44d8d..f25d0540075 100644
@@ -5,6 +5,7 @@
 
 #include <common.h>
 #include <linux/err.h>
+#include <linux/kernel.h>
 #include <dm.h>
 #include <i2c.h>
 #include <i2c_eeprom.h>
@@ -38,7 +39,24 @@ static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
 static int i2c_eeprom_std_write(struct udevice *dev, int offset,
 				const uint8_t *buf, int size)
 {
-	return -ENODEV;
+	struct i2c_eeprom *priv = dev_get_priv(dev);
+	int ret;
+
+	while (size > 0) {
+		int write_size = min_t(int, size, priv->pagesize);
+
+		ret = dm_i2c_write(dev, offset, buf, write_size);
+		if (ret)
+			return ret;
+
+		offset += write_size;
+		buf += write_size;
+		size -= write_size;
+
+		udelay(10000);
+	}
+
+	return 0;
 }
 
 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
Signature · 1 line
2.47.0
#4 lore raw

PATCH 4/12 misc: i2c_eeprom: verify that the chip is functional at probe()

In reply to Tan Siewert

From: Baruch Siach <[email protected]>

Read a single byte from EEPROM to verify that it is actually there.

This is equivalent to Linux kernel commit 00f0ea70d2b8 ("eeprom: at24:
check if the chip is functional in probe()").

Cc Bartosz Golaszewski <[email protected]>

hs: fixed style check prefer kernel type 'u8' over 'uint8_t'

(cherry picked from commit 5ae84860b0428b37063a6d7b03cae26a4e772da7)
---
 drivers/misc/i2c_eeprom.c | 8 ++++++++
 1 file changed, 8 insertions(+)
drivers/misc/i2c_eeprom.c +8 -0
index f25d0540075..8f2349ad5a7 100644
@@ -84,6 +84,14 @@ static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
 
 static int i2c_eeprom_std_probe(struct udevice *dev)
 {
+	u8 test_byte;
+	int ret;
+
+	/* Verify that the chip is functional */
+	ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
+	if (ret)
+		return -ENODEV;
+
 	return 0;
 }
 
Signature · 1 line
2.47.0
#5 lore raw

PATCH 5/12 common: Move old EEPROM functions into a new header

In reply to Tan Siewert

From: Simon Glass <[email protected]>

These functions do not use driver model but are still used. Move them to a new eeprom.h header file.

(cherry picked from commit cb3ef6810a27c8ddf5db8694bcef9337f27d12ce)
---
 board/atmel/common/mac_eeprom.c               |  1 +
 board/compulab/common/eeprom.c                |  1 +
 board/corscience/tricorder/tricorder-eeprom.c |  1 +
 board/dhelectronics/dh_imx6/dh_imx6.c         |  1 +
 board/freescale/mpc8323erdb/mpc8323erdb.c     |  1 +
 board/kosagi/novena/novena.c                  |  1 +
 board/phytec/phycore_rk3288/phycore-rk3288.c  |  1 +
 board/rockchip/tinker_rk3288/tinker-rk3288.c  |  1 +
 board/samtec/vining_fpga/socfpga.c            |  1 +
 board/ti/am43xx/board.c                       |  1 +
 board/ti/ks2_evm/board_k2g.c                  |  1 +
 cmd/eeprom.c                                  |  1 +
 drivers/misc/i2c_eeprom.c                     |  1 +
 drivers/mtd/mw_eeprom.c                       |  1 +
 drivers/rtc/rv3029.c                          |  1 +
 env/eeprom.c                                  |  1 +
 include/common.h                              | 15 ------------
 include/eeprom.h                              | 24 +++++++++++++++++++
 18 files changed, 40 insertions(+), 15 deletions(-)
 create mode 100644 include/eeprom.h
board/atmel/common/mac_eeprom.c +1 -0
index 7ff1ec7462f..5a4e45793a0 100644
@@ -6,6 +6,7 @@
 
 #include <common.h>
 #include <dm.h>
+#include <eeprom.h>
 #include <environment.h>
 #include <i2c_eeprom.h>
 #include <netdev.h>
board/compulab/common/eeprom.c +1 -0
index a2e386f67de..1e21edcd5a8 100644
@@ -7,6 +7,7 @@
  */
 
 #include <common.h>
+#include <eeprom.h>
 #include <i2c.h>
 #include <eeprom_layout.h>
 #include <eeprom_field.h>
board/corscience/tricorder/tricorder-eeprom.c +1 -0
index 16bceea7ba7..46e9119d66e 100644
@@ -5,6 +5,7 @@
  * Andreas Bießmann <[email protected]>
  */
 #include <common.h>
+#include <eeprom.h>
 #include <i2c.h>
 
 #include "tricorder-eeprom.h"
board/dhelectronics/dh_imx6/dh_imx6.c +1 -0
index f9ac5c10e1d..2457b9b0e51 100644
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <eeprom.h>
 #include <asm/arch/clock.h>
 #include <asm/arch/crm_regs.h>
 #include <asm/arch/imx-regs.h>
board/freescale/mpc8323erdb/mpc8323erdb.c +1 -0
index 772688186b5..276e8cb3ef9 100644
@@ -10,6 +10,7 @@
  */
 
 #include <common.h>
+#include <eeprom.h>
 #include <ioports.h>
 #include <mpc83xx.h>
 #include <i2c.h>
board/kosagi/novena/novena.c +1 -0
index 9f2586521d4..72337069f4a 100644
@@ -6,6 +6,7 @@
  */
 
 #include <common.h>
+#include <eeprom.h>
 #include <linux/errno.h>
 #include <asm/gpio.h>
 #include <asm/io.h>
board/phytec/phycore_rk3288/phycore-rk3288.c +1 -0
index ffe1833b06b..1d741533010 100644
@@ -4,6 +4,7 @@
  * Author: Wadim Egorov <[email protected]>
  */
 
+#include <eeprom.h>
 #include <asm/io.h>
 #include <common.h>
 #include <dm.h>
board/rockchip/tinker_rk3288/tinker-rk3288.c +1 -0
index 44f13183a5b..6363bb9e7b8 100644
@@ -5,6 +5,7 @@
 
 #include <common.h>
 #include <dm.h>
+#include <eeprom.h>
 #include <environment.h>
 #include <i2c_eeprom.h>
 #include <netdev.h>
board/samtec/vining_fpga/socfpga.c +1 -0
index d99aac68287..c1d022c4470 100644
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <eeprom.h>
 #include <environment.h>
 #include <asm/arch/reset_manager.h>
 #include <asm/io.h>
board/ti/am43xx/board.c +1 -0
index e0cf0ecde6f..42d3d8689fa 100644
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <eeprom.h>
 #include <environment.h>
 #include <i2c.h>
 #include <linux/errno.h>
board/ti/ks2_evm/board_k2g.c +1 -0
index 39a782e4796..6d2c5f2c35f 100644
@@ -6,6 +6,7 @@
  *     Texas Instruments Incorporated, <www.ti.com>
  */
 #include <common.h>
+#include <eeprom.h>
 #include <asm/arch/clock.h>
 #include <asm/ti-common/keystone_net.h>
 #include <asm/arch/psc_defs.h>
cmd/eeprom.c +1 -0
index 6c29b33ba34..0cc653db40c 100644
@@ -22,6 +22,7 @@
 #include <common.h>
 #include <config.h>
 #include <command.h>
+#include <eeprom.h>
 #include <i2c.h>
 #include <eeprom_layout.h>
 
drivers/misc/i2c_eeprom.c +1 -0
index 8f2349ad5a7..3755dbf74bb 100644
@@ -4,6 +4,7 @@
  */
 
 #include <common.h>
+#include <eeprom.h>
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <dm.h>
drivers/mtd/mw_eeprom.c +1 -0
index f7791b51a04..6a3a6f67518 100644
@@ -1,6 +1,7 @@
 /* Three-wire (MicroWire) serial eeprom driver (for 93C46 and compatibles) */
 
 #include <common.h>
+#include <eeprom.h>
 #include <asm/ic/ssi.h>
 
 /*
drivers/rtc/rv3029.c +1 -0
index 38acb9c9924..23670627770 100644
@@ -10,6 +10,7 @@
 #include <common.h>
 #include <command.h>
 #include <dm.h>
+#include <eeprom.h>
 #include <i2c.h>
 #include <rtc.h>
 
env/eeprom.c +1 -0
index ac2689cc148..593743b8b01 100644
@@ -9,6 +9,7 @@
 
 #include <common.h>
 #include <command.h>
+#include <eeprom.h>
 #include <environment.h>
 #include <linux/stddef.h>
 #if defined(CONFIG_I2C_ENV_EEPROM_BUS)
include/common.h +0 -15
index 2c21dee8505..eb453a50bd8 100644
@@ -269,21 +269,6 @@ phys_size_t get_effective_memsize(void);
 void	reset_phy     (void);
 void	fdc_hw_init   (void);
 
-/* $(BOARD)/eeprom.c */
-#ifdef CONFIG_CMD_EEPROM
-void eeprom_init  (int bus);
-int  eeprom_read  (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt);
-int  eeprom_write (unsigned dev_addr, unsigned offset, uchar *buffer, unsigned cnt);
-#else
-/*
- * Some EEPROM code is depecated because it used the legacy I2C interface. Add
- * some macros here so we don't have to touch every one of those uses
- */
-#define eeprom_init(bus)
-#define eeprom_read(dev_addr, offset, buffer, cnt) ((void)-ENOSYS)
-#define eeprom_write(dev_addr, offset, buffer, cnt) ((void)-ENOSYS)
-#endif
-
 #if !defined(CONFIG_ENV_EEPROM_IS_ON_I2C) && defined(CONFIG_SYS_I2C_EEPROM_ADDR)
 # define CONFIG_SYS_DEF_EEPROM_ADDR CONFIG_SYS_I2C_EEPROM_ADDR
 #endif
include/eeprom.h new file +24 -0
new file mode 100644
index 00000000000..61eb826a734
@@ -0,0 +1,24 @@
+/* SPDX-License-Identifier: GPL-2.0+ */
+/*
+ * (C) Copyright 2000-2009
+ * Wolfgang Denk, DENX Software Engineering, [email protected].
+ */
+
+#ifndef __EEPROM_LEGACY_H
+#define __EEPROM_LEGACY_H
+
+#ifdef CONFIG_CMD_EEPROM
+void eeprom_init(int bus);
+int eeprom_read(uint dev_addr, uint offset, uchar *buffer, uint cnt);
+int eeprom_write(uint dev_addr, uint offset, uchar *buffer, uint cnt);
+#else
+/*
+ * Some EEPROM code is depecated because it used the legacy I2C interface. Add
+ * some macros here so we don't have to touch every one of those uses
+ */
+#define eeprom_init(bus)
+#define eeprom_read(dev_addr, offset, buffer, cnt) ((void)-ENOSYS)
+#define eeprom_write(dev_addr, offset, buffer, cnt) ((void)-ENOSYS)
+#endif
+
+#endif
Signature · 1 line
2.47.0
#6 lore raw

PATCH 6/12 misc: i2c_eeprom: add fixed partitions support

In reply to Tan Siewert

From: Robert Beckett <[email protected]>

Add ability to partition eeprom via devicetree bindings

(cherry picked from commit 1a59cb426d50082821df941146a9ec20decd118f)
---
 drivers/misc/i2c_eeprom.c | 98 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 98 insertions(+)
drivers/misc/i2c_eeprom.c +98 -0
index 3755dbf74bb..741bae27275 100644
@@ -8,6 +8,7 @@
 #include <linux/err.h>
 #include <linux/kernel.h>
 #include <dm.h>
+#include <dm/device-internal.h>
 #include <i2c.h>
 #include <i2c_eeprom.h>
 
@@ -83,6 +84,29 @@ static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
 	return 0;
 }
 
+static int i2c_eeprom_std_bind(struct udevice *dev)
+{
+	ofnode partitions = ofnode_find_subnode(dev_ofnode(dev), "partitions");
+	ofnode partition;
+	const char *name;
+
+	if (!ofnode_valid(partitions))
+		return 0;
+	if (!ofnode_device_is_compatible(partitions, "fixed-partitions"))
+		return -ENOTSUPP;
+
+	ofnode_for_each_subnode(partition, partitions) {
+		name = ofnode_get_name(partition);
+		if (!name)
+			continue;
+
+		device_bind_ofnode(dev, DM_GET_DRIVER(i2c_eeprom_partition),
+				   name, NULL, partition, NULL);
+	}
+
+	return 0;
+}
+
 static int i2c_eeprom_std_probe(struct udevice *dev)
 {
 	u8 test_byte;
@@ -118,12 +142,86 @@ U_BOOT_DRIVER(i2c_eeprom_std) = {
 	.name			= "i2c_eeprom",
 	.id			= UCLASS_I2C_EEPROM,
 	.of_match		= i2c_eeprom_std_ids,
+	.bind			= i2c_eeprom_std_bind,
 	.probe			= i2c_eeprom_std_probe,
 	.ofdata_to_platdata	= i2c_eeprom_std_ofdata_to_platdata,
 	.priv_auto_alloc_size	= sizeof(struct i2c_eeprom),
 	.ops			= &i2c_eeprom_std_ops,
 };
 
+struct i2c_eeprom_partition {
+	u32 offset;
+	u32 size;
+};
+
+static int i2c_eeprom_partition_probe(struct udevice *dev)
+{
+	return 0;
+}
+
+static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
+{
+	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
+	u32 offset, size;
+	int ret;
+
+	ret = dev_read_u32(dev, "offset", &offset);
+	if (ret)
+		return ret;
+
+	ret = dev_read_u32(dev, "size", &size);
+	if (ret)
+		return ret;
+
+	priv->offset = offset;
+	priv->size = size;
+
+	return 0;
+}
+
+static int i2c_eeprom_partition_read(struct udevice *dev, int offset,
+				     u8 *buf, int size)
+{
+	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
+	struct udevice *parent = dev_get_parent(dev);
+
+	if (!parent)
+		return -ENODEV;
+	if (offset + size > priv->size)
+		return -EINVAL;
+
+	return i2c_eeprom_read(parent, offset + priv->offset, buf, size);
+}
+
+static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
+				      const u8 *buf, int size)
+{
+	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
+	struct udevice *parent = dev_get_parent(dev);
+
+	if (!parent)
+		return -ENODEV;
+	if (offset + size > priv->size)
+		return -EINVAL;
+
+	return i2c_eeprom_write(parent, offset + priv->offset, (uint8_t *)buf,
+				size);
+}
+
+static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
+	.read	= i2c_eeprom_partition_read,
+	.write	= i2c_eeprom_partition_write,
+};
+
+U_BOOT_DRIVER(i2c_eeprom_partition) = {
+	.name			= "i2c_eeprom_partition",
+	.id			= UCLASS_I2C_EEPROM,
+	.probe			= i2c_eeprom_partition_probe,
+	.ofdata_to_platdata	= i2c_eeprom_partition_ofdata_to_platdata,
+	.priv_auto_alloc_size	= sizeof(struct i2c_eeprom_partition),
+	.ops			= &i2c_eeprom_partition_ops,
+};
+
 UCLASS_DRIVER(i2c_eeprom) = {
 	.id		= UCLASS_I2C_EEPROM,
 	.name		= "i2c_eeprom",
Signature · 1 line
2.47.0
#7 lore raw

PATCH 7/12 misc: i2c_eeprom: add size query

In reply to Tan Siewert

From: Robert Beckett <[email protected]>

Add ability to query size of eeprom device and partitions

(cherry picked from commit 033e18b47bd020610f84ca88c5ce3b88215d563a)
---
 drivers/misc/i2c_eeprom.c | 145 +++++++++++++++++++++++++++++++++-----
 include/i2c_eeprom.h      |  12 ++++
 2 files changed, 138 insertions(+), 19 deletions(-)
drivers/misc/i2c_eeprom.c +126 -19
index 741bae27275..934f82074d5 100644
@@ -12,6 +12,11 @@
 #include <i2c.h>
 #include <i2c_eeprom.h>
 
+struct i2c_eeprom_drv_data {
+	u32 size; /* size in bytes */
+	u32 pagewidth; /* pagesize = 2^pagewidth */
+};
+
 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
 {
 	const struct i2c_eeprom_ops *ops = device_get_ops(dev);
@@ -32,6 +37,16 @@ int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size)
 	return ops->write(dev, offset, buf, size);
 }
 
+int i2c_eeprom_size(struct udevice *dev)
+{
+	const struct i2c_eeprom_ops *ops = device_get_ops(dev);
+
+	if (!ops->size)
+		return -ENOSYS;
+
+	return ops->size(dev);
+}
+
 static int i2c_eeprom_std_read(struct udevice *dev, int offset, uint8_t *buf,
 			       int size)
 {
@@ -61,25 +76,39 @@ static int i2c_eeprom_std_write(struct udevice *dev, int offset,
 	return 0;
 }
 
+static int i2c_eeprom_std_size(struct udevice *dev)
+{
+	struct i2c_eeprom *priv = dev_get_priv(dev);
+
+	return priv->size;
+}
+
 static const struct i2c_eeprom_ops i2c_eeprom_std_ops = {
 	.read	= i2c_eeprom_std_read,
 	.write	= i2c_eeprom_std_write,
+	.size	= i2c_eeprom_std_size,
 };
 
 static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
 {
 	struct i2c_eeprom *priv = dev_get_priv(dev);
-	u64 data = dev_get_driver_data(dev);
+	struct i2c_eeprom_drv_data *data =
+		(struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
 	u32 pagesize;
+	u32 size;
 
 	if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
 		priv->pagesize = pagesize;
-		return 0;
+	} else {
+		/* 6 bit -> page size of up to 2^63 (should be sufficient) */
+		priv->pagewidth = data->pagewidth;
+		priv->pagesize = (1 << priv->pagewidth);
 	}
 
-	/* 6 bit -> page size of up to 2^63 (should be sufficient) */
-	priv->pagewidth = data & 0x3F;
-	priv->pagesize = (1 << priv->pagewidth);
+	if (dev_read_u32(dev, "size", &size) == 0)
+		priv->size = size;
+	else
+		priv->size = data->size;
 
 	return 0;
 }
@@ -120,21 +149,91 @@ static int i2c_eeprom_std_probe(struct udevice *dev)
 	return 0;
 }
 
+static const struct i2c_eeprom_drv_data eeprom_data = {
+	.size = 0,
+	.pagewidth = 0,
+};
+
+static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
+	.size = 256,
+	.pagewidth = 3,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c01a_data = {
+	.size = 128,
+	.pagewidth = 3,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c02_data = {
+	.size = 256,
+	.pagewidth = 3,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c04_data = {
+	.size = 512,
+	.pagewidth = 4,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c08_data = {
+	.size = 1024,
+	.pagewidth = 4,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c08a_data = {
+	.size = 1024,
+	.pagewidth = 4,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c16a_data = {
+	.size = 2048,
+	.pagewidth = 4,
+};
+
+static const struct i2c_eeprom_drv_data atmel24mac402_data = {
+	.size = 256,
+	.pagewidth = 4,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c32_data = {
+	.size = 4096,
+	.pagewidth = 5,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c64_data = {
+	.size = 8192,
+	.pagewidth = 5,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c128_data = {
+	.size = 16384,
+	.pagewidth = 6,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c256_data = {
+	.size = 32768,
+	.pagewidth = 6,
+};
+
+static const struct i2c_eeprom_drv_data atmel24c512_data = {
+	.size = 65536,
+	.pagewidth = 6,
+};
+
 static const struct udevice_id i2c_eeprom_std_ids[] = {
-	{ .compatible = "i2c-eeprom", .data = 0 },
-	{ .compatible = "microchip,24aa02e48", .data = 3 },
-	{ .compatible = "atmel,24c01a", .data = 3 },
-	{ .compatible = "atmel,24c02", .data = 3 },
-	{ .compatible = "atmel,24c04", .data = 4 },
-	{ .compatible = "atmel,24c08", .data = 4 },
-	{ .compatible = "atmel,24c08a", .data = 4 },
-	{ .compatible = "atmel,24c16a", .data = 4 },
-	{ .compatible = "atmel,24mac402", .data = 4 },
-	{ .compatible = "atmel,24c32", .data = 5 },
-	{ .compatible = "atmel,24c64", .data = 5 },
-	{ .compatible = "atmel,24c128", .data = 6 },
-	{ .compatible = "atmel,24c256", .data = 6 },
-	{ .compatible = "atmel,24c512", .data = 6 },
+	{ .compatible = "i2c-eeprom", (ulong)&eeprom_data },
+	{ .compatible = "microchip,24aa02e48", (ulong)&mc24aa02e48_data },
+	{ .compatible = "atmel,24c01a", (ulong)&atmel24c01a_data },
+	{ .compatible = "atmel,24c02", (ulong)&atmel24c02_data },
+	{ .compatible = "atmel,24c04", (ulong)&atmel24c04_data },
+	{ .compatible = "atmel,24c08", (ulong)&atmel24c08_data },
+	{ .compatible = "atmel,24c08a", (ulong)&atmel24c08a_data },
+	{ .compatible = "atmel,24c16a", (ulong)&atmel24c16a_data },
+	{ .compatible = "atmel,24mac402", (ulong)&atmel24mac402_data },
+	{ .compatible = "atmel,24c32", (ulong)&atmel24c32_data },
+	{ .compatible = "atmel,24c64", (ulong)&atmel24c64_data },
+	{ .compatible = "atmel,24c128", (ulong)&atmel24c128_data },
+	{ .compatible = "atmel,24c256", (ulong)&atmel24c256_data },
+	{ .compatible = "atmel,24c512", (ulong)&atmel24c512_data },
 	{ }
 };
 
@@ -208,9 +307,17 @@ static int i2c_eeprom_partition_write(struct udevice *dev, int offset,
 				size);
 }
 
+static int i2c_eeprom_partition_size(struct udevice *dev)
+{
+	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
+
+	return priv->size;
+}
+
 static const struct i2c_eeprom_ops i2c_eeprom_partition_ops = {
 	.read	= i2c_eeprom_partition_read,
 	.write	= i2c_eeprom_partition_write,
+	.size	= i2c_eeprom_partition_size,
 };
 
 U_BOOT_DRIVER(i2c_eeprom_partition) = {
include/i2c_eeprom.h +12 -0
index 0fcdf3831b3..b96254ae79f 100644
@@ -10,6 +10,7 @@ struct i2c_eeprom_ops {
 	int (*read)(struct udevice *dev, int offset, uint8_t *buf, int size);
 	int (*write)(struct udevice *dev, int offset, const uint8_t *buf,
 		     int size);
+	int (*size)(struct udevice *dev);
 };
 
 struct i2c_eeprom {
@@ -17,6 +18,8 @@ struct i2c_eeprom {
 	unsigned long pagesize;
 	/* The EEPROM's page width in bits (pagesize = 2^pagewidth) */
 	unsigned pagewidth;
+	/* The EEPROM's capacity in bytes */
+	unsigned long size;
 };
 
 /*
@@ -43,4 +46,13 @@ int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size);
  */
 int i2c_eeprom_write(struct udevice *dev, int offset, uint8_t *buf, int size);
 
+/*
+ * i2c_eeprom_size() - get size of I2C EEPROM chip
+ *
+ * @dev:	Chip to query
+ *
+ * @return +ve size in bytes on success, -ve on failure
+ */
+int i2c_eeprom_size(struct udevice *dev);
+
 #endif
Signature · 1 line
2.47.0
#8 lore raw

PATCH 8/12 misc: i2c_eeprom: set offset len and chip addr offset mask

In reply to Tan Siewert

From: Robert Beckett <[email protected]>

Set the correct offset length and chip address offset mask for each device to allow correct access to total capacity of the devices.

(cherry picked from commit 821c982e359a383c8b95106e135da9fd98f16d2b)
---
 drivers/misc/i2c_eeprom.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
drivers/misc/i2c_eeprom.c +35 -0
index 934f82074d5..6c0459dc555 100644
@@ -15,6 +15,8 @@
 struct i2c_eeprom_drv_data {
 	u32 size; /* size in bytes */
 	u32 pagewidth; /* pagesize = 2^pagewidth */
+	u32 addr_offset_mask; /* bits in addr used for offset overflow */
+	u32 offset_len; /* size in bytes of offset */
 };
 
 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
@@ -140,6 +142,11 @@ static int i2c_eeprom_std_probe(struct udevice *dev)
 {
 	u8 test_byte;
 	int ret;
+	struct i2c_eeprom_drv_data *data =
+		(struct i2c_eeprom_drv_data *)dev_get_driver_data(dev);
+
+	i2c_set_chip_offset_len(dev, data->offset_len);
+	i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
 
 	/* Verify that the chip is functional */
 	ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
@@ -152,71 +159,99 @@ static int i2c_eeprom_std_probe(struct udevice *dev)
 static const struct i2c_eeprom_drv_data eeprom_data = {
 	.size = 0,
 	.pagewidth = 0,
+	.addr_offset_mask = 0,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
 	.size = 256,
 	.pagewidth = 3,
+	.addr_offset_mask = 0,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
 	.size = 128,
 	.pagewidth = 3,
+	.addr_offset_mask = 0,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c02_data = {
 	.size = 256,
 	.pagewidth = 3,
+	.addr_offset_mask = 0,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c04_data = {
 	.size = 512,
 	.pagewidth = 4,
+	.addr_offset_mask = 0x1,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c08_data = {
 	.size = 1024,
 	.pagewidth = 4,
+	.addr_offset_mask = 0x3,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
 	.size = 1024,
 	.pagewidth = 4,
+	.addr_offset_mask = 0x3,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
 	.size = 2048,
 	.pagewidth = 4,
+	.addr_offset_mask = 0x7,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
 	.size = 256,
 	.pagewidth = 4,
+	.addr_offset_mask = 0,
+	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c32_data = {
 	.size = 4096,
 	.pagewidth = 5,
+	.addr_offset_mask = 0,
+	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c64_data = {
 	.size = 8192,
 	.pagewidth = 5,
+	.addr_offset_mask = 0,
+	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c128_data = {
 	.size = 16384,
 	.pagewidth = 6,
+	.addr_offset_mask = 0,
+	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c256_data = {
 	.size = 32768,
 	.pagewidth = 6,
+	.addr_offset_mask = 0,
+	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c512_data = {
 	.size = 65536,
 	.pagewidth = 6,
+	.addr_offset_mask = 0,
+	.offset_len = 2,
 };
 
 static const struct udevice_id i2c_eeprom_std_ids[] = {
Signature · 1 line
2.47.0
#9 lore raw

PATCH 9/12 misc: i2c_eeprom: remove pagewidth field from i2c_eeprom

In reply to Tan Siewert

From: Masahiro Yamada <[email protected]>

This struct member is not used in any effective way. Remove it.

(cherry picked from commit ed6dd4e460bd94150dc09fce547a27e2fcca66eb)
---
 drivers/misc/i2c_eeprom.c | 8 +++-----
 include/i2c_eeprom.h      | 2 --
 2 files changed, 3 insertions(+), 7 deletions(-)
drivers/misc/i2c_eeprom.c +3 -5
index 6c0459dc555..728e0fd79ab 100644
@@ -99,13 +99,11 @@ static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
 	u32 pagesize;
 	u32 size;
 
-	if (dev_read_u32(dev, "pagesize", &pagesize) == 0) {
+	if (dev_read_u32(dev, "pagesize", &pagesize) == 0)
 		priv->pagesize = pagesize;
-	} else {
+	else
 		/* 6 bit -> page size of up to 2^63 (should be sufficient) */
-		priv->pagewidth = data->pagewidth;
-		priv->pagesize = (1 << priv->pagewidth);
-	}
+		priv->pagesize = 1 << data->pagewidth;
 
 	if (dev_read_u32(dev, "size", &size) == 0)
 		priv->size = size;
include/i2c_eeprom.h +0 -2
index b96254ae79f..cd620d519fc 100644
@@ -16,8 +16,6 @@ struct i2c_eeprom_ops {
 struct i2c_eeprom {
 	/* The EEPROM's page size in byte */
 	unsigned long pagesize;
-	/* The EEPROM's page width in bits (pagesize = 2^pagewidth) */
-	unsigned pagewidth;
 	/* The EEPROM's capacity in bytes */
 	unsigned long size;
 };
Signature · 1 line
2.47.0
#10 lore raw

PATCH 10/12 misc: i2c_eeprom: store pagesize instead of pagewidth in i2c_eeprom_drv_data

In reply to Tan Siewert

From: Masahiro Yamada <[email protected]>

Associate the pagesize with compatible strings, and copy it to priv->pagesize. This is more straight-forward.

(cherry picked from commit 0c17bb1cbe87c5b00880d3445b46c437f3f22e02)
---
 drivers/misc/i2c_eeprom.c | 32 ++++++++++++++++----------------
 1 file changed, 16 insertions(+), 16 deletions(-)
drivers/misc/i2c_eeprom.c +16 -16
index 728e0fd79ab..ef5f103c98e 100644
@@ -14,7 +14,7 @@
 
 struct i2c_eeprom_drv_data {
 	u32 size; /* size in bytes */
-	u32 pagewidth; /* pagesize = 2^pagewidth */
+	u32 pagesize; /* page size in bytes */
 	u32 addr_offset_mask; /* bits in addr used for offset overflow */
 	u32 offset_len; /* size in bytes of offset */
 };
@@ -103,7 +103,7 @@ static int i2c_eeprom_std_ofdata_to_platdata(struct udevice *dev)
 		priv->pagesize = pagesize;
 	else
 		/* 6 bit -> page size of up to 2^63 (should be sufficient) */
-		priv->pagesize = 1 << data->pagewidth;
+		priv->pagesize = data->pagesize;
 
 	if (dev_read_u32(dev, "size", &size) == 0)
 		priv->size = size;
@@ -156,98 +156,98 @@ static int i2c_eeprom_std_probe(struct udevice *dev)
 
 static const struct i2c_eeprom_drv_data eeprom_data = {
 	.size = 0,
-	.pagewidth = 0,
+	.pagesize = 1,
 	.addr_offset_mask = 0,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data mc24aa02e48_data = {
 	.size = 256,
-	.pagewidth = 3,
+	.pagesize = 8,
 	.addr_offset_mask = 0,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c01a_data = {
 	.size = 128,
-	.pagewidth = 3,
+	.pagesize = 8,
 	.addr_offset_mask = 0,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c02_data = {
 	.size = 256,
-	.pagewidth = 3,
+	.pagesize = 8,
 	.addr_offset_mask = 0,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c04_data = {
 	.size = 512,
-	.pagewidth = 4,
+	.pagesize = 16,
 	.addr_offset_mask = 0x1,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c08_data = {
 	.size = 1024,
-	.pagewidth = 4,
+	.pagesize = 16,
 	.addr_offset_mask = 0x3,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c08a_data = {
 	.size = 1024,
-	.pagewidth = 4,
+	.pagesize = 16,
 	.addr_offset_mask = 0x3,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c16a_data = {
 	.size = 2048,
-	.pagewidth = 4,
+	.pagesize = 16,
 	.addr_offset_mask = 0x7,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24mac402_data = {
 	.size = 256,
-	.pagewidth = 4,
+	.pagesize = 16,
 	.addr_offset_mask = 0,
 	.offset_len = 1,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c32_data = {
 	.size = 4096,
-	.pagewidth = 5,
+	.pagesize = 32,
 	.addr_offset_mask = 0,
 	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c64_data = {
 	.size = 8192,
-	.pagewidth = 5,
+	.pagesize = 32,
 	.addr_offset_mask = 0,
 	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c128_data = {
 	.size = 16384,
-	.pagewidth = 6,
+	.pagesize = 64,
 	.addr_offset_mask = 0,
 	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c256_data = {
 	.size = 32768,
-	.pagewidth = 6,
+	.pagesize = 64,
 	.addr_offset_mask = 0,
 	.offset_len = 2,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c512_data = {
 	.size = 65536,
-	.pagewidth = 6,
+	.pagesize = 64,
 	.addr_offset_mask = 0,
 	.offset_len = 2,
 };
Signature · 1 line
2.47.0
#11 lore raw

PATCH 11/12 misc: i2c_eeprom: implement different probe test eeprom offset

In reply to Tan Siewert

From: Eugen Hristev <[email protected]>

Because of this commit :
5ae84860b0 ("misc: i2c_eeprom: verify that the chip is functional at probe()") at probe time, each eeprom is tested for read at offset 0.

The Atmel AT24MAC402 eeprom has different mapping. One i2c slave address is used for the lower 0x80 bytes and another i2c slave address is used for the upper 0x80 bytes. Because of this basically the i2c master sees 2 different slaves. We need the upper bytes because we read the unique MAC address from this EEPROM area.

However this implies that our slave address will return error on reads from address 0x0 to 0x80.

To solve this, implemented an offset field inside platform data that is by default 0 (as it is used now), but can be changed in the compatible table.

The probe function will now read at this offset and use it, instead of blindly checking offset 0.

This will fix the regression noticed on these EEPROMs since the commit abovementioned that introduces the probe failed issue.

(cherry picked from commit b24dc83f156604f253ef6521776444188c5bff9b)
---
 drivers/misc/i2c_eeprom.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/misc/i2c_eeprom.c +7 -1
index ef5f103c98e..32a1b208567 100644
@@ -17,6 +17,7 @@ struct i2c_eeprom_drv_data {
 	u32 pagesize; /* page size in bytes */
 	u32 addr_offset_mask; /* bits in addr used for offset overflow */
 	u32 offset_len; /* size in bytes of offset */
+	u32 start_offset; /* valid start offset inside memory, by default 0 */
 };
 
 int i2c_eeprom_read(struct udevice *dev, int offset, uint8_t *buf, int size)
@@ -147,7 +148,11 @@ static int i2c_eeprom_std_probe(struct udevice *dev)
 	i2c_set_chip_addr_offset_mask(dev, data->addr_offset_mask);
 
 	/* Verify that the chip is functional */
-	ret = i2c_eeprom_read(dev, 0, &test_byte, 1);
+	/*
+	 * Not all eeproms start from offset 0. Valid offset is available
+	 * in the platform data struct.
+	 */
+	ret = i2c_eeprom_read(dev, data->start_offset, &test_byte, 1);
 	if (ret)
 		return -ENODEV;
 
@@ -215,6 +220,7 @@ static const struct i2c_eeprom_drv_data atmel24mac402_data = {
 	.pagesize = 16,
 	.addr_offset_mask = 0,
 	.offset_len = 1,
+	.start_offset = 0x80,
 };
 
 static const struct i2c_eeprom_drv_data atmel24c32_data = {
Signature · 1 line
2.47.0
#12 lore raw

PATCH 12/12 i2c: eeprom: Use reg property instead of offset and size

In reply to Tan Siewert

From: Michal Simek <[email protected]>

Remove adhoc dt binding for fixed-partition definition for i2c eeprom. fixed-partition are using reg property instead of offset/size pair.

(cherry picked from commit f692b479f02d9b2689b0686f1f6ff2f06c6ecc59)
---
 arch/sandbox/dts/test.dts | 13 +++++++++++++
 configs/sandbox_defconfig |  1 +
 drivers/misc/i2c_eeprom.c | 15 ++++++++-------
 test/dm/bootcount.c       |  8 ++++++++
 4 files changed, 30 insertions(+), 7 deletions(-)
arch/sandbox/dts/test.dts +13 -0
index b7051610865..39e3fb712a6 100644
@@ -282,6 +282,14 @@
 			reg = <0x2c>;
 			compatible = "i2c-eeprom";
 			sandbox,emul = <&emul_eeprom>;
+			partitions {
+				compatible = "fixed-partitions";
+				#address-cells = <1>;
+				#size-cells = <1>;
+				bootcount_i2c: bootcount@10 {
+					reg = <10 2>;
+				};
+			};
 		};
 
 		rtc_0: rtc@43 {
@@ -329,6 +337,11 @@
 		offset = <0x13>;
 	};
 
+	bootcount {
+		compatible = "u-boot,bootcount-i2c-eeprom";
+		i2c-eeprom = <&bootcount_i2c>;
+	};
+
 	adc@0 {
 		compatible = "sandbox,adc";
 		vdd-supply = <&buck2>;
configs/sandbox_defconfig +1 -0
index 193e41896cb..236ba5f69ba 100644
@@ -90,6 +90,7 @@ CONFIG_AXI_SANDBOX=y
 CONFIG_BOOTCOUNT_LIMIT=y
 CONFIG_DM_BOOTCOUNT=y
 CONFIG_DM_BOOTCOUNT_RTC=y
+CONFIG_DM_BOOTCOUNT_I2C_EEPROM=y
 CONFIG_CLK=y
 CONFIG_CPU=y
 CONFIG_DM_DEMO=y
drivers/misc/i2c_eeprom.c +8 -7
index 32a1b208567..5cfcc4729f1 100644
@@ -300,19 +300,20 @@ static int i2c_eeprom_partition_probe(struct udevice *dev)
 static int i2c_eeprom_partition_ofdata_to_platdata(struct udevice *dev)
 {
 	struct i2c_eeprom_partition *priv = dev_get_priv(dev);
-	u32 offset, size;
+	u32 reg[2];
 	int ret;
 
-	ret = dev_read_u32(dev, "offset", &offset);
+	ret = dev_read_u32_array(dev, "reg", reg, 2);
 	if (ret)
 		return ret;
 
-	ret = dev_read_u32(dev, "size", &size);
-	if (ret)
-		return ret;
+	if (!reg[1])
+		return -EINVAL;
+
+	priv->offset = reg[0];
+	priv->size = reg[1];
 
-	priv->offset = offset;
-	priv->size = size;
+	debug("%s: base %x, size %x\n", __func__, priv->offset, priv->size);
 
 	return 0;
 }
test/dm/bootcount.c +8 -0
index 0817b7d3ec1..21f332d9b9d 100644
@@ -23,6 +23,14 @@ static int dm_test_bootcount(struct unit_test_state *uts)
 	ut_assertok(dm_bootcount_get(dev, &val));
 	ut_assert(val == 0xab);
 
+	ut_assertok(uclass_get_device(UCLASS_BOOTCOUNT, 1, &dev));
+	ut_assertok(dm_bootcount_set(dev, 0));
+	ut_assertok(dm_bootcount_get(dev, &val));
+	ut_assert(val == 0);
+	ut_assertok(dm_bootcount_set(dev, 0xab));
+	ut_assertok(dm_bootcount_get(dev, &val));
+	ut_assert(val == 0xab);
+
 	return 0;
 }
 
Signature · 1 line
2.47.0