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 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 */
43 static EEPROM_data mac_data;
48 unsigned char ethaddr[8][18];
50 printf("ID %c%c%c%c\n",
55 printf("Errata %c%c%c%c%c\n",
61 printf("Date %c%c%c%c%c%c%c\n",
69 for (i = 0; i < 8; i++) {
70 sprintf((char *)ethaddr[i],
71 "%02x:%02x:%02x:%02x:%02x:%02x",
78 printf("MAC %d %s\n", i, ethaddr[i]);
81 setenv("ethaddr", (char *)ethaddr[0]);
82 setenv("eth1addr", (char *)ethaddr[1]);
83 setenv("eth2addr", (char *)ethaddr[2]);
84 setenv("eth3addr", (char *)ethaddr[3]);
93 unsigned char dev = ID_EEPROM_ADDR, *data;
95 length = sizeof(EEPROM_data);
96 ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length);
98 printf("Read failed.\n");
102 data = (unsigned char *)(&mac_data);
103 printf("Check CRC on reading ...");
104 crc = crc32(crc, data, length - 4);
105 if (crc != mac_data.crc) {
106 printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n",
119 unsigned int crc = 0;
120 unsigned char dev = ID_EEPROM_ADDR, *ptr;
121 unsigned char *eeprom_data = (unsigned char *)(&mac_data);
123 for (i = 0; i < sizeof(mac_data.res_1); i++)
124 mac_data.res_1[i] = 0;
125 for (i = 0; i < sizeof(mac_data.res_2); i++)
126 mac_data.res_2[i] = 0;
127 length = sizeof(EEPROM_data);
128 crc = crc32(crc, eeprom_data, length - 4);
130 for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) {
132 i2c_write(dev, i, 1, ptr,
133 (length - i) < 8 ? (length - i) : 8);
134 udelay(5000); /* 5ms write cycle timing */
139 printf("Programming failed.\n");
142 printf("Programming %d bytes. Reading back ...\n", length);
148 int do_mac(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
152 unsigned long long mac_val;
154 if (i2c_probe(ID_EEPROM_ADDR) != 0)
160 case 'r': /* display */
167 for (i = 0; i < 4; i++) {
168 mac_data.id[i] = argv[2][i];
171 case 'n': /* serial number */
172 for (i = 0; i < 12; i++) {
173 mac_data.sn[i] = argv[2][i];
176 case 'e': /* errata */
177 for (i = 0; i < 5; i++) {
178 mac_data.errata[i] = argv[2][i];
182 for (i = 0; i < 7; i++) {
183 mac_data.date[i] = argv[2][i];
186 case 'p': /* number of ports */
188 (unsigned char)simple_strtoul(argv[2], NULL, 16);
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] =
202 (((unsigned int)(&mac_val)) + i + 2));
207 printf("Usage:\n%s\n", cmdtp->usage);
216 int mac_read_from_eeprom(void)
219 unsigned char dev = ID_EEPROM_ADDR;
221 unsigned char ethaddr[4][18];
222 unsigned char enetvar[32];
223 unsigned int crc = 0;
225 length = sizeof(EEPROM_data);
226 if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) {
227 printf("Read failed.\n");
231 data = (unsigned char *)(&mac_data);
232 crc = crc32(crc, data, length - 4);
233 if (crc != mac_data.crc) {
236 for (i = 0; i < 4; i++) {
237 if (memcmp(&mac_data.mac[i], "\0\0\0\0\0\0", 6)) {
238 sprintf((char *)ethaddr[i],
239 "%02x:%02x:%02x:%02x:%02x:%02x",
246 sprintf((char *)enetvar,
247 i ? "eth%daddr" : "ethaddr",
249 setenv((char *)enetvar, (char *)ethaddr[i]);