2 * Copyright 2006, 2008-2009 Freescale Semiconductor
3 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
5 * Timur Tabi (timur@freescale.com)
7 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 #include <linux/ctype.h>
31 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
32 #include "../common/eeprom.h"
33 #define MAX_NUM_PORTS 8
36 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
37 #define MAX_NUM_PORTS 8
38 #define NXID_VERSION 0
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
48 * static eeprom: EEPROM layout for CCID or NXID formats
50 * See application note AN3638 for details.
52 static struct __attribute__ ((__packed__)) eeprom {
53 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
54 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 */
63 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
64 u32 crc; /* 0x72 CRC32 checksum */
66 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
67 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 */
79 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */
80 u32 crc; /* x+1 CRC32 checksum */
84 /* Set to 1 if we've read EEPROM into memory */
85 static int has_been_read = 0;
87 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
88 /* Is this a valid NXID EEPROM? */
89 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
90 (e.id[2] == 'I') || (e.id[3] == 'D'))
93 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
94 /* Is this a valid CCID EEPROM? */
95 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
96 (e.id[2] == 'I') || (e.id[3] == 'D'))
100 * show_eeprom - display the contents of the EEPROM
102 static void show_eeprom(void)
107 /* EEPROM tag ID, either CCID or NXID */
108 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
109 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));
112 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
116 printf("SN: %s\n", e.sn);
119 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
120 printf("Errata: %s\n", e.errata);
122 printf("Errata: %c%c\n",
123 e.errata[0] ? e.errata[0] : '.',
124 e.errata[1] ? e.errata[1] : '.');
127 /* 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" : "");
133 /* Show MAC addresses */
134 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
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]);
142 crc = crc32(0, (void *)&e, sizeof(e) - 4);
144 if (crc == be32_to_cpu(e.crc))
145 printf("CRC: %08x\n", be32_to_cpu(e.crc));
147 printf("CRC: %08x (should be %08x)\n",
148 be32_to_cpu(e.crc), crc);
151 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
152 for (i = 0; i < sizeof(e); i++) {
155 printf("%02X ", ((u8 *)&e)[i]);
156 if (((i % 16) == 15) || (i == sizeof(e) - 1))
163 * read_eeprom - read the EEPROM into memory
165 static int read_eeprom(void)
168 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
175 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
176 bus = i2c_get_bus_num();
177 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
180 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
181 (void *)&e, sizeof(e));
183 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
184 i2c_set_bus_num(bus);
191 has_been_read = (ret == 0) ? 1 : 0;
197 * update_crc - update the CRC
199 * This function should be called after each update to the EEPROM structure,
200 * to make sure the CRC is always correct.
202 static void update_crc(void)
206 crc = crc32(0, (void *)&e, sizeof(e) - 4);
207 e.crc = cpu_to_be32(crc);
211 * prog_eeprom - write the EEPROM from memory
213 static int prog_eeprom(void)
218 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
222 /* Set the reserved values to 0xFF */
223 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
225 memset(e.res_1, 0xFF, sizeof(e.res_1));
227 memset(e.res_0, 0xFF, sizeof(e.res_0));
231 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
232 bus = i2c_get_bus_num();
233 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
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.
241 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
242 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
243 p, min((sizeof(e) - i), 8));
246 udelay(5000); /* 5ms write cycle timing */
250 /* Verify the write by reading back the EEPROM and comparing */
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)))
259 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
260 i2c_set_bus_num(bus);
264 printf("Programming failed.\n");
269 printf("Programming passed.\n");
274 * h2i - converts hex character into a number
276 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
277 * the integer equivalent.
279 static inline u8 h2i(char p)
281 if ((p >= '0') && (p <= '9'))
284 if ((p >= 'A') && (p <= 'F'))
285 return (p - 'A') + 10;
287 if ((p >= 'a') && (p <= 'f'))
288 return (p - 'a') + 10;
294 * set_date - stores the build date into the EEPROM
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.
300 static void set_date(const char *string)
304 if (strlen(string) != 12) {
305 printf("Usage: mac date YYMMDDhhmmss\n");
309 for (i = 0; i < 6; i++)
310 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
316 * set_mac_address - stores a MAC address into the EEPROM
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.
322 static void set_mac_address(unsigned int index, const char *string)
324 char *p = (char *) string;
327 if ((index >= MAX_NUM_PORTS) || !string) {
328 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
332 for (i = 0; *p && (i < 6); i++) {
333 e.mac[index][i] = simple_strtoul(p, &p, 16);
341 int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
358 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
359 memcpy(e.id, "NXID", sizeof(e.id));
360 e.version = NXID_VERSION;
362 memcpy(e.id, "CCID", sizeof(e.id));
369 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
379 return cmd_usage(cmdtp);
385 /* We know we have at least one parameter */
388 case 'n': /* serial number */
389 memset(e.sn, 0, sizeof(e.sn));
390 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
393 case 'e': /* errata */
394 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
395 memset(e.errata, 0, 5);
396 strncpy((char *)e.errata, argv[2], 4);
398 e.errata[0] = argv[2][0];
399 e.errata[1] = argv[2][1];
403 case 'd': /* date BCD format YYMMDDhhmmss */
406 case 'p': /* MAC table size */
407 e.mac_count = simple_strtoul(argv[2], NULL, 16);
410 case '0' ... '9': /* "mac 0" through "mac 22" */
411 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
415 return cmd_usage(cmdtp);
422 * mac_read_from_eeprom - read the MAC addresses from EEPROM
424 * This function reads the MAC addresses from EEPROM and sets the
425 * appropriate environment variables for each one read.
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.
430 * This function must be called after relocation.
432 int mac_read_from_eeprom(void)
440 printf("Read failed.\n");
445 printf("Invalid ID (%02x %02x %02x %02x)\n",
446 e.id[0], e.id[1], e.id[2], e.id[3]);
450 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));
456 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
457 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
458 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
462 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
469 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
470 /* Only initialize environment variables that are blank
471 * (i.e. have not yet been set)
473 if (!getenv(enetvar))
474 setenv(enetvar, ethaddr);
478 #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));
482 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
488 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
491 * get_cpu_board_revision - get the CPU board revision on 85xx boards
493 * Read the EEPROM to determine the board revision.
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.
498 * 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,
500 * so that the SPD code will work. This means that all pre-relocation I2C
501 * 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
503 * this function is called. Oh well.
505 unsigned int get_cpu_board_revision(void)
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 */
513 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
514 (void *)&be, sizeof(be));
516 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
517 return MPC85XX_CPU_BOARD_REV(0, 0);
519 if ((be.major == 0xff) && (be.minor == 0xff))
520 return MPC85XX_CPU_BOARD_REV(0, 0);
522 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);