blob: 74e2a3d46ffd9cf6a0ba66d0b88a7a9927f43426 [file] [log] [blame]
Haiying Wangbea3f282006-07-12 10:48:05 -04001/*
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
30#ifdef CFG_ID_EEPROM
31typedef struct {
Jon Loeliger80e955c2006-08-22 12:25:27 -050032 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 */
Haiying Wangbea3f282006-07-12 10:48:05 -040042} EEPROM_data;
43
44static EEPROM_data mac_data;
45
46int mac_show(void)
47{
48 int i;
49 unsigned char ethaddr[8][18];
50
Jon Loeliger80e955c2006-08-22 12:25:27 -050051 printf("ID %c%c%c%c\n",
52 mac_data.id[0],
53 mac_data.id[1],
54 mac_data.id[2],
55 mac_data.id[3]);
56 printf("Errata %c%c%c%c%c\n",
57 mac_data.errata[0],
58 mac_data.errata[1],
59 mac_data.errata[2],
60 mac_data.errata[3],
61 mac_data.errata[4]);
62 printf("Date %c%c%c%c%c%c%c\n",
63 mac_data.date[0],
64 mac_data.date[1],
65 mac_data.date[2],
66 mac_data.date[3],
67 mac_data.date[4],
68 mac_data.date[5],
69 mac_data.date[6]);
Haiying Wangbea3f282006-07-12 10:48:05 -040070 for (i = 0; i < 8; i++) {
Jon Loeliger80e955c2006-08-22 12:25:27 -050071 sprintf(ethaddr[i],
72 "%02x:%02x:%02x:%02x:%02x:%02x",
73 mac_data.mac[i][0],
74 mac_data.mac[i][1],
75 mac_data.mac[i][2],
76 mac_data.mac[i][3],
77 mac_data.mac[i][4],
78 mac_data.mac[i][5]);
Haiying Wangbea3f282006-07-12 10:48:05 -040079 printf("MAC %d %s\n", i, ethaddr[i]);
80 }
81
82 setenv("ethaddr", ethaddr[0]);
83 setenv("eth1addr", ethaddr[1]);
84 setenv("eth2addr", ethaddr[2]);
85 setenv("eth3addr", ethaddr[3]);
86
87 return 0;
88}
89
90int mac_read(void)
91{
Jon Loeliger80e955c2006-08-22 12:25:27 -050092 int ret, length;
Haiying Wangbea3f282006-07-12 10:48:05 -040093 unsigned int crc = 0;
94 unsigned char dev = ID_EEPROM_ADDR, *data;
95
96 length = sizeof(EEPROM_data);
97 ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length);
98 if (ret) {
99 printf("Read failed.\n");
100 return -1;
101 }
102
103 data = (unsigned char *)(&mac_data);
104 printf("Check CRC on reading ...");
Jon Loeliger80e955c2006-08-22 12:25:27 -0500105 crc = crc32(crc, data, length - 4);
Haiying Wangbea3f282006-07-12 10:48:05 -0400106 if (crc != mac_data.crc) {
Jon Loeliger80e955c2006-08-22 12:25:27 -0500107 printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n",
108 mac_data.crc, crc);
Haiying Wangbea3f282006-07-12 10:48:05 -0400109 return -1;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500110 } else {
Haiying Wangbea3f282006-07-12 10:48:05 -0400111 printf("CRC OK\n");
112 mac_show();
113 }
114 return 0;
115}
116
117int mac_prog(void)
118{
119 int ret, i, length;
120 unsigned int crc = 0;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500121 unsigned char dev = ID_EEPROM_ADDR, *ptr;
122 unsigned char *eeprom_data = (unsigned char *)(&mac_data);
Haiying Wangbea3f282006-07-12 10:48:05 -0400123
124 for (i = 0; i < sizeof(mac_data.res_1); i++)
125 mac_data.res_1[i] = 0;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500126 for (i = 0; i < sizeof(mac_data.res_2); i++)
Haiying Wangbea3f282006-07-12 10:48:05 -0400127 mac_data.res_2[i] = 0;
128 length = sizeof(EEPROM_data);
Jon Loeliger80e955c2006-08-22 12:25:27 -0500129 crc = crc32(crc, eeprom_data, length - 4);
Haiying Wangbea3f282006-07-12 10:48:05 -0400130 mac_data.crc = crc;
131 for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) {
Jon Loeliger80e955c2006-08-22 12:25:27 -0500132 ret =
133 i2c_write(dev, i, 1, ptr,
134 (length - i) < 8 ? (length - i) : 8);
135 udelay(5000); /* 5ms write cycle timing */
Haiying Wangbea3f282006-07-12 10:48:05 -0400136 if (ret)
137 break;
138 }
139 if (ret) {
140 printf("Programming failed.\n");
141 return -1;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500142 } else {
143 printf("Programming %d bytes. Reading back ...\n", length);
Haiying Wangbea3f282006-07-12 10:48:05 -0400144 mac_read();
145 }
146 return 0;
147}
148
Jon Loeliger80e955c2006-08-22 12:25:27 -0500149int do_mac(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
Haiying Wangbea3f282006-07-12 10:48:05 -0400150{
151 int i;
152 char cmd = 's';
153 unsigned long long mac_val;
154
Jon Loeliger80e955c2006-08-22 12:25:27 -0500155 if (i2c_probe(ID_EEPROM_ADDR) != 0)
Haiying Wangbea3f282006-07-12 10:48:05 -0400156 return -1;
157
Jon Loeliger80e955c2006-08-22 12:25:27 -0500158 if (argc > 1) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400159 cmd = argv[1][0];
160 switch (cmd) {
Jon Loeliger80e955c2006-08-22 12:25:27 -0500161 case 'r': /* display */
162 mac_read();
163 break;
164 case 's': /* save */
165 mac_prog();
166 break;
167 case 'i': /* id */
168 for (i = 0; i < 4; i++) {
169 mac_data.id[i] = argv[2][i];
170 }
171 break;
172 case 'n': /* serial number */
173 for (i = 0; i < 12; i++) {
174 mac_data.sn[i] = argv[2][i];
175 }
176 break;
177 case 'e': /* errata */
178 for (i = 0; i < 5; i++) {
179 mac_data.errata[i] = argv[2][i];
180 }
181 break;
182 case 'd': /* date */
183 for (i = 0; i < 7; i++) {
184 mac_data.date[i] = argv[2][i];
185 }
186 break;
187 case 'p': /* number of ports */
188 mac_data.tab_size =
189 (unsigned char)simple_strtoul(argv[2], NULL, 16);
190 break;
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] =
202 *((unsigned char *)
203 (((unsigned int)(&mac_val)) + i + 2));
204 }
205 break;
206 case 'h': /* help */
207 default:
208 printf("Usage:\n%s\n", cmdtp->usage);
209 break;
Haiying Wangbea3f282006-07-12 10:48:05 -0400210 }
Jon Loeliger80e955c2006-08-22 12:25:27 -0500211 } else {
Haiying Wangbea3f282006-07-12 10:48:05 -0400212 mac_show();
213 }
214 return 0;
215}
216
217int mac_read_from_eeprom(void)
218{
Jon Loeliger80e955c2006-08-22 12:25:27 -0500219 int length, i;
220 unsigned char dev = ID_EEPROM_ADDR;
221 unsigned char *data;
222 unsigned char ethaddr[4][18];
223 unsigned char enetvar[32];
Haiying Wangbea3f282006-07-12 10:48:05 -0400224 unsigned int crc = 0;
225
226 length = sizeof(EEPROM_data);
Jon Loeliger80e955c2006-08-22 12:25:27 -0500227 if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400228 printf("Read failed.\n");
229 return -1;
230 }
231
232 data = (unsigned char *)(&mac_data);
Jon Loeliger80e955c2006-08-22 12:25:27 -0500233 crc = crc32(crc, data, length - 4);
Haiying Wangbea3f282006-07-12 10:48:05 -0400234 if (crc != mac_data.crc) {
235 return -1;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500236 } else {
237 for (i = 0; i < 4; i++) {
238 if (memcmp(&mac_data.mac[i], "\0\0\0\0\0\0", 6)) {
239 sprintf(ethaddr[i],
240 "%02x:%02x:%02x:%02x:%02x:%02x",
241 mac_data.mac[i][0],
242 mac_data.mac[i][1],
243 mac_data.mac[i][2],
244 mac_data.mac[i][3],
245 mac_data.mac[i][4],
Haiying Wangbea3f282006-07-12 10:48:05 -0400246 mac_data.mac[i][5]);
Jon Loeliger80e955c2006-08-22 12:25:27 -0500247 sprintf(enetvar,
248 i ? "eth%daddr" : "ethaddr",
249 i);
Haiying Wangbea3f282006-07-12 10:48:05 -0400250 setenv(enetvar, ethaddr[i]);
251 }
252 }
253 }
254 return 0;
255}
Jon Loeliger80e955c2006-08-22 12:25:27 -0500256#endif /* CFG_ID_EEPROM */