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)
14 #include <linux/ctype.h>
15 #include <u-boot/crc.h>
17 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
18 #include "../common/eeprom.h"
19 #define MAX_NUM_PORTS 8
22 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
23 /* some boards with non-256-bytes EEPROM have special define */
24 /* for MAX_NUM_PORTS in board-specific file */
26 #define MAX_NUM_PORTS 16
28 #define NXID_VERSION 1
32 * static eeprom: EEPROM layout for CCID or NXID formats
34 * See application note AN3638 for details.
36 static struct __attribute__ ((__packed__)) eeprom {
37 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
38 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
39 u8 major; /* 0x04 Board revision, major */
40 u8 minor; /* 0x05 Board revision, minor */
41 u8 sn[10]; /* 0x06 - 0x0F Serial Number*/
42 u8 errata[2]; /* 0x10 - 0x11 Errata Level */
43 u8 date[6]; /* 0x12 - 0x17 Build Date */
44 u8 res_0[40]; /* 0x18 - 0x3f Reserved */
45 u8 mac_count; /* 0x40 Number of MAC addresses */
46 u8 mac_flag; /* 0x41 MAC table flags */
47 u8 mac[MAX_NUM_PORTS][6]; /* 0x42 - 0x71 MAC addresses */
48 u32 crc; /* 0x72 CRC32 checksum */
50 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
51 u8 id[4]; /* 0x00 - 0x03 EEPROM Tag 'NXID' */
52 u8 sn[12]; /* 0x04 - 0x0F Serial Number */
53 u8 errata[5]; /* 0x10 - 0x14 Errata Level */
54 u8 date[6]; /* 0x15 - 0x1a Build Date */
55 u8 res_0; /* 0x1b Reserved */
56 u32 version; /* 0x1c - 0x1f NXID Version */
57 u8 tempcal[8]; /* 0x20 - 0x27 Temperature Calibration Factors */
58 u8 tempcalsys[2]; /* 0x28 - 0x29 System Temperature Calibration Factors */
59 u8 tempcalflags; /* 0x2a Temperature Calibration Flags */
60 u8 res_1[21]; /* 0x2b - 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 - 0xa1 MAC addresses */
64 u8 res_2[90]; /* 0xa2 - 0xfb Reserved */
65 u32 crc; /* 0xfc - 0xff CRC32 checksum */
69 /* Set to 1 if we've read EEPROM into memory */
70 static int has_been_read = 0;
72 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
73 /* Is this a valid NXID EEPROM? */
74 #define is_valid ((e.id[0] == 'N') || (e.id[1] == 'X') || \
75 (e.id[2] == 'I') || (e.id[3] == 'D'))
78 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
79 /* Is this a valid CCID EEPROM? */
80 #define is_valid ((e.id[0] == 'C') || (e.id[1] == 'C') || \
81 (e.id[2] == 'I') || (e.id[3] == 'D'))
85 * show_eeprom - display the contents of the EEPROM
87 static void show_eeprom(void)
92 /* EEPROM tag ID, either CCID or NXID */
93 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
94 printf("ID: %c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
95 be32_to_cpu(e.version));
97 printf("ID: %c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
101 printf("SN: %s\n", e.sn);
104 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
105 printf("Errata: %s\n", e.errata);
107 printf("Errata: %c%c\n",
108 e.errata[0] ? e.errata[0] : '.',
109 e.errata[1] ? e.errata[1] : '.');
112 /* Build date, BCD date values, as YYMMDDhhmmss */
113 printf("Build date: 20%02x/%02x/%02x %02x:%02x:%02x %s\n",
114 e.date[0], e.date[1], e.date[2],
115 e.date[3] & 0x7F, e.date[4], e.date[5],
116 e.date[3] & 0x80 ? "PM" : "");
118 /* Show MAC addresses */
119 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
123 printf("Eth%u: %02x:%02x:%02x:%02x:%02x:%02x\n", i,
124 p[0], p[1], p[2], p[3], p[4], p[5]);
127 crc = crc32(0, (void *)&e, sizeof(e) - 4);
129 if (crc == be32_to_cpu(e.crc))
130 printf("CRC: %08x\n", be32_to_cpu(e.crc));
132 printf("CRC: %08x (should be %08x)\n",
133 be32_to_cpu(e.crc), crc);
136 printf("EEPROM dump: (0x%x bytes)\n", sizeof(e));
137 for (i = 0; i < sizeof(e); i++) {
140 printf("%02X ", ((u8 *)&e)[i]);
141 if (((i % 16) == 15) || (i == sizeof(e) - 1))
148 * read_eeprom - read the EEPROM into memory
150 static int read_eeprom(void)
153 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
154 #ifndef CONFIG_DM_I2C
162 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
163 #ifndef CONFIG_DM_I2C
164 bus = i2c_get_bus_num();
165 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
169 #ifndef CONFIG_DM_I2C
170 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
171 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
172 (void *)&e, sizeof(e));
175 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
176 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
177 CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
179 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR, 1, &dev);
182 ret = dm_i2c_read(dev, 0, (void *)&e, sizeof(e));
185 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
186 #ifndef CONFIG_DM_I2C
187 i2c_set_bus_num(bus);
195 has_been_read = (ret == 0) ? 1 : 0;
201 * update_crc - update the CRC
203 * This function should be called after each update to the EEPROM structure,
204 * to make sure the CRC is always correct.
206 static void update_crc(void)
210 crc = crc32(0, (void *)&e, sizeof(e) - 4);
211 e.crc = cpu_to_be32(crc);
215 * prog_eeprom - write the EEPROM from memory
217 static int prog_eeprom(void)
222 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
223 #ifndef CONFIG_DM_I2C
228 /* Set the reserved values to 0xFF */
229 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
231 memset(e.res_1, 0xFF, sizeof(e.res_1));
233 memset(e.res_0, 0xFF, sizeof(e.res_0));
237 #ifndef CONFIG_DM_I2C
238 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
239 bus = i2c_get_bus_num();
240 i2c_set_bus_num(CONFIG_SYS_EEPROM_BUS_NUM);
245 * The AT24C02 datasheet says that data can only be written in page
246 * mode, which means 8 bytes at a time, and it takes up to 5ms to
247 * complete a given write.
249 for (i = 0, p = &e; i < sizeof(e); i += 8, p += 8) {
250 #ifndef CONFIG_DM_I2C
251 ret = i2c_write(CONFIG_SYS_I2C_EEPROM_ADDR, i,
252 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
253 p, min((int)(sizeof(e) - i), 8));
256 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
257 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
258 CONFIG_SYS_I2C_EEPROM_ADDR,
259 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
262 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
263 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
267 ret = dm_i2c_write(dev, i, p, min((int)(sizeof(e) - i),
272 udelay(5000); /* 5ms write cycle timing */
276 /* Verify the write by reading back the EEPROM and comparing */
279 #ifndef CONFIG_DM_I2C
280 ret = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0,
281 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
282 (void *)&e2, sizeof(e2));
285 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
286 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
287 CONFIG_SYS_I2C_EEPROM_ADDR,
288 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
291 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
292 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
296 ret = dm_i2c_read(dev, 0, (void *)&e2, sizeof(e2));
298 if (!ret && memcmp(&e, &e2, sizeof(e)))
302 #ifndef CONFIG_DM_I2C
303 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
304 i2c_set_bus_num(bus);
309 printf("Programming failed.\n");
314 printf("Programming passed.\n");
319 * h2i - converts hex character into a number
321 * This function takes a hexadecimal character (e.g. '7' or 'C') and returns
322 * the integer equivalent.
324 static inline u8 h2i(char p)
326 if ((p >= '0') && (p <= '9'))
329 if ((p >= 'A') && (p <= 'F'))
330 return (p - 'A') + 10;
332 if ((p >= 'a') && (p <= 'f'))
333 return (p - 'a') + 10;
339 * set_date - stores the build date into the EEPROM
341 * This function takes a pointer to a string in the format "YYMMDDhhmmss"
342 * (2-digit year, 2-digit month, etc), converts it to a 6-byte BCD string,
343 * and stores it in the build date field of the EEPROM local copy.
345 static void set_date(const char *string)
349 if (strlen(string) != 12) {
350 printf("Usage: mac date YYMMDDhhmmss\n");
354 for (i = 0; i < 6; i++)
355 e.date[i] = h2i(string[2 * i]) << 4 | h2i(string[2 * i + 1]);
361 * set_mac_address - stores a MAC address into the EEPROM
363 * This function takes a pointer to MAC address string
364 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number) and
365 * stores it in one of the MAC address fields of the EEPROM local copy.
367 static void set_mac_address(unsigned int index, const char *string)
369 char *p = (char *) string;
372 if ((index >= MAX_NUM_PORTS) || !string) {
373 printf("Usage: mac <n> XX:XX:XX:XX:XX:XX\n");
377 for (i = 0; *p && (i < 6); i++) {
378 e.mac[index][i] = simple_strtoul(p, &p, 16);
386 int do_mac(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
403 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
404 memcpy(e.id, "NXID", sizeof(e.id));
405 e.version = cpu_to_be32(NXID_VERSION);
407 memcpy(e.id, "CCID", sizeof(e.id));
414 printf("Please read the EEPROM ('r') and/or set the ID ('i') first.\n");
424 return cmd_usage(cmdtp);
430 /* We know we have at least one parameter */
433 case 'n': /* serial number */
434 memset(e.sn, 0, sizeof(e.sn));
435 strncpy((char *)e.sn, argv[2], sizeof(e.sn) - 1);
438 case 'e': /* errata */
439 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
440 memset(e.errata, 0, 5);
441 strncpy((char *)e.errata, argv[2], 4);
443 e.errata[0] = argv[2][0];
444 e.errata[1] = argv[2][1];
448 case 'd': /* date BCD format YYMMDDhhmmss */
451 case 'p': /* MAC table size */
452 e.mac_count = simple_strtoul(argv[2], NULL, 16);
455 case '0' ... '9': /* "mac 0" through "mac 22" */
456 set_mac_address(simple_strtoul(argv[1], NULL, 10), argv[2]);
460 return cmd_usage(cmdtp);
467 * mac_read_from_eeprom - read the MAC addresses from EEPROM
469 * This function reads the MAC addresses from EEPROM and sets the
470 * appropriate environment variables for each one read.
472 * The environment variables are only set if they haven't been set already.
473 * This ensures that any user-saved variables are never overwritten.
475 * This function must be called after relocation.
477 * For NXID v1 EEPROMs, we support loading and up-converting the older NXID v0
478 * format. In a v0 EEPROM, there are only eight MAC addresses and the CRC is
479 * located at a different offset.
481 int mac_read_from_eeprom(void)
484 u32 crc, crc_offset = offsetof(struct eeprom, crc);
485 u32 *crcp; /* Pointer to the CRC in the data read from the EEPROM */
490 printf("Read failed.\n");
495 printf("Invalid ID (%02x %02x %02x %02x)\n",
496 e.id[0], e.id[1], e.id[2], e.id[3]);
500 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
502 * If we've read an NXID v0 EEPROM, then we need to set the CRC offset
503 * to where it is in v0.
509 crc = crc32(0, (void *)&e, crc_offset);
510 crcp = (void *)&e + crc_offset;
511 if (crc != be32_to_cpu(*crcp)) {
512 printf("CRC mismatch (%08x != %08x)\n", crc, be32_to_cpu(e.crc));
516 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
518 * MAC address #9 in v1 occupies the same position as the CRC in v0.
519 * Erase it so that it's not mistaken for a MAC address. We'll
520 * update the CRC later.
523 memset(e.mac[8], 0xff, 6);
526 for (i = 0; i < min(e.mac_count, (u8)MAX_NUM_PORTS); i++) {
527 if (memcmp(&e.mac[i], "\0\0\0\0\0\0", 6) &&
528 memcmp(&e.mac[i], "\xFF\xFF\xFF\xFF\xFF\xFF", 6)) {
532 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
539 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
540 /* Only initialize environment variables that are blank
541 * (i.e. have not yet been set)
543 if (!env_get(enetvar))
544 env_set(enetvar, ethaddr);
548 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
549 printf("%c%c%c%c v%u\n", e.id[0], e.id[1], e.id[2], e.id[3],
550 be32_to_cpu(e.version));
552 printf("%c%c%c%c\n", e.id[0], e.id[1], e.id[2], e.id[3]);
555 #ifdef CONFIG_SYS_I2C_EEPROM_NXID
557 * Now we need to upconvert the data into v1 format. We do this last so
558 * that at boot time, U-Boot will still say "NXID v0".
560 if (e.version == 0) {
561 e.version = cpu_to_be32(NXID_VERSION);
569 #ifdef CONFIG_SYS_I2C_EEPROM_CCID
572 * get_cpu_board_revision - get the CPU board revision on 85xx boards
574 * Read the EEPROM to determine the board revision.
576 * This function is called before relocation, so we need to read a private
577 * copy of the EEPROM into a local variable on the stack.
579 * Also, we assume that CONFIG_SYS_EEPROM_BUS_NUM == CONFIG_SYS_SPD_BUS_NUM. The global
580 * variable i2c_bus_num must be compile-time initialized to CONFIG_SYS_SPD_BUS_NUM,
581 * so that the SPD code will work. This means that all pre-relocation I2C
582 * operations can only occur on the CONFIG_SYS_SPD_BUS_NUM bus. So if
583 * CONFIG_SYS_EEPROM_BUS_NUM != CONFIG_SYS_SPD_BUS_NUM, then we can't read the EEPROM when
584 * this function is called. Oh well.
586 unsigned int get_cpu_board_revision(void)
588 struct board_eeprom {
589 u32 id; /* 0x00 - 0x03 EEPROM Tag 'CCID' */
590 u8 major; /* 0x04 Board revision, major */
591 u8 minor; /* 0x05 Board revision, minor */
594 #ifndef CONFIG_DM_I2C
595 i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, 0, CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
596 (void *)&be, sizeof(be));
600 #ifdef CONFIG_SYS_EEPROM_BUS_NUM
601 ret = i2c_get_chip_for_busnum(CONFIG_SYS_EEPROM_BUS_NUM,
602 CONFIG_SYS_I2C_EEPROM_ADDR,
603 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
606 ret = i2c_get_chip_for_busnum(0, CONFIG_SYS_I2C_EEPROM_ADDR,
607 CONFIG_SYS_I2C_EEPROM_ADDR_LEN,
611 dm_i2c_read(dev, 0, (void *)&be, sizeof(be));
614 if (be.id != (('C' << 24) | ('C' << 16) | ('I' << 8) | 'D'))
615 return MPC85XX_CPU_BOARD_REV(0, 0);
617 if ((be.major == 0xff) && (be.minor == 0xff))
618 return MPC85XX_CPU_BOARD_REV(0, 0);
620 return MPC85XX_CPU_BOARD_REV(be.major, be.minor);