blob: 3ecfb06cc84d5a7a85973f03a7c256b2362d0740 [file] [log] [blame]
Haiying Wangbea3f282006-07-12 10:48:05 -04001/*
Haiying Wang9a611082009-06-04 16:12:40 -04002 * Copyright 2006, 2008-2009 Freescale Semiconductor
Haiying Wangbea3f282006-07-12 10:48:05 -04003 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
Timur Tabie2d31fb2008-06-19 17:56:11 -05005 * Timur Tabi (timur@freescale.com)
Haiying Wangbea3f282006-07-12 10:48:05 -04006 *
7 * See file CREDITS for list of people who contributed to this
8 * project.
9 *
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23 * MA 02111-1307 USA
24 */
25
26#include <common.h>
27#include <command.h>
28#include <i2c.h>
29#include <linux/ctype.h>
30
Timur Tabibfb70712010-09-30 15:36:50 -050031#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -050032#include "../common/eeprom.h"
Timur Tabibfb70712010-09-30 15:36:50 -050033#define MAX_NUM_PORTS 8
Timur Tabie2d31fb2008-06-19 17:56:11 -050034#endif
Haiying Wangbea3f282006-07-12 10:48:05 -040035
Timur Tabibfb70712010-09-30 15:36:50 -050036#ifdef CONFIG_SYS_I2C_EEPROM_NXID
37#define MAX_NUM_PORTS 8
38#define NXID_VERSION 0
39#endif
40
41#ifdef CONFIG_SYS_I2C_EEPROM_NXID_1
42#define CONFIG_SYS_I2C_EEPROM_NXID
43#define MAX_NUM_PORTS 23
44#define NXID_VERSION 1
45#endif
Haiying Wang9a611082009-06-04 16:12:40 -040046
Timur Tabie2d31fb2008-06-19 17:56:11 -050047/**
48 * static eeprom: EEPROM layout for CCID or NXID formats
49 *
50 * See application note AN3638 for details.
51 */
52static struct __attribute__ ((__packed__)) eeprom {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020053#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -050054 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
55 u8 major; /* 0x04 Board revision, major */
56 u8 minor; /* 0x05 Board revision, minor */
57 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
58 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
59 u8 date[6]; /* 0x12 - 0x17 Build Date */
60 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
61 u8 mac_count; /* 0x40 Number of MAC addresses */
62 u8 mac_flag; /* 0x41 MAC table flags */
Haiying Wang9a611082009-06-04 16:12:40 -040063 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
Timur Tabie2d31fb2008-06-19 17:56:11 -050064 u32 crc; /* 0x72 CRC32 checksum */
65#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -050067 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
68 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
69 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
70 u8 date[6]; /* 0x15 - 0x1a Build Date */
71 u8 res_0; /* 0x1b Reserved */
72 u32 version; /* 0x1c - 0x1f NXID Version */
73 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
74 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
75 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
76 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
77 u8 mac_count; /* 0x40 Number of MAC addresses */
78 u8 mac_flag; /* 0x41 MAC table flags */
Timur Tabibfb70712010-09-30 15:36:50 -050079 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */
80 u32 crc; /* x+1 CRC32 checksum */
Timur Tabie2d31fb2008-06-19 17:56:11 -050081#endif
82} e;
83
84/* Set to 1 if we've read EEPROM into memory */
85static int has_been_read = 0;
86
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020087#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -050088/* Is this a valid NXID EEPROM? */
Kumar Galaafb0b1312009-07-03 12:45:44 -050089#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
90 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabie2d31fb2008-06-19 17:56:11 -050091#endif
92
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020093#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -050094/* Is this a valid CCID EEPROM? */
Kumar Galaafb0b1312009-07-03 12:45:44 -050095#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
96 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabie2d31fb2008-06-19 17:56:11 -050097#endif
98
99/**
100 * show_eeprom - display the contents of the EEPROM
101 */
102static void show_eeprom(void)
Haiying Wangbea3f282006-07-12 10:48:05 -0400103{
104 int i;
Timur Tabie2d31fb2008-06-19 17:56:11 -0500105 unsigned int crc;
Haiying Wangbea3f282006-07-12 10:48:05 -0400106
Timur Tabie2d31fb2008-06-19 17:56:11 -0500107 /* EEPROM tag ID, either CCID or NXID */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200108#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500109 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
110 be32_to_cpu(e.version));
111#else
112 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
113#endif
Haiying Wangd59feff2008-01-16 17:12:12 -0500114
Timur Tabie2d31fb2008-06-19 17:56:11 -0500115 /* Serial number */
116 printf("SN: %s\n", e.sn);
Haiying Wangd59feff2008-01-16 17:12:12 -0500117
Timur Tabie2d31fb2008-06-19 17:56:11 -0500118 /* Errata level. */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200119#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500120 printf("Errata: %s\n", e.errata);
121#else
122 printf("Errata: %c%c\n",
123 e.errata[0] ? e.errata[0] : '.',
124 e.errata[1] ? e.errata[1] : '.');
125#endif
Haiying Wangd59feff2008-01-16 17:12:12 -0500126
Timur Tabie2d31fb2008-06-19 17:56:11 -0500127 /* Build date, BCD date values, as YYMMDDhhmmss */
128 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
129 e.date[0], e.date[1], e.date[2],
130 e.date[3] & 0x7F, e.date[4], e.date[5],
131 e.date[3] & 0x80 ? "PM" : "");
Haiying Wangd59feff2008-01-16 17:12:12 -0500132
Timur Tabie2d31fb2008-06-19 17:56:11 -0500133 /* Show MAC addresses */
Haiying Wang9a611082009-06-04 16:12:40 -0400134 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
135
Timur Tabie2d31fb2008-06-19 17:56:11 -0500136 u8 *p = e.mac[i];
137
138 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
139 p[0], p[1], p[2], p[3], p[4], p[5]);
140 }
141
142 crc = crc32(0, (void *)&e, sizeof(e) - 4);
143
144 if (crc == be32_to_cpu(e.crc))
145 printf("CRC: %08x\n", be32_to_cpu(e.crc));
Haiying Wangd59feff2008-01-16 17:12:12 -0500146 else
Timur Tabie2d31fb2008-06-19 17:56:11 -0500147 printf("CRC: %08x (should be %08x)\n",
148 be32_to_cpu(e.crc), crc);
Haiying Wangd59feff2008-01-16 17:12:12 -0500149
Timur Tabie2d31fb2008-06-19 17:56:11 -0500150#ifdef DEBUG
151 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
152 for (i = 0; i < sizeof(e); i++) {
153 if ((i % 16) == 0)
154 printf("%02X: ", i);
155 printf("%02X ", ((u8 *)&e)[i]);
156 if (((i % 16) == 15) || (i == sizeof(e) - 1))
157 printf("\n");
Haiying Wangd59feff2008-01-16 17:12:12 -0500158 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500159#endif
Haiying Wangbea3f282006-07-12 10:48:05 -0400160}
161
Timur Tabie2d31fb2008-06-19 17:56:11 -0500162/**
163 * read_eeprom - read the EEPROM into memory
164 */
165static int read_eeprom(void)
Haiying Wangbea3f282006-07-12 10:48:05 -0400166{
Timur Tabie2d31fb2008-06-19 17:56:11 -0500167 int ret;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200168#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500169 unsigned int bus;
170#endif
Haiying Wangbea3f282006-07-12 10:48:05 -0400171
Timur Tabie2d31fb2008-06-19 17:56:11 -0500172 if (has_been_read)
173 return 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400174
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200175#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500176 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200177 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500178#endif
179
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200180 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabie2d31fb2008-06-19 17:56:11 -0500181 (void *)&e, sizeof(e));
182
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200183#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500184 i2c_set_bus_num(bus);
185#endif
186
187#ifdef DEBUG
188 show_eeprom();
189#endif
190
191 has_been_read = (ret == 0) ? 1 : 0;
192
193 return ret;
Haiying Wangbea3f282006-07-12 10:48:05 -0400194}
195
Timur Tabie2d31fb2008-06-19 17:56:11 -0500196/**
Timur Tabi2d04db02009-08-28 16:56:45 -0500197 * update_crc - update the CRC
198 *
199 * This function should be called after each update to the EEPROM structure,
200 * to make sure the CRC is always correct.
201 */
202static void update_crc(void)
203{
204 u32 crc;
205
206 crc = crc32(0, (void *)&e, sizeof(e) - 4);
207 e.crc = cpu_to_be32(crc);
208}
209
210/**
Timur Tabie2d31fb2008-06-19 17:56:11 -0500211 * prog_eeprom - write the EEPROM from memory
212 */
213static int prog_eeprom(void)
Haiying Wangbea3f282006-07-12 10:48:05 -0400214{
Timur Tabi3addcb92010-08-02 13:03:23 -0500215 int ret = 0;
Anton Vorontsov9c671e72009-09-02 02:17:24 +0400216 int i;
Timur Tabie2d31fb2008-06-19 17:56:11 -0500217 void *p;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200218#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500219 unsigned int bus;
220#endif
Haiying Wangbea3f282006-07-12 10:48:05 -0400221
Timur Tabie2d31fb2008-06-19 17:56:11 -0500222 /* Set the reserved values to 0xFF */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200223#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500224 e.res_0 = 0xFF;
225 memset(e.res_1, 0xFF, sizeof(e.res_1));
226#else
227 memset(e.res_0, 0xFF, sizeof(e.res_0));
228#endif
Timur Tabi2d04db02009-08-28 16:56:45 -0500229 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500230
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200231#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500232 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200233 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500234#endif
235
Timur Tabi3addcb92010-08-02 13:03:23 -0500236 /*
237 * The AT24C02 datasheet says that data can only be written in page
238 * mode, which means 8 bytes at a time, and it takes up to 5ms to
239 * complete a given write.
240 */
Timur Tabi2d04db02009-08-28 16:56:45 -0500241 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200242 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabi2d04db02009-08-28 16:56:45 -0500243 p, min((sizeof(e) - i), 8));
Haiying Wangbea3f282006-07-12 10:48:05 -0400244 if (ret)
245 break;
Timur Tabie2d31fb2008-06-19 17:56:11 -0500246 udelay(5000); /* 5ms write cycle timing */
Haiying Wangbea3f282006-07-12 10:48:05 -0400247 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500248
Timur Tabi3addcb92010-08-02 13:03:23 -0500249 if (!ret) {
250 /* Verify the write by reading back the EEPROM and comparing */
251 struct eeprom e2;
252
253 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
254 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
255 if (!ret && memcmp(&e, &e2, sizeof(e)))
256 ret = -1;
257 }
258
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200259#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabie2d31fb2008-06-19 17:56:11 -0500260 i2c_set_bus_num(bus);
261#endif
262
Haiying Wangbea3f282006-07-12 10:48:05 -0400263 if (ret) {
264 printf("Programming failed.\n");
Timur Tabi3addcb92010-08-02 13:03:23 -0500265 has_been_read = 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400266 return -1;
Haiying Wangbea3f282006-07-12 10:48:05 -0400267 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500268
269 printf("Programming passed.\n");
Haiying Wangbea3f282006-07-12 10:48:05 -0400270 return 0;
271}
272
Timur Tabie2d31fb2008-06-19 17:56:11 -0500273/**
274 * h2i - converts hex character into a number
275 *
276 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
277 * the integer equivalent.
278 */
279static inline u8 h2i(char p)
280{
281 if ((p >= '0') && (p <= '9'))
282 return p - '0';
283
284 if ((p >= 'A') && (p <= 'F'))
285 return (p - 'A') + 10;
286
287 if ((p >= 'a') && (p <= 'f'))
288 return (p - 'a') + 10;
289
290 return 0;
291}
292
293/**
294 * set_date - stores the build date into the EEPROM
295 *
296 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
297 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
298 * and stores it in the build date field of the EEPROM local copy.
299 */
300static void set_date(const char *string)
301{
302 unsigned int i;
303
304 if (strlen(string) != 12) {
305 printf("Usage: mac date YYMMDDhhmmss\n");
306 return;
307 }
308
309 for (i = 0; i < 6; i++)
310 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
Timur Tabi2d04db02009-08-28 16:56:45 -0500311
312 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500313}
314
315/**
316 * set_mac_address - stores a MAC address into the EEPROM
317 *
318 * This function takes a pointer to MAC address string
319 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
320 * stores it in one of the MAC address fields of the EEPROM local copy.
321 */
322static void set_mac_address(unsigned int index, const char *string)
323{
324 char *p = (char *) string;
325 unsigned int i;
326
Timur Tabibfb70712010-09-30 15:36:50 -0500327 if ((index >= MAX_NUM_PORTS) || !string) {
Timur Tabie2d31fb2008-06-19 17:56:11 -0500328 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
329 return;
330 }
331
332 for (i = 0; *p && (i < 6); i++) {
333 e.mac[index][i] = simple_strtoul(p, &p, 16);
334 if (*p == ':')
335 p++;
336 }
Timur Tabi2d04db02009-08-28 16:56:45 -0500337
338 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500339}
340
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200341int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Haiying Wangbea3f282006-07-12 10:48:05 -0400342{
Timur Tabie2d31fb2008-06-19 17:56:11 -0500343 char cmd;
Haiying Wangbea3f282006-07-12 10:48:05 -0400344
Timur Tabie2d31fb2008-06-19 17:56:11 -0500345 if (argc == 1) {
346 show_eeprom();
347 return 0;
348 }
Haiying Wangbea3f282006-07-12 10:48:05 -0400349
Timur Tabie2d31fb2008-06-19 17:56:11 -0500350 cmd = argv[1][0];
351
352 if (cmd == 'r') {
353 read_eeprom();
354 return 0;
355 }
356
Timur Tabi2d04db02009-08-28 16:56:45 -0500357 if (cmd == 'i') {
358#ifdef CONFIG_SYS_I2C_EEPROM_NXID
359 memcpy(e.id, "NXID", sizeof(e.id));
Timur Tabibfb70712010-09-30 15:36:50 -0500360 e.version = NXID_VERSION;
Timur Tabi2d04db02009-08-28 16:56:45 -0500361#else
362 memcpy(e.id, "CCID", sizeof(e.id));
363#endif
Timur Tabi3fee3342011-02-09 13:40:51 -0600364 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500365 return 0;
366 }
367
368 if (!is_valid) {
369 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
370 return 0;
371 }
372
373 if (argc == 2) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400374 switch (cmd) {
Jon Loeliger80e955c2006-08-22 12:25:27 -0500375 case 's': /* save */
Timur Tabie2d31fb2008-06-19 17:56:11 -0500376 prog_eeprom();
Jon Loeliger80e955c2006-08-22 12:25:27 -0500377 break;
Jon Loeliger80e955c2006-08-22 12:25:27 -0500378 default:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200379 return cmd_usage(cmdtp);
Haiying Wangbea3f282006-07-12 10:48:05 -0400380 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500381
382 return 0;
Haiying Wangbea3f282006-07-12 10:48:05 -0400383 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500384
385 /* We know we have at least one parameter */
386
387 switch (cmd) {
388 case 'n': /* serial number */
389 memset(e.sn, 0, sizeof(e.sn));
390 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
Timur Tabi2d04db02009-08-28 16:56:45 -0500391 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500392 break;
393 case 'e': /* errata */
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200394#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500395 memset(e.errata, 0, 5);
396 strncpy((char *)e.errata, argv[2], 4);
397#else
398 e.errata[0] = argv[2][0];
399 e.errata[1] = argv[2][1];
400#endif
Timur Tabi2d04db02009-08-28 16:56:45 -0500401 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500402 break;
403 case 'd': /* date BCD format YYMMDDhhmmss */
404 set_date(argv[2]);
405 break;
406 case 'p': /* MAC table size */
407 e.mac_count = simple_strtoul(argv[2], NULL, 16);
Timur Tabi2d04db02009-08-28 16:56:45 -0500408 update_crc();
Timur Tabie2d31fb2008-06-19 17:56:11 -0500409 break;
Timur Tabibfb70712010-09-30 15:36:50 -0500410 case '0' ... '9': /* "mac 0" through "mac 22" */
411 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500412 break;
413 case 'h': /* help */
414 default:
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200415 return cmd_usage(cmdtp);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500416 }
417
Haiying Wangbea3f282006-07-12 10:48:05 -0400418 return 0;
419}
420
Timur Tabie2d31fb2008-06-19 17:56:11 -0500421/**
422 * mac_read_from_eeprom - read the MAC addresses from EEPROM
423 *
424 * This function reads the MAC addresses from EEPROM and sets the
425 * appropriate environment variables for each one read.
426 *
427 * The environment variables are only set if they haven't been set already.
428 * This ensures that any user-saved variables are never overwritten.
429 *
430 * This function must be called after relocation.
431 */
Haiying Wangbea3f282006-07-12 10:48:05 -0400432int mac_read_from_eeprom(void)
433{
Timur Tabie2d31fb2008-06-19 17:56:11 -0500434 unsigned int i;
Timur Tabi2d04db02009-08-28 16:56:45 -0500435 u32 crc;
436
437 puts("EEPROM: ");
Haiying Wangbea3f282006-07-12 10:48:05 -0400438
Timur Tabie2d31fb2008-06-19 17:56:11 -0500439 if (read_eeprom()) {
Haiying Wangbea3f282006-07-12 10:48:05 -0400440 printf("Read failed.\n");
441 return -1;
442 }
443
Timur Tabie2d31fb2008-06-19 17:56:11 -0500444 if (!is_valid) {
Timur Tabi2d04db02009-08-28 16:56:45 -0500445 printf("Invalid ID (%02x %02x %02x %02x)\n",
446 e.id[0], e.id[1], e.id[2], e.id[3]);
Haiying Wangbea3f282006-07-12 10:48:05 -0400447 return -1;
Timur Tabie2d31fb2008-06-19 17:56:11 -0500448 }
449
Timur Tabi2d04db02009-08-28 16:56:45 -0500450 crc = crc32(0, (void *)&e, sizeof(e) - 4);
451 if (crc != be32_to_cpu(e.crc)) {
452 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
453 return -1;
Haiying Wangbea3f282006-07-12 10:48:05 -0400454 }
Timur Tabie2d31fb2008-06-19 17:56:11 -0500455
Timur Tabi2d04db02009-08-28 16:56:45 -0500456 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
Timur Tabie2d31fb2008-06-19 17:56:11 -0500457 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
458 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
459 char ethaddr[18];
460 char enetvar[9];
461
462 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
463 e.mac[i][0],
464 e.mac[i][1],
465 e.mac[i][2],
466 e.mac[i][3],
467 e.mac[i][4],
468 e.mac[i][5]);
469 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
470 /* Only initialize environment variables that are blank
471 * (i.e. have not yet been set)
472 */
473 if (!getenv(enetvar))
474 setenv(enetvar, ethaddr);
475 }
476 }
477
Timur Tabi2d04db02009-08-28 16:56:45 -0500478#ifdef CONFIG_SYS_I2C_EEPROM_NXID
479 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
480 be32_to_cpu(e.version));
481#else
482 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
483#endif
484
Haiying Wangbea3f282006-07-12 10:48:05 -0400485 return 0;
486}
Timur Tabie2d31fb2008-06-19 17:56:11 -0500487
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200488#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabie2d31fb2008-06-19 17:56:11 -0500489
490/**
491 * get_cpu_board_revision - get the CPU board revision on 85xx boards
492 *
493 * Read the EEPROM to determine the board revision.
494 *
495 * This function is called before relocation, so we need to read a private
496 * copy of the EEPROM into a local variable on the stack.
497 *
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200498 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
499 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
Timur Tabie2d31fb2008-06-19 17:56:11 -0500500 * so that the SPD code will work. This means that all pre-relocation I2C
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200501 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
502 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
Timur Tabie2d31fb2008-06-19 17:56:11 -0500503 * this function is called. Oh well.
504 */
505unsigned int get_cpu_board_revision(void)
506{
507 struct board_eeprom {
508 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
509 u8 major; /* 0x04 Board revision, major */
510 u8 minor; /* 0x05 Board revision, minor */
511 } be;
512
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200513 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabie2d31fb2008-06-19 17:56:11 -0500514 (void *)&be, sizeof(be));
515
516 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
517 return MPC85XX_CPU_BOARD_REV(0, 0);
518
519 if ((be.major == 0xff) && (be.minor == 0xff))
520 return MPC85XX_CPU_BOARD_REV(0, 0);
521
Rafal Czubake46c7bf2008-10-08 13:41:30 +0200522 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
Timur Tabie2d31fb2008-06-19 17:56:11 -0500523}
Timur Tabie2d31fb2008-06-19 17:56:11 -0500524#endif