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>
32 unsigned char id
[4]; /* 0x0000 - 0x0003 */
33 unsigned char sn
[12]; /* 0x0004 - 0x000F */
34 unsigned char errata
[5]; /* 0x0010 - 0x0014 */
35 unsigned char date
[7]; /* 0x0015 - 0x001a */
36 unsigned char res_1
[37]; /* 0x001b - 0x003f */
37 unsigned char tab_size
; /* 0x0040 */
38 unsigned char tab_flag
; /* 0x0041 */
39 unsigned char mac
[8][6]; /* 0x0042 - 0x0071 */
40 unsigned char res_2
[126]; /* 0x0072 - 0x00ef */
41 unsigned int crc
; /* 0x00f0 - 0x00f3 crc32 checksum */
44 static EEPROM_data mac_data
;
49 unsigned char ethaddr
[8][18];
51 printf("ID %c%c%c%c\n",
56 printf("Errata %c%c%c%c%c\n",
62 printf("Date %c%c%c%c%c%c%c\n",
70 for (i
= 0; i
< 8; i
++) {
72 "%02x:%02x:%02x:%02x:%02x:%02x",
79 printf("MAC %d %s\n", i
, ethaddr
[i
]);
82 setenv("ethaddr", ethaddr
[0]);
83 setenv("eth1addr", ethaddr
[1]);
84 setenv("eth2addr", ethaddr
[2]);
85 setenv("eth3addr", ethaddr
[3]);
94 unsigned char dev
= ID_EEPROM_ADDR
, *data
;
96 length
= sizeof(EEPROM_data
);
97 ret
= i2c_read(dev
, 0, 1, (unsigned char *)(&mac_data
), length
);
99 printf("Read failed.\n");
103 data
= (unsigned char *)(&mac_data
);
104 printf("Check CRC on reading ...");
105 crc
= crc32(crc
, data
, length
- 4);
106 if (crc
!= mac_data
.crc
) {
107 printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n",
120 unsigned int crc
= 0;
121 unsigned char dev
= ID_EEPROM_ADDR
, *ptr
;
122 unsigned char *eeprom_data
= (unsigned char *)(&mac_data
);
124 for (i
= 0; i
< sizeof(mac_data
.res_1
); i
++)
125 mac_data
.res_1
[i
] = 0;
126 for (i
= 0; i
< sizeof(mac_data
.res_2
); i
++)
127 mac_data
.res_2
[i
] = 0;
128 length
= sizeof(EEPROM_data
);
129 crc
= crc32(crc
, eeprom_data
, length
- 4);
131 for (i
= 0, ptr
= eeprom_data
; i
< length
; i
+= 8, ptr
+= 8) {
133 i2c_write(dev
, i
, 1, ptr
,
134 (length
- i
) < 8 ? (length
- i
) : 8);
135 udelay(5000); /* 5ms write cycle timing */
140 printf("Programming failed.\n");
143 printf("Programming %d bytes. Reading back ...\n", length
);
149 int do_mac(cmd_tbl_t
* cmdtp
, int flag
, int argc
, char *argv
[])
153 unsigned long long mac_val
;
155 if (i2c_probe(ID_EEPROM_ADDR
) != 0)
161 case 'r': /* display */
168 for (i
= 0; i
< 4; i
++) {
169 mac_data
.id
[i
] = argv
[2][i
];
172 case 'n': /* serial number */
173 for (i
= 0; i
< 12; i
++) {
174 mac_data
.sn
[i
] = argv
[2][i
];
177 case 'e': /* errata */
178 for (i
= 0; i
< 5; i
++) {
179 mac_data
.errata
[i
] = argv
[2][i
];
183 for (i
= 0; i
< 7; i
++) {
184 mac_data
.date
[i
] = argv
[2][i
];
187 case 'p': /* number of ports */
189 (unsigned char)simple_strtoul(argv
[2], NULL
, 16);
191 case '0': /* mac 0 */
192 case '1': /* mac 1 */
193 case '2': /* mac 2 */
194 case '3': /* mac 3 */
195 case '4': /* mac 4 */
196 case '5': /* mac 5 */
197 case '6': /* mac 6 */
198 case '7': /* mac 7 */
199 mac_val
= simple_strtoull(argv
[2], NULL
, 16);
200 for (i
= 0; i
< 6; i
++) {
201 mac_data
.mac
[cmd
- '0'][i
] =
203 (((unsigned int)(&mac_val
)) + i
+ 2));
208 printf("Usage:\n%s\n", cmdtp
->usage
);
217 int mac_read_from_eeprom(void)
220 unsigned char dev
= ID_EEPROM_ADDR
;
222 unsigned char ethaddr
[4][18];
223 unsigned char enetvar
[32];
224 unsigned int crc
= 0;
226 length
= sizeof(EEPROM_data
);
227 if (i2c_read(dev
, 0, 1, (unsigned char *)(&mac_data
), length
)) {
228 printf("Read failed.\n");
232 data
= (unsigned char *)(&mac_data
);
233 crc
= crc32(crc
, data
, length
- 4);
234 if (crc
!= mac_data
.crc
) {
237 for (i
= 0; i
< 4; i
++) {
238 if (memcmp(&mac_data
.mac
[i
], "\0\0\0\0\0\0", 6)) {
240 "%02x:%02x:%02x:%02x:%02x:%02x",
248 i
? "eth%daddr" : "ethaddr",
250 setenv(enetvar
, ethaddr
[i
]);
256 #endif /* CFG_ID_EEPROM */