1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright 2006, 2008-2009, 2011 Freescale Semiconductor
4 * York Sun (yorksun@freescale.com)
5 * Haiying Wang (haiying.wang@freescale.com)
6 * Timur Tabi (timur@freescale.com)
13 #include <linux/ctype.h>
14 #include <u-boot/crc.h>
16 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
17 #include "../common/eeprom.h"
18 #define MAX_NUM_PORTS 8
21 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
22 /* some boards with non-256-bytes EEPROM have special define */
23 /* for MAX_NUM_PORTS in board-specific file */
25 #define MAX_NUM_PORTS 16
27 #define NXID_VERSION 1
31 * static eeprom: EEPROM layout for CCID or NXID formats
33 * See application note AN3638 for details.
35 static struct __attribute__ ((__packed__)) eeprom {
36 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
37 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
38 u8 major; /* 0x04 Board revision, major */
39 u8 minor; /* 0x05 Board revision, minor */
40 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
41 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
42 u8 date[6]; /* 0x12 - 0x17 Build Date */
43 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
44 u8 mac_count; /* 0x40 Number of MAC addresses */
45 u8 mac_flag; /* 0x41 MAC table flags */
46 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
47 u32 crc; /* 0x72 CRC32 checksum */
49 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
50 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
51 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
52 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
53 u8 date[6]; /* 0x15 - 0x1a Build Date */
54 u8 res_0; /* 0x1b Reserved */
55 u32 version; /* 0x1c - 0x1f NXID Version */
56 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
57 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
58 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
59 u8 res_1[21]; /* 0x2b - 0x3f Reserved */
60 u8 mac_count; /* 0x40 Number of MAC addresses */
61 u8 mac_flag; /* 0x41 MAC table flags */
62 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0xa1 MAC addresses */
63 u8 res_2[90]; /* 0xa2 - 0xfb Reserved */
64 u32 crc; /* 0xfc - 0xff CRC32 checksum */
68 /* Set to 1 if we've read EEPROM into memory */
69 static int has_been_read = 0;
71 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
72 /* Is this a valid NXID EEPROM? */
73 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
74 (e.id[2] == 'I') || (e.id[3] == 'D'))
77 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
78 /* Is this a valid CCID EEPROM? */
79 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
80 (e.id[2] == 'I') || (e.id[3] == 'D'))
84 * show_eeprom - display the contents of the EEPROM
86 static void show_eeprom(void)
91 /* EEPROM tag ID, either CCID or NXID */
92 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
93 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
94 be32_to_cpu(e.version));
96 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
100 printf("SN: %s\n", e.sn);
103 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
104 printf("Errata: %s\n", e.errata);
106 printf("Errata: %c%c\n",
107 e.errata[0] ? e.errata[0] : '.',
108 e.errata[1] ? e.errata[1] : '.');
111 /* Build date, BCD date values, as YYMMDDhhmmss */
112 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
113 e.date[0], e.date[1], e.date[2],
114 e.date[3] & 0x7F, e.date[4], e.date[5],
115 e.date[3] & 0x80 ? "PM" : "");
117 /* Show MAC addresses */
118 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
122 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
123 p[0], p[1], p[2], p[3], p[4], p[5]);
126 crc = crc32(0, (void *)&e, sizeof(e) - 4);
128 if (crc == be32_to_cpu(e.crc))
129 printf("CRC: %08x\n", be32_to_cpu(e.crc));
131 printf("CRC: %08x (should be %08x)\n",
132 be32_to_cpu(e.crc), crc);
135 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
136 for (i = 0; i < sizeof(e); i++) {
139 printf("%02X ", ((u8 *)&e)[i]);
140 if (((i % 16) == 15) || (i == sizeof(e) - 1))
147 * read_eeprom - read the EEPROM into memory
149 static int read_eeprom(void)
152 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
153 #ifndef CONFIG_DM_I2C
161 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
162 #ifndef CONFIG_DM_I2C
163 bus = i2c_get_bus_num();
164 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
168 #ifndef CONFIG_DM_I2C
169 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
170 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
171 (void *)&e, sizeof(e));
174 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
175 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
176 CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
178 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
181 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
184 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
185 #ifndef CONFIG_DM_I2C
186 i2c_set_bus_num(bus);
194 has_been_read = (ret == 0) ? 1 : 0;
200 * update_crc - update the CRC
202 * This function should be called after each update to the EEPROM structure,
203 * to make sure the CRC is always correct.
205 static void update_crc(void)
209 crc = crc32(0, (void *)&e, sizeof(e) - 4);
210 e.crc = cpu_to_be32(crc);
214 * prog_eeprom - write the EEPROM from memory
216 static int prog_eeprom(void)
221 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
222 #ifndef CONFIG_DM_I2C
227 /* Set the reserved values to 0xFF */
228 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
230 memset(e.res_1, 0xFF, sizeof(e.res_1));
232 memset(e.res_0, 0xFF, sizeof(e.res_0));
236 #ifndef CONFIG_DM_I2C
237 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
238 bus = i2c_get_bus_num();
239 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
244 * The AT24C02 datasheet says that data can only be written in page
245 * mode, which means 8 bytes at a time, and it takes up to 5ms to
246 * complete a given write.
248 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
249 #ifndef CONFIG_DM_I2C
250 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
251 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
252 p, min((int)(sizeof(e) - i), 8));
255 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
256 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
257 CONFIG_SYS_I2C_EEPROM_ADDR,
258 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
261 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
262 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
266 ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
271 udelay(5000); /* 5ms write cycle timing */
275 /* Verify the write by reading back the EEPROM and comparing */
278 #ifndef CONFIG_DM_I2C
279 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
280 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
281 (void *)&e2, sizeof(e2));
284 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
285 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
286 CONFIG_SYS_I2C_EEPROM_ADDR,
287 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
290 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
291 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
295 ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
297 if (!ret && memcmp(&e, &e2, sizeof(e)))
301 #ifndef CONFIG_DM_I2C
302 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
303 i2c_set_bus_num(bus);
308 printf("Programming failed.\n");
313 printf("Programming passed.\n");
318 * h2i - converts hex character into a number
320 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
321 * the integer equivalent.
323 static inline u8 h2i(char p)
325 if ((p >= '0') && (p <= '9'))
328 if ((p >= 'A') && (p <= 'F'))
329 return (p - 'A') + 10;
331 if ((p >= 'a') && (p <= 'f'))
332 return (p - 'a') + 10;
338 * set_date - stores the build date into the EEPROM
340 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
341 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
342 * and stores it in the build date field of the EEPROM local copy.
344 static void set_date(const char *string)
348 if (strlen(string) != 12) {
349 printf("Usage: mac date YYMMDDhhmmss\n");
353 for (i = 0; i < 6; i++)
354 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
360 * set_mac_address - stores a MAC address into the EEPROM
362 * This function takes a pointer to MAC address string
363 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
364 * stores it in one of the MAC address fields of the EEPROM local copy.
366 static void set_mac_address(unsigned int index, const char *string)
368 char *p = (char *) string;
371 if ((index >= MAX_NUM_PORTS) || !string) {
372 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
376 for (i = 0; *p && (i < 6); i++) {
377 e.mac[index][i] = simple_strtoul(p, &p, 16);
385 int do_mac(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
402 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
403 memcpy(e.id, "NXID", sizeof(e.id));
404 e.version = cpu_to_be32(NXID_VERSION);
406 memcpy(e.id, "CCID", sizeof(e.id));
413 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
423 return cmd_usage(cmdtp);
429 /* We know we have at least one parameter */
432 case 'n': /* serial number */
433 memset(e.sn, 0, sizeof(e.sn));
434 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
437 case 'e': /* errata */
438 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
439 memset(e.errata, 0, 5);
440 strncpy((char *)e.errata, argv[2], 4);
442 e.errata[0] = argv[2][0];
443 e.errata[1] = argv[2][1];
447 case 'd': /* date BCD format YYMMDDhhmmss */
450 case 'p': /* MAC table size */
451 e.mac_count = simple_strtoul(argv[2], NULL, 16);
454 case '0' ... '9': /* "mac 0" through "mac 22" */
455 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
459 return cmd_usage(cmdtp);
466 * mac_read_from_eeprom - read the MAC addresses from EEPROM
468 * This function reads the MAC addresses from EEPROM and sets the
469 * appropriate environment variables for each one read.
471 * The environment variables are only set if they haven't been set already.
472 * This ensures that any user-saved variables are never overwritten.
474 * This function must be called after relocation.
476 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
477 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
478 * located at a different offset.
480 int mac_read_from_eeprom(void)
483 u32 crc, crc_offset = offsetof(struct eeprom, crc);
484 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
489 printf("Read failed.\n");
494 printf("Invalid ID (%02x %02x %02x %02x)\n",
495 e.id[0], e.id[1], e.id[2], e.id[3]);
499 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
501 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
502 * to where it is in v0.
508 crc = crc32(0, (void *)&e, crc_offset);
509 crcp = (void *)&e + crc_offset;
510 if (crc != be32_to_cpu(*crcp)) {
511 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
515 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
517 * MAC address #9 in v1 occupies the same position as the CRC in v0.
518 * Erase it so that it's not mistaken for a MAC address. We'll
519 * update the CRC later.
522 memset(e.mac[8], 0xff, 6);
525 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
526 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
527 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
531 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
538 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
539 /* Only initialize environment variables that are blank
540 * (i.e. have not yet been set)
542 if (!env_get(enetvar))
543 env_set(enetvar, ethaddr);
547 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
548 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
549 be32_to_cpu(e.version));
551 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
554 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
556 * Now we need to upconvert the data into v1 format. We do this last so
557 * that at boot time, U-Boot will still say "NXID v0".
559 if (e.version == 0) {
560 e.version = cpu_to_be32(NXID_VERSION);
568 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
571 * get_cpu_board_revision - get the CPU board revision on 85xx boards
573 * Read the EEPROM to determine the board revision.
575 * This function is called before relocation, so we need to read a private
576 * copy of the EEPROM into a local variable on the stack.
578 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
579 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
580 * so that the SPD code will work. This means that all pre-relocation I2C
581 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
582 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
583 * this function is called. Oh well.
585 unsigned int get_cpu_board_revision(void)
587 struct board_eeprom {
588 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
589 u8 major; /* 0x04 Board revision, major */
590 u8 minor; /* 0x05 Board revision, minor */
593 #ifndef CONFIG_DM_I2C
594 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
595 (void *)&be, sizeof(be));
599 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
600 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
601 CONFIG_SYS_I2C_EEPROM_ADDR,
602 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
605 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
606 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
610 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
613 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
614 return MPC85XX_CPU_BOARD_REV(0, 0);
616 if ((be.major == 0xff) && (be.minor == 0xff))
617 return MPC85XX_CPU_BOARD_REV(0, 0);
619 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);