blob: bb91e67262b0ab8c49d2f39893532f19f84b4a35 [file] [log] [blame]
Haiying Wang34c68782006-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 Wang34c68782006-07-12 10:48:05 -040030typedef struct {
Jon Loeliger4fbb09c2006-08-22 12:25:27 -050031 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 */
Haiying Wang34c68782006-07-12 10:48:05 -040041} EEPROM_data;
42
43static EEPROM_data mac_data;
44
45int mac_show(void)
46{
47 int i;
48 unsigned char ethaddr[8][18];
49
Jon Loeliger4fbb09c2006-08-22 12:25:27 -050050 printf("ID %c%c%c%c\n",
51 mac_data.id[0],
52 mac_data.id[1],
53 mac_data.id[2],
54 mac_data.id[3]);
55 printf("Errata %c%c%c%c%c\n",
56 mac_data.errata[0],
57 mac_data.errata[1],
58 mac_data.errata[2],
59 mac_data.errata[3],
60 mac_data.errata[4]);
61 printf("Date %c%c%c%c%c%c%c\n",
62 mac_data.date[0],
63 mac_data.date[1],
64 mac_data.date[2],
65 mac_data.date[3],
66 mac_data.date[4],
67 mac_data.date[5],
68 mac_data.date[6]);
Haiying Wang34c68782006-07-12 10:48:05 -040069 for (i = 0; i < 8; i++) {
Wolfgang Denk4d5a8e32007-08-02 00:48:45 +020070 sprintf((char *)ethaddr[i],
Jon Loeliger4fbb09c2006-08-22 12:25:27 -050071 "%02x:%02x:%02x:%02x:%02x:%02x",
72 mac_data.mac[i][0],
73 mac_data.mac[i][1],
74 mac_data.mac[i][2],
75 mac_data.mac[i][3],
76 mac_data.mac[i][4],
77 mac_data.mac[i][5]);
Haiying Wang34c68782006-07-12 10:48:05 -040078 printf("MAC %d %s\n", i, ethaddr[i]);
79 }
80
Wolfgang Denk4d5a8e32007-08-02 00:48:45 +020081 setenv("ethaddr", (char *)ethaddr[0]);
82 setenv("eth1addr", (char *)ethaddr[1]);
83 setenv("eth2addr", (char *)ethaddr[2]);
84 setenv("eth3addr", (char *)ethaddr[3]);
Haiying Wang34c68782006-07-12 10:48:05 -040085
86 return 0;
87}
88
89int mac_read(void)
90{
Jon Loeliger4fbb09c2006-08-22 12:25:27 -050091 int ret, length;
Haiying Wang34c68782006-07-12 10:48:05 -040092 unsigned int crc = 0;
93 unsigned char dev = ID_EEPROM_ADDR, *data;
94
95 length = sizeof(EEPROM_data);
96 ret = i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length);
97 if (ret) {
98 printf("Read failed.\n");
99 return -1;
100 }
101
102 data = (unsigned char *)(&mac_data);
103 printf("Check CRC on reading ...");
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500104 crc = crc32(crc, data, length - 4);
Haiying Wang34c68782006-07-12 10:48:05 -0400105 if (crc != mac_data.crc) {
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500106 printf("CRC checksum is invalid, in EEPROM CRC is %x, calculated CRC is %x\n",
107 mac_data.crc, crc);
Haiying Wang34c68782006-07-12 10:48:05 -0400108 return -1;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500109 } else {
Haiying Wang34c68782006-07-12 10:48:05 -0400110 printf("CRC OK\n");
111 mac_show();
112 }
113 return 0;
114}
115
116int mac_prog(void)
117{
118 int ret, i, length;
119 unsigned int crc = 0;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500120 unsigned char dev = ID_EEPROM_ADDR, *ptr;
121 unsigned char *eeprom_data = (unsigned char *)(&mac_data);
Haiying Wang34c68782006-07-12 10:48:05 -0400122
123 for (i = 0; i < sizeof(mac_data.res_1); i++)
124 mac_data.res_1[i] = 0;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500125 for (i = 0; i < sizeof(mac_data.res_2); i++)
Haiying Wang34c68782006-07-12 10:48:05 -0400126 mac_data.res_2[i] = 0;
127 length = sizeof(EEPROM_data);
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500128 crc = crc32(crc, eeprom_data, length - 4);
Haiying Wang34c68782006-07-12 10:48:05 -0400129 mac_data.crc = crc;
130 for (i = 0, ptr = eeprom_data; i < length; i += 8, ptr += 8) {
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500131 ret =
132 i2c_write(dev, i, 1, ptr,
133 (length - i) < 8 ? (length - i) : 8);
134 udelay(5000); /* 5ms write cycle timing */
Haiying Wang34c68782006-07-12 10:48:05 -0400135 if (ret)
136 break;
137 }
138 if (ret) {
139 printf("Programming failed.\n");
140 return -1;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500141 } else {
142 printf("Programming %d bytes. Reading back ...\n", length);
Haiying Wang34c68782006-07-12 10:48:05 -0400143 mac_read();
144 }
145 return 0;
146}
147
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500148int do_mac(cmd_tbl_t * cmdtp, int flag, int argc, char *argv[])
Haiying Wang34c68782006-07-12 10:48:05 -0400149{
150 int i;
151 char cmd = 's';
152 unsigned long long mac_val;
153
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500154 if (i2c_probe(ID_EEPROM_ADDR) != 0)
Haiying Wang34c68782006-07-12 10:48:05 -0400155 return -1;
156
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500157 if (argc > 1) {
Haiying Wang34c68782006-07-12 10:48:05 -0400158 cmd = argv[1][0];
159 switch (cmd) {
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500160 case 'r': /* display */
161 mac_read();
162 break;
163 case 's': /* save */
164 mac_prog();
165 break;
166 case 'i': /* id */
167 for (i = 0; i < 4; i++) {
168 mac_data.id[i] = argv[2][i];
169 }
170 break;
171 case 'n': /* serial number */
172 for (i = 0; i < 12; i++) {
173 mac_data.sn[i] = argv[2][i];
174 }
175 break;
176 case 'e': /* errata */
177 for (i = 0; i < 5; i++) {
178 mac_data.errata[i] = argv[2][i];
179 }
180 break;
181 case 'd': /* date */
182 for (i = 0; i < 7; i++) {
183 mac_data.date[i] = argv[2][i];
184 }
185 break;
186 case 'p': /* number of ports */
187 mac_data.tab_size =
188 (unsigned char)simple_strtoul(argv[2], NULL, 16);
189 break;
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] =
201 *((unsigned char *)
202 (((unsigned int)(&mac_val)) + i + 2));
203 }
204 break;
205 case 'h': /* help */
206 default:
207 printf("Usage:\n%s\n", cmdtp->usage);
208 break;
Haiying Wang34c68782006-07-12 10:48:05 -0400209 }
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500210 } else {
Haiying Wang34c68782006-07-12 10:48:05 -0400211 mac_show();
212 }
213 return 0;
214}
215
216int mac_read_from_eeprom(void)
217{
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500218 int length, i;
219 unsigned char dev = ID_EEPROM_ADDR;
220 unsigned char *data;
221 unsigned char ethaddr[4][18];
222 unsigned char enetvar[32];
Haiying Wang34c68782006-07-12 10:48:05 -0400223 unsigned int crc = 0;
224
225 length = sizeof(EEPROM_data);
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500226 if (i2c_read(dev, 0, 1, (unsigned char *)(&mac_data), length)) {
Haiying Wang34c68782006-07-12 10:48:05 -0400227 printf("Read failed.\n");
228 return -1;
229 }
230
231 data = (unsigned char *)(&mac_data);
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500232 crc = crc32(crc, data, length - 4);
Haiying Wang34c68782006-07-12 10:48:05 -0400233 if (crc != mac_data.crc) {
234 return -1;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500235 } else {
236 for (i = 0; i < 4; i++) {
237 if (memcmp(&mac_data.mac[i], "\0\0\0\0\0\0", 6)) {
Wolfgang Denk4d5a8e32007-08-02 00:48:45 +0200238 sprintf((char *)ethaddr[i],
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500239 "%02x:%02x:%02x:%02x:%02x:%02x",
240 mac_data.mac[i][0],
241 mac_data.mac[i][1],
242 mac_data.mac[i][2],
243 mac_data.mac[i][3],
244 mac_data.mac[i][4],
Haiying Wang34c68782006-07-12 10:48:05 -0400245 mac_data.mac[i][5]);
Wolfgang Denk4d5a8e32007-08-02 00:48:45 +0200246 sprintf((char *)enetvar,
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500247 i ? "eth%daddr" : "ethaddr",
248 i);
Wolfgang Denk4d5a8e32007-08-02 00:48:45 +0200249 setenv((char *)enetvar, (char *)ethaddr[i]);
Haiying Wang34c68782006-07-12 10:48:05 -0400250 }
251 }
252 }
253 return 0;
254}