Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 1 | /* |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 2 | * Copyright 2006, 2008 Freescale Semiconductor |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 3 | * York Sun (yorksun@freescale.com) |
| 4 | * Haiying Wang (haiying.wang@freescale.com) |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 5 | * Timur Tabi (timur@freescale.com) |
Haiying Wang | 34c6878 | 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 | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 31 | #include "../common/eeprom.h" |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 32 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 33 | #if !defined(CONFIG_SYS_I2C_EEPROM_CCID) && !defined(CONFIG_SYS_I2C_EEPROM_NXID) |
| 34 | #error "Please define either CONFIG_SYS_I2C_EEPROM_CCID or CONFIG_SYS_I2C_EEPROM_NXID" |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 35 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 36 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 37 | /** |
| 38 | * static eeprom: EEPROM layout for CCID or NXID formats |
| 39 | * |
| 40 | * See application note AN3638 for details. |
| 41 | */ |
| 42 | static struct __attribute__ ((__packed__)) eeprom { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 43 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 44 | u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ |
| 45 | u8 major; /* 0x04 Board revision, major */ |
| 46 | u8 minor; /* 0x05 Board revision, minor */ |
| 47 | u8 sn[10]; /* 0x06 - 0x0F Serial Number*/ |
| 48 | u8 errata[2]; /* 0x10 - 0x11 Errata Level */ |
| 49 | u8 date[6]; /* 0x12 - 0x17 Build Date */ |
| 50 | u8 res_0[40]; /* 0x18 - 0x3f Reserved */ |
| 51 | u8 mac_count; /* 0x40 Number of MAC addresses */ |
| 52 | u8 mac_flag; /* 0x41 MAC table flags */ |
| 53 | u8 mac[8][6]; /* 0x42 - 0x71 MAC addresses */ |
| 54 | u32 crc; /* 0x72 CRC32 checksum */ |
| 55 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 56 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 57 | u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */ |
| 58 | u8 sn[12]; /* 0x04 - 0x0F Serial Number */ |
| 59 | u8 errata[5]; /* 0x10 - 0x14 Errata Level */ |
| 60 | u8 date[6]; /* 0x15 - 0x1a Build Date */ |
| 61 | u8 res_0; /* 0x1b Reserved */ |
| 62 | u32 version; /* 0x1c - 0x1f NXID Version */ |
| 63 | u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */ |
| 64 | u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */ |
| 65 | u8 tempcalflags; /* 0x2a Temperature Calibration Flags */ |
| 66 | u8 res_1[21]; /* 0x2b - 0x3f Reserved */ |
| 67 | u8 mac_count; /* 0x40 Number of MAC addresses */ |
| 68 | u8 mac_flag; /* 0x41 MAC table flags */ |
| 69 | u8 mac[8][6]; /* 0x42 - 0x71 MAC addresses */ |
| 70 | u32 crc; /* 0x72 CRC32 checksum */ |
| 71 | #endif |
| 72 | } e; |
| 73 | |
| 74 | /* Set to 1 if we've read EEPROM into memory */ |
| 75 | static int has_been_read = 0; |
| 76 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 77 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 78 | /* Is this a valid NXID EEPROM? */ |
| 79 | #define is_valid (*((u32 *)e.id) == (('N' << 24) | ('X' << 16) | ('I' << 8) | 'D')) |
| 80 | #endif |
| 81 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 82 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 83 | /* Is this a valid CCID EEPROM? */ |
| 84 | #define is_valid (*((u32 *)e.id) == (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D')) |
| 85 | #endif |
| 86 | |
| 87 | /** |
| 88 | * show_eeprom - display the contents of the EEPROM |
| 89 | */ |
| 90 | static void show_eeprom(void) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 91 | { |
| 92 | int i; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 93 | unsigned int crc; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 94 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 95 | /* EEPROM tag ID, either CCID or NXID */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 96 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 97 | printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3], |
| 98 | be32_to_cpu(e.version)); |
| 99 | #else |
| 100 | printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]); |
| 101 | #endif |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 102 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 103 | /* Serial number */ |
| 104 | printf("SN: %s\n", e.sn); |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 105 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 106 | /* Errata level. */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 107 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 108 | printf("Errata: %s\n", e.errata); |
| 109 | #else |
| 110 | printf("Errata: %c%c\n", |
| 111 | e.errata[0] ? e.errata[0] : '.', |
| 112 | e.errata[1] ? e.errata[1] : '.'); |
| 113 | #endif |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 114 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 115 | /* Build date, BCD date values, as YYMMDDhhmmss */ |
| 116 | printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n", |
| 117 | e.date[0], e.date[1], e.date[2], |
| 118 | e.date[3] & 0x7F, e.date[4], e.date[5], |
| 119 | e.date[3] & 0x80 ? "PM" : ""); |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 120 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 121 | /* Show MAC addresses */ |
| 122 | for (i = 0; i < min(e.mac_count, 8); i++) { |
| 123 | u8 *p = e.mac[i]; |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 124 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 125 | printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i, |
| 126 | p[0], p[1], p[2], p[3], p[4], p[5]); |
| 127 | } |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 128 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 129 | crc = crc32(0, (void *)&e, sizeof(e) - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 130 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 131 | if (crc == be32_to_cpu(e.crc)) |
| 132 | printf("CRC: %08x\n", be32_to_cpu(e.crc)); |
| 133 | else |
| 134 | printf("CRC: %08x (should be %08x)\n", |
| 135 | be32_to_cpu(e.crc), crc); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 136 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 137 | #ifdef DEBUG |
| 138 | printf("EEPROM dump: (0x%x bytes)\n", sizeof(e)); |
| 139 | for (i = 0; i < sizeof(e); i++) { |
| 140 | if ((i % 16) == 0) |
| 141 | printf("%02X: ", i); |
| 142 | printf("%02X ", ((u8 *)&e)[i]); |
| 143 | if (((i % 16) == 15) || (i == sizeof(e) - 1)) |
| 144 | printf("\n"); |
| 145 | } |
| 146 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 147 | } |
| 148 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 149 | /** |
| 150 | * read_eeprom - read the EEPROM into memory |
| 151 | */ |
| 152 | static int read_eeprom(void) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 153 | { |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 154 | int ret; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 155 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 156 | unsigned int bus; |
| 157 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 158 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 159 | if (has_been_read) |
| 160 | return 0; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 161 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 162 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 163 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 164 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 165 | #endif |
| 166 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 167 | ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 168 | (void *)&e, sizeof(e)); |
| 169 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 170 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 171 | i2c_set_bus_num(bus); |
| 172 | #endif |
| 173 | |
| 174 | #ifdef DEBUG |
| 175 | show_eeprom(); |
| 176 | #endif |
| 177 | |
| 178 | has_been_read = (ret == 0) ? 1 : 0; |
| 179 | |
| 180 | return ret; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 181 | } |
| 182 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 183 | /** |
| 184 | * prog_eeprom - write the EEPROM from memory |
| 185 | */ |
| 186 | static int prog_eeprom(void) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 187 | { |
| 188 | int ret, i, length; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 189 | unsigned int crc; |
| 190 | void *p; |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 191 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 192 | unsigned int bus; |
| 193 | #endif |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 194 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 195 | /* Set the reserved values to 0xFF */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 196 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 197 | e.res_0 = 0xFF; |
| 198 | memset(e.res_1, 0xFF, sizeof(e.res_1)); |
| 199 | #else |
| 200 | memset(e.res_0, 0xFF, sizeof(e.res_0)); |
| 201 | #endif |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame] | 202 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 203 | length = sizeof(e); |
| 204 | crc = crc32(0, (void *)&e, length - 4); |
| 205 | e.crc = cpu_to_be32(crc); |
| 206 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 207 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 208 | bus = i2c_get_bus_num(); |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 209 | i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 210 | #endif |
| 211 | |
| 212 | for (i = 0, p = &e; i < length; i += 8, p += 8) { |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 213 | ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 214 | p, min((length - i), 8)); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 215 | if (ret) |
| 216 | break; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 217 | udelay(5000); /* 5ms write cycle timing */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 218 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 219 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 220 | #ifdef CONFIG_SYS_EEPROM_BUS_NUM |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 221 | i2c_set_bus_num(bus); |
| 222 | #endif |
| 223 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 224 | if (ret) { |
| 225 | printf("Programming failed.\n"); |
| 226 | return -1; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 227 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 228 | |
| 229 | printf("Programming passed.\n"); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 230 | return 0; |
| 231 | } |
| 232 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 233 | /** |
| 234 | * h2i - converts hex character into a number |
| 235 | * |
| 236 | * This function takes a hexadecimal character (e.g. '7' or 'C') and returns |
| 237 | * the integer equivalent. |
| 238 | */ |
| 239 | static inline u8 h2i(char p) |
| 240 | { |
| 241 | if ((p >= '0') && (p <= '9')) |
| 242 | return p - '0'; |
| 243 | |
| 244 | if ((p >= 'A') && (p <= 'F')) |
| 245 | return (p - 'A') + 10; |
| 246 | |
| 247 | if ((p >= 'a') && (p <= 'f')) |
| 248 | return (p - 'a') + 10; |
| 249 | |
| 250 | return 0; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * set_date - stores the build date into the EEPROM |
| 255 | * |
| 256 | * This function takes a pointer to a string in the format "YYMMDDhhmmss" |
| 257 | * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string, |
| 258 | * and stores it in the build date field of the EEPROM local copy. |
| 259 | */ |
| 260 | static void set_date(const char *string) |
| 261 | { |
| 262 | unsigned int i; |
| 263 | |
| 264 | if (strlen(string) != 12) { |
| 265 | printf("Usage: mac date YYMMDDhhmmss\n"); |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | for (i = 0; i < 6; i++) |
| 270 | e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * set_mac_address - stores a MAC address into the EEPROM |
| 275 | * |
| 276 | * This function takes a pointer to MAC address string |
| 277 | * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and |
| 278 | * stores it in one of the MAC address fields of the EEPROM local copy. |
| 279 | */ |
| 280 | static void set_mac_address(unsigned int index, const char *string) |
| 281 | { |
| 282 | char *p = (char *) string; |
| 283 | unsigned int i; |
| 284 | |
| 285 | if (!string) { |
| 286 | printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n"); |
| 287 | return; |
| 288 | } |
| 289 | |
| 290 | for (i = 0; *p && (i < 6); i++) { |
| 291 | e.mac[index][i] = simple_strtoul(p, &p, 16); |
| 292 | if (*p == ':') |
| 293 | p++; |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 298 | { |
| 299 | int i; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 300 | char cmd; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 301 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 302 | if (argc == 1) { |
| 303 | show_eeprom(); |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | cmd = argv[1][0]; |
| 308 | |
| 309 | if (cmd == 'r') { |
| 310 | read_eeprom(); |
| 311 | return 0; |
| 312 | } |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 313 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 314 | if ((cmd == 'i') && (argc > 2)) { |
| 315 | for (i = 0; i < 4; i++) |
| 316 | e.id[i] = argv[2][i]; |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | if (!is_valid) { |
| 321 | printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n"); |
| 322 | return 0; |
| 323 | } |
| 324 | |
| 325 | if (argc == 2) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 326 | switch (cmd) { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 327 | case 's': /* save */ |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 328 | prog_eeprom(); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 329 | break; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 330 | default: |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame^] | 331 | cmd_usage(cmdtp); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 332 | break; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 333 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 334 | |
| 335 | return 0; |
| 336 | } |
| 337 | |
| 338 | /* We know we have at least one parameter */ |
| 339 | |
| 340 | switch (cmd) { |
| 341 | case 'n': /* serial number */ |
| 342 | memset(e.sn, 0, sizeof(e.sn)); |
| 343 | strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1); |
| 344 | break; |
| 345 | case 'e': /* errata */ |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 346 | #ifdef CONFIG_SYS_I2C_EEPROM_NXID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 347 | memset(e.errata, 0, 5); |
| 348 | strncpy((char *)e.errata, argv[2], 4); |
| 349 | #else |
| 350 | e.errata[0] = argv[2][0]; |
| 351 | e.errata[1] = argv[2][1]; |
| 352 | #endif |
| 353 | break; |
| 354 | case 'd': /* date BCD format YYMMDDhhmmss */ |
| 355 | set_date(argv[2]); |
| 356 | break; |
| 357 | case 'p': /* MAC table size */ |
| 358 | e.mac_count = simple_strtoul(argv[2], NULL, 16); |
| 359 | break; |
| 360 | case '0' ... '7': /* "mac 0" through "mac 7" */ |
| 361 | set_mac_address(cmd - '0', argv[2]); |
| 362 | break; |
| 363 | case 'h': /* help */ |
| 364 | default: |
Peter Tyser | ddb3af9 | 2009-01-27 18:03:10 -0600 | [diff] [blame^] | 365 | cmd_usage(cmdtp); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 366 | break; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 367 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 368 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 369 | return 0; |
| 370 | } |
| 371 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 372 | /** |
| 373 | * mac_read_from_eeprom - read the MAC addresses from EEPROM |
| 374 | * |
| 375 | * This function reads the MAC addresses from EEPROM and sets the |
| 376 | * appropriate environment variables for each one read. |
| 377 | * |
| 378 | * The environment variables are only set if they haven't been set already. |
| 379 | * This ensures that any user-saved variables are never overwritten. |
| 380 | * |
| 381 | * This function must be called after relocation. |
| 382 | */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 383 | int mac_read_from_eeprom(void) |
| 384 | { |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 385 | unsigned int i; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 386 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 387 | if (read_eeprom()) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 388 | printf("Read failed.\n"); |
| 389 | return -1; |
| 390 | } |
| 391 | |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 392 | if (!is_valid) { |
| 393 | printf("Invalid ID (%02x %02x %02x %02x)\n", e.id[0], e.id[1], e.id[2], e.id[3]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 394 | return -1; |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | if (be32_to_cpu(e.crc) != 0xFFFFFFFF) { |
| 398 | u32 crc = crc32(0, (void *)&e, sizeof(e) - 4); |
| 399 | |
| 400 | if (crc != be32_to_cpu(e.crc)) { |
| 401 | printf("CRC mismatch (%08x != %08x).\n", crc, |
| 402 | be32_to_cpu(e.crc)); |
| 403 | return -1; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 404 | } |
| 405 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 406 | |
| 407 | for (i = 0; i < min(4, e.mac_count); i++) { |
| 408 | if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) && |
| 409 | memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) { |
| 410 | char ethaddr[18]; |
| 411 | char enetvar[9]; |
| 412 | |
| 413 | sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X", |
| 414 | e.mac[i][0], |
| 415 | e.mac[i][1], |
| 416 | e.mac[i][2], |
| 417 | e.mac[i][3], |
| 418 | e.mac[i][4], |
| 419 | e.mac[i][5]); |
| 420 | sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i); |
| 421 | /* Only initialize environment variables that are blank |
| 422 | * (i.e. have not yet been set) |
| 423 | */ |
| 424 | if (!getenv(enetvar)) |
| 425 | setenv(enetvar, ethaddr); |
| 426 | } |
| 427 | } |
| 428 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 429 | return 0; |
| 430 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 431 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 432 | #ifdef CONFIG_SYS_I2C_EEPROM_CCID |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 433 | |
| 434 | /** |
| 435 | * get_cpu_board_revision - get the CPU board revision on 85xx boards |
| 436 | * |
| 437 | * Read the EEPROM to determine the board revision. |
| 438 | * |
| 439 | * This function is called before relocation, so we need to read a private |
| 440 | * copy of the EEPROM into a local variable on the stack. |
| 441 | * |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 442 | * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global |
| 443 | * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM, |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 444 | * so that the SPD code will work. This means that all pre-relocation I2C |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 445 | * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if |
| 446 | * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 447 | * this function is called. Oh well. |
| 448 | */ |
| 449 | unsigned int get_cpu_board_revision(void) |
| 450 | { |
| 451 | struct board_eeprom { |
| 452 | u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */ |
| 453 | u8 major; /* 0x04 Board revision, major */ |
| 454 | u8 minor; /* 0x05 Board revision, minor */ |
| 455 | } be; |
| 456 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 457 | i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN, |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 458 | (void *)&be, sizeof(be)); |
| 459 | |
| 460 | if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D')) |
| 461 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 462 | |
| 463 | if ((be.major == 0xff) && (be.minor == 0xff)) |
| 464 | return MPC85XX_CPU_BOARD_REV(0, 0); |
| 465 | |
Rafal Czubak | 5c50d14 | 2008-10-08 13:41:30 +0200 | [diff] [blame] | 466 | return MPC85XX_CPU_BOARD_REV(be.major, be.minor); |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 467 | } |
Timur Tabi | bdea837 | 2008-06-19 17:56:11 -0500 | [diff] [blame] | 468 | #endif |