blob: 44c097867f3802c7e29f6ffba5fd345b3f73e94f [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
Haiying Wangbea3f282006-07-12 10:48:05 -040030typedef struct {
Haiying Wangd59feff2008-01-16 17:12:12 -050031 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 */
Haiying Wangbea3f282006-07-12 10:48:05 -040044} EEPROM_data;
45
46static EEPROM_data mac_data;
47
48int mac_show(void)
49{
50 int i;
Haiying Wangd59feff2008-01-16 17:12:12 -050051 u8 mac_size;
Haiying Wangbea3f282006-07-12 10:48:05 -040052 unsigned char ethaddr[8][18];
Haiying Wangd59feff2008-01-16 17:12:12 -050053 unsigned char enetvar[32];
Haiying Wangbea3f282006-07-12 10:48:05 -040054
Haiying Wangd59feff2008-01-16 17:12:12 -050055 /* Show EEPROM tagID,
56 * always the four characters 'NXID'.
57 */
58 printf("ID ");
59 for (i = 0; i < 4; i++)
60 printf("%c", mac_data.id[i]);
61 printf("\n");
62
63 /* Show Serial number,
64 * 0 to 11 charaters of errata information.
65 */
66 printf("SN ");
67 for (i = 0; i < 12; i++)
68 printf("%c", mac_data.sn[i]);
69 printf("\n");
70
71 /* Show Errata Level,
72 * 0 to 4 characters of errata information.
73 */
74 printf("Errata ");
75 for (i = 0; i < 5; i++)
76 printf("%c", mac_data.errata[i]);
77 printf("\n");
78
79 /* Show Build Date,
80 * BCD date values, as YYMMDDhhmmss.
81 */
82 printf("Date 20%02x\/%02x\/%02x %02x:%02x:%02x\n",
Jon Loeliger80e955c2006-08-22 12:25:27 -050083 mac_data.date[0],
84 mac_data.date[1],
85 mac_data.date[2],
86 mac_data.date[3],
87 mac_data.date[4],
Haiying Wangd59feff2008-01-16 17:12:12 -050088 mac_data.date[5]);
89
90 /* Show MAC table size,
91 * Value from 0 to 7 indicating how many MAC
92 * addresses are stored in the system EEPROM.
93 */
94 if((mac_data.mac_size > 0) && (mac_data.mac_size <= 8))
95 mac_size = mac_data.mac_size;
96 else
97 mac_size = 8; /* Set the max size */
98 printf("MACSIZE %x\n", mac_size);
99
100 /* Show Mac addresses */
101 for (i = 0; i < mac_size; i++) {
Wolfgang Denkcdd917a2007-08-02 00:48:45 +0200102 sprintf((char *)ethaddr[i],
Jon Loeliger80e955c2006-08-22 12:25:27 -0500103 "%02x:%02x:%02x:%02x:%02x:%02x",
104 mac_data.mac[i][0],
105 mac_data.mac[i][1],
106 mac_data.mac[i][2],
107 mac_data.mac[i][3],
108 mac_data.mac[i][4],
109 mac_data.mac[i][5]);
Haiying Wangbea3f282006-07-12 10:48:05 -0400110 printf("MAC %d %s\n", i, ethaddr[i]);
Haiying Wangbea3f282006-07-12 10:48:05 -0400111
Haiying Wangd59feff2008-01-16 17:12:12 -0500112 sprintf((char *)enetvar,
113 i ? "eth%daddr" : "ethaddr", i);
114 setenv((char *)enetvar, (char *)ethaddr[i]);
115
116 }
Haiying Wangbea3f282006-07-12 10:48:05 -0400117
118 return 0;
119}
120
121int mac_read(void)
122{
Jon Loeliger80e955c2006-08-22 12:25:27 -0500123 int ret, length;
Haiying Wangbea3f282006-07-12 10:48:05 -0400124 unsigned int crc = 0;
125 unsigned char dev = ID_EEPROM_ADDR, *data;
126
127 length = sizeof(EEPROM_data);
128 ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length);
129 if (ret) {
130 printf("Read failed.\n");
131 return -1;
132 }
133
134 data = (unsigned char *)(&mac_data);
135 printf("Check CRC on reading ...");
Jon Loeliger80e955c2006-08-22 12:25:27 -0500136 crc = crc32(crc, data, length - 4);
Haiying Wangbea3f282006-07-12 10:48:05 -0400137 if (crc != mac_data.crc) {
Jon Loeliger80e955c2006-08-22 12:25:27 -0500138 printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n",
139 mac_data.crc, crc);
Haiying Wangbea3f282006-07-12 10:48:05 -0400140 return -1;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500141 } else {
Haiying Wangbea3f282006-07-12 10:48:05 -0400142 printf("CRC OK\n");
143 mac_show();
144 }
145 return 0;
146}
147
148int mac_prog(void)
149{
150 int ret, i, length;
151 unsigned int crc = 0;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500152 unsigned char dev = ID_EEPROM_ADDR, *ptr;
153 unsigned char *eeprom_data = (unsigned char *)(&mac_data);
Haiying Wangbea3f282006-07-12 10:48:05 -0400154
Haiying Wangd59feff2008-01-16 17:12:12 -0500155 mac_data.res_0 = 0;
156 memset((void *)mac_data.res_1, 0, sizeof(mac_data.res_1));
157
Haiying Wangbea3f282006-07-12 10:48:05 -0400158 length = sizeof(EEPROM_data);
Jon Loeliger80e955c2006-08-22 12:25:27 -0500159 crc = crc32(crc, eeprom_data, length - 4);
Haiying Wangbea3f282006-07-12 10:48:05 -0400160 mac_data.crc = crc;
161 for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) {
Haiying Wangd59feff2008-01-16 17:12:12 -0500162 ret = i2c_write(dev, i, 1, ptr, min((length - i),8));
Jon Loeliger80e955c2006-08-22 12:25:27 -0500163 udelay(5000); /* 5ms write cycle timing */
Haiying Wangbea3f282006-07-12 10:48:05 -0400164 if (ret)
165 break;
166 }
167 if (ret) {
168 printf("Programming failed.\n");
169 return -1;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500170 } else {
171 printf("Programming %d bytes. Reading back ...\n", length);
Haiying Wangbea3f282006-07-12 10:48:05 -0400172 mac_read();
173 }
174 return 0;
175}
176
Jon Loeliger80e955c2006-08-22 12:25:27 -0500177int do_mac(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
Haiying Wangbea3f282006-07-12 10:48:05 -0400178{
179 int i;
180 char cmd = 's';
181 unsigned long long mac_val;
182
Jon Loeliger80e955c2006-08-22 12:25:27 -0500183 if (i2c_probe(ID_EEPROM_ADDR) != 0)
Haiying Wangbea3f282006-07-12 10:48:05 -0400184 return -1;
185
Jon Loeliger80e955c2006-08-22 12:25:27 -0500186 if (argc > 1) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400187 cmd = argv[1][0];
188 switch (cmd) {
Jon Loeliger80e955c2006-08-22 12:25:27 -0500189 case 'r': /* display */
190 mac_read();
191 break;
192 case 's': /* save */
193 mac_prog();
194 break;
195 case 'i': /* id */
196 for (i = 0; i < 4; i++) {
197 mac_data.id[i] = argv[2][i];
198 }
199 break;
200 case 'n': /* serial number */
201 for (i = 0; i < 12; i++) {
202 mac_data.sn[i] = argv[2][i];
203 }
204 break;
205 case 'e': /* errata */
206 for (i = 0; i < 5; i++) {
207 mac_data.errata[i] = argv[2][i];
208 }
209 break;
210 case 'd': /* date */
Haiying Wangd59feff2008-01-16 17:12:12 -0500211 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));
Jon Loeliger80e955c2006-08-22 12:25:27 -0500214 }
215 break;
Haiying Wangd59feff2008-01-16 17:12:12 -0500216 case 'p': /* mac table size */
217 mac_data.mac_size =
Jon Loeliger80e955c2006-08-22 12:25:27 -0500218 (unsigned char)simple_strtoul(argv[2], NULL, 16);
219 break;
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] =
231 *((unsigned char *)
232 (((unsigned int)(&mac_val)) + i + 2));
233 }
234 break;
235 case 'h': /* help */
236 default:
237 printf("Usage:\n%s\n", cmdtp->usage);
238 break;
Haiying Wangbea3f282006-07-12 10:48:05 -0400239 }
Jon Loeliger80e955c2006-08-22 12:25:27 -0500240 } else {
Haiying Wangbea3f282006-07-12 10:48:05 -0400241 mac_show();
242 }
243 return 0;
244}
245
246int mac_read_from_eeprom(void)
247{
Jon Loeliger80e955c2006-08-22 12:25:27 -0500248 int length, i;
249 unsigned char dev = ID_EEPROM_ADDR;
250 unsigned char *data;
251 unsigned char ethaddr[4][18];
252 unsigned char enetvar[32];
Haiying Wangbea3f282006-07-12 10:48:05 -0400253 unsigned int crc = 0;
254
255 length = sizeof(EEPROM_data);
Jon Loeliger80e955c2006-08-22 12:25:27 -0500256 if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400257 printf("Read failed.\n");
258 return -1;
259 }
260
261 data = (unsigned char *)(&mac_data);
Jon Loeliger80e955c2006-08-22 12:25:27 -0500262 crc = crc32(crc, data, length - 4);
Haiying Wangbea3f282006-07-12 10:48:05 -0400263 if (crc != mac_data.crc) {
264 return -1;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500265 } else {
266 for (i = 0; i < 4; i++) {
267 if (memcmp(&mac_data.mac[i], "\0\0\0\0\0\0", 6)) {
Wolfgang Denkcdd917a2007-08-02 00:48:45 +0200268 sprintf((char *)ethaddr[i],
Jon Loeliger80e955c2006-08-22 12:25:27 -0500269 "%02x:%02x:%02x:%02x:%02x:%02x",
270 mac_data.mac[i][0],
271 mac_data.mac[i][1],
272 mac_data.mac[i][2],
273 mac_data.mac[i][3],
274 mac_data.mac[i][4],
Haiying Wangbea3f282006-07-12 10:48:05 -0400275 mac_data.mac[i][5]);
Wolfgang Denkcdd917a2007-08-02 00:48:45 +0200276 sprintf((char *)enetvar,
Jon Loeliger80e955c2006-08-22 12:25:27 -0500277 i ? "eth%daddr" : "ethaddr",
278 i);
Wolfgang Denkcdd917a2007-08-02 00:48:45 +0200279 setenv((char *)enetvar, (char *)ethaddr[i]);
Haiying Wangbea3f282006-07-12 10:48:05 -0400280 }
281 }
282 }
283 return 0;
284}