2 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
3 * York Sun (yorksun@freescale.com)
4 * Haiying Wang (haiying.wang@freescale.com)
5 * Timur Tabi (timur@freescale.com)
7 * SPDX-License-Identifier: GPL-2.0+
13 #include <linux/ctype.h>
15 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
16 #include "../common/eeprom.h"
17 #define MAX_NUM_PORTS 8
20 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
21 #define MAX_NUM_PORTS 23
22 #define NXID_VERSION 1
26 * static eeprom: EEPROM layout for CCID or NXID formats
28 * See application note AN3638 for details.
30 static struct __attribute__ ((__packed__)) eeprom {
31 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
32 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 */
41 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
42 u32 crc; /* 0x72 CRC32 checksum */
44 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
45 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 */
57 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - x MAC addresses */
58 u32 crc; /* x+1 CRC32 checksum */
62 /* Set to 1 if we've read EEPROM into memory */
63 static int has_been_read = 0;
65 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
66 /* Is this a valid NXID EEPROM? */
67 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
68 (e.id[2] == 'I') || (e.id[3] == 'D'))
71 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
72 /* Is this a valid CCID EEPROM? */
73 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
74 (e.id[2] == 'I') || (e.id[3] == 'D'))
78 * show_eeprom - display the contents of the EEPROM
80 static void show_eeprom(void)
85 /* EEPROM tag ID, either CCID or NXID */
86 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
87 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));
90 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
94 printf("SN: %s\n", e.sn);
97 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
98 printf("Errata: %s\n", e.errata);
100 printf("Errata: %c%c\n",
101 e.errata[0] ? e.errata[0] : '.',
102 e.errata[1] ? e.errata[1] : '.');
105 /* 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" : "");
111 /* Show MAC addresses */
112 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
116 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
117 p[0], p[1], p[2], p[3], p[4], p[5]);
120 crc = crc32(0, (void *)&e, sizeof(e) - 4);
122 if (crc == be32_to_cpu(e.crc))
123 printf("CRC: %08x\n", be32_to_cpu(e.crc));
125 printf("CRC: %08x (should be %08x)\n",
126 be32_to_cpu(e.crc), crc);
129 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
130 for (i = 0; i < sizeof(e); i++) {
133 printf("%02X ", ((u8 *)&e)[i]);
134 if (((i % 16) == 15) || (i == sizeof(e) - 1))
141 * read_eeprom - read the EEPROM into memory
143 static int read_eeprom(void)
146 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
153 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
154 bus = i2c_get_bus_num();
155 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
158 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
159 (void *)&e, sizeof(e));
161 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
162 i2c_set_bus_num(bus);
169 has_been_read = (ret == 0) ? 1 : 0;
175 * update_crc - update the CRC
177 * This function should be called after each update to the EEPROM structure,
178 * to make sure the CRC is always correct.
180 static void update_crc(void)
184 crc = crc32(0, (void *)&e, sizeof(e) - 4);
185 e.crc = cpu_to_be32(crc);
189 * prog_eeprom - write the EEPROM from memory
191 static int prog_eeprom(void)
196 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
200 /* Set the reserved values to 0xFF */
201 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
203 memset(e.res_1, 0xFF, sizeof(e.res_1));
205 memset(e.res_0, 0xFF, sizeof(e.res_0));
209 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
210 bus = i2c_get_bus_num();
211 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
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.
219 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
220 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
221 p, min((sizeof(e) - i), 8));
224 udelay(5000); /* 5ms write cycle timing */
228 /* Verify the write by reading back the EEPROM and comparing */
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)))
237 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
238 i2c_set_bus_num(bus);
242 printf("Programming failed.\n");
247 printf("Programming passed.\n");
252 * h2i - converts hex character into a number
254 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
255 * the integer equivalent.
257 static inline u8 h2i(char p)
259 if ((p >= '0') && (p <= '9'))
262 if ((p >= 'A') && (p <= 'F'))
263 return (p - 'A') + 10;
265 if ((p >= 'a') && (p <= 'f'))
266 return (p - 'a') + 10;
272 * set_date - stores the build date into the EEPROM
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.
278 static void set_date(const char *string)
282 if (strlen(string) != 12) {
283 printf("Usage: mac date YYMMDDhhmmss\n");
287 for (i = 0; i < 6; i++)
288 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
294 * set_mac_address - stores a MAC address into the EEPROM
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.
300 static void set_mac_address(unsigned int index, const char *string)
302 char *p = (char *) string;
305 if ((index >= MAX_NUM_PORTS) || !string) {
306 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
310 for (i = 0; *p && (i < 6); i++) {
311 e.mac[index][i] = simple_strtoul(p, &p, 16);
319 int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
336 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
337 memcpy(e.id, "NXID", sizeof(e.id));
338 e.version = NXID_VERSION;
340 memcpy(e.id, "CCID", sizeof(e.id));
347 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
357 return cmd_usage(cmdtp);
363 /* We know we have at least one parameter */
366 case 'n': /* serial number */
367 memset(e.sn, 0, sizeof(e.sn));
368 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
371 case 'e': /* errata */
372 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
373 memset(e.errata, 0, 5);
374 strncpy((char *)e.errata, argv[2], 4);
376 e.errata[0] = argv[2][0];
377 e.errata[1] = argv[2][1];
381 case 'd': /* date BCD format YYMMDDhhmmss */
384 case 'p': /* MAC table size */
385 e.mac_count = simple_strtoul(argv[2], NULL, 16);
388 case '0' ... '9': /* "mac 0" through "mac 22" */
389 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
393 return cmd_usage(cmdtp);
400 * mac_read_from_eeprom - read the MAC addresses from EEPROM
402 * This function reads the MAC addresses from EEPROM and sets the
403 * appropriate environment variables for each one read.
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.
408 * This function must be called after relocation.
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.
414 int mac_read_from_eeprom(void)
417 u32 crc, crc_offset = offsetof(struct eeprom, crc);
418 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
423 printf("Read failed.\n");
428 printf("Invalid ID (%02x %02x %02x %02x)\n",
429 e.id[0], e.id[1], e.id[2], e.id[3]);
433 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
435 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
436 * to where it is in v0.
442 crc = crc32(0, (void *)&e, crc_offset);
443 crcp = (void *)&e + crc_offset;
444 if (crc != be32_to_cpu(*crcp)) {
445 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
449 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
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.
456 memset(e.mac[8], 0xff, 6);
459 for (i = 0; i < min(e.mac_count, MAX_NUM_PORTS); i++) {
460 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
461 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
465 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
472 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
473 /* Only initialize environment variables that are blank
474 * (i.e. have not yet been set)
476 if (!getenv(enetvar))
477 setenv(enetvar, ethaddr);
481 #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));
485 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
488 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
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".
493 if (e.version == 0) {
494 e.version = NXID_VERSION;
502 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
505 * get_cpu_board_revision - get the CPU board revision on 85xx boards
507 * Read the EEPROM to determine the board revision.
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.
512 * 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,
514 * so that the SPD code will work. This means that all pre-relocation I2C
515 * 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
517 * this function is called. Oh well.
519 unsigned int get_cpu_board_revision(void)
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 */
527 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
528 (void *)&be, sizeof(be));
530 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
531 return MPC85XX_CPU_BOARD_REV(0, 0);
533 if ((be.major == 0xff) && (be.minor == 0xff))
534 return MPC85XX_CPU_BOARD_REV(0, 0);
536 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);