Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 1 | /* |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame^] | 2 | * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 3 | * York Sun (yorksun@freescale.com) |
| 4 | * Haiying Wang (haiying.wang@freescale.com) |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 5 | * Timur Tabi (timur@freescale.com) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 6 | * |
| 7 | * See file CREDITS for list of people who contributed to this |
| 8 | * project. |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or |
| 11 | * modify it under the terms of the GNU General Public License as |
| 12 | * published by the Free Software Foundation; either version 2 of |
| 13 | * the License, or (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program; if not, write to the Free Software |
| 22 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 23 | * MA 02111-1307 USA |
| 24 | */ |
| 25 | |
| 26 | #include <common.h> |
| 27 | #include <command.h> |
| 28 | #include <i2c.h> |
| 29 | #include <linux/ctype.h> |
| 30 | |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 31 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 32 | #include "../common/eeprom.h" |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 33 | #define MAX_NUM_PORTS 8 |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 34 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 35 | |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 36 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 37 | #define MAX_NUM_PORTS 23 |
| 38 | #define NXID_VERSION 1 |
| 39 | #endif |
Haiying Wang | 9a61108 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 40 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 41 | /** |
| 42 | * static eeprom: EEPROM layout for CCID or NXID formats |
| 43 | * |
| 44 | * See application note AN3638 for details. |
| 45 | */ |
| 46 | static struct __attribute__ ((__packed__)) eeprom { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 47 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 48 | u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ |
| 49 | u8 major; /* 0x04 Board revision, major */ |
| 50 | u8 minor; /* 0x05 Board revision, minor */ |
| 51 | u8 sn[10]; /* 0x06 - 0x0F Serial Number*/ |
| 52 | u8 errata[2]; /* 0x10 - 0x11 Errata Level */ |
| 53 | u8 date[6]; /* 0x12 - 0x17 Build Date */ |
| 54 | u8 res_0[40]; /* 0x18 - 0x3f Reserved */ |
| 55 | u8 mac_count; /* 0x40 Number of MAC addresses */ |
| 56 | u8 mac_flag; /* 0x41 MAC table flags */ |
Haiying Wang | 9a61108 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 57 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */ |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 58 | u32 crc; /* 0x72 CRC32 checksum */ |
| 59 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 60 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 61 | u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */ |
| 62 | u8 sn[12]; /* 0x04 - 0x0F Serial Number */ |
| 63 | u8 errata[5]; /* 0x10 - 0x14 Errata Level */ |
| 64 | u8 date[6]; /* 0x15 - 0x1a Build Date */ |
| 65 | u8 res_0; /* 0x1b Reserved */ |
| 66 | u32 version; /* 0x1c - 0x1f NXID Version */ |
| 67 | u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */ |
| 68 | u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */ |
| 69 | u8 tempcalflags; /* 0x2a Temperature Calibration Flags */ |
| 70 | u8 res_1[21]; /* 0x2b - 0x3f Reserved */ |
| 71 | u8 mac_count; /* 0x40 Number of MAC addresses */ |
| 72 | u8 mac_flag; /* 0x41 MAC table flags */ |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 73 | u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */ |
| 74 | u32 crc; /* x+1 CRC32 checksum */ |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 75 | #endif |
| 76 | } e; |
| 77 | |
| 78 | /* Set to 1 if we've read EEPROM into memory */ |
| 79 | static int has_been_read = 0; |
| 80 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 81 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 82 | /* Is this a valid NXID EEPROM? */ |
Kumar Gala | afb0b131 | 2009-07-03 12:45:44 -0500 | [diff] [blame] | 83 | #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \ |
| 84 | (e.id[2] == 'I') || (e.id[3] == 'D')) |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 85 | #endif |
| 86 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 87 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 88 | /* Is this a valid CCID EEPROM? */ |
Kumar Gala | afb0b131 | 2009-07-03 12:45:44 -0500 | [diff] [blame] | 89 | #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \ |
| 90 | (e.id[2] == 'I') || (e.id[3] == 'D')) |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 91 | #endif |
| 92 | |
| 93 | /** |
| 94 | * show_eeprom - display the contents of the EEPROM |
| 95 | */ |
| 96 | static void show_eeprom(void) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 97 | { |
| 98 | int i; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 99 | unsigned int crc; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 100 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 101 | /* EEPROM tag ID, either CCID or NXID */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 102 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 103 | printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], |
| 104 | be32_to_cpu(e.version)); |
| 105 | #else |
| 106 | printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); |
| 107 | #endif |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 108 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 109 | /* Serial number */ |
| 110 | printf("SN: %s\n", e.sn); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 111 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 112 | /* Errata level. */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 113 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 114 | printf("Errata: %s\n", e.errata); |
| 115 | #else |
| 116 | printf("Errata: %c%c\n", |
| 117 | e.errata[0] ? e.errata[0] : '.', |
| 118 | e.errata[1] ? e.errata[1] : '.'); |
| 119 | #endif |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 120 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 121 | /* Build date, BCD date values, as YYMMDDhhmmss */ |
| 122 | printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n", |
| 123 | e.date[0], e.date[1], e.date[2], |
| 124 | e.date[3] & 0x7F, e.date[4], e.date[5], |
| 125 | e.date[3] & 0x80 ? "PM" : ""); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 126 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 127 | /* Show MAC addresses */ |
Haiying Wang | 9a61108 | 2009-06-04 16:12:40 -0400 | [diff] [blame] | 128 | for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) { |
| 129 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 130 | u8 *p = e.mac[i]; |
| 131 | |
| 132 | printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i, |
| 133 | p[0], p[1], p[2], p[3], p[4], p[5]); |
| 134 | } |
| 135 | |
| 136 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 137 | |
| 138 | if (crc == be32_to_cpu(e.crc)) |
| 139 | printf("CRC: %08x\n", be32_to_cpu(e.crc)); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 140 | else |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 141 | printf("CRC: %08x (should be %08x)\n", |
| 142 | be32_to_cpu(e.crc), crc); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 143 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 144 | #ifdef DEBUG |
| 145 | printf("EEPROM dump: (0x%x bytes)\n", sizeof(e)); |
| 146 | for (i = 0; i < sizeof(e); i++) { |
| 147 | if ((i % 16) == 0) |
| 148 | printf("%02X: ", i); |
| 149 | printf("%02X ", ((u8 *)&e)[i]); |
| 150 | if (((i % 16) == 15) || (i == sizeof(e) - 1)) |
| 151 | printf("\n"); |
Haiying Wang | d59feff | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 152 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 153 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 154 | } |
| 155 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 156 | /** |
| 157 | * read_eeprom - read the EEPROM into memory |
| 158 | */ |
| 159 | static int read_eeprom(void) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 160 | { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 161 | int ret; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 162 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 163 | unsigned int bus; |
| 164 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 165 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 166 | if (has_been_read) |
| 167 | return 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 168 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 169 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 170 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 171 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 172 | #endif |
| 173 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 174 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 175 | (void *)&e, sizeof(e)); |
| 176 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 177 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 178 | i2c_set_bus_num(bus); |
| 179 | #endif |
| 180 | |
| 181 | #ifdef DEBUG |
| 182 | show_eeprom(); |
| 183 | #endif |
| 184 | |
| 185 | has_been_read = (ret == 0) ? 1 : 0; |
| 186 | |
| 187 | return ret; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 188 | } |
| 189 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 190 | /** |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 191 | * update_crc - update the CRC |
| 192 | * |
| 193 | * This function should be called after each update to the EEPROM structure, |
| 194 | * to make sure the CRC is always correct. |
| 195 | */ |
| 196 | static void update_crc(void) |
| 197 | { |
| 198 | u32 crc; |
| 199 | |
| 200 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 201 | e.crc = cpu_to_be32(crc); |
| 202 | } |
| 203 | |
| 204 | /** |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 205 | * prog_eeprom - write the EEPROM from memory |
| 206 | */ |
| 207 | static int prog_eeprom(void) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 208 | { |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 209 | int ret = 0; |
Anton Vorontsov | 9c671e7 | 2009-09-02 02:17:24 +0400 | [diff] [blame] | 210 | int i; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 211 | void *p; |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 212 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 213 | unsigned int bus; |
| 214 | #endif |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 215 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 216 | /* Set the reserved values to 0xFF */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 217 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 218 | e.res_0 = 0xFF; |
| 219 | memset(e.res_1, 0xFF, sizeof(e.res_1)); |
| 220 | #else |
| 221 | memset(e.res_0, 0xFF, sizeof(e.res_0)); |
| 222 | #endif |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 223 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 224 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 225 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 226 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 227 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 228 | #endif |
| 229 | |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 230 | /* |
| 231 | * The AT24C02 datasheet says that data can only be written in page |
| 232 | * mode, which means 8 bytes at a time, and it takes up to 5ms to |
| 233 | * complete a given write. |
| 234 | */ |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 235 | for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) { |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 236 | ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 237 | p, min((sizeof(e) - i), 8)); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 238 | if (ret) |
| 239 | break; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 240 | udelay(5000); /* 5ms write cycle timing */ |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 241 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 242 | |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 243 | if (!ret) { |
| 244 | /* Verify the write by reading back the EEPROM and comparing */ |
| 245 | struct eeprom e2; |
| 246 | |
| 247 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, |
| 248 | CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2)); |
| 249 | if (!ret && memcmp(&e, &e2, sizeof(e))) |
| 250 | ret = -1; |
| 251 | } |
| 252 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 253 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 254 | i2c_set_bus_num(bus); |
| 255 | #endif |
| 256 | |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 257 | if (ret) { |
| 258 | printf("Programming failed.\n"); |
Timur Tabi | 3addcb9 | 2010-08-02 13:03:23 -0500 | [diff] [blame] | 259 | has_been_read = 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 260 | return -1; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 261 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 262 | |
| 263 | printf("Programming passed.\n"); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 264 | return 0; |
| 265 | } |
| 266 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 267 | /** |
| 268 | * h2i - converts hex character into a number |
| 269 | * |
| 270 | * This function takes a hexadecimal character (e.g. '7' or 'C') and returns |
| 271 | * the integer equivalent. |
| 272 | */ |
| 273 | static inline u8 h2i(char p) |
| 274 | { |
| 275 | if ((p >= '0') && (p <= '9')) |
| 276 | return p - '0'; |
| 277 | |
| 278 | if ((p >= 'A') && (p <= 'F')) |
| 279 | return (p - 'A') + 10; |
| 280 | |
| 281 | if ((p >= 'a') && (p <= 'f')) |
| 282 | return (p - 'a') + 10; |
| 283 | |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | /** |
| 288 | * set_date - stores the build date into the EEPROM |
| 289 | * |
| 290 | * This function takes a pointer to a string in the format "YYMMDDhhmmss" |
| 291 | * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string, |
| 292 | * and stores it in the build date field of the EEPROM local copy. |
| 293 | */ |
| 294 | static void set_date(const char *string) |
| 295 | { |
| 296 | unsigned int i; |
| 297 | |
| 298 | if (strlen(string) != 12) { |
| 299 | printf("Usage: mac date YYMMDDhhmmss\n"); |
| 300 | return; |
| 301 | } |
| 302 | |
| 303 | for (i = 0; i < 6; i++) |
| 304 | e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 305 | |
| 306 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | /** |
| 310 | * set_mac_address - stores a MAC address into the EEPROM |
| 311 | * |
| 312 | * This function takes a pointer to MAC address string |
| 313 | * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and |
| 314 | * stores it in one of the MAC address fields of the EEPROM local copy. |
| 315 | */ |
| 316 | static void set_mac_address(unsigned int index, const char *string) |
| 317 | { |
| 318 | char *p = (char *) string; |
| 319 | unsigned int i; |
| 320 | |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 321 | if ((index >= MAX_NUM_PORTS) || !string) { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 322 | printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n"); |
| 323 | return; |
| 324 | } |
| 325 | |
| 326 | for (i = 0; *p && (i < 6); i++) { |
| 327 | e.mac[index][i] = simple_strtoul(p, &p, 16); |
| 328 | if (*p == ':') |
| 329 | p++; |
| 330 | } |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 331 | |
| 332 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 333 | } |
| 334 | |
Wolfgang Denk | 54841ab | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 335 | int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 336 | { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 337 | char cmd; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 338 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 339 | if (argc == 1) { |
| 340 | show_eeprom(); |
| 341 | return 0; |
| 342 | } |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 343 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 344 | cmd = argv[1][0]; |
| 345 | |
| 346 | if (cmd == 'r') { |
| 347 | read_eeprom(); |
| 348 | return 0; |
| 349 | } |
| 350 | |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 351 | if (cmd == 'i') { |
| 352 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 353 | memcpy(e.id, "NXID", sizeof(e.id)); |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 354 | e.version = NXID_VERSION; |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 355 | #else |
| 356 | memcpy(e.id, "CCID", sizeof(e.id)); |
| 357 | #endif |
Timur Tabi | 3fee334 | 2011-02-09 13:40:51 -0600 | [diff] [blame] | 358 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 359 | return 0; |
| 360 | } |
| 361 | |
| 362 | if (!is_valid) { |
| 363 | printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n"); |
| 364 | return 0; |
| 365 | } |
| 366 | |
| 367 | if (argc == 2) { |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 368 | switch (cmd) { |
Jon Loeliger | 80e955c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 369 | case 's': /* save */ |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 370 | prog_eeprom(); |
Jon Loeliger | 80e955c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 371 | break; |
Jon Loeliger | 80e955c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 372 | default: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 373 | return cmd_usage(cmdtp); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 374 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 375 | |
| 376 | return 0; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 377 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 378 | |
| 379 | /* We know we have at least one parameter */ |
| 380 | |
| 381 | switch (cmd) { |
| 382 | case 'n': /* serial number */ |
| 383 | memset(e.sn, 0, sizeof(e.sn)); |
| 384 | strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 385 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 386 | break; |
| 387 | case 'e': /* errata */ |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 388 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 389 | memset(e.errata, 0, 5); |
| 390 | strncpy((char *)e.errata, argv[2], 4); |
| 391 | #else |
| 392 | e.errata[0] = argv[2][0]; |
| 393 | e.errata[1] = argv[2][1]; |
| 394 | #endif |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 395 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 396 | break; |
| 397 | case 'd': /* date BCD format YYMMDDhhmmss */ |
| 398 | set_date(argv[2]); |
| 399 | break; |
| 400 | case 'p': /* MAC table size */ |
| 401 | e.mac_count = simple_strtoul(argv[2], NULL, 16); |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 402 | update_crc(); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 403 | break; |
Timur Tabi | bfb7071 | 2010-09-30 15:36:50 -0500 | [diff] [blame] | 404 | case '0' ... '9': /* "mac 0" through "mac 22" */ |
| 405 | set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 406 | break; |
| 407 | case 'h': /* help */ |
| 408 | default: |
Wolfgang Denk | 47e26b1 | 2010-07-17 01:06:04 +0200 | [diff] [blame] | 409 | return cmd_usage(cmdtp); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 410 | } |
| 411 | |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 412 | return 0; |
| 413 | } |
| 414 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 415 | /** |
| 416 | * mac_read_from_eeprom - read the MAC addresses from EEPROM |
| 417 | * |
| 418 | * This function reads the MAC addresses from EEPROM and sets the |
| 419 | * appropriate environment variables for each one read. |
| 420 | * |
| 421 | * The environment variables are only set if they haven't been set already. |
| 422 | * This ensures that any user-saved variables are never overwritten. |
| 423 | * |
| 424 | * This function must be called after relocation. |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame^] | 425 | * |
| 426 | * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0 |
| 427 | * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is |
| 428 | * located at a different offset. |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 429 | */ |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 430 | int mac_read_from_eeprom(void) |
| 431 | { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 432 | unsigned int i; |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame^] | 433 | u32 crc, crc_offset = offsetof(struct eeprom, crc); |
| 434 | u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */ |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 435 | |
| 436 | puts("EEPROM: "); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 437 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 438 | if (read_eeprom()) { |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 439 | printf("Read failed.\n"); |
| 440 | return -1; |
| 441 | } |
| 442 | |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 443 | if (!is_valid) { |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 444 | printf("Invalid ID (%02x %02x %02x %02x)\n", |
| 445 | e.id[0], e.id[1], e.id[2], e.id[3]); |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 446 | return -1; |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 447 | } |
| 448 | |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame^] | 449 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 450 | /* |
| 451 | * If we've read an NXID v0 EEPROM, then we need to set the CRC offset |
| 452 | * to where it is in v0. |
| 453 | */ |
| 454 | if (e.version == 0) |
| 455 | crc_offset = 0x72; |
| 456 | #endif |
| 457 | |
| 458 | crc = crc32(0, (void *)&e, crc_offset); |
| 459 | crcp = (void *)&e + crc_offset; |
| 460 | if (crc != be32_to_cpu(*crcp)) { |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 461 | printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc)); |
| 462 | return -1; |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 463 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 464 | |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame^] | 465 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 466 | /* |
| 467 | * MAC address #9 in v1 occupies the same position as the CRC in v0. |
| 468 | * Erase it so that it's not mistaken for a MAC address. We'll |
| 469 | * update the CRC later. |
| 470 | */ |
| 471 | if (e.version == 0) |
| 472 | memset(e.mac[8], 0xff, 6); |
| 473 | #endif |
| 474 | |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 475 | for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) { |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 476 | if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) && |
| 477 | memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { |
| 478 | char ethaddr[18]; |
| 479 | char enetvar[9]; |
| 480 | |
| 481 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 482 | e.mac[i][0], |
| 483 | e.mac[i][1], |
| 484 | e.mac[i][2], |
| 485 | e.mac[i][3], |
| 486 | e.mac[i][4], |
| 487 | e.mac[i][5]); |
| 488 | sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); |
| 489 | /* Only initialize environment variables that are blank |
| 490 | * (i.e. have not yet been set) |
| 491 | */ |
| 492 | if (!getenv(enetvar)) |
| 493 | setenv(enetvar, ethaddr); |
| 494 | } |
| 495 | } |
| 496 | |
Timur Tabi | 2d04db0 | 2009-08-28 16:56:45 -0500 | [diff] [blame] | 497 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 498 | printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], |
| 499 | be32_to_cpu(e.version)); |
| 500 | #else |
| 501 | printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); |
| 502 | #endif |
| 503 | |
Timur Tabi | f098c9c | 2011-02-09 02:00:09 +0000 | [diff] [blame^] | 504 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
| 505 | /* |
| 506 | * Now we need to upconvert the data into v1 format. We do this last so |
| 507 | * that at boot time, U-Boot will still say "NXID v0". |
| 508 | */ |
| 509 | if (e.version == 0) { |
| 510 | e.version = NXID_VERSION; |
| 511 | update_crc(); |
| 512 | } |
| 513 | #endif |
| 514 | |
Haiying Wang | bea3f28 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 515 | return 0; |
| 516 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 517 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 518 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 519 | |
| 520 | /** |
| 521 | * get_cpu_board_revision - get the CPU board revision on 85xx boards |
| 522 | * |
| 523 | * Read the EEPROM to determine the board revision. |
| 524 | * |
| 525 | * This function is called before relocation, so we need to read a private |
| 526 | * copy of the EEPROM into a local variable on the stack. |
| 527 | * |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 528 | * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global |
| 529 | * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM, |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 530 | * so that the SPD code will work. This means that all pre-relocation I2C |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 531 | * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if |
| 532 | * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 533 | * this function is called. Oh well. |
| 534 | */ |
| 535 | unsigned int get_cpu_board_revision(void) |
| 536 | { |
| 537 | struct board_eeprom { |
| 538 | u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ |
| 539 | u8 major; /* 0x04 Board revision, major */ |
| 540 | u8 minor; /* 0x05 Board revision, minor */ |
| 541 | } be; |
| 542 | |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 543 | i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 544 | (void *)&be, sizeof(be)); |
| 545 | |
| 546 | if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D')) |
| 547 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 548 | |
| 549 | if ((be.major == 0xff) && (be.minor == 0xff)) |
| 550 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 551 | |
Rafal Czubak | e46c7bf | 2008-10-08 13:41:30 +0200 | [diff] [blame] | 552 | return MPC85XX_CPU_BOARD_REV(be.major, be.minor); |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 553 | } |
Timur Tabi | e2d31fb | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 554 | #endif |