Read the product name from the environment instead of EEPROM,
as the EEPROM may not be programmed and the environment always
contains this information.

Signed-off-by: Eric Chung <[email protected]>

---
v6:
- Rebased onto latest upstream. Cleaned up board_fit_config_name_match()
  to maintain consistentcy with v5.
---
 board/spacemit/k1/spl.c | 89 +++++++++++++++++++++----------------------------
 1 file changed, 38 insertions(+), 51 deletions(-)

diff --git a/board/spacemit/k1/spl.c b/board/spacemit/k1/spl.c
index 613e8c4cf02..69c992ea57a 100644
--- a/board/spacemit/k1/spl.c
+++ b/board/spacemit/k1/spl.c
@@ -12,9 +12,9 @@
 #include <configs/k1.h>
 #include <dm/device.h>
 #include <dm/uclass.h>
+#include <env.h>
 #include <i2c.h>
 #include <linux/bitfield.h>
-#include <linux/ctype.h>
 #include <linux/delay.h>
 #include <log.h>
 #include <power/regulator.h>
@@ -97,37 +97,41 @@ static void i2c_early_init(void)
        }
 }
 
-int read_product_name(char *name, int size)
+static const struct {
+       const char *eeprom_name;
+       const char *fit_name;
+} k1_board_map[] = {
+       { "k1-x_MUSE-Pi-Pro",   "spacemit/k1-musepi-pro"    },
+       { "k1-x_deb1",          "spacemit/k1-bananapi-f3"   },
+       { "k1-x_milkv-jupiter", "spacemit/k1-milkv-jupiter" },
+};
+
+static void fixup_product_name(void)
 {
-       u8 eeprom_data[TLV_TOTAL_LEN_MAX], *p;
-       struct tlvinfo_header *tlv_hdr;
-       struct tlvinfo_tlv *tlv_entry;
-       int ret, i = 0;
-       u32 entry_size;
+       char fdt_name[I2C_BUF_SIZE], *name;
+       int i;
 
-       if (!name || size <= 0)
-               return -EINVAL;
-       ret = read_tlvinfo_tlv_eeprom(eeprom_data, &tlv_hdr,
-                                     &tlv_entry, i);
-       if (ret)
-               return ret;
-       p = (u8 *)tlv_entry;
-       for (i = 0; i < tlv_hdr->totallen; ) {
-               if (tlv_entry->type == TLV_CODE_PRODUCT_NAME) {
-                       if (tlv_entry->length < size)
-                               size = tlv_entry->length;
-                       memset(name, 0, size);
-                       memcpy(name, &tlv_entry->value[0], size);
-                       return 0;
+       memset(product_name, 0, I2C_BUF_SIZE);
+       env_init();
+       env_load();
+       name = env_get("product_name");
+       if (name)
+               snprintf(product_name, I2C_BUF_SIZE, "%s", name);
+       memset(fdt_name, 0, I2C_BUF_SIZE);
+       for (i = 0; i < ARRAY_SIZE(k1_board_map); i++) {
+               if (!strncmp(product_name, k1_board_map[i].eeprom_name,
+                            strlen(k1_board_map[i].eeprom_name))) {
+                       snprintf(fdt_name, I2C_BUF_SIZE, "%s",
+                                k1_board_map[i].fit_name);
+                       break;
                }
-               if (tlv_entry->type == TLV_CODE_CRC_32)
-                       return -ENOENT;
-               entry_size = tlv_entry->length + sizeof(struct tlvinfo_tlv);
-               i += entry_size;
-               p += entry_size;
-               tlv_entry = (struct tlvinfo_tlv *)p;
        }
-       return -ENOENT;
+       if (fdt_name[0] == '\0') {
+               /* set default board name */
+               sprintf(fdt_name, CONFIG_DEFAULT_DEVICE_TREE);
+       }
+       memset(product_name, 0, I2C_BUF_SIZE);
+       memcpy(product_name, fdt_name, I2C_BUF_SIZE);
 }
 
 static void clk_early_init(void)
@@ -364,11 +368,6 @@ void board_init_f(ulong dummy)
        preloader_console_init();
 
        i2c_early_init();
-       ret = read_product_name(product_name, I2C_BUF_SIZE);
-       if (ret)
-               log_info("Fail to detect board:%d\n", ret);
-       else
-               log_info("Get board name:%s\n", product_name);
        pmic_init();
 
        ddr_early_init();
@@ -430,26 +429,14 @@ void spl_board_init(void)
 {
 }
 
-int board_fit_config_name_match(const char *name)
+void spl_perform_board_fixups(struct spl_image_info *spl_image)
 {
-       char fdt_name[I2C_BUF_SIZE];
-       int i;
+       fixup_product_name();
+}
 
-       memset(fdt_name, 0, I2C_BUF_SIZE);
-       if (!strncmp(product_name, "k1-x_", 5)) {
-               snprintf(fdt_name, I2C_BUF_SIZE, "%s-%s", "k1",
-                        &product_name[5]);
-       }
-       if (fdt_name[0] == '\0') {
-               /* set default board name */
-               sprintf(fdt_name, "k1-musepi-pro");
-       }
-       for (i = 0; i < I2C_BUF_SIZE; i++) {
-               if (fdt_name[i] == '\0')
-                       break;
-               fdt_name[i] = tolower(fdt_name[i]);
-       }
-       if (!strcmp(name, fdt_name))
+int board_fit_config_name_match(const char *name)
+{
+       if (!strcmp(name, product_name))
                return 0;
        return -ENOENT;
 }

-- 
2.51.0

Reply via email to