board: ge: Move VPD reading to the vpd_reader

Merge functionality duplicated in bx50v3 and mx53ppd: the logic
is the same except that process_vpd is called at different phases.
Also read_vpd could end up in error, so there is no VPD data in this
case - it shouldn't be processed.

Signed-off-by: Denis Zalevskiy <denis.zalevskiy@ge.com>
Signed-off-by: Fabien Lahoudere <fabien.lahoudere@collabora.com>
diff --git a/board/ge/common/vpd_reader.c b/board/ge/common/vpd_reader.c
index c471583..12410d9 100644
--- a/board/ge/common/vpd_reader.c
+++ b/board/ge/common/vpd_reader.c
@@ -5,6 +5,7 @@
 
 #include "vpd_reader.h"
 
+#include <i2c.h>
 #include <linux/bch.h>
 #include <stdlib.h>
 
@@ -105,9 +106,9 @@
 
 static const u8 ECC_BLOCK_ID = 0xFF;
 
-int vpd_reader(size_t size, u8 *data, void *userdata,
-	       int (*fn)(void *userdata, u8 id, u8 version, u8 type,
-			 size_t size, u8 const *data))
+static int vpd_reader(size_t size, u8 *data, struct vpd_cache *userdata,
+		      int (*fn)(struct vpd_cache *, u8 id, u8 version, u8 type,
+				size_t size, u8 const *data))
 {
 	if (size < HEADER_BLOCK_LEN || !data || !fn)
 		return -EINVAL;
@@ -194,3 +195,33 @@
 			return ret;
 	}
 }
+
+int read_vpd(struct vpd_cache *cache,
+	     int (*process_block)(struct vpd_cache *, u8 id, u8 version,
+				  u8 type, size_t size, u8 const *data))
+{
+	static const size_t size = CONFIG_SYS_VPD_EEPROM_SIZE;
+
+	int res;
+	u8 *data;
+	unsigned int current_i2c_bus = i2c_get_bus_num();
+
+	res = i2c_set_bus_num(CONFIG_SYS_VPD_EEPROM_I2C_BUS);
+	if (res < 0)
+		return res;
+
+	data = malloc(size);
+	if (!data)
+		return -ENOMEM;
+
+	res = i2c_read(CONFIG_SYS_VPD_EEPROM_I2C_ADDR, 0,
+		       CONFIG_SYS_VPD_EEPROM_I2C_ADDR_LEN,
+		       data, size);
+	if (res == 0)
+		res = vpd_reader(size, data, cache, process_block);
+
+	free(data);
+
+	i2c_set_bus_num(current_i2c_bus);
+	return res;
+}
diff --git a/board/ge/common/vpd_reader.h b/board/ge/common/vpd_reader.h
index e60acf3..3045b7e 100644
--- a/board/ge/common/vpd_reader.h
+++ b/board/ge/common/vpd_reader.h
@@ -5,12 +5,18 @@
 
 #include "common.h"
 
+struct vpd_cache;
+
 /*
- * Read VPD from given data, verify content, and call callback
- * for each vital product data block.
+ * Read VPD from given data, verify content, call callback for each vital
+ * product data block.
+ *
+ * cache: structure used by process block to store VPD information
+ * process_block: callback called for each VPD data block
  *
  * Returns Non-zero on error.  Negative numbers encode errno.
  */
-int vpd_reader(size_t size, u8 *data, void *userdata,
-	       int (*fn)(void *userdata, u8 id, u8 version, u8 type,
-			 size_t size, u8 const *data));
+int read_vpd(struct vpd_cache *cache,
+	     int (*process_block)(struct vpd_cache *,
+				  u8 id, u8 version, u8 type,
+				  size_t size, u8 const *data));