Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2006 Freescale Semiconductor |
| 3 | * York Sun (yorksun@freescale.com) |
| 4 | * Haiying Wang (haiying.wang@freescale.com) |
| 5 | * |
| 6 | * See file CREDITS for list of people who contributed to this |
| 7 | * project. |
| 8 | * |
| 9 | * This program is free software; you can redistribute it and/or |
| 10 | * modify it under the terms of the GNU General Public License as |
| 11 | * published by the Free Software Foundation; either version 2 of |
| 12 | * the License, or (at your option) any later version. |
| 13 | * |
| 14 | * This program is distributed in the hope that it will be useful, |
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | * GNU General Public License for more details. |
| 18 | * |
| 19 | * You should have received a copy of the GNU General Public License |
| 20 | * along with this program; if not, write to the Free Software |
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 22 | * MA 02111-1307 USA |
| 23 | */ |
| 24 | |
| 25 | #include <common.h> |
| 26 | #include <command.h> |
| 27 | #include <i2c.h> |
| 28 | #include <linux/ctype.h> |
| 29 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 30 | typedef struct { |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 31 | u8 id[4]; /* 0x0000 - 0x0003 EEPROM Tag */ |
| 32 | u8 sn[12]; /* 0x0004 - 0x000F Serial Number */ |
| 33 | u8 errata[5]; /* 0x0010 - 0x0014 Errata Level */ |
| 34 | u8 date[6]; /* 0x0015 - 0x001a Build Date */ |
| 35 | u8 res_0; /* 0x001b Reserved */ |
| 36 | u8 version[4]; /* 0x001c - 0x001f Version */ |
| 37 | u8 tempcal[8]; /* 0x0020 - 0x0027 Temperature Calibration Factors*/ |
| 38 | u8 tempcalsys[2]; /* 0x0028 - 0x0029 System Temperature Calibration Factors*/ |
| 39 | u8 res_1[22]; /* 0x0020 - 0x003f Reserved */ |
| 40 | u8 mac_size; /* 0x0040 Mac table size */ |
| 41 | u8 mac_flag; /* 0x0041 Mac table flags */ |
| 42 | u8 mac[8][6]; /* 0x0042 - 0x0071 Mac addresses */ |
| 43 | u32 crc; /* 0x0072 crc32 checksum */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 44 | } EEPROM_data; |
| 45 | |
| 46 | static EEPROM_data mac_data; |
| 47 | |
| 48 | int mac_show(void) |
| 49 | { |
| 50 | int i; |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 51 | u8 mac_size; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 52 | unsigned char ethaddr[8][18]; |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 53 | unsigned char enetvar[32]; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 54 | |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 55 | /* Show EEPROM tagID, |
| 56 | * always the four characters 'NXID'. |
| 57 | */ |
| 58 | printf("ID "); |
| 59 | for (i = 0; i < 4; i++) |
| 60 | printf("%c", mac_data.id[i]); |
| 61 | printf("\n"); |
| 62 | |
| 63 | /* Show Serial number, |
| 64 | * 0 to 11 charaters of errata information. |
| 65 | */ |
| 66 | printf("SN "); |
| 67 | for (i = 0; i < 12; i++) |
| 68 | printf("%c", mac_data.sn[i]); |
| 69 | printf("\n"); |
| 70 | |
| 71 | /* Show Errata Level, |
| 72 | * 0 to 4 characters of errata information. |
| 73 | */ |
| 74 | printf("Errata "); |
| 75 | for (i = 0; i < 5; i++) |
| 76 | printf("%c", mac_data.errata[i]); |
| 77 | printf("\n"); |
| 78 | |
| 79 | /* Show Build Date, |
| 80 | * BCD date values, as YYMMDDhhmmss. |
| 81 | */ |
| 82 | printf("Date 20%02x\/%02x\/%02x %02x:%02x:%02x\n", |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 83 | mac_data.date[0], |
| 84 | mac_data.date[1], |
| 85 | mac_data.date[2], |
| 86 | mac_data.date[3], |
| 87 | mac_data.date[4], |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 88 | mac_data.date[5]); |
| 89 | |
| 90 | /* Show MAC table size, |
| 91 | * Value from 0 to 7 indicating how many MAC |
| 92 | * addresses are stored in the system EEPROM. |
| 93 | */ |
| 94 | if((mac_data.mac_size > 0) && (mac_data.mac_size <= 8)) |
| 95 | mac_size = mac_data.mac_size; |
| 96 | else |
| 97 | mac_size = 8; /* Set the max size */ |
| 98 | printf("MACSIZE %x\n", mac_size); |
| 99 | |
| 100 | /* Show Mac addresses */ |
| 101 | for (i = 0; i < mac_size; i++) { |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 102 | sprintf((char *)ethaddr[i], |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 103 | "%02x:%02x:%02x:%02x:%02x:%02x", |
| 104 | mac_data.mac[i][0], |
| 105 | mac_data.mac[i][1], |
| 106 | mac_data.mac[i][2], |
| 107 | mac_data.mac[i][3], |
| 108 | mac_data.mac[i][4], |
| 109 | mac_data.mac[i][5]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 110 | printf("MAC %d %s\n", i, ethaddr[i]); |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 111 | |
| 112 | sprintf((char *)enetvar, |
| 113 | i ? "eth%daddr" : "ethaddr", i); |
| 114 | setenv((char *)enetvar, (char *)ethaddr[i]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 115 | |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 116 | } |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 117 | |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | int mac_read(void) |
| 122 | { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 123 | int ret, length; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 124 | unsigned int crc = 0; |
| 125 | unsigned char dev = ID_EEPROM_ADDR, *data; |
| 126 | |
| 127 | length = sizeof(EEPROM_data); |
| 128 | ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length); |
| 129 | if (ret) { |
| 130 | printf("Read failed.\n"); |
| 131 | return -1; |
| 132 | } |
| 133 | |
| 134 | data = (unsigned char *)(&mac_data); |
| 135 | printf("Check CRC on reading ..."); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 136 | crc = crc32(crc, data, length - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 137 | if (crc != mac_data.crc) { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 138 | printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n", |
| 139 | mac_data.crc, crc); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 140 | return -1; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 141 | } else { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 142 | printf("CRC OK\n"); |
| 143 | mac_show(); |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
| 148 | int mac_prog(void) |
| 149 | { |
| 150 | int ret, i, length; |
| 151 | unsigned int crc = 0; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 152 | unsigned char dev = ID_EEPROM_ADDR, *ptr; |
| 153 | unsigned char *eeprom_data = (unsigned char *)(&mac_data); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 154 | |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 155 | mac_data.res_0 = 0; |
| 156 | memset((void *)mac_data.res_1, 0, sizeof(mac_data.res_1)); |
| 157 | |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 158 | length = sizeof(EEPROM_data); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 159 | crc = crc32(crc, eeprom_data, length - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 160 | mac_data.crc = crc; |
| 161 | for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) { |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 162 | ret = i2c_write(dev, i, 1, ptr, min((length - i),8)); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 163 | udelay(5000); /* 5ms write cycle timing */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 164 | if (ret) |
| 165 | break; |
| 166 | } |
| 167 | if (ret) { |
| 168 | printf("Programming failed.\n"); |
| 169 | return -1; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 170 | } else { |
| 171 | printf("Programming %d bytes. Reading back ...\n", length); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 172 | mac_read(); |
| 173 | } |
| 174 | return 0; |
| 175 | } |
| 176 | |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 177 | 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] | 178 | { |
| 179 | int i; |
| 180 | char cmd = 's'; |
| 181 | unsigned long long mac_val; |
| 182 | |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 183 | if (i2c_probe(ID_EEPROM_ADDR) != 0) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 184 | return -1; |
| 185 | |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 186 | if (argc > 1) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 187 | cmd = argv[1][0]; |
| 188 | switch (cmd) { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 189 | case 'r': /* display */ |
| 190 | mac_read(); |
| 191 | break; |
| 192 | case 's': /* save */ |
| 193 | mac_prog(); |
| 194 | break; |
| 195 | case 'i': /* id */ |
| 196 | for (i = 0; i < 4; i++) { |
| 197 | mac_data.id[i] = argv[2][i]; |
| 198 | } |
| 199 | break; |
| 200 | case 'n': /* serial number */ |
| 201 | for (i = 0; i < 12; i++) { |
| 202 | mac_data.sn[i] = argv[2][i]; |
| 203 | } |
| 204 | break; |
| 205 | case 'e': /* errata */ |
| 206 | for (i = 0; i < 5; i++) { |
| 207 | mac_data.errata[i] = argv[2][i]; |
| 208 | } |
| 209 | break; |
| 210 | case 'd': /* date */ |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 211 | mac_val = simple_strtoull(argv[2], NULL, 16); |
| 212 | for (i = 0; i < 6; i++) { |
| 213 | mac_data.date[i] = (mac_val >> (40 - 8 * i)); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 214 | } |
| 215 | break; |
Haiying Wang | 34cf959 | 2008-01-16 17:12:12 -0500 | [diff] [blame^] | 216 | case 'p': /* mac table size */ |
| 217 | mac_data.mac_size = |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 218 | (unsigned char)simple_strtoul(argv[2], NULL, 16); |
| 219 | break; |
| 220 | case '0': /* mac 0 */ |
| 221 | case '1': /* mac 1 */ |
| 222 | case '2': /* mac 2 */ |
| 223 | case '3': /* mac 3 */ |
| 224 | case '4': /* mac 4 */ |
| 225 | case '5': /* mac 5 */ |
| 226 | case '6': /* mac 6 */ |
| 227 | case '7': /* mac 7 */ |
| 228 | mac_val = simple_strtoull(argv[2], NULL, 16); |
| 229 | for (i = 0; i < 6; i++) { |
| 230 | mac_data.mac[cmd - '0'][i] = |
| 231 | *((unsigned char *) |
| 232 | (((unsigned int)(&mac_val)) + i + 2)); |
| 233 | } |
| 234 | break; |
| 235 | case 'h': /* help */ |
| 236 | default: |
| 237 | printf("Usage:\n%s\n", cmdtp->usage); |
| 238 | break; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 239 | } |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 240 | } else { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 241 | mac_show(); |
| 242 | } |
| 243 | return 0; |
| 244 | } |
| 245 | |
| 246 | int mac_read_from_eeprom(void) |
| 247 | { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 248 | int length, i; |
| 249 | unsigned char dev = ID_EEPROM_ADDR; |
| 250 | unsigned char *data; |
| 251 | unsigned char ethaddr[4][18]; |
| 252 | unsigned char enetvar[32]; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 253 | unsigned int crc = 0; |
| 254 | |
| 255 | length = sizeof(EEPROM_data); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 256 | if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 257 | printf("Read failed.\n"); |
| 258 | return -1; |
| 259 | } |
| 260 | |
| 261 | data = (unsigned char *)(&mac_data); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 262 | crc = crc32(crc, data, length - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 263 | if (crc != mac_data.crc) { |
| 264 | return -1; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 265 | } else { |
| 266 | for (i = 0; i < 4; i++) { |
| 267 | if (memcmp(&mac_data.mac[i], "\0\0\0\0\0\0", 6)) { |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 268 | sprintf((char *)ethaddr[i], |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 269 | "%02x:%02x:%02x:%02x:%02x:%02x", |
| 270 | mac_data.mac[i][0], |
| 271 | mac_data.mac[i][1], |
| 272 | mac_data.mac[i][2], |
| 273 | mac_data.mac[i][3], |
| 274 | mac_data.mac[i][4], |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 275 | mac_data.mac[i][5]); |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 276 | sprintf((char *)enetvar, |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 277 | i ? "eth%daddr" : "ethaddr", |
| 278 | i); |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 279 | setenv((char *)enetvar, (char *)ethaddr[i]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 280 | } |
| 281 | } |
| 282 | } |
| 283 | return 0; |
| 284 | } |