]> git.kernelconcepts.de Git - karo-tx-uboot.git/blobdiff - board/freescale/common/sys_eeprom.c
Merge branch 'master' of git://git.denx.de/u-boot-usb
[karo-tx-uboot.git] / board / freescale / common / sys_eeprom.c
index 9bef92e8155603e6252317cd1d41015b397f0367..5cb7570c8b8a82c6bf6233fd753c8b991cdcb689 100644 (file)
@@ -1,26 +1,10 @@
 /*
- * Copyright 2006, 2008 Freescale Semiconductor
+ * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
  * York Sun (yorksun@freescale.com)
  * Haiying Wang (haiying.wang@freescale.com)
  * Timur Tabi (timur@freescale.com)
  *
- * See file CREDITS for list of people who contributed to this
- * project.
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License as
- * published by the Free Software Foundation; either version 2 of
- * the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
- * MA 02111-1307 USA
+ * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
 #include <i2c.h>
 #include <linux/ctype.h>
 
+#ifdef CONFIG_SYS_I2C_EEPROM_CCID
 #include "../common/eeprom.h"
+#define MAX_NUM_PORTS  8
+#endif
 
-#if !defined(CFG_I2C_EEPROM_CCID) && !defined(CFG_I2C_EEPROM_NXID)
-#error "Please define either CFG_I2C_EEPROM_CCID or CFG_I2C_EEPROM_NXID"
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
+/* some boards with non-256-bytes EEPROM have special define */
+/* for MAX_NUM_PORTS in board-specific file */
+#ifndef MAX_NUM_PORTS
+#define MAX_NUM_PORTS  16
+#endif
+#define NXID_VERSION   1
 #endif
 
 /**
@@ -40,7 +32,7 @@
  * See application note AN3638 for details.
  */
 static struct __attribute__ ((__packed__)) eeprom {
-#ifdef CFG_I2C_EEPROM_CCID
+#ifdef CONFIG_SYS_I2C_EEPROM_CCID
        u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'CCID' */
        u8 major;         /* 0x04        Board revision, major */
        u8 minor;         /* 0x05        Board revision, minor */
@@ -50,10 +42,10 @@ 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 CFG_I2C_EEPROM_NXID
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
        u8 id[4];         /* 0x00 - 0x03 EEPROM Tag 'NXID' */
        u8 sn[12];        /* 0x04 - 0x0F Serial Number */
        u8 errata[5];     /* 0x10 - 0x14 Errata Level */
@@ -66,22 +58,25 @@ 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 - 0xa1 MAC addresses */
+       u8 res_2[90];     /* 0xa2 - 0xfb Reserved */    
+       u32 crc;          /* 0xfc - 0xff CRC32 checksum */
 #endif
 } e;
 
 /* Set to 1 if we've read EEPROM into memory */
 static int has_been_read = 0;
 
-#ifdef CFG_I2C_EEPROM_NXID
+#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 CFG_I2C_EEPROM_CCID
+#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
 
 /**
@@ -93,9 +88,9 @@ static void show_eeprom(void)
        unsigned int crc;
 
        /* EEPROM tag ID, either CCID or NXID */
-#ifdef CFG_I2C_EEPROM_NXID
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
        printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
-               be32_to_cpu(e.version));
+              e.version);
 #else
        printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
 #endif
@@ -104,7 +99,7 @@ static void show_eeprom(void)
        printf("SN: %s\n", e.sn);
 
        /* Errata level. */
-#ifdef CFG_I2C_EEPROM_NXID
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
        printf("Errata: %s\n", e.errata);
 #else
        printf("Errata: %c%c\n",
@@ -119,7 +114,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, (u8)MAX_NUM_PORTS); i++) {
+
                u8 *p = e.mac[i];
 
                printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
@@ -152,22 +148,22 @@ static void show_eeprom(void)
 static int read_eeprom(void)
 {
        int ret;
-#ifdef CFG_EEPROM_BUS_NUM
+#ifdef CONFIG_SYS_EEPROM_BUS_NUM
        unsigned int bus;
 #endif
 
        if (has_been_read)
                return 0;
 
-#ifdef CFG_EEPROM_BUS_NUM
+#ifdef CONFIG_SYS_EEPROM_BUS_NUM
        bus = i2c_get_bus_num();
-       i2c_set_bus_num(CFG_EEPROM_BUS_NUM);
+       i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
 #endif
 
-       ret = i2c_read(CFG_I2C_EEPROM_ADDR, 0, CFG_I2C_EEPROM_ADDR_LEN,
+       ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
                (void *)&e, sizeof(e));
 
-#ifdef CFG_EEPROM_BUS_NUM
+#ifdef CONFIG_SYS_EEPROM_BUS_NUM
        i2c_set_bus_num(bus);
 #endif
 
@@ -180,49 +176,76 @@ 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 CFG_EEPROM_BUS_NUM
+#ifdef CONFIG_SYS_EEPROM_BUS_NUM
        unsigned int bus;
 #endif
 
        /* Set the reserved values to 0xFF   */
-#ifdef CFG_I2C_EEPROM_NXID
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
        e.res_0 = 0xFF;
        memset(e.res_1, 0xFF, sizeof(e.res_1));
 #else
        memset(e.res_0, 0xFF, sizeof(e.res_0));
 #endif
+       update_crc();
 
-       length = sizeof(e);
-       crc = crc32(0, (void *)&e, length - 4);
-       e.crc = cpu_to_be32(crc);
-
-#ifdef CFG_EEPROM_BUS_NUM
+#ifdef CONFIG_SYS_EEPROM_BUS_NUM
        bus = i2c_get_bus_num();
-       i2c_set_bus_num(CFG_EEPROM_BUS_NUM);
+       i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
 #endif
 
-       for (i = 0, p = &e; i < length; i += 8, p += 8) {
-               ret = i2c_write(CFG_I2C_EEPROM_ADDR, i, CFG_I2C_EEPROM_ADDR_LEN,
-                       p, min((length - i), 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((int)(sizeof(e) - i), 8));
                if (ret)
                        break;
                udelay(5000);   /* 5ms write cycle timing */
        }
 
-#ifdef CFG_EEPROM_BUS_NUM
+       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 +291,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 +307,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 +317,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 +337,14 @@ 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
+               update_crc();
                return 0;
        }
 
@@ -328,8 +359,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,29 +371,31 @@ 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 CFG_I2C_EEPROM_NXID
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
                memset(e.errata, 0, 5);
                strncpy((char *)e.errata, argv[2], 4);
 #else
                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;
@@ -379,32 +411,57 @@ int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
  * This ensures that any user-saved variables are never overwritten.
  *
  * This function must be called after relocation.
+ *
+ * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
+ * format.  In a v0 EEPROM, there are only eight MAC addresses and the CRC is
+ * located at a different offset.
  */
 int mac_read_from_eeprom(void)
 {
        unsigned int i;
+       u32 crc, crc_offset = offsetof(struct eeprom, crc);
+       u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
+
+       puts("EEPROM: ");
 
        if (read_eeprom()) {
                printf("Read failed.\n");
-               return -1;
+               return 0;
        }
 
        if (!is_valid) {
-               printf("Invalid ID (%02x %02x %02x %02x)\n", e.id[0], e.id[1], e.id[2], e.id[3]);
-               return -1;
+               printf("Invalid ID (%02x %02x %02x %02x)\n",
+                      e.id[0], e.id[1], e.id[2], e.id[3]);
+               return 0;
        }
 
-       if (be32_to_cpu(e.crc) != 0xFFFFFFFF) {
-               u32 crc = crc32(0, (void *)&e, sizeof(e) - 4);
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
+       /*
+        * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
+        * to where it is in v0.
+        */
+       if (e.version == 0)
+               crc_offset = 0x72;
+#endif
 
-               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, crc_offset);
+       crcp = (void *)&e + crc_offset;
+       if (crc != be32_to_cpu(*crcp)) {
+               printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
+               return 0;
        }
 
-       for (i = 0; i < min(4, e.mac_count); i++) {
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
+       /*
+        * MAC address #9 in v1 occupies the same position as the CRC in v0.
+        * Erase it so that it's not mistaken for a MAC address.  We'll
+        * update the CRC later.
+        */
+       if (e.version == 0)
+               memset(e.mac[8], 0xff, 6);
+#endif
+
+       for (i = 0; i < min(e.mac_count, (u8)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,10 +483,28 @@ 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],
+              e.version);
+#else
+       printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
+#endif
+
+#ifdef CONFIG_SYS_I2C_EEPROM_NXID
+       /*
+        * Now we need to upconvert the data into v1 format.  We do this last so
+        * that at boot time, U-Boot will still say "NXID v0".
+        */
+       if (e.version == 0) {
+               e.version = NXID_VERSION;
+               update_crc();
+       }
+#endif
+
        return 0;
 }
 
-#ifdef CFG_I2C_EEPROM_CCID
+#ifdef CONFIG_SYS_I2C_EEPROM_CCID
 
 /**
  * get_cpu_board_revision - get the CPU board revision on 85xx boards
@@ -439,11 +514,11 @@ int mac_read_from_eeprom(void)
  * This function is called before relocation, so we need to read a private
  * copy of the EEPROM into a local variable on the stack.
  *
- * Also, we assume that CFG_EEPROM_BUS_NUM == CFG_SPD_BUS_NUM.  The global
- * variable i2c_bus_num must be compile-time initialized to CFG_SPD_BUS_NUM,
+ * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM.  The global
+ * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
  * so that the SPD code will work.  This means that all pre-relocation I2C
- * operations can only occur on the CFG_SPD_BUS_NUM bus.  So if
- * CFG_EEPROM_BUS_NUM != CFG_SPD_BUS_NUM, then we can't read the EEPROM when
+ * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus.  So if
+ * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
  * this function is called.  Oh well.
  */
 unsigned int get_cpu_board_revision(void)
@@ -454,7 +529,7 @@ unsigned int get_cpu_board_revision(void)
                u8 minor;         /* 0x05        Board revision, minor */
        } be;
 
-       i2c_read(CFG_I2C_EEPROM_ADDR, 0, CFG_I2C_EEPROM_ADDR_LEN,
+       i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
                (void *)&be, sizeof(be));
 
        if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))