blob: d7893644cd4816519e04d7fce8a69a52cba051e2 [file] [log] [blame]
Haiying Wang34c68782006-07-12 10:48:05 -04001/*
Timur Tabia2b41cb2011-02-09 02:00:09 +00002 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
Haiying Wang34c68782006-07-12 10:48:05 -04003 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
Timur Tabibdea8372008-06-19 17:56:11 -05005 * Timur Tabi (timur@freescale.com)
Haiying Wang34c68782006-07-12 10:48:05 -04006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Haiying Wang34c68782006-07-12 10:48:05 -04008 */
9
10#include <common.h>
11#include <command.h>
12#include <i2c.h>
13#include <linux/ctype.h>
14
Timur Tabi11e98e72010-09-30 15:36:50 -050015#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050016#include "../common/eeprom.h"
Timur Tabi11e98e72010-09-30 15:36:50 -050017#define MAX_NUM_PORTS 8
18#endif
Haiying Wang34c68782006-07-12 10:48:05 -040019
Timur Tabi11e98e72010-09-30 15:36:50 -050020#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabi11e98e72010-09-30 15:36:50 -050021#define MAX_NUM_PORTS 23
22#define NXID_VERSION 1
23#endif
Haiying Wange5607542009-06-04 16:12:40 -040024
Timur Tabibdea8372008-06-19 17:56:11 -050025/**
26 * static eeprom: EEPROM layout for CCID or NXID formats
27 *
28 * See application note AN3638 for details.
29 */
30static struct __attribute__ ((__packed__)) eeprom {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020031#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050032 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
33 u8 major; /* 0x04 Board revision, major */
34 u8 minor; /* 0x05 Board revision, minor */
35 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
36 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
37 u8 date[6]; /* 0x12 - 0x17 Build Date */
38 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
39 u8 mac_count; /* 0x40 Number of MAC addresses */
40 u8 mac_flag; /* 0x41 MAC table flags */
Haiying Wange5607542009-06-04 16:12:40 -040041 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
Timur Tabibdea8372008-06-19 17:56:11 -050042 u32 crc; /* 0x72 CRC32 checksum */
43#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020044#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050045 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
46 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
47 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
48 u8 date[6]; /* 0x15 - 0x1a Build Date */
49 u8 res_0; /* 0x1b Reserved */
50 u32 version; /* 0x1c - 0x1f NXID Version */
51 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
52 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
53 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
54 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
55 u8 mac_count; /* 0x40 Number of MAC addresses */
56 u8 mac_flag; /* 0x41 MAC table flags */
Timur Tabi11e98e72010-09-30 15:36:50 -050057 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */
58 u32 crc; /* x+1 CRC32 checksum */
Timur Tabibdea8372008-06-19 17:56:11 -050059#endif
60} e;
61
62/* Set to 1 if we've read EEPROM into memory */
63static int has_been_read = 0;
64
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020065#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050066/* Is this a valid NXID EEPROM? */
Kumar Gala4dc62452009-07-03 12:45:44 -050067#define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
68 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabibdea8372008-06-19 17:56:11 -050069#endif
70
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020071#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -050072/* Is this a valid CCID EEPROM? */
Kumar Gala4dc62452009-07-03 12:45:44 -050073#define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
74 (e.id[2] == 'I') || (e.id[3] == 'D'))
Timur Tabibdea8372008-06-19 17:56:11 -050075#endif
76
77/**
78 * show_eeprom - display the contents of the EEPROM
79 */
80static void show_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -040081{
82 int i;
Timur Tabibdea8372008-06-19 17:56:11 -050083 unsigned int crc;
Haiying Wang34c68782006-07-12 10:48:05 -040084
Timur Tabibdea8372008-06-19 17:56:11 -050085 /* EEPROM tag ID, either CCID or NXID */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020086#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050087 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
88 be32_to_cpu(e.version));
89#else
90 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
91#endif
Haiying Wang34cf9592008-01-16 17:12:12 -050092
Timur Tabibdea8372008-06-19 17:56:11 -050093 /* Serial number */
94 printf("SN: %s\n", e.sn);
Haiying Wang34cf9592008-01-16 17:12:12 -050095
Timur Tabibdea8372008-06-19 17:56:11 -050096 /* Errata level. */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020097#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -050098 printf("Errata: %s\n", e.errata);
99#else
100 printf("Errata: %c%c\n",
101 e.errata[0] ? e.errata[0] : '.',
102 e.errata[1] ? e.errata[1] : '.');
103#endif
Haiying Wang34cf9592008-01-16 17:12:12 -0500104
Timur Tabibdea8372008-06-19 17:56:11 -0500105 /* Build date, BCD date values, as YYMMDDhhmmss */
106 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
107 e.date[0], e.date[1], e.date[2],
108 e.date[3] & 0x7F, e.date[4], e.date[5],
109 e.date[3] & 0x80 ? "PM" : "");
Haiying Wang34cf9592008-01-16 17:12:12 -0500110
Timur Tabibdea8372008-06-19 17:56:11 -0500111 /* Show MAC addresses */
Haiying Wange5607542009-06-04 16:12:40 -0400112 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
113
Timur Tabibdea8372008-06-19 17:56:11 -0500114 u8 *p = e.mac[i];
Haiying Wang34cf9592008-01-16 17:12:12 -0500115
Timur Tabibdea8372008-06-19 17:56:11 -0500116 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
117 p[0], p[1], p[2], p[3], p[4], p[5]);
118 }
Haiying Wang34cf9592008-01-16 17:12:12 -0500119
Timur Tabibdea8372008-06-19 17:56:11 -0500120 crc = crc32(0, (void *)&e, sizeof(e) - 4);
Haiying Wang34c68782006-07-12 10:48:05 -0400121
Timur Tabibdea8372008-06-19 17:56:11 -0500122 if (crc == be32_to_cpu(e.crc))
123 printf("CRC: %08x\n", be32_to_cpu(e.crc));
124 else
125 printf("CRC: %08x (should be %08x)\n",
126 be32_to_cpu(e.crc), crc);
Haiying Wang34c68782006-07-12 10:48:05 -0400127
Timur Tabibdea8372008-06-19 17:56:11 -0500128#ifdef DEBUG
129 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
130 for (i = 0; i < sizeof(e); i++) {
131 if ((i % 16) == 0)
132 printf("%02X: ", i);
133 printf("%02X ", ((u8 *)&e)[i]);
134 if (((i % 16) == 15) || (i == sizeof(e) - 1))
135 printf("\n");
136 }
137#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400138}
139
Timur Tabibdea8372008-06-19 17:56:11 -0500140/**
141 * read_eeprom - read the EEPROM into memory
142 */
143static int read_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -0400144{
Timur Tabibdea8372008-06-19 17:56:11 -0500145 int ret;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200146#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500147 unsigned int bus;
148#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400149
Timur Tabibdea8372008-06-19 17:56:11 -0500150 if (has_been_read)
151 return 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400152
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200153#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500154 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200155 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabibdea8372008-06-19 17:56:11 -0500156#endif
157
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200158 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabibdea8372008-06-19 17:56:11 -0500159 (void *)&e, sizeof(e));
160
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200161#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500162 i2c_set_bus_num(bus);
163#endif
164
165#ifdef DEBUG
166 show_eeprom();
167#endif
168
169 has_been_read = (ret == 0) ? 1 : 0;
170
171 return ret;
Haiying Wang34c68782006-07-12 10:48:05 -0400172}
173
Timur Tabibdea8372008-06-19 17:56:11 -0500174/**
Timur Tabi13c312b2009-08-28 16:56:45 -0500175 * update_crc - update the CRC
176 *
177 * This function should be called after each update to the EEPROM structure,
178 * to make sure the CRC is always correct.
179 */
180static void update_crc(void)
181{
182 u32 crc;
183
184 crc = crc32(0, (void *)&e, sizeof(e) - 4);
185 e.crc = cpu_to_be32(crc);
186}
187
188/**
Timur Tabibdea8372008-06-19 17:56:11 -0500189 * prog_eeprom - write the EEPROM from memory
190 */
191static int prog_eeprom(void)
Haiying Wang34c68782006-07-12 10:48:05 -0400192{
Timur Tabia58e8802010-08-02 13:03:23 -0500193 int ret = 0;
Anton Vorontsovc33e3b82009-09-02 02:17:24 +0400194 int i;
Timur Tabibdea8372008-06-19 17:56:11 -0500195 void *p;
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200196#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500197 unsigned int bus;
198#endif
Haiying Wang34c68782006-07-12 10:48:05 -0400199
Timur Tabibdea8372008-06-19 17:56:11 -0500200 /* Set the reserved values to 0xFF */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200201#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500202 e.res_0 = 0xFF;
203 memset(e.res_1, 0xFF, sizeof(e.res_1));
204#else
205 memset(e.res_0, 0xFF, sizeof(e.res_0));
206#endif
Timur Tabi13c312b2009-08-28 16:56:45 -0500207 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500208
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200209#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500210 bus = i2c_get_bus_num();
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200211 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
Timur Tabibdea8372008-06-19 17:56:11 -0500212#endif
213
Timur Tabia58e8802010-08-02 13:03:23 -0500214 /*
215 * The AT24C02 datasheet says that data can only be written in page
216 * mode, which means 8 bytes at a time, and it takes up to 5ms to
217 * complete a given write.
218 */
Timur Tabi13c312b2009-08-28 16:56:45 -0500219 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200220 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabi13c312b2009-08-28 16:56:45 -0500221 p, min((sizeof(e) - i), 8));
Haiying Wang34c68782006-07-12 10:48:05 -0400222 if (ret)
223 break;
Timur Tabibdea8372008-06-19 17:56:11 -0500224 udelay(5000); /* 5ms write cycle timing */
Haiying Wang34c68782006-07-12 10:48:05 -0400225 }
Timur Tabibdea8372008-06-19 17:56:11 -0500226
Timur Tabia58e8802010-08-02 13:03:23 -0500227 if (!ret) {
228 /* Verify the write by reading back the EEPROM and comparing */
229 struct eeprom e2;
230
231 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
232 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, (void *)&e2, sizeof(e2));
233 if (!ret && memcmp(&e, &e2, sizeof(e)))
234 ret = -1;
235 }
236
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200237#ifdef CONFIG_SYS_EEPROM_BUS_NUM
Timur Tabibdea8372008-06-19 17:56:11 -0500238 i2c_set_bus_num(bus);
239#endif
240
Haiying Wang34c68782006-07-12 10:48:05 -0400241 if (ret) {
242 printf("Programming failed.\n");
Timur Tabia58e8802010-08-02 13:03:23 -0500243 has_been_read = 0;
Haiying Wang34c68782006-07-12 10:48:05 -0400244 return -1;
Haiying Wang34c68782006-07-12 10:48:05 -0400245 }
Timur Tabibdea8372008-06-19 17:56:11 -0500246
247 printf("Programming passed.\n");
Haiying Wang34c68782006-07-12 10:48:05 -0400248 return 0;
249}
250
Timur Tabibdea8372008-06-19 17:56:11 -0500251/**
252 * h2i - converts hex character into a number
253 *
254 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
255 * the integer equivalent.
256 */
257static inline u8 h2i(char p)
258{
259 if ((p >= '0') && (p <= '9'))
260 return p - '0';
261
262 if ((p >= 'A') && (p <= 'F'))
263 return (p - 'A') + 10;
264
265 if ((p >= 'a') && (p <= 'f'))
266 return (p - 'a') + 10;
267
268 return 0;
269}
270
271/**
272 * set_date - stores the build date into the EEPROM
273 *
274 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
275 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
276 * and stores it in the build date field of the EEPROM local copy.
277 */
278static void set_date(const char *string)
279{
280 unsigned int i;
281
282 if (strlen(string) != 12) {
283 printf("Usage: mac date YYMMDDhhmmss\n");
284 return;
285 }
286
287 for (i = 0; i < 6; i++)
288 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
Timur Tabi13c312b2009-08-28 16:56:45 -0500289
290 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500291}
292
293/**
294 * set_mac_address - stores a MAC address into the EEPROM
295 *
296 * This function takes a pointer to MAC address string
297 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
298 * stores it in one of the MAC address fields of the EEPROM local copy.
299 */
300static void set_mac_address(unsigned int index, const char *string)
301{
302 char *p = (char *) string;
303 unsigned int i;
304
Timur Tabi11e98e72010-09-30 15:36:50 -0500305 if ((index >= MAX_NUM_PORTS) || !string) {
Timur Tabibdea8372008-06-19 17:56:11 -0500306 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
307 return;
308 }
309
310 for (i = 0; *p && (i < 6); i++) {
311 e.mac[index][i] = simple_strtoul(p, &p, 16);
312 if (*p == ':')
313 p++;
314 }
Timur Tabi13c312b2009-08-28 16:56:45 -0500315
316 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500317}
318
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200319int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Haiying Wang34c68782006-07-12 10:48:05 -0400320{
Timur Tabibdea8372008-06-19 17:56:11 -0500321 char cmd;
Haiying Wang34c68782006-07-12 10:48:05 -0400322
Timur Tabibdea8372008-06-19 17:56:11 -0500323 if (argc == 1) {
324 show_eeprom();
325 return 0;
326 }
327
328 cmd = argv[1][0];
329
330 if (cmd == 'r') {
331 read_eeprom();
332 return 0;
333 }
Haiying Wang34c68782006-07-12 10:48:05 -0400334
Timur Tabi13c312b2009-08-28 16:56:45 -0500335 if (cmd == 'i') {
336#ifdef CONFIG_SYS_I2C_EEPROM_NXID
337 memcpy(e.id, "NXID", sizeof(e.id));
Timur Tabi11e98e72010-09-30 15:36:50 -0500338 e.version = NXID_VERSION;
Timur Tabi13c312b2009-08-28 16:56:45 -0500339#else
340 memcpy(e.id, "CCID", sizeof(e.id));
341#endif
Timur Tabi2f9f65f2011-02-09 13:40:51 -0600342 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500343 return 0;
344 }
345
346 if (!is_valid) {
347 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
348 return 0;
349 }
350
351 if (argc == 2) {
Haiying Wang34c68782006-07-12 10:48:05 -0400352 switch (cmd) {
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500353 case 's': /* save */
Timur Tabibdea8372008-06-19 17:56:11 -0500354 prog_eeprom();
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500355 break;
Jon Loeliger4fbb09c2006-08-22 12:25:27 -0500356 default:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200357 return cmd_usage(cmdtp);
Haiying Wang34c68782006-07-12 10:48:05 -0400358 }
Timur Tabibdea8372008-06-19 17:56:11 -0500359
360 return 0;
361 }
362
363 /* We know we have at least one parameter */
364
365 switch (cmd) {
366 case 'n': /* serial number */
367 memset(e.sn, 0, sizeof(e.sn));
368 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
Timur Tabi13c312b2009-08-28 16:56:45 -0500369 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500370 break;
371 case 'e': /* errata */
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200372#ifdef CONFIG_SYS_I2C_EEPROM_NXID
Timur Tabibdea8372008-06-19 17:56:11 -0500373 memset(e.errata, 0, 5);
374 strncpy((char *)e.errata, argv[2], 4);
375#else
376 e.errata[0] = argv[2][0];
377 e.errata[1] = argv[2][1];
378#endif
Timur Tabi13c312b2009-08-28 16:56:45 -0500379 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500380 break;
381 case 'd': /* date BCD format YYMMDDhhmmss */
382 set_date(argv[2]);
383 break;
384 case 'p': /* MAC table size */
385 e.mac_count = simple_strtoul(argv[2], NULL, 16);
Timur Tabi13c312b2009-08-28 16:56:45 -0500386 update_crc();
Timur Tabibdea8372008-06-19 17:56:11 -0500387 break;
Timur Tabi11e98e72010-09-30 15:36:50 -0500388 case '0' ... '9': /* "mac 0" through "mac 22" */
389 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
Timur Tabibdea8372008-06-19 17:56:11 -0500390 break;
391 case 'h': /* help */
392 default:
Wolfgang Denk3b683112010-07-17 01:06:04 +0200393 return cmd_usage(cmdtp);
Haiying Wang34c68782006-07-12 10:48:05 -0400394 }
Timur Tabibdea8372008-06-19 17:56:11 -0500395
Haiying Wang34c68782006-07-12 10:48:05 -0400396 return 0;
397}
398
Timur Tabibdea8372008-06-19 17:56:11 -0500399/**
400 * mac_read_from_eeprom - read the MAC addresses from EEPROM
401 *
402 * This function reads the MAC addresses from EEPROM and sets the
403 * appropriate environment variables for each one read.
404 *
405 * The environment variables are only set if they haven't been set already.
406 * This ensures that any user-saved variables are never overwritten.
407 *
408 * This function must be called after relocation.
Timur Tabia2b41cb2011-02-09 02:00:09 +0000409 *
410 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
411 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
412 * located at a different offset.
Timur Tabibdea8372008-06-19 17:56:11 -0500413 */
Haiying Wang34c68782006-07-12 10:48:05 -0400414int mac_read_from_eeprom(void)
415{
Timur Tabibdea8372008-06-19 17:56:11 -0500416 unsigned int i;
Timur Tabia2b41cb2011-02-09 02:00:09 +0000417 u32 crc, crc_offset = offsetof(struct eeprom, crc);
418 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
Timur Tabi13c312b2009-08-28 16:56:45 -0500419
420 puts("EEPROM: ");
Haiying Wang34c68782006-07-12 10:48:05 -0400421
Timur Tabibdea8372008-06-19 17:56:11 -0500422 if (read_eeprom()) {
Haiying Wang34c68782006-07-12 10:48:05 -0400423 printf("Read failed.\n");
424 return -1;
425 }
426
Timur Tabibdea8372008-06-19 17:56:11 -0500427 if (!is_valid) {
Timur Tabi13c312b2009-08-28 16:56:45 -0500428 printf("Invalid ID (%02x %02x %02x %02x)\n",
429 e.id[0], e.id[1], e.id[2], e.id[3]);
Haiying Wang34c68782006-07-12 10:48:05 -0400430 return -1;
Timur Tabibdea8372008-06-19 17:56:11 -0500431 }
432
Timur Tabia2b41cb2011-02-09 02:00:09 +0000433#ifdef CONFIG_SYS_I2C_EEPROM_NXID
434 /*
435 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
436 * to where it is in v0.
437 */
438 if (e.version == 0)
439 crc_offset = 0x72;
440#endif
441
442 crc = crc32(0, (void *)&e, crc_offset);
443 crcp = (void *)&e + crc_offset;
444 if (crc != be32_to_cpu(*crcp)) {
Timur Tabi13c312b2009-08-28 16:56:45 -0500445 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
446 return -1;
Haiying Wange5607542009-06-04 16:12:40 -0400447 }
448
Timur Tabia2b41cb2011-02-09 02:00:09 +0000449#ifdef CONFIG_SYS_I2C_EEPROM_NXID
450 /*
451 * MAC address #9 in v1 occupies the same position as the CRC in v0.
452 * Erase it so that it's not mistaken for a MAC address. We'll
453 * update the CRC later.
454 */
455 if (e.version == 0)
456 memset(e.mac[8], 0xff, 6);
457#endif
458
Timur Tabi13c312b2009-08-28 16:56:45 -0500459 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
Timur Tabibdea8372008-06-19 17:56:11 -0500460 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
461 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
462 char ethaddr[18];
463 char enetvar[9];
464
465 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
466 e.mac[i][0],
467 e.mac[i][1],
468 e.mac[i][2],
469 e.mac[i][3],
470 e.mac[i][4],
471 e.mac[i][5]);
472 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
473 /* Only initialize environment variables that are blank
474 * (i.e. have not yet been set)
475 */
476 if (!getenv(enetvar))
477 setenv(enetvar, ethaddr);
478 }
479 }
480
Timur Tabi13c312b2009-08-28 16:56:45 -0500481#ifdef CONFIG_SYS_I2C_EEPROM_NXID
482 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
483 be32_to_cpu(e.version));
484#else
485 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
486#endif
487
Timur Tabia2b41cb2011-02-09 02:00:09 +0000488#ifdef CONFIG_SYS_I2C_EEPROM_NXID
489 /*
490 * Now we need to upconvert the data into v1 format. We do this last so
491 * that at boot time, U-Boot will still say "NXID v0".
492 */
493 if (e.version == 0) {
494 e.version = NXID_VERSION;
495 update_crc();
496 }
497#endif
498
Haiying Wang34c68782006-07-12 10:48:05 -0400499 return 0;
500}
Timur Tabibdea8372008-06-19 17:56:11 -0500501
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200502#ifdef CONFIG_SYS_I2C_EEPROM_CCID
Timur Tabibdea8372008-06-19 17:56:11 -0500503
504/**
505 * get_cpu_board_revision - get the CPU board revision on 85xx boards
506 *
507 * Read the EEPROM to determine the board revision.
508 *
509 * This function is called before relocation, so we need to read a private
510 * copy of the EEPROM into a local variable on the stack.
511 *
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200512 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
513 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
Timur Tabibdea8372008-06-19 17:56:11 -0500514 * so that the SPD code will work. This means that all pre-relocation I2C
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200515 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
516 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
Timur Tabibdea8372008-06-19 17:56:11 -0500517 * this function is called. Oh well.
518 */
519unsigned int get_cpu_board_revision(void)
520{
521 struct board_eeprom {
522 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
523 u8 major; /* 0x04 Board revision, major */
524 u8 minor; /* 0x05 Board revision, minor */
525 } be;
526
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200527 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
Timur Tabibdea8372008-06-19 17:56:11 -0500528 (void *)&be, sizeof(be));
529
530 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
531 return MPC85XX_CPU_BOARD_REV(0, 0);
532
533 if ((be.major == 0xff) && (be.minor == 0xff))
534 return MPC85XX_CPU_BOARD_REV(0, 0);
535
Rafal Czubak5c50d142008-10-08 13:41:30 +0200536 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);
Timur Tabibdea8372008-06-19 17:56:11 -0500537}
Timur Tabibdea8372008-06-19 17:56:11 -0500538#endif