Merge tag 'u-boot-atmel-fixes-2021.01-b' of https://gitlab.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / board / ge / common / vpd_reader.c
index 7367427..c28d2c0 100644 (file)
@@ -1,13 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * Copyright 2016 General Electric Company
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include "vpd_reader.h"
+#include <malloc.h>
 
+#include <i2c.h>
 #include <linux/bch.h>
 #include <stdlib.h>
+#include <dm/uclass.h>
+#include <i2c_eeprom.h>
+#include <hexdump.h>
 
 /* BCH configuration */
 
@@ -106,8 +110,8 @@ static const size_t HEADER_BLOCK_ECC_LEN = 4;
 
 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,
+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)
@@ -195,3 +199,39 @@ int vpd_reader(size_t size, u8 *data, void *userdata,
                        return ret;
        }
 }
+
+int read_i2c_vpd(struct vpd_cache *cache,
+                int (*process_block)(struct vpd_cache *, u8 id, u8 version,
+                                     u8 type, size_t size, u8 const *data))
+{
+       struct udevice *dev;
+       int ret;
+       u8 *data;
+       int size;
+
+       ret = uclass_get_device_by_name(UCLASS_I2C_EEPROM, "vpd@0", &dev);
+       if (ret)
+               return ret;
+
+       size = i2c_eeprom_size(dev);
+       if (size < 0) {
+               printf("Unable to get size of eeprom: %d\n", ret);
+               return ret;
+       }
+
+       data = malloc(size);
+       if (!data)
+               return -ENOMEM;
+
+       ret = i2c_eeprom_read(dev, 0, data, size);
+       if (ret) {
+               free(data);
+               return ret;
+       }
+
+       ret = vpd_reader(size, data, cache, process_block);
+
+       free(data);
+
+       return ret;
+}