2 * Copyright 2006 Freescale Semiconductor
3 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
6 * See file CREDITS for list of people who contributed to this
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.
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.
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,
28 #include <linux/ctype.h>
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 */
46 static EEPROM_data mac_data;
52 unsigned char ethaddr[8][18];
53 unsigned char enetvar[32];
56 * always the four characters 'NXID'.
59 for (i = 0; i < 4; i++)
60 printf("%c", mac_data.id[i]);
63 /* Show Serial number,
64 * 0 to 11 charaters of errata information.
67 for (i = 0; i < 12; i++)
68 printf("%c", mac_data.sn[i]);
72 * 0 to 4 characters of errata information.
75 for (i = 0; i < 5; i++)
76 printf("%c", mac_data.errata[i]);
80 * BCD date values, as YYMMDDhhmmss.
82 printf("Date 20%02x/%02x/%02x %02x:%02x:%02x\n",
90 /* Show MAC table size,
91 * Value from 0 to 7 indicating how many MAC
92 * addresses are stored in the system EEPROM.
94 if((mac_data.mac_size > 0) && (mac_data.mac_size <= 8))
95 mac_size = mac_data.mac_size;
97 mac_size = 8; /* Set the max size */
98 printf("MACSIZE %x\n", mac_size);
100 /* Show Mac addresses */
101 for (i = 0; i < mac_size; i++) {
102 sprintf((char *)ethaddr[i],
103 "%02x:%02x:%02x:%02x:%02x:%02x",
110 printf("MAC %d %s\n", i, ethaddr[i]);
112 sprintf((char *)enetvar,
113 i ? "eth%daddr" : "ethaddr", i);
114 setenv((char *)enetvar, (char *)ethaddr[i]);
124 unsigned int crc = 0;
125 unsigned char dev = ID_EEPROM_ADDR, *data;
127 length = sizeof(EEPROM_data);
128 ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length);
130 printf("Read failed.\n");
134 data = (unsigned char *)(&mac_data);
135 printf("Check CRC on reading ...");
136 crc = crc32(crc, data, length - 4);
137 if (crc != mac_data.crc) {
138 printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n",
151 unsigned int crc = 0;
152 unsigned char dev = ID_EEPROM_ADDR, *ptr;
153 unsigned char *eeprom_data = (unsigned char *)(&mac_data);
156 memset((void *)mac_data.res_1, 0, sizeof(mac_data.res_1));
158 length = sizeof(EEPROM_data);
159 crc = crc32(crc, eeprom_data, length - 4);
161 for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) {
162 ret = i2c_write(dev, i, 1, ptr, min((length - i),8));
163 udelay(5000); /* 5ms write cycle timing */
168 printf("Programming failed.\n");
171 printf("Programming %d bytes. Reading back ...\n", length);
177 int do_mac(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
181 unsigned long long mac_val;
183 if (i2c_probe(ID_EEPROM_ADDR) != 0)
189 case 'r': /* display */
196 for (i = 0; i < 4; i++) {
197 mac_data.id[i] = argv[2][i];
200 case 'n': /* serial number */
201 for (i = 0; i < 12; i++) {
202 mac_data.sn[i] = argv[2][i];
205 case 'e': /* errata */
206 for (i = 0; i < 5; i++) {
207 mac_data.errata[i] = argv[2][i];
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));
216 case 'p': /* mac table size */
218 (unsigned char)simple_strtoul(argv[2], NULL, 16);
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] =
232 (((unsigned int)(&mac_val)) + i + 2));
237 printf("Usage:\n%s\n", cmdtp->usage);
246 int mac_read_from_eeprom(void)
249 unsigned char dev = ID_EEPROM_ADDR;
251 unsigned char ethaddr[4][18];
252 unsigned char enetvar[32];
253 unsigned int crc = 0;
255 length = sizeof(EEPROM_data);
256 if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) {
257 printf("Read failed.\n");
261 data = (unsigned char *)(&mac_data);
262 crc = crc32(crc, data, length - 4);
263 if (crc != mac_data.crc) {
266 for (i = 0; i < 4; i++) {
267 if (memcmp(&mac_data.mac[i], "\0\0\0\0\0\0", 6)) {
268 sprintf((char *)ethaddr[i],
269 "%02x:%02x:%02x:%02x:%02x:%02x",
276 sprintf((char *)enetvar,
277 i ? "eth%daddr" : "ethaddr",
279 setenv((char *)enetvar, (char *)ethaddr[i]);