]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - board/freescale/common/sys_eeprom.c
fsl: add support for NXID v1 EEPROM format
[karo-tx-uboot.git] / board / freescale / common / sys_eeprom.c
index eb58c7fd57c0ab3a78d399756731bbe435fee59e..11dfd84fe6e72d9675b22ac907d37a0e51594762 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2006, 2008 Freescale Semiconductor
+ * Copyright 2006, 2008-2009 Freescale Semiconductor
  * York Sun (yorksun@freescale.com)
  * Haiying Wang (haiying.wang@freescale.com)
  * Timur Tabi (timur@freescale.com)
 #include <i2c.h>
 #include <linux/ctype.h>
 
+#ifdef CONFIG_SYS_I2C_EEPROM_CCID
 #include "../common/eeprom.h"
+#define MAX_NUM_PORTS  8
+#endif
+
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
+#define MAX_NUM_PORTS  8
+#define NXID_VERSION   0
+#endif
 
-#if !defined(CONFIG_SYS_I2C_EEPROM_CCID) && !defined(CONFIG_SYS_I2C_EEPROM_NXID)
-#error "Please define either CONFIG_SYS_I2C_EEPROM_CCID or CONFIG_SYS_I2C_EEPROM_NXID"
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID_1
+#define CONFIG_SYS_I2C_EEPROM_NXID
+#define MAX_NUM_PORTS  23
+#define NXID_VERSION   1
 #endif
 
 /**
@@ -50,7 +60,7 @@ static struct __attribute__ ((__packed__)) eeprom {
        u8 res_0[40];     /* 0x18 - 0x3f Reserved */
        u8 mac_count;     /* 0x40        Number of MAC addresses */
        u8 mac_flag;      /* 0x41        MAC table flags */
-       u8 mac[8][6];     /* 0x42 - 0x71 MAC addresses */
+       u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - 0x71 MAC addresses */
        u32 crc;          /* 0x72        CRC32 checksum */
 #endif
 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
@@ -66,8 +76,8 @@ static struct __attribute__ ((__packed__)) eeprom {
        u8 res_1[21];     /* 0x2b - 0x3f Reserved */
        u8 mac_count;     /* 0x40        Number of MAC addresses */
        u8 mac_flag;      /* 0x41        MAC table flags */
-       u8 mac[8][6];     /* 0x42 - 0x71 MAC addresses */
-       u32 crc;          /* 0x72        CRC32 checksum */
+       u8 mac[MAX_NUM_PORTS][6];     /* 0x42 - x MAC addresses */
+       u32 crc;          /* x+1         CRC32 checksum */
 #endif
 } e;
 
@@ -76,12 +86,14 @@ static int has_been_read = 0;
 
 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
 /* Is this a valid NXID EEPROM? */
-#define is_valid (*((u32 *)e.id) == (('N' << 24) | ('X' << 16) | ('I' << 8) | 'D'))
+#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
+                 (e.id[2] == 'I') || (e.id[3] == 'D'))
 #endif
 
 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
 /* Is this a valid CCID EEPROM? */
-#define is_valid (*((u32 *)e.id) == (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
+#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
+                 (e.id[2] == 'I') || (e.id[3] == 'D'))
 #endif
 
 /**
@@ -119,7 +131,8 @@ static void show_eeprom(void)
                e.date[3] & 0x80 ? "PM" : "");
 
        /* Show MAC addresses  */
-       for (i = 0; i < min(e.mac_count, 8); i++) {
+       for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
+
                u8 *p = e.mac[i];
 
                printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
@@ -180,13 +193,27 @@ static int read_eeprom(void)
        return ret;
 }
 
+/**
+ *  update_crc - update the CRC
+ *
+ *  This function should be called after each update to the EEPROM structure,
+ *  to make sure the CRC is always correct.
+ */
+static void update_crc(void)
+{
+       u32 crc;
+
+       crc = crc32(0, (void *)&e, sizeof(e) - 4);
+       e.crc = cpu_to_be32(crc);
+}
+
 /**
  * prog_eeprom - write the EEPROM from memory
  */
 static int prog_eeprom(void)
 {
-       int ret, i, length;
-       unsigned int crc;
+       int ret = 0;
+       int i;
        void *p;
 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
        unsigned int bus;
@@ -199,30 +226,43 @@ static int prog_eeprom(void)
 #else
        memset(e.res_0, 0xFF, sizeof(e.res_0));
 #endif
-
-       length = sizeof(e);
-       crc = crc32(0, (void *)&e, length - 4);
-       e.crc = cpu_to_be32(crc);
+       update_crc();
 
 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
        bus = i2c_get_bus_num();
        i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
 #endif
 
-       for (i = 0, p = &e; i < length; i += 8, p += 8) {
+       /*
+        * The AT24C02 datasheet says that data can only be written in page
+        * mode, which means 8 bytes at a time, and it takes up to 5ms to
+        * complete a given write.
+        */
+       for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
                ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
-                       p, min((length - i), 8));
+                       p, min((sizeof(e) - i), 8));
                if (ret)
                        break;
                udelay(5000);   /* 5ms write cycle timing */
        }
 
+       if (!ret) {
+               /* Verify the write by reading back the EEPROM and comparing */
+               struct eeprom e2;
+
+               ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
+                       CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
+               if (!ret && memcmp(&e, &e2, sizeof(e)))
+                       ret = -1;
+       }
+
 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
        i2c_set_bus_num(bus);
 #endif
 
        if (ret) {
                printf("Programming failed.\n");
+               has_been_read = 0;
                return -1;
        }
 
@@ -268,6 +308,8 @@ static void set_date(const char *string)
 
        for (i = 0; i < 6; i++)
                e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
+
+       update_crc();
 }
 
 /**
@@ -282,7 +324,7 @@ static void set_mac_address(unsigned int index, const char *string)
        char *p = (char *) string;
        unsigned int i;
 
-       if (!string) {
+       if ((index >= MAX_NUM_PORTS) || !string) {
                printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
                return;
        }
@@ -292,11 +334,12 @@ static void set_mac_address(unsigned int index, const char *string)
                if (*p == ':')
                        p++;
        }
+
+       update_crc();
 }
 
-int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
+int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 {
-       int i;
        char cmd;
 
        if (argc == 1) {
@@ -311,9 +354,13 @@ int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
                return 0;
        }
 
-       if ((cmd == 'i') && (argc > 2)) {
-               for (i = 0; i < 4; i++)
-                       e.id[i] = argv[2][i];
+       if (cmd == 'i') {
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
+               memcpy(e.id, "NXID", sizeof(e.id));
+               e.version = NXID_VERSION;
+#else
+               memcpy(e.id, "CCID", sizeof(e.id));
+#endif
                return 0;
        }
 
@@ -328,8 +375,7 @@ int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
                        prog_eeprom();
                        break;
                default:
-                       printf("Usage:\n%s\n", cmdtp->usage);
-                       break;
+                       return cmd_usage(cmdtp);
                }
 
                return 0;
@@ -341,6 +387,7 @@ int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
        case 'n':       /* serial number */
                memset(e.sn, 0, sizeof(e.sn));
                strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
+               update_crc();
                break;
        case 'e':       /* errata */
 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
@@ -350,20 +397,21 @@ int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
                e.errata[0] = argv[2][0];
                e.errata[1] = argv[2][1];
 #endif
+               update_crc();
                break;
        case 'd':       /* date BCD format YYMMDDhhmmss */
                set_date(argv[2]);
                break;
        case 'p':       /* MAC table size */
                e.mac_count = simple_strtoul(argv[2], NULL, 16);
+               update_crc();
                break;
-       case '0' ... '7':       /* "mac 0" through "mac 7" */
-               set_mac_address(cmd - '0', argv[2]);
+       case '0' ... '9':       /* "mac 0" through "mac 22" */
+               set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
                break;
        case 'h':       /* help */
        default:
-               printf("Usage:\n%s\n", cmdtp->usage);
-               break;
+               return cmd_usage(cmdtp);
        }
 
        return 0;
@@ -383,6 +431,9 @@ int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
 int mac_read_from_eeprom(void)
 {
        unsigned int i;
+       u32 crc;
+
+       puts("EEPROM: ");
 
        if (read_eeprom()) {
                printf("Read failed.\n");
@@ -390,21 +441,18 @@ int mac_read_from_eeprom(void)
        }
 
        if (!is_valid) {
-               printf("Invalid ID (%02x %02x %02x %02x)\n", e.id[0], e.id[1], e.id[2], e.id[3]);
+               printf("Invalid ID (%02x %02x %02x %02x)\n",
+                      e.id[0], e.id[1], e.id[2], e.id[3]);
                return -1;
        }
 
-       if (be32_to_cpu(e.crc) != 0xFFFFFFFF) {
-               u32 crc = crc32(0, (void *)&e, sizeof(e) - 4);
-
-               if (crc != be32_to_cpu(e.crc)) {
-                       printf("CRC mismatch (%08x != %08x).\n", crc,
-                               be32_to_cpu(e.crc));
-                       return -1;
-               }
+       crc = crc32(0, (void *)&e, sizeof(e) - 4);
+       if (crc != be32_to_cpu(e.crc)) {
+               printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
+               return -1;
        }
 
-       for (i = 0; i < min(4, e.mac_count); i++) {
+       for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
                if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
                    memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
                        char ethaddr[18];
@@ -426,6 +474,13 @@ int mac_read_from_eeprom(void)
                }
        }
 
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
+       printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
+               be32_to_cpu(e.version));
+#else
+       printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
+#endif
+
        return 0;
 }