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 { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 31 | unsigned char id[4]; /* 0x0000 - 0x0003 */ |
| 32 | unsigned char sn[12]; /* 0x0004 - 0x000F */ |
| 33 | unsigned char errata[5]; /* 0x0010 - 0x0014 */ |
| 34 | unsigned char date[7]; /* 0x0015 - 0x001a */ |
| 35 | unsigned char res_1[37]; /* 0x001b - 0x003f */ |
| 36 | unsigned char tab_size; /* 0x0040 */ |
| 37 | unsigned char tab_flag; /* 0x0041 */ |
| 38 | unsigned char mac[8][6]; /* 0x0042 - 0x0071 */ |
| 39 | unsigned char res_2[126]; /* 0x0072 - 0x00ef */ |
| 40 | unsigned int crc; /* 0x00f0 - 0x00f3 crc32 checksum */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 41 | } EEPROM_data; |
| 42 | |
| 43 | static EEPROM_data mac_data; |
| 44 | |
| 45 | int mac_show(void) |
| 46 | { |
| 47 | int i; |
| 48 | unsigned char ethaddr[8][18]; |
| 49 | |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 50 | printf("ID %c%c%c%c\n", |
| 51 | mac_data.id[0], |
| 52 | mac_data.id[1], |
| 53 | mac_data.id[2], |
| 54 | mac_data.id[3]); |
| 55 | printf("Errata %c%c%c%c%c\n", |
| 56 | mac_data.errata[0], |
| 57 | mac_data.errata[1], |
| 58 | mac_data.errata[2], |
| 59 | mac_data.errata[3], |
| 60 | mac_data.errata[4]); |
| 61 | printf("Date %c%c%c%c%c%c%c\n", |
| 62 | mac_data.date[0], |
| 63 | mac_data.date[1], |
| 64 | mac_data.date[2], |
| 65 | mac_data.date[3], |
| 66 | mac_data.date[4], |
| 67 | mac_data.date[5], |
| 68 | mac_data.date[6]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 69 | for (i = 0; i < 8; i++) { |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 70 | sprintf((char *)ethaddr[i], |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 71 | "%02x:%02x:%02x:%02x:%02x:%02x", |
| 72 | mac_data.mac[i][0], |
| 73 | mac_data.mac[i][1], |
| 74 | mac_data.mac[i][2], |
| 75 | mac_data.mac[i][3], |
| 76 | mac_data.mac[i][4], |
| 77 | mac_data.mac[i][5]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 78 | printf("MAC %d %s\n", i, ethaddr[i]); |
| 79 | } |
| 80 | |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 81 | setenv("ethaddr", (char *)ethaddr[0]); |
| 82 | setenv("eth1addr", (char *)ethaddr[1]); |
| 83 | setenv("eth2addr", (char *)ethaddr[2]); |
| 84 | setenv("eth3addr", (char *)ethaddr[3]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | int mac_read(void) |
| 90 | { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 91 | int ret, length; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 92 | unsigned int crc = 0; |
| 93 | unsigned char dev = ID_EEPROM_ADDR, *data; |
| 94 | |
| 95 | length = sizeof(EEPROM_data); |
| 96 | ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length); |
| 97 | if (ret) { |
| 98 | printf("Read failed.\n"); |
| 99 | return -1; |
| 100 | } |
| 101 | |
| 102 | data = (unsigned char *)(&mac_data); |
| 103 | printf("Check CRC on reading ..."); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 104 | crc = crc32(crc, data, length - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 105 | if (crc != mac_data.crc) { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 106 | printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n", |
| 107 | mac_data.crc, crc); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 108 | return -1; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 109 | } else { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 110 | printf("CRC OK\n"); |
| 111 | mac_show(); |
| 112 | } |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | int mac_prog(void) |
| 117 | { |
| 118 | int ret, i, length; |
| 119 | unsigned int crc = 0; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 120 | unsigned char dev = ID_EEPROM_ADDR, *ptr; |
| 121 | unsigned char *eeprom_data = (unsigned char *)(&mac_data); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 122 | |
| 123 | for (i = 0; i < sizeof(mac_data.res_1); i++) |
| 124 | mac_data.res_1[i] = 0; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 125 | for (i = 0; i < sizeof(mac_data.res_2); i++) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 126 | mac_data.res_2[i] = 0; |
| 127 | length = sizeof(EEPROM_data); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 128 | crc = crc32(crc, eeprom_data, length - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 129 | mac_data.crc = crc; |
| 130 | for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 131 | ret = |
| 132 | i2c_write(dev, i, 1, ptr, |
| 133 | (length - i) < 8 ? (length - i) : 8); |
| 134 | udelay(5000); /* 5ms write cycle timing */ |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 135 | if (ret) |
| 136 | break; |
| 137 | } |
| 138 | if (ret) { |
| 139 | printf("Programming failed.\n"); |
| 140 | return -1; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 141 | } else { |
| 142 | printf("Programming %d bytes. Reading back ...\n", length); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 143 | mac_read(); |
| 144 | } |
| 145 | return 0; |
| 146 | } |
| 147 | |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 148 | 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] | 149 | { |
| 150 | int i; |
| 151 | char cmd = 's'; |
| 152 | unsigned long long mac_val; |
| 153 | |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 154 | if (i2c_probe(ID_EEPROM_ADDR) != 0) |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 155 | return -1; |
| 156 | |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 157 | if (argc > 1) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 158 | cmd = argv[1][0]; |
| 159 | switch (cmd) { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 160 | case 'r': /* display */ |
| 161 | mac_read(); |
| 162 | break; |
| 163 | case 's': /* save */ |
| 164 | mac_prog(); |
| 165 | break; |
| 166 | case 'i': /* id */ |
| 167 | for (i = 0; i < 4; i++) { |
| 168 | mac_data.id[i] = argv[2][i]; |
| 169 | } |
| 170 | break; |
| 171 | case 'n': /* serial number */ |
| 172 | for (i = 0; i < 12; i++) { |
| 173 | mac_data.sn[i] = argv[2][i]; |
| 174 | } |
| 175 | break; |
| 176 | case 'e': /* errata */ |
| 177 | for (i = 0; i < 5; i++) { |
| 178 | mac_data.errata[i] = argv[2][i]; |
| 179 | } |
| 180 | break; |
| 181 | case 'd': /* date */ |
| 182 | for (i = 0; i < 7; i++) { |
| 183 | mac_data.date[i] = argv[2][i]; |
| 184 | } |
| 185 | break; |
| 186 | case 'p': /* number of ports */ |
| 187 | mac_data.tab_size = |
| 188 | (unsigned char)simple_strtoul(argv[2], NULL, 16); |
| 189 | break; |
| 190 | case '0': /* mac 0 */ |
| 191 | case '1': /* mac 1 */ |
| 192 | case '2': /* mac 2 */ |
| 193 | case '3': /* mac 3 */ |
| 194 | case '4': /* mac 4 */ |
| 195 | case '5': /* mac 5 */ |
| 196 | case '6': /* mac 6 */ |
| 197 | case '7': /* mac 7 */ |
| 198 | mac_val = simple_strtoull(argv[2], NULL, 16); |
| 199 | for (i = 0; i < 6; i++) { |
| 200 | mac_data.mac[cmd - '0'][i] = |
| 201 | *((unsigned char *) |
| 202 | (((unsigned int)(&mac_val)) + i + 2)); |
| 203 | } |
| 204 | break; |
| 205 | case 'h': /* help */ |
| 206 | default: |
| 207 | printf("Usage:\n%s\n", cmdtp->usage); |
| 208 | break; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 209 | } |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 210 | } else { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 211 | mac_show(); |
| 212 | } |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | int mac_read_from_eeprom(void) |
| 217 | { |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 218 | int length, i; |
| 219 | unsigned char dev = ID_EEPROM_ADDR; |
| 220 | unsigned char *data; |
| 221 | unsigned char ethaddr[4][18]; |
| 222 | unsigned char enetvar[32]; |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 223 | unsigned int crc = 0; |
| 224 | |
| 225 | length = sizeof(EEPROM_data); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 226 | if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) { |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 227 | printf("Read failed.\n"); |
| 228 | return -1; |
| 229 | } |
| 230 | |
| 231 | data = (unsigned char *)(&mac_data); |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 232 | crc = crc32(crc, data, length - 4); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 233 | if (crc != mac_data.crc) { |
| 234 | return -1; |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 235 | } else { |
| 236 | for (i = 0; i < 4; i++) { |
| 237 | 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] | 238 | sprintf((char *)ethaddr[i], |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 239 | "%02x:%02x:%02x:%02x:%02x:%02x", |
| 240 | mac_data.mac[i][0], |
| 241 | mac_data.mac[i][1], |
| 242 | mac_data.mac[i][2], |
| 243 | mac_data.mac[i][3], |
| 244 | mac_data.mac[i][4], |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 245 | mac_data.mac[i][5]); |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 246 | sprintf((char *)enetvar, |
Jon Loeliger | 4fbb09c | 2006-08-22 12:25:27 -0500 | [diff] [blame] | 247 | i ? "eth%daddr" : "ethaddr", |
| 248 | i); |
Wolfgang Denk | 4d5a8e3 | 2007-08-02 00:48:45 +0200 | [diff] [blame] | 249 | setenv((char *)enetvar, (char *)ethaddr[i]); |
Haiying Wang | 34c6878 | 2006-07-12 10:48:05 -0400 | [diff] [blame] | 250 | } |
| 251 | } |
| 252 | } |
| 253 | return 0; |
| 254 | } |