All of lore.kernel.org
 help / color / mirror / Atom feed
From: Fabien Lahoudere <fabien.lahoudere@collabora.com>
To: u-boot@lists.denx.de
Subject: [U-Boot] [PATCH V3 3/6] bootcount: i2c: Add bus switching to the I2C bootcount driver
Date: Wed, 17 Oct 2018 10:33:28 +0200	[thread overview]
Message-ID: <3c8faf8b897812a5026438733884a1ad3362ce3f.1539764688.git.fabien.lahoudere@collabora.com> (raw)
In-Reply-To: <cover.1539764688.git.fabien.lahoudere@collabora.com>

From: Denis Zalevskiy <denis.zalevskiy@ge.com>

If there is an I2C mux, current bus should be switched before
manipulating with I2C.

Signed-off-by: Denis Zalevskiy <denis.zalevskiy@ge.com>
Signed-off-by: Fabien Lahoudere <fabien.lahoudere@collabora.com>
---
 drivers/bootcount/Kconfig         | 15 ++++++++-
 drivers/bootcount/bootcount_i2c.c | 71 ++++++++++++++++++++++++++++++++++-----
 2 files changed, 77 insertions(+), 9 deletions(-)

diff --git a/drivers/bootcount/Kconfig b/drivers/bootcount/Kconfig
index 9a0bd51..f67f518 100644
--- a/drivers/bootcount/Kconfig
+++ b/drivers/bootcount/Kconfig
@@ -62,7 +62,9 @@ config BOOTCOUNT_I2C
 	bool "Boot counter on I2C device"
 	help
 	  Enable support for the bootcounter on an i2c (like RTC) device.
-	  CONFIG_SYS_I2C_RTC_ADDR = i2c chip address
+	  CONFIG_SYS_BOOTCOUNT_I2C_BUS = bus of the target I2C device,
+	                                 CONFIG_SYS_I2C_RTC_ADDR is used as fallback
+	  CONFIG_SYS_BOOTCOUNT_I2C_ADDR = target I2C device address
 	  CONFIG_SYS_BOOTCOUNT_ADDR = i2c addr which is used for
 	                              the bootcounter.
 
@@ -127,4 +129,15 @@ config SYS_BOOTCOUNT_ADDR
 	help
 	  Set the address used for reading and writing the boot counter.
 
+config SYS_BOOTCOUNT_I2C_BUS
+	int "I2C bootcounter device bus"
+	depends on BOOTCOUNT_I2C
+	help
+	  I2C bus of the device used to store bootcounter
+
+config SYS_BOOTCOUNT_I2C_ADDR
+	hex "I2C bootcounter device address"
+	depends on BOOTCOUNT_I2C
+	help
+	  I2C address of the device used to store bootcounter
 endif
diff --git a/drivers/bootcount/bootcount_i2c.c b/drivers/bootcount/bootcount_i2c.c
index 496741d..a1fc219 100644
--- a/drivers/bootcount/bootcount_i2c.c
+++ b/drivers/bootcount/bootcount_i2c.c
@@ -8,36 +8,91 @@
 #include <linux/compiler.h>
 #include <i2c.h>
 
+#ifndef CONFIG_SYS_BOOTCOUNT_I2C_ADDR
+/* compatibility with the previous logic:
+ * previous version of driver used RTC device to store bootcount
+ */
+#define CONFIG_SYS_BOOTCOUNT_I2C_ADDR CONFIG_SYS_I2C_RTC_ADDR
+#endif
+
 #define BC_MAGIC	0xbc
 
+#ifdef CONFIG_SYS_BOOTCOUNT_I2C_BUS
+static int bootcount_set_bus(void)
+{
+	unsigned int current_bus = i2c_get_bus_num();
+
+	assert(current_bus <= INT_MAX);
+
+	int res = i2c_set_bus_num(CONFIG_SYS_BOOTCOUNT_I2C_BUS);
+
+	if (res < 0) {
+		puts("Error switching I2C bus\n");
+		return res;
+	}
+	return (int)current_bus;
+}
+
+static void bootcount_set_bus_back(int prev_bus)
+{
+	if (i2c_set_bus_num(prev_bus) < 0)
+		puts("Can't switch I2C bus back\n");
+}
+#else
+static inline void bootcount_set_bus(void) { return ; }
+
+static inline int bootcount_set_bus_back(int prev_bus __attribute__((unused)))
+{
+	return 0;
+}
+#endif
+
 void bootcount_store(ulong a)
 {
+	int prev_i2c_bus = bootcount_set_bus();
+
+	if (prev_i2c_bus < 0)
+		return;
+
 	unsigned char buf[3];
 	int ret;
 
 	buf[0] = BC_MAGIC;
 	buf[1] = (a & 0xff);
-	ret = i2c_write(CONFIG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
-		  CONFIG_BOOTCOUNT_ALEN, buf, 2);
+	ret = i2c_write(CONFIG_SYS_BOOTCOUNT_I2C_ADDR,
+			CONFIG_SYS_BOOTCOUNT_ADDR,
+			CONFIG_BOOTCOUNT_ALEN, buf, 2);
 	if (ret != 0)
 		puts("Error writing bootcount\n");
+
+	bootcount_set_bus_back(prev_i2c_bus);
 }
 
 ulong bootcount_load(void)
 {
+	ulong count = 0;
+
+	int prev_i2c_bus = bootcount_set_bus();
+
+	if (prev_i2c_bus < 0)
+		return count;
+
 	unsigned char buf[3];
 	int ret;
 
-	ret = i2c_read(CONFIG_SYS_I2C_RTC_ADDR, CONFIG_SYS_BOOTCOUNT_ADDR,
+	ret = i2c_read(CONFIG_SYS_BOOTCOUNT_I2C_ADDR,
+		       CONFIG_SYS_BOOTCOUNT_ADDR,
 		       CONFIG_BOOTCOUNT_ALEN, buf, 2);
 	if (ret != 0) {
 		puts("Error loading bootcount\n");
-		return 0;
+		goto out;
 	}
 	if (buf[0] == BC_MAGIC)
-		return buf[1];
-
-	bootcount_store(0);
+		count = buf[1];
+	else
+		bootcount_store(count);
 
-	return 0;
+out:
+	bootcount_set_bus_back(prev_i2c_bus);
+	return count;
 }
-- 
1.8.3.1

  parent reply	other threads:[~2018-10-17  8:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-17  8:33 [U-Boot] [PATCH V3 0/6] board: ge: Move bootcount to EEPROM Fabien Lahoudere
2018-10-17  8:33 ` [U-Boot] [PATCH V3 1/6] board: ge: Remove EEPROM bus param from read_vpd() Fabien Lahoudere
2018-12-08 17:27   ` Stefano Babic
2018-10-17  8:33 ` [U-Boot] [PATCH V3 2/6] board: ge: Move VPD EEPROM configuration to the defconfig Fabien Lahoudere
2018-12-08 17:28   ` Stefano Babic
2018-10-17  8:33 ` Fabien Lahoudere [this message]
2018-12-08 17:28   ` [U-Boot] [PATCH V3 3/6] bootcount: i2c: Add bus switching to the I2C bootcount driver Stefano Babic
2018-10-17  8:33 ` [U-Boot] [PATCH V3 4/6] bootcount: Configure length limit for I2C bootcount Fabien Lahoudere
2018-12-08 17:28   ` Stefano Babic
2018-10-17  8:33 ` [U-Boot] [PATCH V3 5/6] board: ge: Move VPD reading to the vpd_reader Fabien Lahoudere
2018-12-08 17:30   ` Stefano Babic
2018-10-17  8:33 ` [U-Boot] [PATCH V3 6/6] board: ge: Store bootcount in EEPROM on PPD and Bx50v3 Fabien Lahoudere
2018-12-08 17:30   ` Stefano Babic

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3c8faf8b897812a5026438733884a1ad3362ce3f.1539764688.git.fabien.lahoudere@collabora.com \
    --to=fabien.lahoudere@collabora.com \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.